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(
2275 &mut PlaneBuf<'static>,2275 &mut PlaneBuf<'static>,
2276 &mut PlaneBuf<'static>,2276 &mut PlaneBuf<'static>,
2277 &crate::decode::FrameCtx,2277 &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+ ),
2279 >,2283 >,
2280 );2284 );
2281 2285
@@ -2290,6 +2294,21 @@ fn recon_deferred(fctx: &crate::decode::FrameCtx) -> bool {
2290 fctx.recon_planes.with(std::cell::Cell::get).is_none()2294 fctx.recon_planes.with(std::cell::Cell::get).is_none()
2291 }2295 }
2292 2296
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+
2293 /// Records one block's prediction build for the worker that reconstructs it.2312 /// Records one block's prediction build for the worker that reconstructs it.
2294 #[allow(unsafe_code)]2313 #[allow(unsafe_code)]
2295 fn push_pred<'a>(2314 fn push_pred<'a>(
@@ -2299,17 +2318,16 @@ fn push_pred<'a>(
2299 &mut PlaneBuf<'static>,2318 &mut PlaneBuf<'static>,
2300 &mut PlaneBuf<'static>,2319 &mut PlaneBuf<'static>,
2301 &crate::decode::FrameCtx,2320 &crate::decode::FrameCtx,
2302- ) -> [Vec<u16>; 3]2321+ &mut [Vec<u16>; 3],
2303- + 'a,2322+ ) + 'a,
2304 ) {2323 ) {
2305- type Built = [Vec<u16>; 3];
2306 type Dyn<'x> = dyn FnOnce(2324 type Dyn<'x> = dyn FnOnce(
2307 &mut PlaneBuf<'static>,2325 &mut PlaneBuf<'static>,
2308 &mut PlaneBuf<'static>,2326 &mut PlaneBuf<'static>,
2309 &mut PlaneBuf<'static>,2327 &mut PlaneBuf<'static>,
2310 &crate::decode::FrameCtx,2328 &crate::decode::FrameCtx,
2311- ) -> Built2329+ &mut [Vec<u16>; 3],
2312- + 'x;2330+ ) + 'x;
2313 let boxed: Box<Dyn<'a>> = Box::new(f);2331 let boxed: Box<Dyn<'a>> = Box::new(f);
2314 // SAFETY: see [`PredBuild`] -- every worker is joined by [`WaveGuard`]2332 // SAFETY: see [`PredBuild`] -- every worker is joined by [`WaveGuard`]
2315 // before the borrows the closure holds go out of scope.2333 // before the borrows the closure holds go out of scope.
@@ -2723,7 +2741,15 @@ impl Drop for WaveGuard<'_> {
2723 fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool {2741 fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool {
2724 let st = fctx.wave.with(|w| w.borrow().clone());2742 let st = fctx.wave.with(|w| w.borrow().clone());
2725 let Some(st) = st else { return false };2743 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+ });
2727 // The job owns its coefficients: the arena is replaced by an empty one2753 // The job owns its coefficients: the arena is replaced by an empty one
2728 // sized for the next superblock, so parse never regrows from nothing and2754 // sized for the next superblock, so parse never regrows from nothing and
2729 // the worker frees this buffer once, instead of once per unit.2755 // the worker frees this buffer once, instead of once per unit.
@@ -3095,7 +3121,7 @@ fn exec_op(
3095 ) {3121 ) {
3096 let mut scratch = Vec::new();3122 let mut scratch = Vec::new();
3097 match op {3123 match op {
3098- ReconOp::Pred(b) => *cur = (b.0)(y, u, v, fctx),3124+ ReconOp::Pred(b) => (b.0)(y, u, v, fctx, cur),
3099 ReconOp::Intra {3125 ReconOp::Intra {
3100 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,3126 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,
3101 filter_intra, smooth_neighbor, pred,3127 filter_intra, smooth_neighbor, pred,
@@ -24700,8 +24726,8 @@ fn decode_inter_block(
24700 let build = move |_y: &mut PlaneBuf<'static>,24726 let build = move |_y: &mut PlaneBuf<'static>,
24701 _u: &mut PlaneBuf<'static>,24727 _u: &mut PlaneBuf<'static>,
24702 _v: &mut PlaneBuf<'static>,24728 _v: &mut PlaneBuf<'static>,
24703- fctx: &crate::decode::FrameCtx|24729+ fctx: &crate::decode::FrameCtx,
24704- -> [Vec<u16>; 3] {24730+ out: &mut [Vec<u16>; 3]| {
24705 let mut inter0_y = vec![0i32; side * side];24731 let mut inter0_y = vec![0i32; side * side];
24706 mc::predict_compound_intermediate_kern(24732 mc::predict_compound_intermediate_kern(
24707 &py0.data,24733 &py0.data,
@@ -24756,7 +24782,7 @@ fn decode_inter_block(
24756 side as i32, side as i32, 0, 0, fctx,24782 side as i32, side as i32, 0, 0, fctx,
24757 );24783 );
24758 }24784 }
24759- let mut pred_y = vec![0u16; side * side];24785+ let mut pred_y = refill(std::mem::take(&mut out[0]), side * side);
24760 let mut diffwtd_mask_y;24786 let mut diffwtd_mask_y;
24761 // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR24787 // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR
24762 // the wedge codebook lookup (comp_group_idx==1's two mutually24788 // the wedge codebook lookup (comp_group_idx==1's two mutually
@@ -24836,7 +24862,7 @@ fn decode_inter_block(
24836 );24862 );
24837 }24863 }
24838 }24864 }
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);
24840 if let Some(mask_y) = mask_y {24866 if let Some(mask_y) = mask_y {
24841 mc::blend_masked_compound(24867 mc::blend_masked_compound(
24842 &inter0_u,24868 &inter0_u,
@@ -24910,7 +24936,7 @@ fn decode_inter_block(
24910 );24936 );
24911 }24937 }
24912 }24938 }
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);
24914 if let Some(mask_y) = mask_y {24940 if let Some(mask_y) = mask_y {
24915 mc::blend_masked_compound(24941 mc::blend_masked_compound(
24916 &inter0_v,24942 &inter0_v,
@@ -24928,7 +24954,7 @@ fn decode_inter_block(
24928 24954
24929 // lane-inter4 r2: a rectangular strip's transform tree starts from24955 // lane-inter4 r2: a rectangular strip's transform tree starts from
24930 // its own rect `max_txsize_rect_lookup` entry, not a square one.24956 // 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];
24932 };24958 };
24933 let (pred_y, pred_u, pred_v): (Vec<u16>, Vec<u16>, Vec<u16>);24959 let (pred_y, pred_u, pred_v): (Vec<u16>, Vec<u16>, Vec<u16>);
24934 let (sy, su, sv) = if recon_deferred(fctx) {24960 let (sy, su, sv) = if recon_deferred(fctx) {
@@ -24939,7 +24965,9 @@ fn decode_inter_block(
24939 Pred::Cur { plane: 2, stride: chroma_side, off: 0 },24965 Pred::Cur { plane: 2, stride: chroma_side, off: 0 },
24940 )24966 )
24941 } else {24967 } 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;
24943 (pred_y, pred_u, pred_v) = (by, bu, bv);24971 (pred_y, pred_u, pred_v) = (by, bu, bv);
24944 (24972 (
24945 Pred::Inline(&pred_y, side),24973 Pred::Inline(&pred_y, side),
@@ -25922,11 +25950,11 @@ fn decode_inter_block(
25922 let build = move |y: &mut PlaneBuf<'static>,25950 let build = move |y: &mut PlaneBuf<'static>,
25923 u: &mut PlaneBuf<'static>,25951 u: &mut PlaneBuf<'static>,
25924 v: &mut PlaneBuf<'static>,25952 v: &mut PlaneBuf<'static>,
25925- fctx: &crate::decode::FrameCtx|25953+ fctx: &crate::decode::FrameCtx,
25926- -> [Vec<u16>; 3] {25954+ out: &mut [Vec<u16>; 3]| {
25927- let mut pred_y = vec![0u16; side * side];25955+ let mut pred_y = refill(std::mem::take(&mut out[0]), side * side);
25928- let mut pred_u = vec![0u16; chroma_side * chroma_side];25956+ let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side);
25929- let mut pred_v = vec![0u16; chroma_side * chroma_side];25957+ let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side);
25930 if luma_scale == mc::REF_NO_SCALE {25958 if luma_scale == mc::REF_NO_SCALE {
25931 mc::predict_with_filters_kern(25959 mc::predict_with_filters_kern(
25932 &py_ref.data,25960 &py_ref.data,
@@ -26083,7 +26111,7 @@ fn decode_inter_block(
26083 &mut pred_v, fctx,26111 &mut pred_v, fctx,
26084 );26112 );
26085 }26113 }
26086- [pred_y, pred_u, pred_v]26114+ *out = [pred_y, pred_u, pred_v];
26087 };26115 };
26088 // OBMC and interintra are mutually exclusive (`motion_mode` is26116 // OBMC and interintra are mutually exclusive (`motion_mode` is
26089 // never read for an interintra block), so blending interintra26117 // never read for an interintra block), so blending interintra
@@ -26108,7 +26136,9 @@ fn decode_inter_block(
26108 if interintra_mode.is_some() {26136 if interintra_mode.is_some() {
26109 flush_recon(fctx, y, u, v);26137 flush_recon(fctx, y, u, v);
26110 }26138 }
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;
26112 (pred_y, pred_u, pred_v) = (by, bu, bv);26142 (pred_y, pred_u, pred_v) = (by, bu, bv);
26113 if obmc_selected {26143 if obmc_selected {
26114 obmc_blend(26144 obmc_blend(