forge
Explore
Log in

Don't you dare go Hollow

Answer 'this unit coded nothing' by pointer, not by scanning its grid

Browse files at this commit

7002e78a41b38312c79128030d5fdcc5705ce44bAhmet Kaan GümüşParent: f969486
  • perf
    • answer 'this unit coded nothing' by pointer, not by scanning its grid
Show full message
perf(av1): answer 'this unit coded nothing' by pointer, not by scanning its grid

The zero grid is now shared (previous commit), so is_zero_grid recognises it
with one pointer compare. The dequantizer's all-zero test and the neighbour
state's cul_level sum both walked the whole grid for the 60% of transform
units that code no coefficient -- the sum has no early exit at all.
-1.2% instructions:u at 1 thread, -0.7% at (16,4,2); byte-exact.
crates/ec-av1/src/decode.rsmodified+15 -7
@@ -2140,12 +2140,7 @@ impl Residual {
21402140 /// the empty marker, so neither the grid nor the residual is cloned and
21412141 /// the worker never runs the transform (60% of the 4K segment's units).
21422142 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) {
21492144 return Self::Done(0, 0);
21502145 }
21512146 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 {
22022197 /// and the reconstruction loops below read this instead of a per-unit
22032198 /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry
22042199 /// (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];
22062201
22072202 /// lane-blockscratch: one plane's coefficient levels as the block-tail
22082203 /// neighbour writes want them. A plane that coded nothing (a skipped block, a
@@ -2236,6 +2231,13 @@ impl std::ops::Deref for Grid {
22362231 }
22372232 }
22382233
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+
22392241 /// `residual`, or `n` zeros when it is the empty all-zero marker.
22402242 fn dense_residual(residual: &[i32], n: usize) -> &[i32] {
22412243 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) {
63326334 }
63336335
63346336 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+ }
63356343 let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum();
63366344 Neighbour {
63376345 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(
13401340 // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the
13411341 // `to_vec` clone the recorded [`crate::decode::Residual`] made of it --
13421342 // 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) {
13441344 return Vec::new();
13451345 }
13461346 DQ_SCRATCH.with(|cell| {