Skip to content

perf(avio): the persistent window holds the delivered chunks, so a trim copies nothing (AE#619) - #620

Merged
superuser404notfound merged 1 commit into
mainfrom
perf/619-window-trim
Sep 24, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
perf/619-window-trim

Conversation

@superuser404notfound

@superuser404notfound superuser404notfound commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Closes #619.

What

AVIOReader's persistent window is now a ChunkedByteWindow, which keeps the Data chunks the connection delivered.

  • Before: trimWindowLocked() copied everything ahead of the cut (up to ~18 MB) once per 4 MB consumed, and each later append grew the fresh buffer by reallocation.
  • After:
    • A trim advances an offset and releases the chunks it passed.
    • A truncate for a reconnect slices one chunk.
    • Reads copy from the chunks into the AVIO buffer, as they did before.
    • winStart, the lookback, and every count the reader reports are unchanged.

Measured, honestly small

Setup: release aetherctl on an M1 Air, a 4K HEVC + AC-3 5.1 fixture at 91.6 Mbit/s, served by Scripts/slowrange.py at 1 Gbit/s with 20 ms latency. Process CPU is counted over seconds 20 to 110 of 120 s sessions, with the arms alternated.

route 8710a245 this branch
software 0.208 / 0.203 / 0.207 0.202 / 0.203 / 0.205
native 0.167 / 0.165 / 0.167 0.160 / 0.166 / 0.164

That is about -0.003 cores on both routes, inside the spread of the arms, so process CPU cannot tell the change apart from noise. My estimate in #619 (~0.01 core) was read off profile shares and was too high.

What the change removes does show in a 20 s sample of the software route:

frame 8710a245 this branch
trimWindowLocked 35 0
appendPersistentData 13 1
_platform_memmove 205 140

No run of either arm rebuffered.

Memory, which is what this is worth

Peak memory from /usr/bin/time -l, 45 s sessions on the same fixture and origin, three runs per arm, arms alternated:

route 8710a245 peak footprint this branch peak footprint
software 604 / 623 / 651 MB 193 / 196 / 197 MB
native 828 / 889 / 911 MB 551 / 551 / 553 MB

Max RSS moves the same way (software 492 to 537 MB -> 217 to 229 MB, native 791 to 890 MB -> 546 to 568 MB). That is roughly 420 MB less on the software route and 330 MB less on the native one, with no overlap between the arms. The saving is far larger than the transient double buffer of a single trim. The old path allocated a fresh ~12 to 18 MB buffer on every trim and then grew it by reallocation on every append, and the allocator evidently kept much of that churn resident. I did not trace which allocator path holds it. On an Apple TV 4K (3rd gen, 3.88 GB RAM) this is the change that matters, not the CPU.

Tests

  • ChunkedByteWindow is checked against a contiguous Data reference over 5000 random appends, reads, drops and truncates. There are also edge cases (empty, slices, a partly consumed chunk, truncation on and inside a chunk edge), and a pin that a drop keeps the storage of what remains.
  • A reader-level test against a patterned origin reads 12 MB forward and 256 KB back inside the lookback, after trims. It checks every byte, and that the backward read made no request. ThrottledOriginServer gains an opt-in patternedBody for this; its default is unchanged.
  • 3388 swift-testing and 629 XCTest tests are green.

🤖 Generated with Claude Code

https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f

…im copies nothing (AE#619)

trimWindowLocked dropped the consumed head with window.subdata(in:), which
copied everything still ahead of the cut into a fresh buffer: up to ~18 MB
once per 4 MB consumed, and each append after it grew that fresh buffer by
reallocation. The window is now a ChunkedByteWindow that keeps the Data
chunks as the connection delivered them. A trim advances an offset and
releases the chunks it passed. A truncate for a reconnect slices one
chunk. Reads copy out of the chunks into the AVIO buffer, which they did
before as well. winStart, the lookback and every count the reader reports
are unchanged.

Measured with release aetherctl on an M1 Air, one FFmpeg-muxed 4K HEVC +
AC-3 5.1 fixture at 91.6 Mbit/s over Scripts/slowrange.py (1 Gbit/s,
20 ms), process CPU over seconds 20 to 110 of 120 s sessions, arms
alternated:

  software  8710a24 0.208 / 0.203 / 0.207   this 0.202 / 0.203 / 0.205
  native    8710a24 0.167 / 0.165 / 0.167   this 0.160 / 0.166 / 0.164

About -0.003 cores on both routes, inside the spread of the arms: the
change is not separable from noise in process CPU. What it removes is
visible in a 20 s sample of the software route: trimWindowLocked 35 -> 0
samples, appendPersistentData 13 -> 1, _platform_memmove 205 -> 140. No
rebuffer on any run.

Tests: ChunkedByteWindow against a contiguous Data reference over 5000
random appends, reads, drops and truncates, plus edge cases and a pin that
a drop keeps the storage of what remains. A reader-level test reads 12 MB
forward and 256 KB back inside the lookback from a patterned origin and
checks every byte and that the backward read made no request.
ThrottledOriginServer gains an opt-in patterned body for it. 3388
swift-testing and 629 XCTest tests green.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f
@superuser404notfound
superuser404notfound merged commit e7b2204 into main Sep 24, 2026
7 checks passed
@superuser404notfound
superuser404notfound deleted the perf/619-window-trim branch September 24, 2026 08:17
brandomoore pushed a commit to brandomoore/AetherEngine that referenced this pull request Sep 24, 2026
…re and peak footprint

Regenerated from aetherengine-bench's 2026-09-24 session on 7.15.2. Every
engine now plays each file from disk and over HTTP from a range origin at
1000 Mbit/s and 20 ms, at 38 and 90 Mbit/s, with a peak footprint column.
The previous block was measured with 6.26.0, from disk only, and could not
show what superuser404notfound#620 changed in the HTTP reader.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f
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.

perf(avio): trimming the persistent window copies the whole remaining window every 4 MB

1 participant