forge
Explore
Log in

Don't you dare go Hollow

The DPB shares one Arc per picture instead of copying it per slot

Browse files at this commit

68c67bf9b2c11830fc5a391a5d89d4e2e7b16ae2Ahmet Kaan GümüşParent: b83300d
  • perf
    • the DPB shares one Arc per picture instead of copying it per slot
Show full message
perf(av1): the DPB shares one Arc per picture instead of copying it per slot

`refresh_frame_flags` names up to eight reference slots, and each one took a
full `Picture` copy -- ~25 MB at 4K, so a key frame alone memcpy'd ~200 MB and
every inter frame paid one copy per slot it refreshed. `perf --call-graph fp`
attributes 2.2 of the 5.7% `memmove` self time to `decode_stream`'s own frame
(the slot stores), the rest to the tile decode, `decode_inter_block`, loop
restoration and the per-TU transform buffer.

The slots now hold `Arc<Picture>`: one allocation per decoded frame, pointer
copies per slot, and the shown-frame sink borrows either the grain-synthesised
picture or the stored one instead of moving a copy through `output`.

seg4k 38.59 s -> 37.62 s (medians of 3 interleaved pairs); byte-exact on
seg1080/seg4k/seg4k_lr/palette.
crates/ec-av1/src/stream.rsmodified+14 -7
@@ -281,7 +281,11 @@ pub fn decode_stream_with(
281 // luma-only (chroma coincidentally shares this fixture's flat regions)281 // luma-only (chroma coincidentally shares this fixture's flat regions)
282 // and small-magnitude (the two pictures mostly agree), which is exactly282 // and small-magnitude (the two pictures mostly agree), which is exactly
283 // what made it look like a rounding bug rather than a wrong reference.283 // what made it look like a rounding bug rather than a wrong reference.
284- let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None);284+ // lane-perf9: the DPB holds each picture behind an `Arc`, so a frame whose
285+ // `refresh_frame_flags` names several slots (a key frame names all eight)
286+ // stores eight pointers instead of eight ~25 MB 4K picture copies.
287+ let mut ref_slots: [Option<std::sync::Arc<Picture>>; NUM_REF_FRAMES] =
288+ std::array::from_fn(|_| None);
285 // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion289 // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion
286 // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom290 // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom
287 // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was291 // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was
@@ -371,7 +375,7 @@ pub fn decode_stream_with(
371 bit_depth as u32,375 bit_depth as u32,
372 )376 )
373 } else {377 } else {
374- picture378+ (*picture).clone()
375 };379 };
376 sink(&output, pictures_decoded, true)?;380 sink(&output, pictures_decoded, true)?;
377 pictures_shown += 1;381 pictures_shown += 1;
@@ -851,7 +855,7 @@ pub fn decode_stream_with(
851 } else {855 } else {
852 ref_slots856 ref_slots
853 .get(header.ref_frame_idx[ref_frame - 1] as usize)857 .get(header.ref_frame_idx[ref_frame - 1] as usize)
854- .and_then(Option::as_ref)858+ .and_then(|slot| slot.as_deref())
855 }859 }
856 });860 });
857 // spec 7.9's own driver, `av1_setup_motion_field`: only run when861 // spec 7.9's own driver, `av1_setup_motion_field`: only run when
@@ -923,6 +927,9 @@ pub fn decode_stream_with(
923 )?;927 )?;
924 (picture, end_cdfs, motion_field)928 (picture, end_cdfs, motion_field)
925 };929 };
930+ // lane-perf9: one allocation per decoded frame, then every reference
931+ // slot this frame refreshes (and the sink) shares it.
932+ let picture = std::sync::Arc::new(picture);
926 // lane-lr r4: the Wiener/self-guided pixel filters are wired933 // lane-lr r4: the Wiener/self-guided pixel filters are wired
927 // (`decode.rs::apply_loop_restoration`) -- no more refusal here,934 // (`decode.rs::apply_loop_restoration`) -- no more refusal here,
928 // `uses_lr` frames now decode all the way to pixels.935 // `uses_lr` frames now decode all the way to pixels.
@@ -1029,16 +1036,16 @@ pub fn decode_stream_with(
1029 let mc_identity = parser1036 let mc_identity = parser
1030 .sequence_header()1037 .sequence_header()
1031 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);1038 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);
1032- crate::film_grain::apply_grain(1039+ Some(crate::film_grain::apply_grain(
1033 &picture,1040 &picture,
1034 &header.film_grain,1041 &header.film_grain,
1035 mc_identity,1042 mc_identity,
1036 bit_depth as u32,1043 bit_depth as u32,
1037- )1044+ ))
1038 } else {1045 } else {
1039- picture1046+ None
1040 };1047 };
1041- sink(&output, pictures_decoded - 1, true)?;1048+ sink(output.as_ref().unwrap_or(&picture), pictures_decoded - 1, true)?;
1042 pictures_shown += 1;1049 pictures_shown += 1;
1043 } else {1050 } else {
1044 // Hidden (alt-ref/forward-key) frames never reach1051 // Hidden (alt-ref/forward-key) frames never reach