Skip to content

fix(demux): the detour cache grows to a working set larger than its 8 blocks (AE#909) - #911

Closed
oxyc wants to merge 1 commit into
superuser404notfound:mainfrom
oxyc:fix/detour-cache-grows-on-refetch
Closed

oxyc wants to merge 1 commit into
superuser404notfound:mainfrom
oxyc:fix/detour-cache-grows-on-refetch

Conversation

@oxyc

@oxyc oxyc commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Problem

#909: an MP4 that stores each subtitle track in its own region keeps about one detour block per track live during playback, because every embedded subtitle track is harvested (#112). With 14 such tracks the 8-block LRU evicts each block just before its next read, so the same blocks are fetched over and over. Against an origin that takes a few hundred milliseconds per request, playback rebuffers every 10–30 s.

Change

DetourBlockCache raises its ceiling by one when a block the ceiling evicted is inserted again, up to 16 blocks (~64 MB). The existing idle release (PERF-108) brings it back to what is still resident, never below 8. These never grow it:

  • a working set within 8 blocks;
  • blocks a scrub passes through once;
  • a working set the grown ceiling could not hold either.

On the reader, the change is the extra argument the cache is constructed with.

The one-line alternative

For this file, a fixed ceiling of 12 is enough on its own. It was the smallest that held: 0 rebuffers in two runs, against 3 rebuffers at 10.

This PR offers the adaptive version because the size needed depends on how many regions a file keeps live, so any fixed number fits one file's layout. Its cost is about 40 lines in one class, against one line for a fixed ceiling. A fixed 16, for example, can hold up to 32 MB more of a scrub's blocks until idle release; the adaptive ceiling grows only when blocks come back.

DetourCacheWorkingSetReaderTests passes with either version, so switching to a constant is just that line.

Test plan

Media: the #909 file. MP4, HEVC 2160p Dolby Vision, E-AC-3, 14 mov_text tracks stored at 0.76–1.04 GB while video and audio sit at about 4.8 GB.

Harness: aetherctl play --seconds 90 --subs eng --start-position 1500 against a local range server adding 400 ms per response; macOS 27.0, Apple silicon.

build rebuffers in 90 s detour fetches
main 612eda0 4, 4 251, 253
this change on 612eda0 0, 0 207, 187
fixed ceiling of 10 / 12 / 16 (on 7.46.0, from #909) 3 / 0, 0 / 0 253 / 244, 246 / 188

Tests:

  • DetourBlockCacheTests: seven new cache-level tests covering growth, the bound, the three cases that must not grow, idle shrink and clear.
  • DetourCacheWorkingSetReaderTests: three passes over twelve regions behind the streaming window. The third pass fetches nothing. With growth disabled every pass fetches all twelve ([12, 12, 12]).

Run locally on Xcode 27:

  • full swift test;
  • the one-thread cooperative pool pass;
  • xcodebuild build-for-testing for tvOS Simulator;
  • swift build -c release --product aetherctl.

Not covered:

🤖 Generated with Claude Code

@quint-aether quint-aether Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review

The PR lets DetourBlockCache raise its ceiling by one each time a block the ceiling evicted is inserted again, up to 16 blocks (~64 MB). Idle release brings the ceiling back down, never below 8. The fix sits in the engine at the cause: a working set one larger than the LRU evicts every block just before its next read. The direction is right.

Findings

  • No blocking or should-fix findings. I traced insert, evictIdle and clear at the head against each new test, and they behave as the tests claim:
    • 5 regions over a ceiling of 3 grows to 5 and stops fetching.
    • 9 regions with a cap of 5 never grows, because evictedByCeiling is capped at grownMaxBlocks, so a block that returns too late no longer counts.
    • 12 regions over 8 grows to 12 in the second pass and fetches nothing in the third.
  • evictIdle returns early when nothing is idle, so the once-a-second sweep does not wipe the eviction history between passes. When blocks are released, the shrink goes down to lru.count, so it never causes an eviction cascade.
  • For the maintainer: the PR body offers a fixed ceiling of 12 as a one-line alternative, and DetourCacheWorkingSetReaderTests passes with either version. Adaptive or fixed is a judgment call, not a defect.

Checklist: CHANGELOG yes, docs n.a. (no doc quotes the detour cache size), tests yes, traits both ways n.a.

Needs a device run: the per-reader memory peak on Apple TV with a grown ceiling (up to 64 MB of detour blocks, until idle release). Also whether the #909 file plays without rebuffering on tvOS against a slow origin. The author ran this only on macOS.

Thanks for the careful measurements and for offering the fixed-ceiling alternative alongside the adaptive one.

Automated review by Quint (Claude) of 72776f6. It reads the code, it does not build or run it; a maintainer reviews and merges.

… blocks (AE#909)

An MP4 that stores each subtitle track in its own region keeps about one detour block per track
live during playback. With more such regions than the 8-block ceiling, LRU evicted every block
just before its next read and each read fetched it again. A block the ceiling evicted being
inserted again now raises the ceiling by one, up to 16 blocks; idle release brings it back down,
never below 8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oxyc
oxyc force-pushed the fix/detour-cache-grows-on-refetch branch from 72776f6 to 1af29e2 Compare October 26, 2026 18:11

@quint-aether quint-aether Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review

I checked the new head 1af29e2 and found nothing new since the last review of 72776f6. The cache logic in DetourBlockCache (insert, evictIdle, clear) is the same as before, and the earlier summary still applies.

Automated review by Quint (Claude) of 1af29e2. It reads the code, it does not build or run it; a maintainer reviews and merges.

superuser404notfound pushed a commit that referenced this pull request Oct 27, 2026
…old (#909)

The growth from 6a337ad counted any return of an evicted block, so a
cycle longer than the ceiling still grew the cache to 24 blocks (~96 MB)
and kept missing on every lookup at that size. The evictions since the
returning block measure how far the cycle outruns the cache; growth now
needs that to fit under the ceiling.

Takes the reach rule and the reader-level working-set test from #911,
which arrived at the same design independently and first.

Re #909, supersedes #911

Co-Authored-By: oxyc <m@oxy.fi>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SeT6NfHusKtayhPhQfndbo
superuser404notfound pushed a commit that referenced this pull request Oct 27, 2026
@superuser404notfound

Copy link
Copy Markdown
Owner

Thank you for this, and sorry for the overlap. #909 got a fix on main in 6a337ad (merged in 2ef4c42) that landed the day after this PR was opened, and it turned out to be the same design almost line for line: a list of blocks evicted for capacity, +1 on a re-fetch, the idle sweep shrinking back, clear() resetting. I missed this PR when that went in, which is why it now conflicts.

Two things here were better than what landed, so I took them over in 1e42237 (merged in ffbbc3b), with you as co-author:

  • The reach rule. The merged fix grew on any return of an evicted block, so a cycle longer than the ceiling still pushed the cache to 24 blocks and kept missing at that size. Growth now needs capacity + evictions since that block <= ceiling, a slightly tighter form of your history bound (a 16-entry history still let cycles of 17 to 24 grow toward 16 without ever hitting). Your noGrowthBeyondReach case is in the suite as "a cycle the ceiling could not hold either does not grow the cache".
  • DetourCacheWorkingSetReaderTests, taken over as is: three passes over twelve regions through the real reader, the third fetches nothing.

The ceiling stays at 24 rather than 16: with the reach rule the cache only grows when the larger size actually turns misses into hits, and #104 had 16 subtitle tracks plus blocks straddling a boundary.

Closing this as superseded. Both pieces will ship in the next release, and a retest of the #909 file on top of main would still be very welcome.

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.

2 participants