forge
Explore
Log in

Don't you dare go Hollow

The reference bank and the output queue share the picture

Browse files at this commit

900cf2dae324a6041f8bee0c03d6a2c38a47f44bAhmet Kaan GümüşParent: a02c7bd
  • perf
    • the reference bank and the output queue share the picture
Show full message
perf(av1): the reference bank and the output queue share the picture

A decoded frame's picture was copied once per reference slot it
refreshes and once more to leave the worker (`Arc::try_unwrap` never
succeeded -- the slot promise holds the other handle), 24 MB per copy at
4K 10-bit. `ref_slots`, `Reorder` and the worker reply now carry
`Arc<Picture>`: the sink still takes `&Picture`, so no public signature
moves, and grain (which must not be stored in the bank) still allocates
its own output.

seg4k, all 12 sha6t hashes unchanged at (1,1,1), (6,2,4), (1,1,4).
Single thread: 30.39 s -> 29.71 s, page faults 397 k -> 111 k,
instructions:u 599.40 -> 600.57 G (+0.20%). Composed (6,2,4) best-of-2
interleaved: 12.58 s base -> 12.08 s, page faults 3.77 M -> 0.85 M.
crates/ec-av1/src/stream.rsmodified+27 -17
@@ -296,11 +296,15 @@ fn decode_threads() -> usize {
296296 #[derive(Default)]
297297 struct Reorder {
298298 next: usize,
299- pending: std::collections::BTreeMap<usize, (Picture, usize, bool)>,
299+ // lane-pool1: the picture is shared, not owned -- a decoded frame that
300+ // refreshes a reference slot is already behind an `Arc` for the slot, and
301+ // handing the output an owned `Picture` meant copying every plane of it
302+ // (24 MB per 4K 10-bit frame) purely to put it in this queue.
303+ pending: std::collections::BTreeMap<usize, (std::sync::Arc<Picture>, usize, bool)>,
300304 }
301305
302306 impl Reorder {
303- fn push(&mut self, seq: usize, picture: Picture, decode_idx: usize, shown: bool) {
307+ fn push(&mut self, seq: usize, picture: std::sync::Arc<Picture>, decode_idx: usize, shown: bool) {
304308 self.pending.insert(seq, (picture, decode_idx, shown));
305309 }
306310
@@ -431,7 +435,7 @@ impl SlotPromise {
431435 /// against separate in-flight bounds, since a waiting output job costs no
432436 /// CPU), and either the (grain-synthesised) picture with the decode index to
433437 /// report or the error that aborts the stream.
434-type LeafResult = (usize, bool, Result<(Picture, usize, bool)>);
438+type LeafResult = (usize, bool, Result<(std::sync::Arc<Picture>, usize, bool)>);
435439
436440 /// Waits for one in-flight job and emits whatever that unblocks.
437441 fn collect_one(
@@ -503,7 +507,10 @@ pub fn decode_stream_with_threads(
503507 // luma-only (chroma coincidentally shares this fixture's flat regions)
504508 // and small-magnitude (the two pictures mostly agree), which is exactly
505509 // what made it look like a rounding bug rather than a wrong reference.
506- let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None);
510+ // lane-pool1: shared, not owned -- a frame refreshing k slots used to copy
511+ // its whole picture k times, and its output a (k+1)-th time.
512+ let mut ref_slots: [Option<std::sync::Arc<Picture>>; NUM_REF_FRAMES] =
513+ std::array::from_fn(|_| None);
507514 // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion
508515 // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom
509516 // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was
@@ -626,15 +633,15 @@ pub fn decode_stream_with_threads(
626633 let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
627634 let value = promise.wait()?;
628635 let output = if grain.apply_grain && !no_grain() {
629- crate::film_grain::apply_grain(
636+ std::sync::Arc::new(crate::film_grain::apply_grain(
630637 &value.picture,
631638 &grain,
632639 seq.mc_identity,
633640 u32::from(seq.bit_depth),
634641 &crate::decode::FrameCtx::new(),
635- )
642+ ))
636643 } else {
637- value.picture.as_ref().clone()
644+ std::sync::Arc::clone(&value.picture)
638645 };
639646 Ok((output, decode_idx, true))
640647 }))
@@ -680,12 +687,12 @@ pub fn decode_stream_with_threads(
680687 let mc_identity = parser
681688 .sequence_header()
682689 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);
683- crate::film_grain::apply_grain(
690+ std::sync::Arc::new(crate::film_grain::apply_grain(
684691 &picture,
685692 &header.film_grain,
686693 mc_identity,
687694 bit_depth as u32, fctx,
688- )
695+ ))
689696 } else {
690697 picture
691698 };
@@ -901,15 +908,15 @@ pub fn decode_stream_with_threads(
901908 // reported to the sink clean, exactly as the inline path
902909 // below reports it (spec 7.18.3.1).
903910 let output = if shown && header.film_grain.apply_grain && !no_grain() {
904- crate::film_grain::apply_grain(
911+ std::sync::Arc::new(crate::film_grain::apply_grain(
905912 &picture,
906913 &header.film_grain,
907914 seq.mc_identity,
908915 u32::from(seq.bit_depth),
909916 &fctx,
910- )
917+ ))
911918 } else {
912- std::sync::Arc::try_unwrap(picture).unwrap_or_else(|p| (*p).clone())
919+ picture
913920 };
914921 Ok((output, decode_idx, shown))
915922 }))
@@ -945,7 +952,7 @@ pub fn decode_stream_with_threads(
945952 let snap = RefSnapshot::take(
946953 &header,
947954 seq,
948- std::array::from_fn(|i| ref_slots[i].as_ref()),
955+ std::array::from_fn(|i| ref_slots[i].as_deref()),
949956 std::array::from_fn(|i| cdf_slots[i].as_ref()),
950957 std::array::from_fn(|i| seg_map_slots[i].as_deref()),
951958 &std::array::from_fn(|i| motion_field_slots[i].as_ref()),
@@ -968,13 +975,16 @@ pub fn decode_stream_with_threads(
968975 final_dump.as_deref(),
969976 )?;
970977 pictures_decoded += 1;
978+ // lane-pool1: one allocation, then every slot store and the output
979+ // itself are refcount bumps instead of plane copies.
980+ let picture = std::sync::Arc::new(picture);
971981 if threads > 1 {
972982 // lane-thread3: only a key frame reaches this inline path at
973983 // `threads > 1`, and the bank it fills is the promise array --
974984 // already resolved, since the picture exists right here.
975985 if header.refresh_frame_flags != 0 {
976986 let promise = SlotPromise::ready(std::sync::Arc::new(SlotValue {
977- picture: std::sync::Arc::new(picture.clone()),
987+ picture: std::sync::Arc::clone(&picture),
978988 cdfs: stored_cdfs,
979989 motion_field,
980990 seg_map: decoded_seg_map,
@@ -995,7 +1005,7 @@ pub fn decode_stream_with_threads(
9951005 // clean, pre-grain decode, even though the frame pushed
9961006 // onto the caller's output below carries the grained
9971007 // picture.
998- ref_slots[i] = Some(picture.clone());
1008+ ref_slots[i] = Some(std::sync::Arc::clone(&picture));
9991009 motion_field_slots[i] = Some(motion_field.clone());
10001010 seg_map_slots[i] = Some(decoded_seg_map.clone());
10011011 order_hint_slots[i] = header.order_hint;
@@ -1012,12 +1022,12 @@ pub fn decode_stream_with_threads(
10121022 if header.show_frame {
10131023 let output = if header.film_grain.apply_grain && !no_grain() {
10141024 let mc_identity = seq.mc_identity;
1015- crate::film_grain::apply_grain(
1025+ std::sync::Arc::new(crate::film_grain::apply_grain(
10161026 &picture,
10171027 &header.film_grain,
10181028 mc_identity,
10191029 bit_depth as u32, fctx,
1020- )
1030+ ))
10211031 } else {
10221032 picture
10231033 };