From c80d9e934025ab5d06e5c83ba6288a507ff5b87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 20:25:14 +0300 Subject: [PATCH] perf(av1): sgrproj box sums slide down the grid as well as across it `compute_ab` (3.6% self time at 4K, the remaining loop-restoration cost now that Wiener runs on the MC kernels) recomputed each grid row's column sums from scratch, so every source sample was read -- through `lr_sample`'s stripe-boundary test, the expensive part -- (2r+1) times. Consecutive grid rows share all but two source rows: subtract the row leaving the window, add the one entering it. The horizontal sum slides the same way, one add and one subtract per grid point instead of 2r+1 adds. Exact, not approximate: i64 addition is associative and subtracting a row that was added back is exact, and `lr_sample` is a pure function of its (row, col). Charter asked for an AVX2 kernel over the per-pixel A/B arithmetic; the sliding window removes strictly more work in a quarter of the lines, so it took that rung first. seg4k_lr 35.49 s -> 35.02 s, seg4k 36.45 s -> 36.27 s (medians of 3 interleaved pairs each); byte-exact on all four inputs. --- crates/ec-av1/src/restoration.rs | 58 ++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/crates/ec-av1/src/restoration.rs b/crates/ec-av1/src/restoration.rs index bc357cfe..e7dd7dc6 100644 --- a/crates/ec-av1/src/restoration.rs +++ b/crates/ec-av1/src/restoration.rs @@ -751,30 +751,52 @@ fn compute_ab( let mut col_b = vec![0i64; ncols]; // Hoisted out of the per-point body: one thread-local read per call. let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8); + let base_col = h_start as i64 - 1 - r as i64; + // lane-perf9: the column sums slide down the grid too. Grid row `gi+1`'s + // window is row `gi`'s minus its top source row plus one new bottom row, + // so each source sample is read once per grid row instead of (2r+1) + // times -- the same exactness argument as the separable split above + // (i64 addition is associative, and subtracting a row that was added is + // exact), and `lr_sample`'s stripe-boundary test, which is what makes a + // sample read expensive, is a pure function of `(row, col)`. for gi in 0..gh { let i = gi as i64 - 1; - let base_col = h_start as i64 - 1 - r as i64; - for k in 0..ncols { - let col = base_col + k as i64; - let (mut ca, mut cb) = (0i64, 0i64); - for dr in -r..=r { - let row = v_start as i64 + i + dr as i64; - let px = lr_sample( - cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col, - ) as i64; - ca += px * px; - cb += px; + { + let mut accumulate = |row: i64, sign: i64| { + for k in 0..ncols { + let px = lr_sample( + cdef, + deblocked, + stride, + plane_w, + plane_h, + v_start, + v_end, + row, + base_col + k as i64, + ) as i64; + col_a[k] += sign * px * px; + col_b[k] += sign * px; + } + }; + if gi == 0 { + for dr in -r..=r { + accumulate(v_start as i64 + i + dr as i64, 1); + } + } else { + accumulate(v_start as i64 + i - 1 - r as i64, -1); + accumulate(v_start as i64 + i + r as i64, 1); } - col_a[k] = ca; - col_b[k] = cb; } + // The horizontal sum slides the same way (one add and one subtract + // per grid point instead of 2r+1 adds). + let mut a = col_a[..2 * ru + 1].iter().sum::(); + let mut b = col_b[..2 * ru + 1].iter().sum::(); for gj in 0..gw { let j = gj as i64 - 1; - let mut a = 0i64; - let mut b = 0i64; - for k in gj..gj + 2 * ru + 1 { - a += col_a[k]; - b += col_b[k]; + if gj > 0 { + a += col_a[gj + 2 * ru] - col_a[gj - 1]; + b += col_b[gj + 2 * ru] - col_b[gj - 1]; } // libaom `restoration.c:660` -- the box sums are brought back // to the 8-bit scale before `p` is formed (`a` is a sum of