forge
Explore
Log in

Don't you dare go Hollow

Unscaled MC does one contiguous horizontal pass, no per-call alloc

Browse files at this commit

62f159c29e56dd4fa52f93dc6db95a1975c6d7bfAhmet Kaan GümüşParent: 1a54674
  • perf
    • unscaled MC does one contiguous horizontal pass, no per-call alloc
Show full message
perf(av1): unscaled MC does one contiguous horizontal pass, no per-call alloc

predict_with_filters_kern spent 17% of decode self time in a doubly nested
per-sample loop: every tap re-read the filter table, every sample went
through the Clip3 edge helper, and each call allocated its own (h+7)-row
intermediate.

Restructured dav1d style, keeping the rounding chain untouched: taps are
fetched once into a [i32; 8]; a row whose whole 8-tap footprint is inside
the reference's true extent is one contiguous slice walked with windows(8)
(the clamped form stays for edge rows and is what a past-the-edge motion
vector still takes); the vertical pass accumulates tap-major over two
contiguous i32 slices and skips the zero taps the 4-tap kernels carry; the
whole-pel case copies a row at a time. The intermediate is a thread-local
scratch buffer instead of a per-call Vec.

Byte-exact: EC_PROBE_OUT16 dumps of a 299-frame 1080p 10-bit segment and a
4 s 2160p HDR segment both cmp identical to the pre-change binary.
Interleaved medians on the 1080p segment: 22.61 s -> 20.66 s.
crates/ec-av1/src/mc.rsmodified+129 -25
@@ -243,6 +243,111 @@ fn sample(
243 i32::from(reference[cy * stride + cx])243 i32::from(reference[cy * stride + cx])
244 }244 }
245 245
246+/// lane-perf2: one row of the separable filter's horizontal pass, dav1d
247+/// style. When the row's whole 8-tap footprint (`x0-3 .. x0+block_w+4`) lies
248+/// inside the reference's true extent -- the overwhelmingly common case --
249+/// the row is one contiguous slice and the taps are a fixed `[i32; 8]`, so
250+/// the per-sample `Clip3` and the per-tap table load both leave the inner
251+/// loop and it auto-vectorises. The edge case keeps the exact spec form
252+/// (`sample`'s clamp per tap); both arms compute the same sums.
253+#[inline]
254+fn hpass_row(reference: &[u16], row_base: usize, true_width: usize, x0: i32, taps: &[i32; 8], out: &mut [i32]) {
255+ let block_w = out.len();
256+ let start = x0 - 3;
257+ if start >= 0 && start as usize + block_w + 7 <= true_width {
258+ let s = row_base + start as usize;
259+ if let Some(src) = reference.get(s..s + block_w + 7) {
260+ for (o, w) in out.iter_mut().zip(src.windows(8)) {
261+ let mut sum = 0i32;
262+ for t in 0..8 {
263+ sum += taps[t] * i32::from(w[t]);
264+ }
265+ *o = round2(sum, INTER_ROUND_0);
266+ }
267+ return;
268+ }
269+ }
270+ for (c, o) in out.iter_mut().enumerate() {
271+ let mut sum = 0i32;
272+ for (t, &tap) in taps.iter().enumerate() {
273+ let x = (x0 + c as i32 + t as i32 - 3).clamp(0, true_width as i32 - 1) as usize;
274+ sum += tap * i32::from(reference[row_base + x]);
275+ }
276+ *o = round2(sum, INTER_ROUND_0);
277+ }
278+}
279+
280+/// lane-perf2: the unscaled horizontal pass, `block_h + 7` rows of
281+/// [`hpass_row`] with the vertical clamp hoisted out of the row.
282+#[allow(clippy::too_many_arguments)]
283+fn horizontal_pass_unscaled(
284+ reference: &[u16],
285+ stride: usize,
286+ true_width: usize,
287+ true_height: usize,
288+ x0: i32,
289+ y0: i32,
290+ taps: &[i32; 8],
291+ block_w: usize,
292+ rows: usize,
293+ intermediate: &mut [i32],
294+) {
295+ for r in 0..rows {
296+ let y = (y0 - 3 + r as i32).clamp(0, true_height as i32 - 1) as usize;
297+ hpass_row(
298+ reference,
299+ y * stride,
300+ true_width,
301+ x0,
302+ taps,
303+ &mut intermediate[r * block_w..(r + 1) * block_w],
304+ );
305+ }
306+}
307+
308+/// lane-perf2: one output row of the vertical pass, accumulated tap-major so
309+/// the inner loop walks two contiguous `i32` slices (vectorisable) instead of
310+/// striding the intermediate by `block_w` per tap. A zero tap is skipped --
311+/// adding `0 * x` is exactly nothing, and the 4-tap kernels zero four of the
312+/// eight slots. `acc` is left holding the unrounded sums.
313+#[inline]
314+fn vpass_row(intermediate: &[i32], block_w: usize, row: usize, taps: &[i32; 8], acc: &mut [i32]) {
315+ acc.fill(0);
316+ for (t, &tap) in taps.iter().enumerate() {
317+ if tap == 0 {
318+ continue;
319+ }
320+ let base = (row + t) * block_w;
321+ for (a, &s) in acc.iter_mut().zip(&intermediate[base..base + block_w]) {
322+ *a += tap * s;
323+ }
324+ }
325+}
326+
327+thread_local! {
328+ /// lane-perf2: the separable filter's intermediate buffer, reused across
329+ /// calls -- a 4K inter frame runs `predict*` hundreds of thousands of
330+ /// times and each call used to `vec![0i32; (block_h+7) * block_w]`. The
331+ /// horizontal pass writes every element it later reads, so no clearing is
332+ /// needed; the tail `block_w` elements are the vertical pass's row
333+ /// accumulator.
334+ static MC_SCRATCH: std::cell::RefCell<Vec<i32>> = const { std::cell::RefCell::new(Vec::new()) };
335+}
336+
337+/// Runs `f` on a scratch slice of `rows * block_w` intermediate elements plus
338+/// a `block_w` accumulator (see [`MC_SCRATCH`]).
339+fn with_scratch<R>(rows: usize, block_w: usize, f: impl FnOnce(&mut [i32], &mut [i32]) -> R) -> R {
340+ MC_SCRATCH.with(|s| {
341+ let mut buf = s.borrow_mut();
342+ let need = rows * block_w + block_w;
343+ if buf.len() < need {
344+ buf.resize(need, 0);
345+ }
346+ let (inter, acc) = buf[..need].split_at_mut(rows * block_w);
347+ f(inter, acc)
348+ })
349+}
350+
246 /// Predicts one `block_w * block_h` block into `dst`, row-major, from351 /// Predicts one `block_w * block_h` block into `dst`, row-major, from
247 /// `reference` (row-major, stride `stride`, true content352 /// `reference` (row-major, stride `stride`, true content
248 /// `true_width` x `true_height` -- a reference frame's decoded picture353 /// `true_width` x `true_height` -- a reference frame's decoded picture
@@ -395,11 +500,19 @@ pub fn predict_with_filters_kern(
395 // it whole-pel candidates from stage 1).500 // it whole-pel candidates from stage 1).
396 if xfrac == 0 && yfrac == 0 {501 if xfrac == 0 && yfrac == 0 {
397 for row in 0..block_h {502 for row in 0..block_h {
398- let y = y0 + row as i32;503+ let y = (y0 + row as i32).clamp(0, true_height as i32 - 1) as usize;
399- for col in 0..block_w {504+ let row_base = y * stride;
400- let x = x0 + col as i32;505+ let out = &mut dst[row * block_w..(row + 1) * block_w];
401- dst[row * block_w + col] =506+ // lane-perf2: an interior row is one contiguous copy.
402- sample(reference, stride, true_width, true_height, x, y) as u16;507+ if x0 >= 0 && x0 as usize + block_w <= true_width {
508+ if let Some(src) = reference.get(row_base + x0 as usize..row_base + x0 as usize + block_w) {
509+ out.copy_from_slice(src);
510+ continue;
511+ }
512+ }
513+ for (col, o) in out.iter_mut().enumerate() {
514+ let x = (x0 + col as i32).clamp(0, true_width as i32 - 1) as usize;
515+ *o = reference[row_base + x];
403 }516 }
404 }517 }
405 #[cfg(test)]518 #[cfg(test)]
@@ -424,28 +537,19 @@ pub fn predict_with_filters_kern(
424 // The vertical pass reads 3 rows above and 4 below the block, so the537 // The vertical pass reads 3 rows above and 4 below the block, so the
425 // horizontal pass must produce that many extra intermediate rows.538 // horizontal pass must produce that many extra intermediate rows.
426 let rows = block_h + 7;539 let rows = block_h + 7;
427- let mut intermediate = vec![0i32; rows * block_w];540+ let max = crate::decode::sample_max();
428- for r in 0..rows {541+ with_scratch(rows, block_w, |intermediate, acc| {
429- let y = y0 - 3 + r as i32;542+ horizontal_pass_unscaled(
430- for c in 0..block_w {543+ reference, stride, true_width, true_height, x0, y0, h_filter, block_w, rows,
431- let mut sum = 0;544+ intermediate,
432- for (t, &tap) in h_filter.iter().enumerate() {545+ );
433- let x = x0 + c as i32 + t as i32 - 3;546+ for row in 0..block_h {
434- sum += tap * sample(reference, stride, true_width, true_height, x, y);547+ vpass_row(intermediate, block_w, row, v_filter, acc);
435- }548+ for (d, &a) in dst[row * block_w..(row + 1) * block_w].iter_mut().zip(acc.iter()) {
436- intermediate[r * block_w + c] = round2(sum, INTER_ROUND_0);549+ *d = round2(a, INTER_ROUND_1).clamp(0, max) as u16;
437- }
438- }
439-
440- for row in 0..block_h {
441- for col in 0..block_w {
442- let mut sum = 0;
443- for (t, &tap) in v_filter.iter().enumerate() {
444- sum += tap * intermediate[(row + t) * block_w + col];
445 }550 }
446- dst[row * block_w + col] = round2(sum, INTER_ROUND_1).clamp(0, crate::decode::sample_max()) as u16;
447 }551 }
448- }552+ });
449 553
450 #[cfg(test)]554 #[cfg(test)]
451 crate::encode::stage_add(1, stage_t.elapsed());555 crate::encode::stage_add(1, stage_t.elapsed());