forge
Explore
Log in

Don't you dare go Hollow

The wavefront's op list and prediction buffers are reused per block

Browse files at this commit

e1f00c96c93e2029c911487ded5ed0f213bebeafAhmet Kaan GümüşParent: bccf2c8
  • perf
    • the wavefront's op list and prediction buffers are reused per block
Show full message
perf(av1): the wavefront's op list and prediction buffers are reused per block

`wave_submit` took the recorded op list and left a zero-capacity `Vec`
behind, so every superblock regrew it -- and memmoved every op already
recorded -- from nothing; it is now replaced sized for the next
superblock, exactly as the two arenas beside it already were.

A deferred prediction build returned three fresh `Vec<u16>` per block
and dropped the previous three; the build now refills the buffers the
worker already holds (`refill`, which still allocates zeroed when the
buffer has to grow, since `calloc` beats an element-wise resize).

Byte-exact 12/12 at 1/4/16 threads. seg4k instructions:u at (16,4,2)
409.809 -> 408.259 G (-0.38%); at 1 thread 404.579 -> 405.005 G
(+0.11%, the inline path has no previous buffer to reuse and pays the
plumbing) -- kept for the threaded config the 4K target names.
crates/ec-av1/src/decode.rsmodified+52 -22
@@ -2275,7 +2275,11 @@ pub(crate) struct PredBuild(
22752275 &mut PlaneBuf<'static>,
22762276 &mut PlaneBuf<'static>,
22772277 &crate::decode::FrameCtx,
2278- ) -> [Vec<u16>; 3],
2278+ // lane-blockmem: the previous block's three prediction buffers,
2279+ // refilled in place -- the build used to return three fresh
2280+ // `Vec`s per block and drop these.
2281+ &mut [Vec<u16>; 3],
2282+ ),
22792283 >,
22802284 );
22812285
@@ -2290,6 +2294,21 @@ fn recon_deferred(fctx: &crate::decode::FrameCtx) -> bool {
22902294 fctx.recon_planes.with(std::cell::Cell::get).is_none()
22912295 }
22922296
2297+/// The previous block's prediction buffer, cleared and zero-filled to `n`
2298+/// (lane-blockmem): the same bytes the `vec![0u16; n]` it replaces produced,
2299+/// without the per-block allocation and free.
2300+fn refill(mut v: Vec<u16>, n: usize) -> Vec<u16> {
2301+ // A buffer that has to grow anyway is cheaper allocated zeroed (`calloc`)
2302+ // than resized element by element -- which is every block on the inline
2303+ // path, where there is no previous buffer to reuse.
2304+ if v.capacity() < n {
2305+ return vec![0u16; n];
2306+ }
2307+ v.clear();
2308+ v.resize(n, 0);
2309+ v
2310+}
2311+
22932312 /// Records one block's prediction build for the worker that reconstructs it.
22942313 #[allow(unsafe_code)]
22952314 fn push_pred<'a>(
@@ -2299,17 +2318,16 @@ fn push_pred<'a>(
22992318 &mut PlaneBuf<'static>,
23002319 &mut PlaneBuf<'static>,
23012320 &crate::decode::FrameCtx,
2302- ) -> [Vec<u16>; 3]
2303- + 'a,
2321+ &mut [Vec<u16>; 3],
2322+ ) + 'a,
23042323 ) {
2305- type Built = [Vec<u16>; 3];
23062324 type Dyn<'x> = dyn FnOnce(
23072325 &mut PlaneBuf<'static>,
23082326 &mut PlaneBuf<'static>,
23092327 &mut PlaneBuf<'static>,
23102328 &crate::decode::FrameCtx,
2311- ) -> Built
2312- + 'x;
2329+ &mut [Vec<u16>; 3],
2330+ ) + 'x;
23132331 let boxed: Box<Dyn<'a>> = Box::new(f);
23142332 // SAFETY: see [`PredBuild`] -- every worker is joined by [`WaveGuard`]
23152333 // before the borrows the closure holds go out of scope.
@@ -2723,7 +2741,15 @@ impl Drop for WaveGuard<'_> {
27232741 fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool {
27242742 let st = fctx.wave.with(|w| w.borrow().clone());
27252743 let Some(st) = st else { return false };
2726- let ops = fctx.recon_ops.with(|c| std::mem::take(&mut *c.borrow_mut()));
2744+ // lane-blockmem: the op list is replaced sized for the next superblock,
2745+ // exactly as the two arenas below are -- taking it left a zero-capacity
2746+ // `Vec` that regrew (and memmoved every recorded op) from nothing on
2747+ // every superblock.
2748+ let ops = fctx.recon_ops.with(|c| {
2749+ let mut c = c.borrow_mut();
2750+ let cap = c.len();
2751+ std::mem::replace(&mut *c, Vec::with_capacity(cap))
2752+ });
27272753 // The job owns its coefficients: the arena is replaced by an empty one
27282754 // sized for the next superblock, so parse never regrows from nothing and
27292755 // the worker frees this buffer once, instead of once per unit.
@@ -3095,7 +3121,7 @@ fn exec_op(
30953121 ) {
30963122 let mut scratch = Vec::new();
30973123 match op {
3098- ReconOp::Pred(b) => *cur = (b.0)(y, u, v, fctx),
3124+ ReconOp::Pred(b) => (b.0)(y, u, v, fctx, cur),
30993125 ReconOp::Intra {
31003126 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,
31013127 filter_intra, smooth_neighbor, pred,
@@ -24700,8 +24726,8 @@ fn decode_inter_block(
2470024726 let build = move |_y: &mut PlaneBuf<'static>,
2470124727 _u: &mut PlaneBuf<'static>,
2470224728 _v: &mut PlaneBuf<'static>,
24703- fctx: &crate::decode::FrameCtx|
24704- -> [Vec<u16>; 3] {
24729+ fctx: &crate::decode::FrameCtx,
24730+ out: &mut [Vec<u16>; 3]| {
2470524731 let mut inter0_y = vec![0i32; side * side];
2470624732 mc::predict_compound_intermediate_kern(
2470724733 &py0.data,
@@ -24756,7 +24782,7 @@ fn decode_inter_block(
2475624782 side as i32, side as i32, 0, 0, fctx,
2475724783 );
2475824784 }
24759- let mut pred_y = vec![0u16; side * side];
24785+ let mut pred_y = refill(std::mem::take(&mut out[0]), side * side);
2476024786 let mut diffwtd_mask_y;
2476124787 // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR
2476224788 // the wedge codebook lookup (comp_group_idx==1's two mutually
@@ -24836,7 +24862,7 @@ fn decode_inter_block(
2483624862 );
2483724863 }
2483824864 }
24839- let mut pred_u = vec![0u16; chroma_side * chroma_side];
24865+ let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side);
2484024866 if let Some(mask_y) = mask_y {
2484124867 mc::blend_masked_compound(
2484224868 &inter0_u,
@@ -24910,7 +24936,7 @@ fn decode_inter_block(
2491024936 );
2491124937 }
2491224938 }
24913- let mut pred_v = vec![0u16; chroma_side * chroma_side];
24939+ let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side);
2491424940 if let Some(mask_y) = mask_y {
2491524941 mc::blend_masked_compound(
2491624942 &inter0_v,
@@ -24928,7 +24954,7 @@ fn decode_inter_block(
2492824954
2492924955 // lane-inter4 r2: a rectangular strip's transform tree starts from
2493024956 // its own rect `max_txsize_rect_lookup` entry, not a square one.
24931- [pred_y, pred_u, pred_v]
24957+ *out = [pred_y, pred_u, pred_v];
2493224958 };
2493324959 let (pred_y, pred_u, pred_v): (Vec<u16>, Vec<u16>, Vec<u16>);
2493424960 let (sy, su, sv) = if recon_deferred(fctx) {
@@ -24939,7 +24965,9 @@ fn decode_inter_block(
2493924965 Pred::Cur { plane: 2, stride: chroma_side, off: 0 },
2494024966 )
2494124967 } else {
24942- let [by, bu, bv] = build(y, u, v, fctx);
24968+ let mut built: [Vec<u16>; 3] = Default::default();
24969+ build(y, u, v, fctx, &mut built);
24970+ let [by, bu, bv] = built;
2494324971 (pred_y, pred_u, pred_v) = (by, bu, bv);
2494424972 (
2494524973 Pred::Inline(&pred_y, side),
@@ -25922,11 +25950,11 @@ fn decode_inter_block(
2592225950 let build = move |y: &mut PlaneBuf<'static>,
2592325951 u: &mut PlaneBuf<'static>,
2592425952 v: &mut PlaneBuf<'static>,
25925- fctx: &crate::decode::FrameCtx|
25926- -> [Vec<u16>; 3] {
25927- let mut pred_y = vec![0u16; side * side];
25928- let mut pred_u = vec![0u16; chroma_side * chroma_side];
25929- let mut pred_v = vec![0u16; chroma_side * chroma_side];
25953+ fctx: &crate::decode::FrameCtx,
25954+ out: &mut [Vec<u16>; 3]| {
25955+ let mut pred_y = refill(std::mem::take(&mut out[0]), side * side);
25956+ let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side);
25957+ let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side);
2593025958 if luma_scale == mc::REF_NO_SCALE {
2593125959 mc::predict_with_filters_kern(
2593225960 &py_ref.data,
@@ -26083,7 +26111,7 @@ fn decode_inter_block(
2608326111 &mut pred_v, fctx,
2608426112 );
2608526113 }
26086- [pred_y, pred_u, pred_v]
26114+ *out = [pred_y, pred_u, pred_v];
2608726115 };
2608826116 // OBMC and interintra are mutually exclusive (`motion_mode` is
2608926117 // never read for an interintra block), so blending interintra
@@ -26108,7 +26136,9 @@ fn decode_inter_block(
2610826136 if interintra_mode.is_some() {
2610926137 flush_recon(fctx, y, u, v);
2611026138 }
26111- let [by, bu, bv] = build(y, u, v, fctx);
26139+ let mut built: [Vec<u16>; 3] = Default::default();
26140+ build(y, u, v, fctx, &mut built);
26141+ let [by, bu, bv] = built;
2611226142 (pred_y, pred_u, pred_v) = (by, bu, bv);
2611326143 if obmc_selected {
2611426144 obmc_blend(