From 63f3f62f31e8a64b039fb765de534e55f89e211f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 12:33:59 +0300 Subject: [PATCH] perf(av1): band the loop-restoration unit search by unit row `pick_restoration` priced every unit of every Wiener candidate on the frame thread while the whole pool sat idle. A unit's cost is read off its own samples and written to its own slot, so the walk is `chunks_mut` bands of the same `best` -- the choice cannot depend on how many bands there are. 4 frames at 3840x1608, 4x2 tiles, 8 threads: the LR search goes 31.0 -> 13.4 ms/frame; 1080p 8.4 -> 4.5 ms/frame. --- crates/ec-av1/src/filter_search.rs | 47 ++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/crates/ec-av1/src/filter_search.rs b/crates/ec-av1/src/filter_search.rs index ac01ee4a..d2e94a5d 100644 --- a/crates/ec-av1/src/filter_search.rs +++ b/crates/ec-av1/src/filter_search.rs @@ -500,13 +500,33 @@ pub(crate) fn pick_restoration( sse }; // Cost of leaving every unit alone, then one whole-plane pass per - // candidate to price taking it. - let mut best: Vec<(f64, Option)> = (0..horz * vert) - .map(|i| { - let sse = unit_sse(&stages.cdefed[0], i / horz, i % horz) as f64; - (sse + lambda * LR_NONE_BITS, None) - }) - .collect(); + // candidate to price taking it. Both walks are banded by unit ROW + // (lane-av1fpar): a unit's cost is read off its own samples alone and + // written to its own slot, so the bands are `chunks_mut` of the same + // `best` and the choice cannot depend on how many of them there are. + let rows_of = |plane: &[u16], r0: usize, slice: &mut [(f64, Option)], score: &dyn Fn(u64, &mut (f64, Option))| { + for (i, slot) in slice.iter_mut().enumerate() { + score(unit_sse(plane, r0 + i / horz, i % horz), slot); + } + }; + let banded = |plane: &[u16], best: &mut [(f64, Option)], score: &(dyn Fn(u64, &mut (f64, Option)) + Sync)| { + let threads = crate::par::filter_threads().min(vert); + if threads <= 1 { + rows_of(plane, 0, best, score); + return; + } + let per = vert.div_ceil(threads); + let batch = crate::par::Batch::new("ec-av1-filter"); + for (band, slice) in best.chunks_mut(per * horz).enumerate() { + let rows_of = &rows_of; + batch.submit(move || rows_of(plane, band * per, slice, score)); + } + }; + let mut best: Vec<(f64, Option)> = + vec![(0.0, None); horz * vert]; + banded(&stages.cdefed[0], &mut best, &|sse, slot| { + *slot = (sse as f64 + lambda * LR_NONE_BITS, None); + }); let mut out = Vec::new(); for taps in WIENER_CANDIDATES { let info = crate::restoration::wiener_from_taps(taps, taps); @@ -531,15 +551,12 @@ pub(crate) fn pick_restoration( None, fctx, ); - for rrow in 0..vert { - for rcol in 0..horz { - let cost = unit_sse(&out, rrow, rcol) as f64 + lambda * LR_WIENER_BITS; - let slot = &mut best[rcol + rrow * horz]; - if cost < slot.0 { - *slot = (cost, Some(info)); - } + banded(&out, &mut best, &|sse, slot| { + let cost = sse as f64 + lambda * LR_WIENER_BITS; + if cost < slot.0 { + *slot = (cost, Some(info)); } - } + }); } let chosen: Vec> = best.into_iter().map(|(_, f)| f).collect();