Skip to content

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

Closed

Description

@superuser404notfound

Found while profiling the software VOD route for #592. It is not specific to that route: every session that reads through AVIOReader's persistent connection pays it.

What

trimWindowLocked() (AVIOReader.swift:2240) drops the consumed head of the read window with

window = window.subdata(in: drop..<window.count)

It runs whenever more than winLookback + winTrimBatch (2 MB + 4 MB, :625, :627) sits behind the read position. Each call copies everything still ahead of the cut into a fresh buffer, and the window runs up to winHighWaterDefault (16 MB, :566) ahead. So a reader that streams forward copies the remaining window once per 4 MB consumed. At 91.6 Mbit/s that is about three trims a second, each copying up to ~18 MB, several times the stream's own rate in memmove.

Measured

  • Where: release aetherctl play --sw on an M1 Air, one 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, sample over 20 s of steady state, at 8710a245.
  • Share: _platform_memmove is the largest active frame at 17.3 % of active samples. Of that, 59 samples come from trimWindowLocked() <- readPersistent(into:size:), which is 4.9 % of all active samples and roughly 0.01 of a core at this rate.
  • Not measured: the native route's share. It reads through the same reader, so the per-byte cost should be the same.

Direction

Consume by offset instead of by copy, for example a read offset into the window that is compacted rarely, or a deque of the received chunks. The chunks already arrive as separate Data from appendPersistentData. winStart and the lookback semantics have to stay exactly as they are: the backward-read and retained-head behaviour (#281, #585) depends on them.

Done when

The same A/B shows the trim's memmove gone, with process CPU compared on both routes. The reader's existing tests stay green, and a test pins that a backward read inside the lookback still hits the window after a trim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions