Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(rendering): keep only the last frames below a backward scrub request so the reader keeps vending

Richie McIlroyaa4b35959c8e
diff --git a/crates/rendering/src/decoder/avassetreader.rs b/crates/rendering/src/decoder/avassetreader.rs
index 3e718c18e..9f2fda451 100644
--- a/crates/rendering/src/decoder/avassetreader.rs
+++ b/crates/rendering/src/decoder/avassetreader.rs
@@ -46,6 +46,7 @@ const SEQUENTIAL_FRAME_CACHE_SIZE: usize = 8;
// starts decoding eagerly, so only requested tracks may hold native readers.
const MAX_ACTIVE_READERS: usize = 16;
const IDLE_READER_TIMEOUT: Duration = Duration::from_millis(250);
+const MAX_CACHED_FRAMES_BELOW_REQUEST: u32 = 10;
fn reader_capacity() -> &'static Arc<Semaphore> {
static CAPACITY: OnceLock<Arc<Semaphore>> = OnceLock::new();
@@ -1062,10 +1063,16 @@ impl AVAssetReaderDecoder {
if let Some(buf) = frame.image_buf() {
hold_candidate = Some((current_frame, buf.retained()));
// A seek decodes from the keyframe before the
- // request, and scrubbing backwards asks for these
- // frames next; caching them makes those requests
- // hits instead of another seek over the same span.
- if current_frame >= cache_min {
+ // request, and scrubbing backwards asks for the
+ // frames just below it next; caching them makes
+ // those requests hits instead of another seek over
+ // the same span. Only the last few are kept: each
+ // pins a buffer of the reader's own pool, and with
+ // dozens outstanding the reader stops vending.
+ if current_frame >= cache_min
+ && current_frame + MAX_CACHED_FRAMES_BELOW_REQUEST
+ >= minimum_fallback_frame
+ {
insert_cached_frame(
&mut cache,
&mut cache_bytes,
@@ -1424,8 +1431,6 @@ fn answer_misses_previous_vend(
.is_some_and(|expected| answer != Some(expected))
}
-// Drops from the end the cache is trimmed from, skipping pinned frames unless
-// every frame is pinned.
#[allow(clippy::too_many_arguments)]
fn insert_cached_frame(
cache: &mut BTreeMap<u32, CachedFrame>,
@@ -1465,6 +1470,8 @@ fn insert_cached_frame(
cache.insert(number, frame);
}
+// Drops from the end the cache is trimmed from, skipping pinned frames unless
+// every frame is pinned.
fn cache_eviction_victim<V>(
cache: &BTreeMap<u32, V>,
from_oldest: bool,
diff --git a/crates/rendering/src/decoder/model/mod.rs b/crates/rendering/src/decoder/model/mod.rs
index 6f02d0c85..c536c7237 100644
--- a/crates/rendering/src/decoder/model/mod.rs
+++ b/crates/rendering/src/decoder/model/mod.rs
@@ -1144,7 +1144,7 @@ fn ffmpeg_preserves_late_pts_across_playback_and_seeks() {
#[cfg(target_os = "macos")]
#[test]
-fn avassetreader_scrubbing_backwards_seeks_once_per_keyframe_span() {
+fn avassetreader_scrubbing_backwards_reuses_the_frames_below_each_seek() {
let script = Script {
time_base: (1, 30),
frames: (0..120)
@@ -1175,8 +1175,8 @@ fn avassetreader_scrubbing_backwards_seeks_once_per_keyframe_span() {
assert!(outcome.failures.is_empty(), "{:?}", outcome.failures);
assert_eq!(outcome.requests, case.requests.len());
assert!(
- outcome.starts <= 6,
- "{} reader starts and seeks for a backward scrub over four keyframe spans",
+ outcome.starts <= 16,
+ "{} reader starts and seeks for 60 backward scrub steps",
outcome.starts
);
}