forge
Explore
Log in

Don't you dare go Hollow

Pool the post-filter plane buffers instead of allocating per frame

Browse files at this commit

583ed9036e6f68f3e36342116107a0b0622fe575Ahmet Kaan GümüşParent: 126001f
  • perf
    • pool the post-filter plane buffers instead of allocating per frame
Show full message
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.rsmodified+74 -8
@@ -16354,6 +16354,43 @@ fn deblock_plane(
16354 }16354 }
16355 }16355 }
16356 16356
16357+thread_local! {
16358+ /// lane-perf6: reused whole-plane `u16` buffers. Every frame takes a
16359+ /// pre-CDEF snapshot of all three planes, a post-deblock snapshot when
16360+ /// loop restoration is active, and a fresh LR destination per plane --
16361+ /// freshly allocated that is ~25 MB of `mmap`/`munmap` per frame at 4K,
16362+ /// so every sample written also takes a first-touch page fault. The
16363+ /// buffers are handed back here instead and refilled in place.
16364+ static PLANE_SCRATCH: std::cell::RefCell<Vec<Vec<u16>>> =
16365+ const { std::cell::RefCell::new(Vec::new()) };
16366+}
16367+
16368+/// A pooled buffer holding a copy of `src`.
16369+fn scratch_from(src: &[u16]) -> Vec<u16> {
16370+ let mut v = PLANE_SCRATCH.with(|p| p.borrow_mut().pop()).unwrap_or_default();
16371+ v.clear();
16372+ v.extend_from_slice(src);
16373+ v
16374+}
16375+
16376+/// An empty pooled buffer, for a caller that fills it itself.
16377+fn scratch_empty() -> Vec<u16> {
16378+ let mut v = PLANE_SCRATCH.with(|p| p.borrow_mut().pop()).unwrap_or_default();
16379+ v.clear();
16380+ v
16381+}
16382+
16383+/// A plane's post-deblock snapshot, its samples taken from the pool (every
16384+/// other field is `usize`).
16385+fn plane_snapshot(p: &PlaneBuf) -> PlaneBuf {
16386+ PlaneBuf { data: scratch_from(&p.data), ..*p }
16387+}
16388+
16389+/// Hands a buffer back to the pool; the capacity is what is worth keeping.
16390+fn scratch_return(v: Vec<u16>) {
16391+ PLANE_SCRATCH.with(|p| p.borrow_mut().push(v));
16392+}
16393+
16357 fn apply_cdef(16394 fn apply_cdef(
16358 y: &mut PlaneBuf,16395 y: &mut PlaneBuf,
16359 u: &mut PlaneBuf,16396 u: &mut PlaneBuf,
@@ -16424,6 +16461,8 @@ fn apply_cdef(
16424 return;16461 return;
16425 }16462 }
16426 let sb_cols = CDEF_SB_COLS.with(|c| c.get());16463 let sb_cols = CDEF_SB_COLS.with(|c| c.get());
16464+ // One entry per superblock (a few hundred at 4K), so this clone is not
16465+ // one of the per-frame copies lane-perf6 pooled below.
16427 let idx_grid = CDEF_IDX_GRID.with(|g| g.borrow().clone());16466 let idx_grid = CDEF_IDX_GRID.with(|g| g.borrow().clone());
16428 let strength_idx = |mi_r: usize, mi_c: usize| -> usize {16467 let strength_idx = |mi_r: usize, mi_c: usize| -> usize {
16429 if sb_cols == 0 {16468 if sb_cols == 0 {
@@ -16435,9 +16474,9 @@ fn apply_cdef(
16435 .copied()16474 .copied()
16436 .unwrap_or(0) as usize16475 .unwrap_or(0) as usize
16437 };16476 };
16438- let src_y = y.data.clone();16477+ let src_y = scratch_from(&y.data);
16439- let src_u = u.data.clone();16478+ let src_u = scratch_from(&u.data);
16440- let src_v = v.data.clone();16479+ let src_v = scratch_from(&v.data);
16441 // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,16480 // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,
16442 // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.16481 // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.
16443 let coeff_shift = i32::from(bit_depth()) - 8;16482 let coeff_shift = i32::from(bit_depth()) - 8;
@@ -16561,6 +16600,9 @@ fn apply_cdef(
16561 }16600 }
16562 mi_r += 2;16601 mi_r += 2;
16563 }16602 }
16603+ scratch_return(src_y);
16604+ scratch_return(src_u);
16605+ scratch_return(src_v);
16564 }16606 }
16565 16607
16566 /// Spec 7.17: loop restoration, run after [`apply_cdef`] (a sibling lane's16608 /// Spec 7.17: loop restoration, run after [`apply_cdef`] (a sibling lane's
@@ -16598,7 +16640,8 @@ fn apply_loop_restoration(
16598 // (class av1-truesize-lane: true frame size vs mi-rounded).16640 // (class av1-truesize-lane: true frame size vs mi-rounded).
16599 (frame_w, frame_h): (usize, usize),16641 (frame_w, frame_h): (usize, usize),
16600 ) {16642 ) {
16601- y.data = crate::restoration::apply_loop_restoration_plane(16643+ let mut out = scratch_empty();
16644+ crate::restoration::apply_loop_restoration_plane(
16602 &y.data,16645 &y.data,
16603 &deblocked_y.data,16646 &deblocked_y.data,
16604 y.width,16647 y.width,
@@ -16609,8 +16652,12 @@ fn apply_loop_restoration(
16609 lr.loop_restoration_size[0],16652 lr.loop_restoration_size[0],
16610 grid,16653 grid,
16611 0,16654 0,
16655+ &mut out,
16612 );16656 );
16613- u.data = crate::restoration::apply_loop_restoration_plane(16657+ std::mem::swap(&mut y.data, &mut out);
16658+ scratch_return(out);
16659+ let mut out = scratch_empty();
16660+ crate::restoration::apply_loop_restoration_plane(
16614 &u.data,16661 &u.data,
16615 &deblocked_u.data,16662 &deblocked_u.data,
16616 u.width,16663 u.width,
@@ -16621,8 +16668,12 @@ fn apply_loop_restoration(
16621 lr.loop_restoration_size[1],16668 lr.loop_restoration_size[1],
16622 grid,16669 grid,
16623 1,16670 1,
16671+ &mut out,
16624 );16672 );
16625- v.data = crate::restoration::apply_loop_restoration_plane(16673+ std::mem::swap(&mut u.data, &mut out);
16674+ scratch_return(out);
16675+ let mut out = scratch_empty();
16676+ crate::restoration::apply_loop_restoration_plane(
16626 &v.data,16677 &v.data,
16627 &deblocked_v.data,16678 &deblocked_v.data,
16628 v.width,16679 v.width,
@@ -16633,7 +16684,10 @@ fn apply_loop_restoration(
16633 lr.loop_restoration_size[2],16684 lr.loop_restoration_size[2],
16634 grid,16685 grid,
16635 2,16686 2,
16687+ &mut out,
16636 );16688 );
16689+ std::mem::swap(&mut v.data, &mut out);
16690+ scratch_return(out);
16637 }16691 }
16638 16692
16639 /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes,16693 /// Decodes the payload [`crate::tile::sb_coeff_key_frame_tile`] writes,
@@ -18949,7 +19003,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
18949 lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None19003 lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
18950 && lr.loop_restoration_size[p] != 019004 && lr.loop_restoration_size[p] != 0
18951 });19005 });
18952- let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone()));19006+ let deblocked =
19007+ lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v)));
18953 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own19008 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
18954 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter19009 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
18955 // dump above these bisect WHICH filter stage introduced a mismatch.19010 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -19007,6 +19062,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
19007 (lr_w, frame_height as usize),19062 (lr_w, frame_height as usize),
19008 );19063 );
19009 }19064 }
19065+ if let Some((db_y, db_u, db_v)) = deblocked {
19066+ scratch_return(db_y.data);
19067+ scratch_return(db_u.data);
19068+ scratch_return(db_v.data);
19069+ }
19010 19070
19011 let (fw, fh) = (lr_w, frame_height as usize);19071 let (fw, fh) = (lr_w, frame_height as usize);
19012 // An upscaled frame is already at its final size with no mi-aligned19072 // 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(
30977 lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None31037 lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
30978 && lr.loop_restoration_size[p] != 031038 && lr.loop_restoration_size[p] != 0
30979 });31039 });
30980- let deblocked = lr_active.then(|| (y.clone(), u.clone(), v.clone()));31040+ let deblocked =
31041+ lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v)));
30981 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own31042 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
30982 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter31043 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
30983 // dump above these bisect WHICH filter stage introduced a mismatch.31044 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -31035,6 +31096,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
31035 (lr_w, frame_height as usize),31096 (lr_w, frame_height as usize),
31036 );31097 );
31037 }31098 }
31099+ if let Some((db_y, db_u, db_v)) = deblocked {
31100+ scratch_return(db_y.data);
31101+ scratch_return(db_u.data);
31102+ scratch_return(db_v.data);
31103+ }
31038 31104
31039 let motion_field = build_motion_field(31105 let motion_field = build_motion_field(
31040 &grid,31106 &grid,
crates/ec-av1/src/restoration.rsmodified+10 -6
@@ -929,7 +929,7 @@ fn filter_restoration_unit(
929 /// restoration-unit pixels replaced -- a restoration unit must never read929 /// restoration-unit pixels replaced -- a restoration unit must never read
930 /// another, already-filtered unit's output (spec keeps a separate930 /// another, already-filtered unit's output (spec keeps a separate
931 /// destination buffer for exactly this reason), so this never filters in931 /// destination buffer for exactly this reason), so this never filters in
932-/// place.932+/// place -- `out` is that buffer, refilled from `cdef` on entry.
933 #[allow(clippy::too_many_arguments)]933 #[allow(clippy::too_many_arguments)]
934 pub(crate) fn apply_loop_restoration_plane(934 pub(crate) fn apply_loop_restoration_plane(
935 cdef: &[u16],935 cdef: &[u16],
@@ -942,10 +942,15 @@ pub(crate) fn apply_loop_restoration_plane(
942 unit_size: u32,942 unit_size: u32,
943 grid: &RestorationGrid,943 grid: &RestorationGrid,
944 plane: usize,944 plane: usize,
945-) -> Vec<u16> {945+ // lane-perf6: the destination buffer, refilled from `cdef` here. It used
946- let mut out = cdef.to_vec();946+ // to be a fresh `cdef.to_vec()` per plane per frame -- a multi-megabyte
947+ // allocation whose every page then faulted in on first touch.
948+ out: &mut Vec<u16>,
949+) {
950+ out.clear();
951+ out.extend_from_slice(cdef);
947 if ftype == RestorationType::None || unit_size == 0 {952 if ftype == RestorationType::None || unit_size == 0 {
948- return out;953+ return;
949 }954 }
950 let voffset = 8u32 >> ss_y;955 let voffset = 8u32 >> ss_y;
951 let ext_size = (unit_size * 3) / 2;956 let ext_size = (unit_size * 3) / 2;
@@ -968,7 +973,7 @@ pub(crate) fn apply_loop_restoration_plane(
968 let filter = grid.get(plane, rrow, rcol);973 let filter = grid.get(plane, rrow, rcol);
969 if filter != UnitFilter::None {974 if filter != UnitFilter::None {
970 filter_restoration_unit(975 filter_restoration_unit(
971- &mut out,976+ out,
972 cdef,977 cdef,
973 deblocked,978 deblocked,
974 stride,979 stride,
@@ -989,7 +994,6 @@ pub(crate) fn apply_loop_restoration_plane(
989 rrow += 1;994 rrow += 1;
990 }995 }
991 debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units");996 debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units");
992- out
993 }997 }
994 998
995 #[cfg(test)]999 #[cfg(test)]