Answer 'this unit coded nothing' by pointer, not by scanning its grid
Browse files at this commit7002e78a41b38312c79128030d5fdcc5705ce44bParent: f969486- perf
- answer 'this unit coded nothing' by pointer, not by scanning its grid
2 changed files +16 -8
crates/ec-av1/src/decode.rsmodified+15 -7
| @@ -2140,12 +2140,7 @@ impl Residual { | ||
| 2140 | 2140 | /// the empty marker, so neither the grid nor the residual is cloned and |
| 2141 | 2141 | /// the worker never runs the transform (60% of the 4K segment's units). |
| 2142 | 2142 | fn coeffs(tx: TxParams, grid: &[i32], fctx: &crate::decode::FrameCtx) -> Self { |
| 2143 | - // lane-coefgrid: a [`Grid::Zero`] unit IS the shared zero grid -- one | |
| 2144 | - // pointer compare instead of scanning up to 16 K coefficients. | |
| 2145 | - if std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) { | |
| 2146 | - return Self::Done(0, 0); | |
| 2147 | - } | |
| 2148 | - if grid.iter().all(|&c| c == 0) { | |
| 2143 | + if is_zero_grid(grid) { | |
| 2149 | 2144 | return Self::Done(0, 0); |
| 2150 | 2145 | } |
| 2151 | 2146 | Self::Coeffs(tx, push_coeffs(grid, fctx), grid.len() as u32) |
| @@ -2202,7 +2197,7 @@ fn push_pred_samples(v: &[u16], fctx: &crate::decode::FrameCtx) -> u32 { | ||
| 2202 | 2197 | /// and the reconstruction loops below read this instead of a per-unit |
| 2203 | 2198 | /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry |
| 2204 | 2199 | /// (a 128-wide inter block's prediction stride). |
| 2205 | -static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; | |
| 2200 | +pub(crate) static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; | |
| 2206 | 2201 | |
| 2207 | 2202 | /// lane-blockscratch: one plane's coefficient levels as the block-tail |
| 2208 | 2203 | /// neighbour writes want them. A plane that coded nothing (a skipped block, a |
| @@ -2236,6 +2231,13 @@ impl std::ops::Deref for Grid { | ||
| 2236 | 2231 | } |
| 2237 | 2232 | } |
| 2238 | 2233 | |
| 2234 | +/// Whether a coefficient grid codes nothing: a [`Grid::Zero`] unit IS the | |
| 2235 | +/// shared [`ZERO_RESIDUAL`], so one pointer compare answers the 60% of | |
| 2236 | +/// transform units that code no coefficient without the scan (lane-coefgrid). | |
| 2237 | +pub(crate) fn is_zero_grid(grid: &[i32]) -> bool { | |
| 2238 | + std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) || grid.iter().all(|&c| c == 0) | |
| 2239 | +} | |
| 2240 | + | |
| 2239 | 2241 | /// `residual`, or `n` zeros when it is the empty all-zero marker. |
| 2240 | 2242 | fn dense_residual(residual: &[i32], n: usize) -> &[i32] { |
| 2241 | 2243 | if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual } |
| @@ -6332,6 +6334,12 @@ fn fill_span<T: Copy>(dst: &mut [T], start: usize, n: usize, v: T) { | ||
| 6332 | 6334 | } |
| 6333 | 6335 | |
| 6334 | 6336 | fn neighbour_state(grid: &[i32]) -> Neighbour { |
| 6337 | + // lane-coefgrid: the shared zero grid sums to zero by construction; this | |
| 6338 | + // sum has no early exit, so the 60% of units that code nothing used to | |
| 6339 | + // walk their whole grid here. | |
| 6340 | + if is_zero_grid(grid) { | |
| 6341 | + return Neighbour::default(); | |
| 6342 | + } | |
| 6335 | 6343 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 6336 | 6344 | Neighbour { |
| 6337 | 6345 | level: cul.min(7) as u8, |
crates/ec-av1/src/transform.rsmodified+1 -1
| @@ -1340,7 +1340,7 @@ pub fn dequant_and_inverse_typed_wh( | ||
| 1340 | 1340 | // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the |
| 1341 | 1341 | // `to_vec` clone the recorded [`crate::decode::Residual`] made of it -- |
| 1342 | 1342 | // together 6.5% of the 4K segment's profile (memset + memmove). |
| 1343 | - if levels.iter().all(|&l| l == 0) { | |
| 1343 | + if crate::decode::is_zero_grid(levels) { | |
| 1344 | 1344 | return Vec::new(); |
| 1345 | 1345 | } |
| 1346 | 1346 | DQ_SCRATCH.with(|cell| { |