forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-sballoc' (e9bd3392): the three per-superblock reconstruction buffers are recycled through a bounded free list on the wave queue (no three allocations per superblock); byte-exact on 32 hashes across both feature builds, -0.12% instructions at the composed setting

Browse files at this commit

d2d7dd10b169b54c2dafd2a361cac237497224faAhmet Kaan GümüşParent: e8b9893ce3d0d9
crates/ec-av1/src/decode.rsmodified+40 -6
@@ -2559,8 +2559,19 @@ struct WaveQueue {
25592559 /// Jobs popped but not finished -- what [`wave_barrier`] waits out.
25602560 active: usize,
25612561 closed: bool,
2562+ /// lane-sballoc: the three per-superblock buffers a finished job hands
2563+ /// back, so [`wave_submit`] takes a warm allocation (capacity kept, no
2564+ /// first-touch page faults) instead of the `Vec::with_capacity` it used
2565+ /// to make per superblock -- 3 allocations and 3 frees per superblock,
2566+ /// 4680 per 4K frame. Bounded at `recon_threads + 2` sets, and it lives
2567+ /// inside the queue's own mutex, which both sides already take.
2568+ free: Vec<JobBufs>,
2569+ free_cap: usize,
25622570 }
25632571
2572+/// One recycled set of [`ReconJob`] buffers (lane-sballoc).
2573+type JobBufs = (Vec<ReconOp>, Vec<i32>, Vec<u16>);
2574+
25642575 impl WaveQueue {
25652576 /// The lowest-row queued job whose wavefront dependencies are satisfied,
25662577 /// or `None` (with the blocking condition counted when instrumented).
@@ -2701,15 +2712,22 @@ fn wave_worker(st: &WaveState, idx: usize) {
27012712 // every superblock that could have written or be reading them is
27022713 // complete by the wavefront rule above.
27032714 let mut cur: [Vec<u16>; 3] = Default::default();
2715+ let ReconJob { mut ops, mut coeffs, mut preds } = job;
27042716 unsafe {
2705- for op in job.ops {
2706- exec_op(op, &mut *y, &mut *u, &mut *v, &mut cur, &job.coeffs, &job.preds, &fctx);
2717+ for op in ops.drain(..) {
2718+ exec_op(op, &mut *y, &mut *u, &mut *v, &mut cur, &coeffs, &preds, &fctx);
27072719 }
27082720 }
27092721 if stats {
27102722 WS_BUSY[slot].fetch_add(t1.elapsed().as_nanos() as u64, Relaxed);
27112723 }
2724+ coeffs.clear();
2725+ preds.clear();
27122726 let mut q = st.q.lock().unwrap();
2727+ // lane-sballoc: hand the three buffers back for the next submit.
2728+ if q.free.len() < q.free_cap {
2729+ q.free.push((ops, coeffs, preds));
2730+ }
27132731 q.done[r] = c as isize;
27142732 q.active -= 1;
27152733 st.cv.notify_all();
@@ -2750,6 +2768,8 @@ impl<'a> WaveGuard<'a> {
27502768 last_push: vec![-1; rows],
27512769 active: 0,
27522770 closed: false,
2771+ free: Vec::new(),
2772+ free_cap: threads + 2,
27532773 }),
27542774 cv: std::sync::Condvar::new(),
27552775 // SAFETY: the planes are locals of the tile decoder and outlive
@@ -2792,6 +2812,16 @@ impl Drop for WaveGuard<'_> {
27922812 }
27932813 }
27942814
2815+/// An empty buffer for the next superblock: the recycled one when it is at
2816+/// least as big as the superblock just parsed, else a fresh sized one
2817+/// (lane-sballoc; `v` is always empty).
2818+fn reuse<T>(mut v: Vec<T>, cap: usize) -> Vec<T> {
2819+ if v.capacity() < cap {
2820+ v.reserve_exact(cap - v.len());
2821+ }
2822+ v
2823+}
2824+
27952825 /// Hands superblock `(r, c)`'s recorded writes to the wavefront. `false`
27962826 /// when there is no wavefront (one reconstruction thread), in which case the
27972827 /// caller replays them itself.
@@ -2802,10 +2832,15 @@ fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool {
28022832 // exactly as the two arenas below are -- taking it left a zero-capacity
28032833 // `Vec` that regrew (and memmoved every recorded op) from nothing on
28042834 // every superblock.
2835+ // lane-sballoc: the replacement comes from the finished jobs' free list
2836+ // when there is one (warm capacity, already faulted in); only the first
2837+ // superblocks of a tile allocate.
2838+ let mut q = st.q.lock().unwrap();
2839+ let (sops, scoeffs, spreds) = q.free.pop().unwrap_or_default();
28052840 let ops = fctx.recon_ops.with(|c| {
28062841 let mut c = c.borrow_mut();
28072842 let cap = c.len();
2808- std::mem::replace(&mut *c, Vec::with_capacity(cap))
2843+ std::mem::replace(&mut *c, reuse(sops, cap))
28092844 });
28102845 // The job owns its coefficients: the arena is replaced by an empty one
28112846 // sized for the next superblock, so parse never regrows from nothing and
@@ -2813,15 +2848,14 @@ fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool {
28132848 let coeffs = fctx.coeff_arena.with(|c| {
28142849 let mut c = c.borrow_mut();
28152850 let cap = c.len();
2816- std::mem::replace(&mut *c, Vec::with_capacity(cap))
2851+ std::mem::replace(&mut *c, reuse(scoeffs, cap))
28172852 });
28182853 let preds = fctx.pred_arena.with(|c| {
28192854 let mut c = c.borrow_mut();
28202855 let cap = c.len();
2821- std::mem::replace(&mut *c, Vec::with_capacity(cap))
2856+ std::mem::replace(&mut *c, reuse(spreds, cap))
28222857 });
28232858 {
2824- let mut q = st.q.lock().unwrap();
28252859 if (c as isize) > q.last_sub[r] {
28262860 q.last_sub[r] = c as isize;
28272861 }