The wavefront's op list and prediction buffers are reused per block
Browse files at this commite1f00c96c93e2029c911487ded5ed0f213bebeafParent: bccf2c8- perf
- the wavefront's op list and prediction buffers are reused per block
1 changed file +52 -22
- crates/ec-av1/src/decode.rs +52 -22
crates/ec-av1/src/decode.rsmodified+52 -22
| @@ -2275,7 +2275,11 @@ pub(crate) struct PredBuild( | ||
| 2275 | 2275 | &mut PlaneBuf<'static>, |
| 2276 | 2276 | &mut PlaneBuf<'static>, |
| 2277 | 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 | 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 | 2312 | /// Records one block's prediction build for the worker that reconstructs it. |
| 2294 | 2313 | #[allow(unsafe_code)] |
| 2295 | 2314 | fn push_pred<'a>( |
| @@ -2299,17 +2318,16 @@ fn push_pred<'a>( | ||
| 2299 | 2318 | &mut PlaneBuf<'static>, |
| 2300 | 2319 | &mut PlaneBuf<'static>, |
| 2301 | 2320 | &crate::decode::FrameCtx, |
| 2302 | - ) -> [Vec<u16>; 3] | |
| 2303 | - + 'a, | |
| 2321 | + &mut [Vec<u16>; 3], | |
| 2322 | + ) + 'a, | |
| 2304 | 2323 | ) { |
| 2305 | - type Built = [Vec<u16>; 3]; | |
| 2306 | 2324 | type Dyn<'x> = dyn FnOnce( |
| 2307 | 2325 | &mut PlaneBuf<'static>, |
| 2308 | 2326 | &mut PlaneBuf<'static>, |
| 2309 | 2327 | &mut PlaneBuf<'static>, |
| 2310 | 2328 | &crate::decode::FrameCtx, |
| 2311 | - ) -> Built | |
| 2312 | - + 'x; | |
| 2329 | + &mut [Vec<u16>; 3], | |
| 2330 | + ) + 'x; | |
| 2313 | 2331 | let boxed: Box<Dyn<'a>> = Box::new(f); |
| 2314 | 2332 | // SAFETY: see [`PredBuild`] -- every worker is joined by [`WaveGuard`] |
| 2315 | 2333 | // before the borrows the closure holds go out of scope. |
| @@ -2723,7 +2741,15 @@ impl Drop for WaveGuard<'_> { | ||
| 2723 | 2741 | fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool { |
| 2724 | 2742 | let st = fctx.wave.with(|w| w.borrow().clone()); |
| 2725 | 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 | 2753 | // The job owns its coefficients: the arena is replaced by an empty one |
| 2728 | 2754 | // sized for the next superblock, so parse never regrows from nothing and |
| 2729 | 2755 | // the worker frees this buffer once, instead of once per unit. |
| @@ -3095,7 +3121,7 @@ fn exec_op( | ||
| 3095 | 3121 | ) { |
| 3096 | 3122 | let mut scratch = Vec::new(); |
| 3097 | 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 | 3125 | ReconOp::Intra { |
| 3100 | 3126 | plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl, |
| 3101 | 3127 | filter_intra, smooth_neighbor, pred, |
| @@ -24700,8 +24726,8 @@ fn decode_inter_block( | ||
| 24700 | 24726 | let build = move |_y: &mut PlaneBuf<'static>, |
| 24701 | 24727 | _u: &mut PlaneBuf<'static>, |
| 24702 | 24728 | _v: &mut PlaneBuf<'static>, |
| 24703 | - fctx: &crate::decode::FrameCtx| | |
| 24704 | - -> [Vec<u16>; 3] { | |
| 24729 | + fctx: &crate::decode::FrameCtx, | |
| 24730 | + out: &mut [Vec<u16>; 3]| { | |
| 24705 | 24731 | let mut inter0_y = vec![0i32; side * side]; |
| 24706 | 24732 | mc::predict_compound_intermediate_kern( |
| 24707 | 24733 | &py0.data, |
| @@ -24756,7 +24782,7 @@ fn decode_inter_block( | ||
| 24756 | 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 | 24786 | let mut diffwtd_mask_y; |
| 24761 | 24787 | // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR |
| 24762 | 24788 | // 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 | 24866 | if let Some(mask_y) = mask_y { |
| 24841 | 24867 | mc::blend_masked_compound( |
| 24842 | 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 | 24940 | if let Some(mask_y) = mask_y { |
| 24915 | 24941 | mc::blend_masked_compound( |
| 24916 | 24942 | &inter0_v, |
| @@ -24928,7 +24954,7 @@ fn decode_inter_block( | ||
| 24928 | 24954 | |
| 24929 | 24955 | // lane-inter4 r2: a rectangular strip's transform tree starts from |
| 24930 | 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 | 24959 | let (pred_y, pred_u, pred_v): (Vec<u16>, Vec<u16>, Vec<u16>); |
| 24934 | 24960 | let (sy, su, sv) = if recon_deferred(fctx) { |
| @@ -24939,7 +24965,9 @@ fn decode_inter_block( | ||
| 24939 | 24965 | Pred::Cur { plane: 2, stride: chroma_side, off: 0 }, |
| 24940 | 24966 | ) |
| 24941 | 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 | 24971 | (pred_y, pred_u, pred_v) = (by, bu, bv); |
| 24944 | 24972 | ( |
| 24945 | 24973 | Pred::Inline(&pred_y, side), |
| @@ -25922,11 +25950,11 @@ fn decode_inter_block( | ||
| 25922 | 25950 | let build = move |y: &mut PlaneBuf<'static>, |
| 25923 | 25951 | u: &mut PlaneBuf<'static>, |
| 25924 | 25952 | 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); | |
| 25930 | 25958 | if luma_scale == mc::REF_NO_SCALE { |
| 25931 | 25959 | mc::predict_with_filters_kern( |
| 25932 | 25960 | &py_ref.data, |
| @@ -26083,7 +26111,7 @@ fn decode_inter_block( | ||
| 26083 | 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 | 26116 | // OBMC and interintra are mutually exclusive (`motion_mode` is |
| 26089 | 26117 | // never read for an interintra block), so blending interintra |
| @@ -26108,7 +26136,9 @@ fn decode_inter_block( | ||
| 26108 | 26136 | if interintra_mode.is_some() { |
| 26109 | 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 | 26142 | (pred_y, pred_u, pred_v) = (by, bu, bv); |
| 26113 | 26143 | if obmc_selected { |
| 26114 | 26144 | obmc_blend( |