Repository navigation
perf(sw): the seconds frontier searches instead of walking (AE#592) - #604
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
SoftwarePacketCoveragecarries two frontier queries over the same sorted, disjoint ranges.frontier(containing:)binary searches.frontierSeconds(containing:timeBaseNumerator:timeBaseDenominator:), right beside it, walked, converting twoInt64bounds toDoubleand running two exactness guards per range.The walking one is the one
SoftwarePacketReadAheadcalls on every produced packet, for video and audio coverage both, through the backpressure check atSoftwarePacketReadAhead.swift:459, and again in everysnapshot.What it cost
Measured over the island count, with the tests written before the change:
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-clocksamplecannot 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 itfrontierSecondsplus theIndexingIterator.next()andDouble.initof 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