forge
Explore
Log in

Don't you dare go Hollow

Sgrproj box sums slide down the grid as well as across it

Browse files at this commit

c80d9e934025ab5d06e5c83ba6288a507ff5b87bAhmet Kaan GümüşParent: 8d96c7f
  • perf
    • sgrproj box sums slide down the grid as well as across it
Show full message
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.rsmodified+40 -18
@@ -751,30 +751,52 @@ fn compute_ab(
751 let mut col_b = vec![0i64; ncols];751 let mut col_b = vec![0i64; ncols];
752 // Hoisted out of the per-point body: one thread-local read per call.752 // Hoisted out of the per-point body: one thread-local read per call.
753 let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8);753 let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8);
754+ let base_col = h_start as i64 - 1 - r as i64;
755+ // lane-perf9: the column sums slide down the grid too. Grid row `gi+1`'s
756+ // window is row `gi`'s minus its top source row plus one new bottom row,
757+ // so each source sample is read once per grid row instead of (2r+1)
758+ // times -- the same exactness argument as the separable split above
759+ // (i64 addition is associative, and subtracting a row that was added is
760+ // exact), and `lr_sample`'s stripe-boundary test, which is what makes a
761+ // sample read expensive, is a pure function of `(row, col)`.
754 for gi in 0..gh {762 for gi in 0..gh {
755 let i = gi as i64 - 1;763 let i = gi as i64 - 1;
756- let base_col = h_start as i64 - 1 - r as i64;764+ {
757- for k in 0..ncols {765+ let mut accumulate = |row: i64, sign: i64| {
758- let col = base_col + k as i64;766+ for k in 0..ncols {
759- let (mut ca, mut cb) = (0i64, 0i64);767+ let px = lr_sample(
760- for dr in -r..=r {768+ cdef,
761- let row = v_start as i64 + i + dr as i64;769+ deblocked,
762- let px = lr_sample(770+ stride,
763- cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col,771+ plane_w,
764- ) as i64;772+ plane_h,
765- ca += px * px;773+ v_start,
766- cb += px;774+ v_end,
775+ row,
776+ base_col + k as i64,
777+ ) as i64;
778+ col_a[k] += sign * px * px;
779+ col_b[k] += sign * px;
780+ }
781+ };
782+ if gi == 0 {
783+ for dr in -r..=r {
784+ accumulate(v_start as i64 + i + dr as i64, 1);
785+ }
786+ } else {
787+ accumulate(v_start as i64 + i - 1 - r as i64, -1);
788+ accumulate(v_start as i64 + i + r as i64, 1);
767 }789 }
768- col_a[k] = ca;
769- col_b[k] = cb;
770 }790 }
791+ // The horizontal sum slides the same way (one add and one subtract
792+ // per grid point instead of 2r+1 adds).
793+ let mut a = col_a[..2 * ru + 1].iter().sum::<i64>();
794+ let mut b = col_b[..2 * ru + 1].iter().sum::<i64>();
771 for gj in 0..gw {795 for gj in 0..gw {
772 let j = gj as i64 - 1;796 let j = gj as i64 - 1;
773- let mut a = 0i64;797+ if gj > 0 {
774- let mut b = 0i64;798+ a += col_a[gj + 2 * ru] - col_a[gj - 1];
775- for k in gj..gj + 2 * ru + 1 {799+ b += col_b[gj + 2 * ru] - col_b[gj - 1];
776- a += col_a[k];
777- b += col_b[k];
778 }800 }
779 // libaom `restoration.c:660` -- the box sums are brought back801 // libaom `restoration.c:660` -- the box sums are brought back
780 // to the 8-bit scale before `p` is formed (`a` is a sum of802 // to the 8-bit scale before `p` is formed (`a` is a sum of