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 {
296 #[derive(Default)]296 #[derive(Default)]
297 struct Reorder {297 struct Reorder {
298 next: usize,298 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)>,
300 }304 }
301 305
302 impl Reorder {306 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) {
304 self.pending.insert(seq, (picture, decode_idx, shown));308 self.pending.insert(seq, (picture, decode_idx, shown));
305 }309 }
306 310
@@ -431,7 +435,7 @@ impl SlotPromise {
431 /// against separate in-flight bounds, since a waiting output job costs no435 /// against separate in-flight bounds, since a waiting output job costs no
432 /// CPU), and either the (grain-synthesised) picture with the decode index to436 /// CPU), and either the (grain-synthesised) picture with the decode index to
433 /// report or the error that aborts the stream.437 /// 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)>);
435 439
436 /// Waits for one in-flight job and emits whatever that unblocks.440 /// Waits for one in-flight job and emits whatever that unblocks.
437 fn collect_one(441 fn collect_one(
@@ -503,7 +507,10 @@ pub fn decode_stream_with_threads(
503 // luma-only (chroma coincidentally shares this fixture's flat regions)507 // luma-only (chroma coincidentally shares this fixture's flat regions)
504 // and small-magnitude (the two pictures mostly agree), which is exactly508 // and small-magnitude (the two pictures mostly agree), which is exactly
505 // what made it look like a rounding bug rather than a wrong reference.509 // 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);
507 // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion514 // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion
508 // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom515 // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom
509 // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was516 // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was
@@ -626,15 +633,15 @@ pub fn decode_stream_with_threads(
626 let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {633 let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
627 let value = promise.wait()?;634 let value = promise.wait()?;
628 let output = if grain.apply_grain && !no_grain() {635 let output = if grain.apply_grain && !no_grain() {
629- crate::film_grain::apply_grain(636+ std::sync::Arc::new(crate::film_grain::apply_grain(
630 &value.picture,637 &value.picture,
631 &grain,638 &grain,
632 seq.mc_identity,639 seq.mc_identity,
633 u32::from(seq.bit_depth),640 u32::from(seq.bit_depth),
634 &crate::decode::FrameCtx::new(),641 &crate::decode::FrameCtx::new(),
635- )642+ ))
636 } else {643 } else {
637- value.picture.as_ref().clone()644+ std::sync::Arc::clone(&value.picture)
638 };645 };
639 Ok((output, decode_idx, true))646 Ok((output, decode_idx, true))
640 }))647 }))
@@ -680,12 +687,12 @@ pub fn decode_stream_with_threads(
680 let mc_identity = parser687 let mc_identity = parser
681 .sequence_header()688 .sequence_header()
682 .is_some_and(|seq| seq.color_config.matrix_coefficients == 0);689 .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(
684 &picture,691 &picture,
685 &header.film_grain,692 &header.film_grain,
686 mc_identity,693 mc_identity,
687 bit_depth as u32, fctx,694 bit_depth as u32, fctx,
688- )695+ ))
689 } else {696 } else {
690 picture697 picture
691 };698 };
@@ -901,15 +908,15 @@ pub fn decode_stream_with_threads(
901 // reported to the sink clean, exactly as the inline path908 // reported to the sink clean, exactly as the inline path
902 // below reports it (spec 7.18.3.1).909 // below reports it (spec 7.18.3.1).
903 let output = if shown && header.film_grain.apply_grain && !no_grain() {910 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(
905 &picture,912 &picture,
906 &header.film_grain,913 &header.film_grain,
907 seq.mc_identity,914 seq.mc_identity,
908 u32::from(seq.bit_depth),915 u32::from(seq.bit_depth),
909 &fctx,916 &fctx,
910- )917+ ))
911 } else {918 } else {
912- std::sync::Arc::try_unwrap(picture).unwrap_or_else(|p| (*p).clone())919+ picture
913 };920 };
914 Ok((output, decode_idx, shown))921 Ok((output, decode_idx, shown))
915 }))922 }))
@@ -945,7 +952,7 @@ pub fn decode_stream_with_threads(
945 let snap = RefSnapshot::take(952 let snap = RefSnapshot::take(
946 &header,953 &header,
947 seq,954 seq,
948- std::array::from_fn(|i| ref_slots[i].as_ref()),955+ std::array::from_fn(|i| ref_slots[i].as_deref()),
949 std::array::from_fn(|i| cdf_slots[i].as_ref()),956 std::array::from_fn(|i| cdf_slots[i].as_ref()),
950 std::array::from_fn(|i| seg_map_slots[i].as_deref()),957 std::array::from_fn(|i| seg_map_slots[i].as_deref()),
951 &std::array::from_fn(|i| motion_field_slots[i].as_ref()),958 &std::array::from_fn(|i| motion_field_slots[i].as_ref()),
@@ -968,13 +975,16 @@ pub fn decode_stream_with_threads(
968 final_dump.as_deref(),975 final_dump.as_deref(),
969 )?;976 )?;
970 pictures_decoded += 1;977 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);
971 if threads > 1 {981 if threads > 1 {
972 // lane-thread3: only a key frame reaches this inline path at982 // lane-thread3: only a key frame reaches this inline path at
973 // `threads > 1`, and the bank it fills is the promise array --983 // `threads > 1`, and the bank it fills is the promise array --
974 // already resolved, since the picture exists right here.984 // already resolved, since the picture exists right here.
975 if header.refresh_frame_flags != 0 {985 if header.refresh_frame_flags != 0 {
976 let promise = SlotPromise::ready(std::sync::Arc::new(SlotValue {986 let promise = SlotPromise::ready(std::sync::Arc::new(SlotValue {
977- picture: std::sync::Arc::new(picture.clone()),987+ picture: std::sync::Arc::clone(&picture),
978 cdfs: stored_cdfs,988 cdfs: stored_cdfs,
979 motion_field,989 motion_field,
980 seg_map: decoded_seg_map,990 seg_map: decoded_seg_map,
@@ -995,7 +1005,7 @@ pub fn decode_stream_with_threads(
995 // clean, pre-grain decode, even though the frame pushed1005 // clean, pre-grain decode, even though the frame pushed
996 // onto the caller's output below carries the grained1006 // onto the caller's output below carries the grained
997 // picture.1007 // picture.
998- ref_slots[i] = Some(picture.clone());1008+ ref_slots[i] = Some(std::sync::Arc::clone(&picture));
999 motion_field_slots[i] = Some(motion_field.clone());1009 motion_field_slots[i] = Some(motion_field.clone());
1000 seg_map_slots[i] = Some(decoded_seg_map.clone());1010 seg_map_slots[i] = Some(decoded_seg_map.clone());
1001 order_hint_slots[i] = header.order_hint;1011 order_hint_slots[i] = header.order_hint;
@@ -1012,12 +1022,12 @@ pub fn decode_stream_with_threads(
1012 if header.show_frame {1022 if header.show_frame {
1013 let output = if header.film_grain.apply_grain && !no_grain() {1023 let output = if header.film_grain.apply_grain && !no_grain() {
1014 let mc_identity = seq.mc_identity;1024 let mc_identity = seq.mc_identity;
1015- crate::film_grain::apply_grain(1025+ std::sync::Arc::new(crate::film_grain::apply_grain(
1016 &picture,1026 &picture,
1017 &header.film_grain,1027 &header.film_grain,
1018 mc_identity,1028 mc_identity,
1019 bit_depth as u32, fctx,1029 bit_depth as u32, fctx,
1020- )1030+ ))
1021 } else {1031 } else {
1022 picture1032 picture
1023 };1033 };