Skip to content

Cold start: keep the open window across the parse seek, prefetch the tail (#281) - #282

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/281-parse-detour-and-tail-prefetch
Aug 2, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/281-parse-detour-and-tail-prefetch

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

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:

bytes=0-33554431          data connection from byte zero
bytes=53593819-53631685   the trailing moov
bytes=48-33554479         back to the first sample     <- self-inflicted

seekReconnect resets window, 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=-n needs 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: mfra on fragmented MP4, and a trailing moov whose sample tables fit.

A feature-length file's moov runs 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. TailPrefetchDelegate decides at the response header and hangs up there, the same shape ProbeDelegate uses. Only a 206 whose Content-Range agrees with the received byte count is installed, so a proxy answering with a different region cannot place bytes at the wrong offset. Redirects go through redirectPreservingHeaders, so the #126 credential policy holds here too.

Verification

Measured with aetherctl probe against a Range-logging origin, on a 51 MB moov-at-end fixture:

requests shape
before 3 sequential
after 2 concurrent

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 persistentRangeBytes is 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, and Content-Range disagreement is refused. 1424 tests green, -strict-concurrency=complete clean, tvOS Simulator build green.

Issue220BoundedRangeTests needed 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

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
@superuser404notfound
superuser404notfound merged commit 2e01713 into main Aug 2, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/281-parse-detour-and-tail-prefetch branch August 2, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant