From 583ed9036e6f68f3e36342116107a0b0622fe575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 17:35:05 +0300 Subject: [PATCH] perf(av1): pool the post-filter plane buffers instead of allocating per frame Each frame allocated a pre-CDEF snapshot of all three planes, a post-deblock snapshot of all three when loop restoration is active, and one fresh `cdef.to_vec()` destination per restored plane: ~25 MB of multi-megabyte `Vec`s per frame at 4K, every one of them served by a fresh `mmap` whose pages then fault in on first touch, and returned to the allocator at the end of the frame. `PLANE_SCRATCH` hands the same buffers back out; `apply_loop_restoration_plane` now fills a caller-owned `out` buffer (refilled from `cdef` on entry, same "never filter in place" contract) which the caller swaps into the plane and returns to the pool. Byte-exact on seg1080/seg4k/seg4k_lr/palette; 4K 60.48 s -> 49.55 s (1.221x, from 1.156x at the previous step), LR-heavy 4K cut 59.95 s -> 54.16 s, 1080p 11.14 s. Peak RSS 417 MB -> 466 MB, the retained pool. --- crates/ec-av1/src/decode.rs | 82 ++++++++++++++++++++++++++++---- crates/ec-av1/src/restoration.rs | 16 ++++--- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 08990c41..e38682a2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -16354,6 +16354,43 @@ fn deblock_plane( } } +thread_local! { + /// lane-perf6: reused whole-plane `u16` buffers. Every frame takes a + /// pre-CDEF snapshot of all three planes, a post-deblock snapshot when + /// loop restoration is active, and a fresh LR destination per plane -- + /// freshly allocated that is ~25 MB of `mmap`/`munmap` per frame at 4K, + /// so every sample written also takes a first-touch page fault. The + /// buffers are handed back here instead and refilled in place. + static PLANE_SCRATCH: std::cell::RefCell>> = + const { std::cell::RefCell::new(Vec::new()) }; +} + +/// A pooled buffer holding a copy of `src`. +fn scratch_from(src: &[u16]) -> Vec { + let mut v = PLANE_SCRATCH.with(|p| p.borrow_mut().pop()).unwrap_or_default(); + v.clear(); + v.extend_from_slice(src); + v +} + +/// An empty pooled buffer, for a caller that fills it itself. +fn scratch_empty() -> Vec { + let mut v = PLANE_SCRATCH.with(|p| p.borrow_mut().pop()).unwrap_or_default(); + v.clear(); + v +} + +/// A plane's post-deblock snapshot, its samples taken from the pool (every +/// other field is `usize`). +fn plane_snapshot(p: &PlaneBuf) -> PlaneBuf { + PlaneBuf { data: scratch_from(&p.data), ..*p } +} + +/// Hands a buffer back to the pool; the capacity is what is worth keeping. +fn scratch_return(v: Vec) { + PLANE_SCRATCH.with(|p| p.borrow_mut().push(v)); +} + fn apply_cdef( y: &mut PlaneBuf, u: &mut PlaneBuf, @@ -16424,6 +16461,8 @@ fn apply_cdef( return; } let sb_cols = CDEF_SB_COLS.with(|c| c.get()); + // One entry per superblock (a few hundred at 4K), so this clone is not + // one of the per-frame copies lane-perf6 pooled below. let idx_grid = CDEF_IDX_GRID.with(|g| g.borrow().clone()); let strength_idx = |mi_r: usize, mi_c: usize| -> usize { if sb_cols == 0 { @@ -16435,9 +16474,9 @@ fn apply_cdef( .copied() .unwrap_or(0) as usize }; - let src_y = y.data.clone(); - let src_u = u.data.clone(); - let src_v = v.data.clone(); + let src_y = scratch_from(&y.data); + let src_u = scratch_from(&u.data); + let src_v = scratch_from(&v.data); // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. let coeff_shift = i32::from(bit_depth()) - 8; @@ -16561,6 +16600,9 @@ fn apply_cdef( } mi_r += 2; } + scratch_return(src_y); + scratch_return(src_u); + scratch_return(src_v); } /// Spec 7.17: loop restoration, run after [`apply_cdef`] (a sibling lane's @@ -16598,7 +16640,8 @@ fn apply_loop_restoration( // (class av1-truesize-lane: true frame size vs mi-rounded). (frame_w, frame_h): (usize, usize), ) { - y.data = crate::restoration::apply_loop_restoration_plane( + let mut out = scratch_empty(); + crate::restoration::apply_loop_restoration_plane( &y.data, &deblocked_y.data, y.width, @@ -16609,8 +16652,12 @@ fn apply_loop_restoration( lr.loop_restoration_size[0], grid, 0, + &mut out, ); - u.data = crate::restoration::apply_loop_restoration_plane( + std::mem::swap(&mut y.data, &mut out); + scratch_return(out); + let mut out = scratch_empty(); + crate::restoration::apply_loop_restoration_plane( &u.data, &deblocked_u.data, u.width, @@ -16621,8 +16668,12 @@ fn apply_loop_restoration( lr.loop_restoration_size[1], grid, 1, + &mut out, ); - v.data = crate::restoration::apply_loop_restoration_plane( + std::mem::swap(&mut u.data, &mut out); + scratch_return(out); + let mut out = scratch_empty(); + crate::restoration::apply_loop_restoration_plane( &v.data, &deblocked_v.data, v.width, @@ -16633,7 +16684,10 @@ fn apply_loop_restoration( lr.loop_restoration_size[2], grid, 2, + &mut out, ); + std::mem::swap(&mut v.data, &mut out); + scratch_return(out); } /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes, @@ -18949,7 +19003,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None && lr.loop_restoration_size[p] != 0 }); - let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone())); + let deblocked = + lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v))); // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter // dump above these bisect WHICH filter stage introduced a mismatch. @@ -19007,6 +19062,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( (lr_w, frame_height as usize), ); } + if let Some((db_y, db_u, db_v)) = deblocked { + scratch_return(db_y.data); + scratch_return(db_u.data); + scratch_return(db_v.data); + } let (fw, fh) = (lr_w, frame_height as usize); // An upscaled frame is already at its final size with no mi-aligned @@ -30977,7 +31037,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None && lr.loop_restoration_size[p] != 0 }); - let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone())); + let deblocked = + lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v))); // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter // dump above these bisect WHICH filter stage introduced a mismatch. @@ -31035,6 +31096,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( (lr_w, frame_height as usize), ); } + if let Some((db_y, db_u, db_v)) = deblocked { + scratch_return(db_y.data); + scratch_return(db_u.data); + scratch_return(db_v.data); + } let motion_field = build_motion_field( &grid, diff --git a/crates/ec-av1/src/restoration.rs b/crates/ec-av1/src/restoration.rs index 3a37bd71..017be673 100644 --- a/crates/ec-av1/src/restoration.rs +++ b/crates/ec-av1/src/restoration.rs @@ -929,7 +929,7 @@ fn filter_restoration_unit( /// restoration-unit pixels replaced -- a restoration unit must never read /// another, already-filtered unit's output (spec keeps a separate /// destination buffer for exactly this reason), so this never filters in -/// place. +/// place -- `out` is that buffer, refilled from `cdef` on entry. #[allow(clippy::too_many_arguments)] pub(crate) fn apply_loop_restoration_plane( cdef: &[u16], @@ -942,10 +942,15 @@ pub(crate) fn apply_loop_restoration_plane( unit_size: u32, grid: &RestorationGrid, plane: usize, -) -> Vec { - let mut out = cdef.to_vec(); + // lane-perf6: the destination buffer, refilled from `cdef` here. It used + // to be a fresh `cdef.to_vec()` per plane per frame -- a multi-megabyte + // allocation whose every page then faulted in on first touch. + out: &mut Vec, +) { + out.clear(); + out.extend_from_slice(cdef); if ftype == RestorationType::None || unit_size == 0 { - return out; + return; } let voffset = 8u32 >> ss_y; let ext_size = (unit_size * 3) / 2; @@ -968,7 +973,7 @@ pub(crate) fn apply_loop_restoration_plane( let filter = grid.get(plane, rrow, rcol); if filter != UnitFilter::None { filter_restoration_unit( - &mut out, + out, cdef, deblocked, stride, @@ -989,7 +994,6 @@ pub(crate) fn apply_loop_restoration_plane( rrow += 1; } debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); - out } #[cfg(test)]