Skip to content

perf(sw): the seconds frontier searches instead of walking (AE#592) - #604

Merged
superuser404notfound merged 1 commit into
mainfrom
perf/592-coverage-frontier
Sep 22, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
perf/592-coverage-frontier

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Found with a CPU-attributed profile, not a guess. It is the first thing in this thread that is both measurable and fixable.

The defect

SoftwarePacketCoverage carries two frontier queries over the same sorted, disjoint ranges. frontier(containing:) binary searches. frontierSeconds(containing:timeBaseNumerator:timeBaseDenominator:), right beside it, walked, converting two Int64 bounds to Double and running two exactness guards per range.

The walking one is the one SoftwarePacketReadAhead calls on every produced packet, for video and audio coverage both, through the backpressure check at SoftwarePacketReadAhead.swift:459, and again in every snapshot.

What it cost

Measured over the island count, with the tests written before the change:

islands before after
8 909 ns 378 ns
600 49593 ns 607 ns
4000 326443 ns 742 ns

The walk grew exactly linearly, 6.7x the ranges for 6.6x the time, so at the 4096-range cap one query cost 326 us. That is the shape of a session that is cheap early and expensive later, which is the shape this whole thread is about.

End to end, 4K HEVC at 40 Mbps with AC-3 5.1 over an HTTP origin on the software path, 30 steady-state samples per run: process CPU 14.71% / 15.28% before, 13.97% / 13.02% after.

Read that end-to-end number with its caveat: the spool budget is derived from free disk space and the after-runs happened to get a larger one (2.2 GB against 0.5 to 0.9 GB), so the two pairs are not a clean A/B. The microbenchmark is the clean measurement; the session number says the direction holds where it matters.

How it was found

A CPU-attributed profile (xctrace, CPU Profiler), because a wall-clock sample cannot answer this: it counts a sleeping thread the same as a running one, and the demux loop is mostly asleep. Attributed by thread, the read-ahead producer was the single largest consumer at 31.2% of process CPU, and inside it frontierSeconds plus the IndexingIterator.next() and Double.init of its own loop were about a sixth of that thread.

The guard

The exactness guard stays fail-closed on every range the search examines, and needs no wider reach. Lossiness is monotonic in sorted ranges, so a lossy range earlier than the answer would have to sit before a finite clock it cannot precede, and the walk would never have reached it either.

Tests

Six, written before the change so they pin the semantics rather than describe the new code: the frontier is the end of the span holding the clock, a clock in a gap has none and a future island is not one, a clock before all coverage has none, adjacent spans still merge to one range, and 600 islands queried inside, on both edges and in every gap all answer exactly as the walk did. Plus a reported cost line, asserting nothing about timing.

Full suite green, 3343 tests in 451 suites.

🤖 Generated with Claude Code

https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f

SoftwarePacketReadAhead computes the frontier on every produced packet, for
video and audio coverage both, through its backpressure check. frontierSeconds
walked the sorted, disjoint ranges linearly, converting two Int64 bounds to
Double and running two exactness guards per range, while frontier(containing:)
right beside it already binary searched the same data.

Measured over the island count, before and after:

     8 islands:    909 ns ->  378 ns
   600 islands:  49593 ns ->  607 ns
  4000 islands: 326443 ns ->  742 ns

The walk grew exactly linearly with the island count, which is the shape of a
session that is cheap early and expensive later. End to end on a 4K HEVC session
over an HTTP origin the process CPU went from 14.71/15.28% to 13.97/13.02%.

A CPU-attributed profile is what found it: the read-ahead producer was the
largest single consumer at 31.2% of process CPU, and frontierSeconds plus the
iterator and Double conversions inside its loop were about a sixth of that
thread.

The exactness guard stays fail-closed on every range the search examines. It
needs no wider reach: lossiness is monotonic in sorted ranges, so a lossy range
earlier than the answer would have to sit before a finite clock it cannot
precede.

Six tests written before the change pin the semantics the search has to keep,
including 600 islands queried inside, on both edges and in every gap.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_014AVNwe8YDG9EQM7uHdjT3f
@superuser404notfound
superuser404notfound merged commit 9eaaf71 into main Sep 22, 2026
7 checks passed
@superuser404notfound
superuser404notfound deleted the perf/592-coverage-frontier branch September 22, 2026 15:44
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