Repository navigation
fix(protocols): keep recent blocks cached in PrefetchReader - #282
Merged
Merged
Conversation
A non-interleaved MP4 stores all audio samples together and the video far from them, so the demuxer alternates between two distant regions packet by packet. With a single cached block each hop was a synchronous 1MB refetch of a block read moments before, and it also threw away the video read-ahead in flight. On an 8K60 SFTP file this fetched ~1.2GB to advance ~57MB (601 fetches per 600 hops in the new test) and playback never got past the first frames. PrefetchReader now keeps the last MAX_CACHED_BLOCKS (4) blocks in an LRU: returning to a cached region neither refetches nor cancels the pending read-ahead, and a prefetch drained by a seek is kept instead of discarded. With 12MB playback blocks this costs at most 48MB resident. Verified on Quest 3: the same file now plays at ~60 fps (76 block fetches in 71 s, previously 859 in ~30 s); a second, interleaved file is unaffected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DKhrqRn7bTFHuxW5XFTqvh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
An 8K60 HEVC MP4 streamed over SFTP never played: the first frame took ~6 s and playback stalled right after (
sem frame novo ha 500ms, repeatedVideo catch-up), even though the link sustained ~30 MiB/s.Root cause. The file is not interleaved: all audio samples sit together near the start (~520 KB) and the video ~178 MB further in, so the demuxer alternates between two distant regions packet by packet.
PrefetchReadercached a single block, so every audio↔video hop was:blocksDiscardedclimbing).On the headset this fetched 1194 MiB in 859 fetches to advance ~57 MB of the file (~11 Mbit/s of useful data for a ~96 Mbit/s stream).
Fix (
rust/protocols/src/prefetch.rs):MAX_CACHED_BLOCKS - 1blocks stay in an LRU (MAX_CACHED_BLOCKS = 4). Returning to a cached region neither refetches it nor cancels the read-ahead pending for the region it left.blocks_discardednow counts in-flight prefetches a miss had to wait out without them covering the offset; the block itself stays cached. Its doc comment and the module header were updated.Type of change
Affected layers
app/) — app shell, UI panels, credentials, history, i18nnative/) — OpenXR session, Vulkan/GLES render loop, controller inputrust/) — demux, decode, audio, network protocolsHow has this been tested?
cargo test)Host:
prefetch::tests:alternating_between_two_regions_does_not_refetch_on_every_hop— 300 audio/video hops between two distant regions. Fails on the previous code with 601 fetches for 600 hops, the same pattern seen on the headset.prefetch_drained_by_a_seek_is_reused_when_reading_comes_back— returning to a drained read-ahead block must not refetch it (fails on the previous code: 4 fetches instead of 3).cargo fmt --check,cargo clippy -p protocols -p media-logic --all-targets --all-features -- -D warnings,cargo test -p protocols -p media-logic(148 + 302 tests) all pass.Quest 3, same file over SFTP (from the session telemetry CSVs):
A second, interleaved file (fisheye 190, also SFTP) was played afterwards to check for regressions: ~60 fps, 59 fetches in 55 s, several seeks each recovering in 0.2–1 s.
Not covered: the integration tests against real servers (
./scripts/test-network-protocols.sh) were not run.Checklist:
clippy -D warnings).Follow-up (not in this PR)
After a seek, the decoder's catch-up drops feed
state.droppedFps(native/src/vr_player_app_vulkan.cpp), andQualityController(rust/media-logic/src/quality.rs) degrades the render scale (HIGH → MEDIUM,DROPPED_FRAMES) even though rendering is fine. It recovers after 30 healthy samples, but ignoring drops for a short window after a seek would avoid the false degrade.🤖 Generated with Claude Code
https://claude.ai/code/session_01DKhrqRn7bTFHuxW5XFTqvh