Skip to content

feat(buffer): bound the opt-in prefetch in bytes, raise the window ceiling to 2700 (#207) - #212

Merged
superuser404notfound merged 1 commit into
mainfrom
feat/issue-207-byte-bounded-prefetch
Jul 24, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
feat/issue-207-byte-bounded-prefetch

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Follow-up to #102, addressing #207.

Why the ceiling alone was not enough

SegmentCache.pruneOutsideWindow never evicts the hard window, so the retention budget (min(2 GiB, a quarter of the tmp volume's free space)) bounds only the extras outside it. That is correct for the historical windows (10 segments ~ 100 MB, the 150 ceiling ~ 1.5 GB), which fit under the budget by construction. A window spanning a whole film makes the window itself the dominant footprint and the budget unreachable: ~18 GB of 4K HEVC lands on the volume unchecked, and SegmentCache.store degrades a failed write to a cache miss, i.e. a stall. That is the failure the option exists to prevent, with disk instead of network as the cause.

What this does

  • clampedForwardWindow ceiling 150 -> 2700 (~3 h at 4 s), a sanity bound that covers a whole feature film, so a host can pass Int.max. Floor (4) and the nil default (10) unchanged; 150 still passes through.
  • Windows past the historical 150 count as an explicit opt-in, so vodRetentionBudgetBytes drops its 2 GiB default cap for them. The quarter-of-free-space clamp, which is what protects the volume, always applies, and an unknown capacity keeps the conservative cap.
  • New PrefetchDiskBudget: the producer parks once its race-ahead (bytes at or above the consumer's target) has filled the budget, while the consumer still has minAheadSegments (10) of produced content ahead of it. The extras eviction that follows the advancing playhead releases the park, so the prefetch tracks the budget instead of the source length.

Counting only bytes at or above the consumer target, rather than the whole window, keeps retained backward history (already bounded by the extras eviction) from throttling a producer whose actual race-ahead is a handful of segments. The park has no wedge breaker by design: it only holds with a safe lead of produced content, and a short lead keeps producing regardless of the budget, so bounding disk can never win over playback.

Behavior for hosts that do not opt in

Unchanged. forwardBufferSegments nil or <= 150 keeps the same budget, and the park's condition (race-ahead bytes >= budget) is unreachable at those window sizes.

Tests

16 new tests (999 total, all green): budget sizing with and without the relaxed cap, the park decision (no budget / under budget / at budget / short consumer lead), SegmentCache.forwardBytes semantics, the headroom park primitive including release as the playhead advances, and a regression guard that retained history never withholds headroom. Ceiling test deltas taken from the reporter's patch.

Verified: swift test 999 passed, swift build -Xswiftc -strict-concurrency=complete clean, tvOS Simulator build succeeded. Not yet device-verified with a real opt-in prefetch session.

Reported with a ceiling-raise patch by @fxndxs.

…iling to 2700 (#207)

The 150-segment ceiling on LoadOptions.forwardBufferSegments (#102) silently capped a
host's "buffer without limit" option: any request above it clamped down, so a user who
deliberately opted into pre-buffering a whole film for a flaky WAN still stalled once the
connection dropped for longer than ~10 min of content.

Raising the ceiling alone would have been unsafe. SegmentCache.pruneOutsideWindow never
evicts the hard window, so the retention budget (min(2 GiB, a quarter of the tmp volume's
free space), the only guard that keeps a nearly full device playable) bounds just the
extras outside it. That holds by construction for the historical windows (10 segments
~ 100 MB, the 150 ceiling ~ 1.5 GB), but a window spanning a whole film would make the
window itself the footprint and put ~18 GB of 4K HEVC on the volume unchecked. A failed
segment write degrades to a cache miss and stalls playback, i.e. exactly the failure the
option exists to prevent, with disk instead of network as the cause.

So the bound moves from segments to bytes:

- The ceiling becomes 2700 (~3 h at 4 s), a sanity bound against accidental values that
  covers a whole feature film, so hosts can pass Int.max. Floor (4) and the nil default
  (10) are unchanged and 150 still passes through, so sessions that do not opt in are
  unaffected.
- Windows past the historical 150 are an explicit opt-in, so the retention budget drops
  its 2 GiB default cap for them and keeps the quarter-of-free-space clamp, which is what
  actually protects the volume.
- The producer parks once its race-ahead (bytes at or above the consumer's target) has
  filled that budget, while the consumer still has PrefetchDiskBudget.minAheadSegments of
  produced content ahead of it. The extras eviction that follows the advancing playhead
  releases the park, so the prefetch tracks the budget instead of the source length.

Counting only bytes at or above the consumer target, rather than the whole window, keeps
retained backward history (which the extras eviction already bounds) from throttling a
producer whose actual race-ahead is a handful of segments. The park deliberately has no
wedge breaker: it only ever holds with a safe lead of produced content, and a short lead
keeps producing regardless of the budget, so bounding disk can never win over playback.

Reported with a ceiling-raise patch by fxndxs; the ceiling change and its test deltas are
taken from that patch.

Co-Authored-By: findussoft <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01M7tL3aHF9rGKfHCSE3DhJ4
@superuser404notfound
superuser404notfound merged commit 33709f5 into main Jul 24, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the feat/issue-207-byte-bounded-prefetch branch July 24, 2026 19:19
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