Recycle the three per-superblock recon-job buffers instead of allocating them per submit
Browse files at this commitce3d0d92ce4209671f45405234084fb5be9a298cParent: 2426e3d- perf
- recycle the three per-superblock recon-job buffers instead of allocating them per submit
1 changed file +40 -6
- crates/ec-av1/src/decode.rs +40 -6
crates/ec-av1/src/decode.rsmodified+40 -6
| @@ -2559,8 +2559,19 @@ struct WaveQueue { | |||
| 2559 | /// Jobs popped but not finished -- what [`wave_barrier`] waits out. | 2559 | /// Jobs popped but not finished -- what [`wave_barrier`] waits out. |
| 2560 | active: usize, | 2560 | active: usize, |
| 2561 | closed: bool, | 2561 | 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, | ||
| 2562 | } | 2570 | } |
| 2563 | 2571 | ||
| 2572 | +/// One recycled set of [`ReconJob`] buffers (lane-sballoc). | ||
| 2573 | +type JobBufs = (Vec<ReconOp>, Vec<i32>, Vec<u16>); | ||
| 2574 | + | ||
| 2564 | impl WaveQueue { | 2575 | impl WaveQueue { |
| 2565 | /// The lowest-row queued job whose wavefront dependencies are satisfied, | 2576 | /// The lowest-row queued job whose wavefront dependencies are satisfied, |
| 2566 | /// or `None` (with the blocking condition counted when instrumented). | 2577 | /// or `None` (with the blocking condition counted when instrumented). |
| @@ -2701,15 +2712,22 @@ fn wave_worker(st: &WaveState, idx: usize) { | |||
| 2701 | // every superblock that could have written or be reading them is | 2712 | // every superblock that could have written or be reading them is |
| 2702 | // complete by the wavefront rule above. | 2713 | // complete by the wavefront rule above. |
| 2703 | let mut cur: [Vec<u16>; 3] = Default::default(); | 2714 | let mut cur: [Vec<u16>; 3] = Default::default(); |
| 2715 | + let ReconJob { mut ops, mut coeffs, mut preds } = job; | ||
| 2704 | unsafe { | 2716 | unsafe { |
| 2705 | - for op in job.ops { | 2717 | + for op in ops.drain(..) { |
| 2706 | - exec_op(op, &mut *y, &mut *u, &mut *v, &mut cur, &job.coeffs, &job.preds, &fctx); | 2718 | + exec_op(op, &mut *y, &mut *u, &mut *v, &mut cur, &coeffs, &preds, &fctx); |
| 2707 | } | 2719 | } |
| 2708 | } | 2720 | } |
| 2709 | if stats { | 2721 | if stats { |
| 2710 | WS_BUSY[slot].fetch_add(t1.elapsed().as_nanos() as u64, Relaxed); | 2722 | WS_BUSY[slot].fetch_add(t1.elapsed().as_nanos() as u64, Relaxed); |
| 2711 | } | 2723 | } |
| 2724 | + coeffs.clear(); | ||
| 2725 | + preds.clear(); | ||
| 2712 | let mut q = st.q.lock().unwrap(); | 2726 | 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 | + } | ||
| 2713 | q.done[r] = c as isize; | 2731 | q.done[r] = c as isize; |
| 2714 | q.active -= 1; | 2732 | q.active -= 1; |
| 2715 | st.cv.notify_all(); | 2733 | st.cv.notify_all(); |
| @@ -2750,6 +2768,8 @@ impl<'a> WaveGuard<'a> { | |||
| 2750 | last_push: vec![-1; rows], | 2768 | last_push: vec![-1; rows], |
| 2751 | active: 0, | 2769 | active: 0, |
| 2752 | closed: false, | 2770 | closed: false, |
| 2771 | + free: Vec::new(), | ||
| 2772 | + free_cap: threads + 2, | ||
| 2753 | }), | 2773 | }), |
| 2754 | cv: std::sync::Condvar::new(), | 2774 | cv: std::sync::Condvar::new(), |
| 2755 | // SAFETY: the planes are locals of the tile decoder and outlive | 2775 | // SAFETY: the planes are locals of the tile decoder and outlive |
| @@ -2792,6 +2812,16 @@ impl Drop for WaveGuard<'_> { | |||
| 2792 | } | 2812 | } |
| 2793 | } | 2813 | } |
| 2794 | 2814 | ||
| 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 | + | ||
| 2795 | /// Hands superblock `(r, c)`'s recorded writes to the wavefront. `false` | 2825 | /// Hands superblock `(r, c)`'s recorded writes to the wavefront. `false` |
| 2796 | /// when there is no wavefront (one reconstruction thread), in which case the | 2826 | /// when there is no wavefront (one reconstruction thread), in which case the |
| 2797 | /// caller replays them itself. | 2827 | /// caller replays them itself. |
| @@ -2802,10 +2832,15 @@ fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool { | |||
| 2802 | // exactly as the two arenas below are -- taking it left a zero-capacity | 2832 | // exactly as the two arenas below are -- taking it left a zero-capacity |
| 2803 | // `Vec` that regrew (and memmoved every recorded op) from nothing on | 2833 | // `Vec` that regrew (and memmoved every recorded op) from nothing on |
| 2804 | // every superblock. | 2834 | // 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(); | ||
| 2805 | let ops = fctx.recon_ops.with(|c| { | 2840 | let ops = fctx.recon_ops.with(|c| { |
| 2806 | let mut c = c.borrow_mut(); | 2841 | let mut c = c.borrow_mut(); |
| 2807 | let cap = c.len(); | 2842 | let cap = c.len(); |
| 2808 | - std::mem::replace(&mut *c, Vec::with_capacity(cap)) | 2843 | + std::mem::replace(&mut *c, reuse(sops, cap)) |
| 2809 | }); | 2844 | }); |
| 2810 | // The job owns its coefficients: the arena is replaced by an empty one | 2845 | // The job owns its coefficients: the arena is replaced by an empty one |
| 2811 | // sized for the next superblock, so parse never regrows from nothing and | 2846 | // 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 { | |||
| 2813 | let coeffs = fctx.coeff_arena.with(|c| { | 2848 | let coeffs = fctx.coeff_arena.with(|c| { |
| 2814 | let mut c = c.borrow_mut(); | 2849 | let mut c = c.borrow_mut(); |
| 2815 | let cap = c.len(); | 2850 | let cap = c.len(); |
| 2816 | - std::mem::replace(&mut *c, Vec::with_capacity(cap)) | 2851 | + std::mem::replace(&mut *c, reuse(scoeffs, cap)) |
| 2817 | }); | 2852 | }); |
| 2818 | let preds = fctx.pred_arena.with(|c| { | 2853 | let preds = fctx.pred_arena.with(|c| { |
| 2819 | let mut c = c.borrow_mut(); | 2854 | let mut c = c.borrow_mut(); |
| 2820 | let cap = c.len(); | 2855 | let cap = c.len(); |
| 2821 | - std::mem::replace(&mut *c, Vec::with_capacity(cap)) | 2856 | + std::mem::replace(&mut *c, reuse(spreds, cap)) |
| 2822 | }); | 2857 | }); |
| 2823 | { | 2858 | { |
| 2824 | - let mut q = st.q.lock().unwrap(); | ||
| 2825 | if (c as isize) > q.last_sub[r] { | 2859 | if (c as isize) > q.last_sub[r] { |
| 2826 | q.last_sub[r] = c as isize; | 2860 | q.last_sub[r] = c as isize; |
| 2827 | } | 2861 | } |