Repository navigation
Cold start: keep the open window across the parse seek, prefetch the tail (#281) - #282
Merged
Merged
Conversation
A non-faststart MP4 opens in three sequential round trips before the frame rate is known: the data connection from byte zero, a seek to the trailing moov, and a return to the first sample. The first two are the file layout. The third was ours: seekReconnect discards the window, so the return trip re-fetched bytes that had been resident moments earlier. The open window is now parked across a parse seek and serves that return trip as a copy. It is bounded (4 MB, cut from the window's start, which is where the demuxer comes back to) and released when the demuxer reports its header pass done, via a new AVIOProvider.markOpenPhaseFinished with a no-op default. Afterwards a far seek is a scrub, whose landing zone the old window cannot serve, so holding it would be pure cost. open() also issues one speculative 64 KB suffix range. `bytes=-n` needs no size, so it goes out before the first response header exists and overlaps the data connection instead of queueing behind it. It covers what an open actually reads at the tail: mfra on fragmented MP4, and a trailing moov whose sample tables fit. A feature-length moov does not fit and still costs its own request, deliberately: fetching megabytes on a guess would compete with playback bytes on the slow links this is meant to help. The fetch runs on a delegate, not a completion handler, and that is load-bearing. A completion handler only fires once the body has arrived, so an origin that does not implement suffix ranges and answers 200 with the whole file would be downloaded in full before the status could be inspected: the #255 failure shape, on a 4 GB source. The delegate hangs up at the response header, and only a 206 whose Content-Range agrees with the byte count is installed, so a proxy answering with some other region cannot place bytes at the wrong offset. Measured with aetherctl against a Range-logging origin. A 51 MB moov-at-end fixture goes from three sequential requests to two concurrent ones; the moov read and the return trip both disappear. Fixtures below 32 MB reproduce nothing, since their tail already lies inside the first window. Re #281 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01HX5zV3Fcf7Nzq4DYGdXvQE
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.
Opening a non-faststart MP4 costs three sequential round trips before the frame rate is known. Reported in #281 with before/after traces from a debrid-style proxy, where this is the single largest contributor to VOD time-to-first-frame.
What was actually wrong
Two of the three round trips are the file layout, one was ours:
seekReconnectresetswindow, so the seek to the tail discarded bytes that were resident moments earlier, and the return trip re-fetched them.Two changes
The open window is parked across a parse seek (4 MB cap, cut from the window's start, which is where the demuxer comes back to) and serves the return trip as a copy. It is released when the demuxer reports its header pass done, through a new
AVIOProvider.markOpenPhaseFinished()with a no-op default. After that point a far seek is a scrub, whose landing zone the old window cannot serve, so keeping it would be pure cost. No additional bytes are fetched for this half.open()issues one speculative 64 KB suffix range.bytes=-nneeds no size, so it goes out before any response header exists and overlaps the data connection rather than queueing behind it. It covers what an open actually reads at the tail:mfraon fragmented MP4, and a trailingmoovwhose sample tables fit.A feature-length file's
moovruns into megabytes, does not fit, and still costs its own request. That is deliberate: fetching megabytes on a guess would compete with playback bytes on exactly the slow links this is meant to help. The parked window still removes the return trip there.Why a delegate and not a completion handler
Suffix ranges are not universally implemented. An origin that lacks them answers with 200 and the whole file. A completion handler only fires once the body is in hand, so that response would be downloaded in full before its status could be inspected: the #255 failure shape, on a 4 GB source.
TailPrefetchDelegatedecides at the response header and hangs up there, the same shapeProbeDelegateuses. Only a 206 whoseContent-Rangeagrees with the received byte count is installed, so a proxy answering with a different region cannot place bytes at the wrong offset. Redirects go throughredirectPreservingHeaders, so the #126 credential policy holds here too.Verification
Measured with
aetherctl probeagainst a Range-logging origin, on a 51 MB moov-at-end fixture:The moov read and the return trip both disappear. A fast-start file pays the 64 KB it never uses.
Worth recording for the next person: a fixture below 32 MB reproduces none of this, because
persistentRangeBytesis 32 MB and the whole tail already lands inside the first window. The 8 MB fixture looked healthy; the same file at 51 MB showed the defect immediately.New tests (
Issue281ColdStartRoundTripTests,ResidentSpanTests): the suffix request is issued, a tail read costs no extra request, the return trip costs no extra request, parking stops after the open phase, a 200-answering origin is hung up on before its body, andContent-Rangedisagreement is refused. 1424 tests green,-strict-concurrency=completeclean, tvOS Simulator build green.Issue220BoundedRangeTestsneeded one line: it took the first bounded range as the data connection, which is now the tail prefetch. It selects by offset instead.Re #281