From c15898fee9e968bca3991ad9053043c30788fe49 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 27 Sep 2026 17:01:13 -0700 Subject: [PATCH 1/2] perf(net): search the track timeline instead of scanning it The latency guard scanned every retained group per popped group and per re-evaluation, and the cache scanned it per publish. Keep the timeline sorted and binary-search it, and skip the prune scan until an entry can be due. Adds js/net/bench/track.ts to the nightly. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 6 ++ js/net/bench/track.ts | 101 ++++++++++++++++++++++++ js/net/src/track.test.ts | 52 +++++++++++++ js/net/src/track.ts | 139 +++++++++++++++++++++++----------- quest/m1/README.md | 1 - quest/m1/js-timeline-scans.md | 32 -------- 6 files changed, 252 insertions(+), 79 deletions(-) create mode 100644 js/net/bench/track.ts delete mode 100644 quest/m1/js-timeline-scans.md diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 91aadf86e07..546458adae5 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -89,6 +89,12 @@ jobs: if: ${{ !cancelled() }} run: nix develop --command bun js/net/bench/frames.ts + # Fails if publishing a group costs more as the track retains more groups, + # which means the latency guard or the cache went back to scanning them all. + - name: JS track retention benchmark + if: ${{ !cancelled() }} + run: nix develop --command bun js/net/bench/track.ts + # Runs each moq-net Criterion routine once, so a bench that panics (an # unparsable version, a shape that deadlocks) fails here instead of on the # next manual run. Timing is not compared; nextest never runs a diff --git a/js/net/bench/track.ts b/js/net/bench/track.ts new file mode 100644 index 00000000000..588d041ee2c --- /dev/null +++ b/js/net/bench/track.ts @@ -0,0 +1,101 @@ +/** Sweep retained groups and subscribers for one group published through a track and received. */ +import { type Consumer as GroupConsumer, Producer as GroupProducer } from "../src/group.ts"; +import { Milli, Timestamp } from "../src/time.ts"; +import { Producer, type Subscriber } from "../src/track.ts"; + +const retainedCounts = [25, 100, 400, 1500]; +const subscriberCounts = [1, 4, 16]; +// Open groups each subscriber is still reading, so their latency guards re-evaluate on every arrival. +const heldCounts = [0, 16]; +const groups = 100; +const reps = 7; +// Publishing a group must not cost more as the retained window grows, or a guard or cache pass +// has gone back to scanning every retained group. A scan costs 4x to 40x across the sweep; the +// margin is loose because the machine is noisy. +const maxSlope = 5; +// Long enough that nothing ages out, and nothing held is ever stale. +const window = Milli(3_600_000); +const payload = new Uint8Array(80); +let checksum = 0; + +// Let every woken reader re-evaluate its guard before the next group. +const flush = () => new Promise((resolve) => setImmediate(resolve)); + +function receive(subscribers: Subscriber[]): GroupConsumer[] { + const received: GroupConsumer[] = []; + for (const subscriber of subscribers) { + const group = subscriber.tryRecvGroup(); + if (!group) throw new Error("group was not delivered"); + const frame = group.tryReadFrame(); + if (!frame) throw new Error("frame was not delivered"); + checksum += frame.payload.byteLength; + received.push(group); + } + return received; +} + +async function measure(retained: number, subscriberCount: number, held: number): Promise { + const producer = new Producer("bench").accept({ maxAge: window }); + const subscribers = Array.from({ length: subscriberCount }, () => producer.subscribe({ maxAge: window })); + let sequence = 0; + // Inserted by sequence, the way the wire hands a subscribed track its groups. + const publish = (close: boolean) => { + const group = new GroupProducer(sequence); + producer.writeGroup(group); + group.writeFrame({ payload, timestamp: Timestamp.fromMillis(sequence++) }); + if (close) group.close(); + return group; + }; + + // The held groups stay open, each with a reader parked on its next frame. + const open: GroupProducer[] = []; + const reads: Promise[] = []; + for (let index = 0; index < retained; index++) { + const isHeld = index < held; + const group = publish(!isHeld); + const received = receive(subscribers); + if (isHeld) { + open.push(group); + for (const consumer of received) reads.push(consumer.readFrame()); + } + } + await flush(); + + const start = performance.now(); + for (let index = 0; index < groups; index++) { + publish(true); + receive(subscribers); + await flush(); + } + const elapsed = performance.now() - start; + + for (const group of open) group.close(); + await Promise.all(reads); + producer.close(); + return (elapsed * 1000) / groups; +} + +// Warm the JIT so the first row isn't the slowest. +await measure(100, 1, 0); + +console.log("retained,subscribers,held,publish_us"); +for (const held of heldCounts) { + for (const subscriberCount of subscriberCounts) { + let baseline: number | undefined; + for (const retained of retainedCounts) { + const samples: number[] = []; + for (let rep = 0; rep < reps; rep++) samples.push(await measure(retained, subscriberCount, held)); + // The fastest run is the one least disturbed by the rest of the machine. + const us = Math.min(...samples); + console.log(`${retained},${subscriberCount},${held},${us.toFixed(1)}`); + + baseline ??= us; + if (us > baseline * maxSlope) { + throw new Error( + `${retained} retained groups cost ${us.toFixed(1)} us/group, over ${maxSlope}x ${baseline.toFixed(1)}`, + ); + } + } + } +} +if (checksum === 0) throw new Error("benchmark did no work"); diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index b2efb8eb995..f707fa4d7d5 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -453,6 +453,29 @@ test("an unstamped immediate successor leaves reach unbounded", async () => { expect((await track.recvGroup())?.sequence).toBe(2); }); +// Groups can arrive out of sequence order; the immediate successor bounds reach even when it +// arrived last. +test("a late-arriving successor bounds a group's reach", async () => { + const producer = new TrackProducer("test").accept({ maxAge: Milli(5000) }); + const track = producer.subscribe({ maxAge: Milli(500) }); + + for (const [sequence, ms] of [ + [0, 0], + [2, 3000], + [1, 200], + ]) { + const group = new GroupProducer(sequence); + group.writeFrame({ payload: enc.encode(`${ms}`), timestamp: Timestamp.fromMillis(ms) }); + group.close(); + producer.writeGroup(group); + } + + // Group 0 reaches at most 200ms, where group 1 starts, so it is past the budget. + // Group 1 reaches 3000ms, the edge itself. + expect((await track.recvGroup())?.sequence).toBe(1); + expect((await track.recvGroup())?.sequence).toBe(2); +}); + // The ordered frame helpers ride the same cursor, so they see the same budget: a // backlog inside it is drained in full, and what is past it is skipped. test("ordered frame reads follow the budget", async () => { @@ -963,6 +986,35 @@ test("retention reclaims a group the publisher abandoned open", async () => { } }); +test("an idle live edge ages out as soon as a newer group arrives", async () => { + const clock = mockMonotonicTime(10_000); + try { + const producer = new TrackProducer("test").accept({ maxAge: Milli(100) }); + const track = producer.subscribe({ maxAge: Milli(100) }); + const edge = producer.appendGroup(); + edge.writeString("first"); + + const group = await track.recvGroup(); + if (!group) throw new Error("missing group"); + expect(await group.readString()).toBe("first"); + + // A prune while it is still the live edge keeps it, and finds nothing else to age out. + clock.set(10_200); + producer.subscribe({ maxAge: Milli(100) }); + + // A successor ends the exemption, and the group is long past the window, so that + // write evicts it rather than a later wakeup. + producer.appendGroup(); + const read = group.readFrame().then( + () => "clean end", + (err: unknown) => err, + ); + expect(await Promise.race([read, settle().then(() => "still parked")])).toBeInstanceOf(TooFarBehind); + } finally { + clock.restore(); + } +}); + test("an abandoned open group ages out with no further write", async () => { // Real time, since this is about the wakeup: nothing writes to the track again, so // without a timer the read below parks forever. diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 913d1ce70f2..6970fbef18b 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -285,6 +285,31 @@ export class Consumer { } } +// The index of the first timeline group at or after `sequence`. +function timelineIndex(timeline: GroupConsumer[], sequence: number): number { + let lo = 0; + let hi = timeline.length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (timeline[mid].sequence < sequence) lo = mid + 1; + else hi = mid; + } + return lo; +} + +// Add a group to a sorted timeline, replacing any group with the same sequence. Groups +// usually arrive in order, so the append is checked first. +function timelineInsert(timeline: GroupConsumer[], group: GroupConsumer): void { + const last = timeline.at(-1); + if (!last || last.sequence < group.sequence) { + timeline.push(group); + return; + } + const index = timelineIndex(timeline, group.sequence); + if (timeline[index]?.sequence === group.sequence) timeline[index] = group; + else timeline.splice(index, 0, group); +} + // The shared state behind a Producer / Subscriber pair. Package-internal // wiring, unexported so it never appears in the published type declarations. class TrackState { @@ -292,9 +317,10 @@ class TrackState { producer?: Producer; groups = new Signal([]); // Every group still in the producer's replay cache, including groups this - // subscriber already consumed, paired with its source queue time. Drift anchors - // have the same lifetime as content. - timeline = new Map(); + // subscriber already consumed, sorted by sequence. Drift anchors have the same + // lifetime as content. Sorted so the latency guard, evaluated per group and per + // arrival, searches it instead of scanning the whole retained window. + timeline: GroupConsumer[] = []; // First timestamps mutate group state rather than track state, so held groups // watch this revision as well as arrivals when enforcing latency after handoff. timelineChanged = new Signal(0); @@ -346,7 +372,7 @@ async function resolveInfo(state: TrackState): Promise { // A source group retained in the producer cache, with the mirror handed to each sink // so eviction can drop them together. -type CachedGroup = { group: GroupProducer; time: number; mirrors: Map }; +type CachedGroup = { group: GroupProducer; mirrors: Map }; function bindProducer(name: string, producer: Producer, sequences: TrackSequences): void { let shared = sequences.get(name); @@ -403,6 +429,11 @@ export class Producer { // it handed to every sink so eviction can drop them too: otherwise a slow consumer // that never reads would pin old groups (and their frame bytes) forever. #cache: CachedGroup[] = []; + // The same entries by sequence, so a write finds a duplicate without a scan. + #cached = new Map(); + // When the stalest entry the last prune kept ages out. Activity only moves forward, so + // until then a prune would evict nothing, and skips the scan. + #pruneAt = 0; // Wakeup for the next entry due to age out. Writes settle retention inline, but a // publisher that stalls stops writing, so without this an abandoned group (and any @@ -465,6 +496,8 @@ export class Producer { if (this.#state.closed.peek() !== undefined) return this; const resolved = infoDefaults(info); this.#state.info.set(resolved); + // The window may have shrunk, so the next prune scans. + this.#pruneAt = 0; // Propagate to any sink handed out before accept (the on-demand path). for (const sink of this.#sinks) sink.info.set(resolved); this.#updateSubscription(); @@ -569,7 +602,7 @@ export class Producer { #mirror(entry: CachedGroup, sink: TrackState): void { const dst = entry.group.mirror(); entry.mirrors.set(sink, dst); - sink.timeline.set(dst.sequence, { group: dst, time: entry.time }); + timelineInsert(sink.timeline, dst); void dst.readable().then(() => sink.timelineChanged.update((revision) => revision + 1)); sink.latest = Math.max(sink.latest ?? 0, dst.sequence); sink.groups.mutate((groups) => { @@ -591,7 +624,8 @@ export class Producer { const i = groups.indexOf(mirror); if (i >= 0) groups.splice(i, 1); }); - if (sink.timeline.get(mirror.sequence)?.group === mirror) sink.timeline.delete(mirror.sequence); + const index = timelineIndex(sink.timeline, mirror.sequence); + if (sink.timeline[index] === mirror) sink.timeline.splice(index, 1); mirror.close(); } entry.mirrors.clear(); @@ -610,19 +644,35 @@ export class Producer { // written, so an abandoned open group ages out instead of pinning its buffer (and // any reader parked in it) forever. #prune(): void { + if (performance.now() < this.#pruneAt) return; + const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS; const cutoff = performance.now() - maxAgeMs; const live = this.#liveEdge(); + let oldest = Number.POSITIVE_INFINITY; const retained: CachedGroup[] = []; for (const entry of this.#cache) { - if (entry.group === live || entry.group.activity >= cutoff) { + if (entry.group === live) { retained.push(entry); - continue; + } else if (entry.group.activity >= cutoff) { + retained.push(entry); + oldest = Math.min(oldest, entry.group.activity); + } else { + this.#cached.delete(entry.group.sequence); + this.#evict(entry); } - this.#evict(entry); } this.#cache = retained; + this.#pruneAt = oldest + maxAgeMs; + this.#schedulePrune(); + } + + // Pull the next prune forward to when `group` ages out, if that is sooner. + #pruneBy(group: GroupProducer): void { + const at = group.activity + (this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS); + if (at >= this.#pruneAt) return; + this.#pruneAt = at; this.#schedulePrune(); } @@ -633,24 +683,16 @@ export class Producer { clearTimeout(this.#pruneTimer); this.#pruneTimer = undefined; if (this.#state.closed.peek() !== undefined) return; + if (this.#pruneAt === Number.POSITIVE_INFINITY) return; - // One pass, no intermediate arrays: this runs on every publish, and a spread - // over the cache would also cap how many groups a track can hold. - const live = this.#liveEdge(); - let oldest: number | undefined; - for (const entry of this.#cache) { - if (entry.group === live) continue; - if (oldest === undefined || entry.group.activity < oldest) oldest = entry.group.activity; - } - if (oldest === undefined) return; - - const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS; // setTimeout truncates its delay to a signed 32-bit int, so a longer window // would fire immediately and spin. Wake at the cap instead and re-arm: #prune // retains anything still fresh, so the extra wakeups are the only cost. - const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, oldest + maxAgeMs - performance.now())); + const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, this.#pruneAt - performance.now())); const timer = setTimeout(() => { this.#pruneTimer = undefined; + // Scan even if the cap woke this early, so the scan re-arms the wakeup. + this.#pruneAt = 0; this.#prune(); }, delay); // A cache prune is never a reason to hold a Node/Bun process open. @@ -660,9 +702,15 @@ export class Producer { // Retain a source group and fan it out to every live sink. #publish(group: GroupProducer): void { - const entry: CachedGroup = { group, time: performance.now(), mirrors: new Map() }; + // The previous newest group was exempt from the last prune while open, and a written + // group may already be idle, so both count toward the next prune. + const previous = this.#cache.at(-1)?.group; + const entry: CachedGroup = { group, mirrors: new Map() }; this.#cache.push(entry); + this.#cached.set(group.sequence, entry); for (const sink of this.#sinks) this.#mirror(entry, sink); + if (previous) this.#pruneBy(previous); + this.#pruneBy(group); // Give held mirrors the new live edge before pruning their timeline entry, // so their latency guard can preserve a terminal expiry verdict. this.#prune(); @@ -699,14 +747,14 @@ export class Producer { writeGroup(group: GroupProducer) { this.#writable(group.sequence); - const existing = this.#cache.findIndex((entry) => entry.group.sequence === group.sequence); - if (existing >= 0) { - const entry = this.#cache[existing]; - if (!(entry.group.closed.peek() instanceof Error)) { + const existing = this.#cached.get(group.sequence); + if (existing) { + if (!(existing.group.closed.peek() instanceof Error)) { throw new Error(`duplicate group: sequence=${group.sequence}`); } - this.#evict(entry); - this.#cache.splice(existing, 1); + this.#evict(existing); + this.#cache.splice(this.#cache.indexOf(existing), 1); + this.#cached.delete(group.sequence); } // Only advance the shared counter upward (for appendGroup auto-increment). @@ -876,16 +924,17 @@ export class Subscriber { end?: number; } { const { end } = this.#cursor.peek(); + const timeline = this.#state.timeline; let presentation: { sequence: number; timestamp: Timestamp } | undefined; - for (const { group } of this.#state.timeline.values()) { - if (end !== undefined && group.sequence >= end) continue; + // The edge wants the newest content that exists, so it takes the newest + // stamped group's latest frame: walk back from the cap to the first one. + for (let i = (end === undefined ? timeline.length : timelineIndex(timeline, end)) - 1; i >= 0; i--) { + const group = timeline[i]; if (group.closed.peek() instanceof Error) continue; - // The edge wants the newest content that exists, so it takes the newest - // stamped group's latest frame. const timestamp = hooks.groupTimestamp(group); - if (timestamp !== undefined && (!presentation || group.sequence > presentation.sequence)) { - presentation = { sequence: group.sequence, timestamp: hooks.groupLatest(group) ?? timestamp }; - } + if (timestamp === undefined) continue; + presentation = { sequence: group.sequence, timestamp: hooks.groupLatest(group) ?? timestamp }; + break; } const requested = this.#state.update.peek()?.maxAge ?? 0; @@ -910,15 +959,14 @@ export class Subscriber { // unstamped successor will begin, and shrinking the bound is the unsafe direction. // An unstamped successor leaves the reach unbounded until it presents a frame. #reach(sequence: number, end?: number): number | undefined { - let successor: GroupConsumer | undefined; - for (const { group } of this.#state.timeline.values()) { - if (group.sequence <= sequence) continue; - if (end !== undefined && group.sequence >= end) continue; - if (group.closed.peek() instanceof Error) continue; - if (!successor || group.sequence < successor.sequence) successor = group; + const timeline = this.#state.timeline; + for (let i = timelineIndex(timeline, sequence + 1); i < timeline.length; i++) { + const successor = timeline[i]; + if (end !== undefined && successor.sequence >= end) break; + if (successor.closed.peek() instanceof Error) continue; + return hooks.groupTimestamp(successor)?.asMillis(); } - if (!successor) return undefined; - return hooks.groupTimestamp(successor)?.asMillis(); + return undefined; } // Whether the drift budget says to give up on `group`. @@ -945,8 +993,7 @@ export class Subscriber { end?: number; }, ): boolean { - const candidate = this.#state.timeline.get(group.sequence); - if (candidate?.group !== group) return false; + if (this.#state.timeline[timelineIndex(this.#state.timeline, group.sequence)] !== group) return false; const reach = this.#reach(group.sequence, drift.end); return ( @@ -1145,7 +1192,7 @@ export class Subscriber { }); this.#frameGroup?.close(abort); this.#frameGroup = undefined; - this.#state.timeline.clear(); + this.#state.timeline.length = 0; } /** diff --git a/quest/m1/README.md b/quest/m1/README.md index e78483fdbab..9d3e8724764 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -114,7 +114,6 @@ transport, benchmark tooling); worktrees isolate commits, not semantics. - [Scope track priority](/quest/m1/track-priority-scope.md) - priority orders one owner's streams, and a shared cluster session is fair across tenants - [Stream sessions](/quest/m1/uring-tcp/README.md) - serve WebSocket and HTTP from the io_uring workers, where io_uring pays off most - [IETF on the ring](/quest/m1/uring-ietf.md) - the io_uring workers serve moq-transport sessions too, so a uring relay drops no client protocol -- [JS timeline scans](/quest/m1/js-timeline-scans.md) - a `@moq/net` publisher's per-group cost stays flat as the retained window grows - [BBR ACK cleanup](/quest/m1/bbr-ack-cleanup.md) - packet bookkeeping scales with completed entries instead of scanning the flight on every ACK - [Perf](/quest/m1/perf/README.md) - eliminate measured hot-path costs across moq-uring, kio, and the moq-net model - [#2924](/quest/m1/2924-moq-relay-tls-rotation-is-not-atomic-across-thread-per.md) - every listener on both runtimes shares one reloadable served identity, so rotation is atomic and generate works with workers diff --git a/quest/m1/js-timeline-scans.md b/quest/m1/js-timeline-scans.md deleted file mode 100644 index 9c0d856afaf..00000000000 --- a/quest/m1/js-timeline-scans.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] JS timeline scans - -## Goal - -A `@moq/net` publisher's per-group cost stays flat as the retained window -grows. `Subscriber.#drift()`, `#reach()`, and the prune pass stop scanning -every cached group. - -## Plan - -- `js/net/src/track.ts` keeps the timeline in a `Map` of every cached group, - consumed ones included. `#drift` and `#reach` scan it per popped group and on - every guard re-evaluation, and `#prune`/`#schedulePrune` scan it per publish. -- Mirror Rust's `rs/moq-net/src/model/track.rs`: keep sequences sorted (append - fast path, binary insert otherwise). Drift walks backward to the first - stamped, non-errored group in range; reach binary-searches the successor. - Fold the prune scans in if the benchmark shows them. Memoizing per revision - was rejected: a new group still costs O(n) per subscriber. An incremental - edge was rejected: invalidation on aborts and cursor ends gets fiddly. -- Add `js/net/bench/track.ts`: microseconds per published group through - Producer, Subscriber, `tryRecvGroup`, and the guard, with no transport. Sweep - retained groups (about 25 to 1500) against subscribers (1 to 16), plus a case - with guards in flight. Wire it into `nightly.yml` beside `broadcasts.ts`. The - fix passes when the retained-groups axis is flat. - -## Closes - -- [#4246](https://github.com/moq-dev/moq/issues/4246) - per-group publishing cost grows with the retained window - -## Related - -- [Browser benchmarks](/quest/m1/browser-benchmarks.md) - the browser media path; this bench covers the track model From ead5230019625299eb11aed8377577b30e111247 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 27 Sep 2026 17:30:34 -0700 Subject: [PATCH 2/2] perf(net): scan the track cache once per slice of its window Replace the prune deadline fast path, which skipped the scan forever after an exempt live edge closed and scanned every write once the window filled, with a throttle: at most PRUNE_SLICES scans per window, and a throttled call arms a wakeup so a scan still follows. The bench now ages one group out per write for the unheld rows. Co-Authored-By: Claude Opus 5.5 --- js/net/bench/track.ts | 19 +++++++---- js/net/src/track.test.ts | 5 +-- js/net/src/track.ts | 68 ++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/js/net/bench/track.ts b/js/net/bench/track.ts index 588d041ee2c..a9d910c8b60 100644 --- a/js/net/bench/track.ts +++ b/js/net/bench/track.ts @@ -10,14 +10,18 @@ const heldCounts = [0, 16]; const groups = 100; const reps = 7; // Publishing a group must not cost more as the retained window grows, or a guard or cache pass -// has gone back to scanning every retained group. A scan costs 4x to 40x across the sweep; the -// margin is loose because the machine is noisy. +// has gone back to scanning every retained group. The sweep spans 60x, so a scan lands well past +// this; the margin is loose because the machine is noisy. const maxSlope = 5; -// Long enough that nothing ages out, and nothing held is ever stale. -const window = Milli(3_600_000); const payload = new Uint8Array(80); let checksum = 0; +// A clock that advances a millisecond per group, so a window of N ms retains N groups and every +// publish ages the oldest one out. Timing reads the real clock. +const now = performance.now.bind(performance); +let clock = 0; +performance.now = () => clock; + // Let every woken reader re-evaluate its guard before the next group. const flush = () => new Promise((resolve) => setImmediate(resolve)); @@ -35,11 +39,14 @@ function receive(subscribers: Subscriber[]): GroupConsumer[] { } async function measure(retained: number, subscriberCount: number, held: number): Promise { + // Held groups would age out too, so those rows keep everything instead. + const window = Milli(held > 0 ? 3_600_000 : retained); const producer = new Producer("bench").accept({ maxAge: window }); const subscribers = Array.from({ length: subscriberCount }, () => producer.subscribe({ maxAge: window })); let sequence = 0; // Inserted by sequence, the way the wire hands a subscribed track its groups. const publish = (close: boolean) => { + clock++; const group = new GroupProducer(sequence); producer.writeGroup(group); group.writeFrame({ payload, timestamp: Timestamp.fromMillis(sequence++) }); @@ -61,13 +68,13 @@ async function measure(retained: number, subscriberCount: number, held: number): } await flush(); - const start = performance.now(); + const start = now(); for (let index = 0; index < groups; index++) { publish(true); receive(subscribers); await flush(); } - const elapsed = performance.now() - start; + const elapsed = now() - start; for (const group of open) group.close(); await Promise.all(reads); diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index f707fa4d7d5..e3ca4517ff0 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -986,7 +986,7 @@ test("retention reclaims a group the publisher abandoned open", async () => { } }); -test("an idle live edge ages out as soon as a newer group arrives", async () => { +test("an idle live edge ages out once a newer group arrives", async () => { const clock = mockMonotonicTime(10_000); try { const producer = new TrackProducer("test").accept({ maxAge: Milli(100) }); @@ -1002,8 +1002,9 @@ test("an idle live edge ages out as soon as a newer group arrives", async () => clock.set(10_200); producer.subscribe({ maxAge: Milli(100) }); - // A successor ends the exemption, and the group is long past the window, so that + // A successor ends the exemption, and the group is long past the window, so the // write evicts it rather than a later wakeup. + clock.set(10_300); producer.appendGroup(); const read = group.readFrame().then( () => "clean end", diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 6970fbef18b..dc6e8747ec7 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -24,6 +24,9 @@ export type { Datagram } from "./datagram.ts"; // and fires right away. const MAX_TIMEOUT_MS = 2 ** 31 - 1; +// The cache scans at most this many times per retention window. +const PRUNE_SLICES = 8; + /** Default {@link Info.maxAge} window (milliseconds) when the publisher does not set one. */ export const DEFAULT_MAX_AGE_MS = Milli(5000); @@ -431,14 +434,14 @@ export class Producer { #cache: CachedGroup[] = []; // The same entries by sequence, so a write finds a duplicate without a scan. #cached = new Map(); - // When the stalest entry the last prune kept ages out. Activity only moves forward, so - // until then a prune would evict nothing, and skips the scan. - #pruneAt = 0; + // When the cache was last scanned. See #prune. + #pruned = Number.NEGATIVE_INFINITY; // Wakeup for the next entry due to age out. Writes settle retention inline, but a // publisher that stalls stops writing, so without this an abandoned group (and any // reader parked in it) would wait for a write that never comes. #pruneTimer?: ReturnType; + #pruneTimerAt = 0; // One independent downstream state per live subscriber. #sinks = new Set(); @@ -496,8 +499,6 @@ export class Producer { if (this.#state.closed.peek() !== undefined) return this; const resolved = infoDefaults(info); this.#state.info.set(resolved); - // The window may have shrunk, so the next prune scans. - this.#pruneAt = 0; // Propagate to any sink handed out before accept (the on-demand path). for (const sink of this.#sinks) sink.info.set(resolved); this.#updateSubscription(); @@ -643,74 +644,73 @@ export class Producer { // Evict cached groups idle for longer than the cache window. Idle means nothing // written, so an abandoned open group ages out instead of pinning its buffer (and // any reader parked in it) forever. + // + // Scans at most once per slice of the window, so a track publishing faster than that + // evicts a run of groups per scan instead of scanning everything to evict one per + // write. A group can outlive the window by up to one slice. #prune(): void { - if (performance.now() < this.#pruneAt) return; - const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS; - const cutoff = performance.now() - maxAgeMs; + const now = performance.now(); + const slice = maxAgeMs / PRUNE_SLICES; + if (now < this.#pruned + slice) { + // Something may have come due since the last scan, so make sure another follows. + this.#wake(this.#pruned + slice); + return; + } + this.#pruned = now; + + const cutoff = now - maxAgeMs; const live = this.#liveEdge(); - let oldest = Number.POSITIVE_INFINITY; + let oldest: number | undefined; const retained: CachedGroup[] = []; for (const entry of this.#cache) { if (entry.group === live) { retained.push(entry); } else if (entry.group.activity >= cutoff) { retained.push(entry); - oldest = Math.min(oldest, entry.group.activity); + if (oldest === undefined || entry.group.activity < oldest) oldest = entry.group.activity; } else { this.#cached.delete(entry.group.sequence); this.#evict(entry); } } this.#cache = retained; - this.#pruneAt = oldest + maxAgeMs; - this.#schedulePrune(); - } - // Pull the next prune forward to when `group` ages out, if that is sooner. - #pruneBy(group: GroupProducer): void { - const at = group.activity + (this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS); - if (at >= this.#pruneAt) return; - this.#pruneAt = at; - this.#schedulePrune(); - } - - // Arm the wakeup for the next entry due to age out, replacing any pending one. - // Writes settle retention inline, so this only has to cover the case no write - // follows. Cheap to over-arm: an entry written since is retained and re-armed. - #schedulePrune(): void { + // Replace the wakeup with one for the next entry due to age out. Writes settle + // retention inline, so this only has to cover the case no write follows. Cheap to + // over-arm: an entry written since is retained and re-armed. clearTimeout(this.#pruneTimer); this.#pruneTimer = undefined; + if (oldest !== undefined) this.#wake(Math.max(oldest + maxAgeMs, now + slice)); + } + + // Arm the prune wakeup for `at`, unless one is already armed sooner. + #wake(at: number): void { if (this.#state.closed.peek() !== undefined) return; - if (this.#pruneAt === Number.POSITIVE_INFINITY) return; + if (this.#pruneTimer !== undefined && this.#pruneTimerAt <= at) return; + clearTimeout(this.#pruneTimer); // setTimeout truncates its delay to a signed 32-bit int, so a longer window // would fire immediately and spin. Wake at the cap instead and re-arm: #prune // retains anything still fresh, so the extra wakeups are the only cost. - const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, this.#pruneAt - performance.now())); + const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, at - performance.now())); const timer = setTimeout(() => { this.#pruneTimer = undefined; - // Scan even if the cap woke this early, so the scan re-arms the wakeup. - this.#pruneAt = 0; this.#prune(); }, delay); // A cache prune is never a reason to hold a Node/Bun process open. (timer as unknown as { unref?: () => void }).unref?.(); this.#pruneTimer = timer; + this.#pruneTimerAt = at; } // Retain a source group and fan it out to every live sink. #publish(group: GroupProducer): void { - // The previous newest group was exempt from the last prune while open, and a written - // group may already be idle, so both count toward the next prune. - const previous = this.#cache.at(-1)?.group; const entry: CachedGroup = { group, mirrors: new Map() }; this.#cache.push(entry); this.#cached.set(group.sequence, entry); for (const sink of this.#sinks) this.#mirror(entry, sink); - if (previous) this.#pruneBy(previous); - this.#pruneBy(group); // Give held mirrors the new live edge before pruning their timeline entry, // so their latency guard can preserve a terminal expiry verdict. this.#prune();