diff --git a/js/justfile b/js/justfile index 5950721c873..8226f330646 100644 --- a/js/justfile +++ b/js/justfile @@ -71,7 +71,7 @@ test-changed list: coverage package="": python3 ../sh/js/coverage.py '{{ package }}' -# Run one net benchmark: broadcasts, forward, requests, fills, fill-probe, reader, frames, track, resume, observed, drift, ledger, bandwidth, replay, varint, lite-varint, priority, subscribe-drop, refresh, compression, or datagrams. +# Run one net benchmark: broadcasts, forward, requests, fills, fill-probe, reader, frames, track, resume, observed, drift, ledger, bandwidth, replay, varint, lite-varint, priority, subscribe-drop, window-drop, refresh, compression, or datagrams. bench name: uv run --no-project python ../sh/js/install.py frozen bun net/bench/{{ name }}.ts diff --git a/js/net/bench/window-drop.ts b/js/net/bench/window-drop.ts new file mode 100644 index 00000000000..138e672a035 --- /dev/null +++ b/js/net/bench/window-drop.ts @@ -0,0 +1,54 @@ +/** + * Sweep what a drop costs a reader with many ranges: one-group time ranges a group apart, each + * waiting for the groups held to place it, given up lowest first so each drop places one more. + * Mirrors the Rust `publisher_drop` bench. A drop must cost about the same however many ranges + * the reader has, or the window has gone back to trying every range or rebuilding them all. + */ + +import { StreamCode, StreamError } from "../src/error.ts"; +import { hooks } from "../src/internal.ts"; +import { Timescale } from "../src/time.ts"; +import { Producer, type Range } from "../src/track.ts"; + +// `WINDOW_DROP_MAX` caps both axes, for a quick run. +const sweep = [1, 32, 1024, 8192].filter( + (count) => count <= Number(process.env.WINDOW_DROP_MAX ?? Number.POSITIVE_INFINITY), +); +// Each cell times at least this many drops, so a cell of one drop isn't one noisy sample. +const timed = 256; + +function measure(ranges: number, drops: number): number { + const producer = new Producer("bench").accept({ timescale: Timescale.MILLI, maxAge: null }); + // Every other group, so no two ranges merge into one run, and one open range past them all so + // the reader never settles. + const spans: Range[] = Array.from({ length: ranges }, (_, range) => { + const at = BigInt(2 * range); + return { start: { time: at }, end: { time: at } }; + }); + spans.push({ start: { time: BigInt(2 * ranges) } }); + const subscriber = producer.subscribe({ ranges: spans }); + subscriber.tryRecvGroup(); + // Alternating errors keep adjacent drops separate runs. + const errors = [new StreamError(StreamCode.NotFound), new StreamError(StreamCode.Cancel)]; + const start = performance.now(); + for (let range = 0; range < drops; range++) { + const from = range === 0 ? 0n : BigInt(2 * range - 1); + hooks.dropTrackGroups(producer, from, BigInt(2 * range + 1), { error: errors[range % 2] }); + subscriber.tryRecvGroup(); + } + const elapsed = performance.now() - start; + producer.close(); + return (elapsed * 1000) / drops; +} + +// Untimed, so the first cell doesn't measure the JIT warming up. +measure(32, 32); +console.log("ranges,drops,drop_us"); +for (const ranges of sweep) { + for (const drops of sweep) { + if (drops > ranges) continue; + const reps = Math.max(3, Math.ceil(timed / drops)); + const times = Array.from({ length: reps }, () => measure(ranges, drops)).sort((a, b) => a - b); + console.log(`${ranges},${drops},${times[Math.floor(reps / 2)].toFixed(2)}`); + } +} diff --git a/js/net/src/drops.test.ts b/js/net/src/drops.test.ts index ba67b88977d..a9610e60828 100644 --- a/js/net/src/drops.test.ts +++ b/js/net/src/drops.test.ts @@ -54,3 +54,31 @@ test("drops: what a change replaced or forgot is reported since its revision, un expect(drops.removedSince(recent)).toBeUndefined(); expect(drops.removedSince(drops.revision)).toEqual([]); }); + +test("drops: the keys given up from a key on stop where a walk of the runs stops", () => { + const drops = new DropSet(); + const errors = [new NotFound("a"), new NotFound("b")]; + let seed = 7; + const random = (below: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + // The high bits: an LCG's low bits cycle. + return (seed >>> 16) % below; + }; + for (let step = 0; step < 400; step++) { + const start = BigInt(random(64)); + const end = start + BigInt(1 + random(6)); + if (random(5) === 0) drops.clear([{ start, end }]); + else if (random(4) === 0) drops.set(start, end); + else drops.set(start, end, { error: errors[random(2)], skip: random(3) === 0 || undefined }); + for (let from = 0n; from < 72n; from++) { + let at = from; + for (const run of drops.overlapping(from, 1000n)) { + if (run.start > at) break; + at = run.end; + } + expect(drops.coveredFrom(from)).toBe(at); + expect(drops.covers(from, at)).toBe(true); + expect(drops.covers(from, at + 1n)).toBe(false); + } + } +}); diff --git a/js/net/src/drops.ts b/js/net/src/drops.ts index bcc6262dcc0..fc33263ab51 100644 --- a/js/net/src/drops.ts +++ b/js/net/src/drops.ts @@ -131,6 +131,9 @@ const LOG_SLACK = 64; */ export class DropSet { readonly #runs = new Runs(sameHole); + // The keys in `#runs`, whatever the reason, as coalesced runs, so whether drops cover a span is + // one lookup rather than a walk of every run in it. Mirrors the Rust `TrackState::dropped`. + readonly #keys = new Runs(() => true); // Each run's last change, oldest first. An entry whose run was since replaced is dead, and // the log is rebuilt once dead entries clearly outnumber live ones. #log: { revision: number; run: DropRun }[] = []; @@ -169,13 +172,25 @@ export class DropSet { return this.#runs.overlapping(start, end); } + /** Where the keys given up from `from` on, whatever the reason, stop: `from` when it isn't given up. */ + coveredFrom(from: bigint): bigint { + return this.#keys.at(from)?.end ?? from; + } + + /** Whether every key in `[start, end)` is given up, whatever the reason. An empty run is. */ + covers(start: bigint, end: bigint): boolean { + return start >= end || this.coveredFrom(start) >= end; + } + /** Give up `[start, end)` as `run` says, or forget it when `run` is undefined. */ set(start: bigint, end: bigint, run?: Omit): void { + this.#keys.set(start, end, run && { start, end }); this.#apply(this.#runs.set(start, end, run && { ...run, start, end })); } /** Forget each of `ranges`, sorted and disjoint, in one pass. */ clear(ranges: readonly Run[]): void { + this.#keys.clear(ranges); this.#apply(this.#runs.clear(ranges)); } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index d517d3172da..16f33d0b75e 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -33,7 +33,7 @@ import type { } from "./track.ts"; import { max, min } from "./util/key.ts"; import * as Varint from "./varint.ts"; -import type { Resolved, Window } from "./window.ts"; +import type { Holdings, Resolved, Window } from "./window.ts"; import type { Advertised, Advertisements } from "./wire.ts"; /** A null {@link Delay.max} in milliseconds, which the model holds as the largest value every peer can read. */ @@ -703,6 +703,8 @@ export const hooks: { subscriberEnd: (subscriber: Subscriber) => Position | undefined; /** The error that end carries, if the track does not end cleanly. Assigned by `track.ts`. */ subscriberEndError: (subscriber: Subscriber) => Error | undefined; + /** What this subscriber resolves its ranges against, for a test. Assigned by `track.ts`. */ + subscriberHoldings: (subscriber: Subscriber) => Holdings; /** * How many keys just below `key` the publisher declared it never produced: the skip run * ending at `key`, which a moq-transport Prior Group ID Gap names. Mirrors the Rust @@ -1131,6 +1133,9 @@ export const hooks: { subscriberEndError: () => { throw new Error("track.ts not loaded"); }, + subscriberHoldings: () => { + throw new Error("track.ts not loaded"); + }, skippedBelow: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 4f6773e0aa1..bc70a80ed3a 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5624,3 +5624,36 @@ test("finishing returns handles that write nothing new but fill a late group", a expect(hooks.tryRecvGroup(subscriber, false)).toEqual({ kind: "done" }); finished.close(); }); + +test("track: a reader's unplaced times are the times its holdings can't place", () => { + let seed = 5; + const random = (below: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + // The high bits: an LCG's low bits cycle. + return (seed >>> 16) % below; + }; + const errors = [new StreamError(StreamCode.NotFound), new StreamError(StreamCode.Cancel)]; + for (let round = 0; round < 400; round++) { + const received = random(2) === 0; + const producer = received ? receivedTrack() : new TrackProducer("test").accept({ maxAge: null }); + const reader = producer.subscribe(); + for (let step = random(16); step >= 0; step--) { + const holdings = hooks.subscriberHoldings(reader); + const unplaced = holdings.unplaced?.(); + expect(unplaced).toBeDefined(); + for (let at = 0n; at < 64n; at++) { + const open = unplaced?.runs.some((run) => run.start <= at && at < run.end) ?? false; + expect([received, at, holdings.containing(at) === "unknown"]).toEqual([received, at, open]); + } + const key = BigInt(random(48)); + if (random(2) === 0) { + try { + putGroup(producer, key); + } catch { + // A key this copy refuses, such as one a skip covers. + } + } else hooks.dropTrackGroups(producer, key, key + BigInt(1 + random(4)), { error: errors[random(2)] }); + } + producer.close(); + } +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 93c029ad153..dfbbbebc457 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1054,6 +1054,8 @@ let timeAnchors: (producer: Producer) => { atKeys: boolean; starts: ReadonlyMap; relative: ReadonlyMap; + /** Bumped whenever the others, or whether the producer is a received copy, change. */ + placing: number; }; /** The groups a reader reads, and the exclusive end it stops at; undefined before its ranges resolve. */ @@ -1318,6 +1320,9 @@ export class Producer { #timeStarts = new Map(); // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); + // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, + // `#timeStarts`, and `#startsAtKeys`. Mirrors the Rust `TrackState::placing`. + #placing = 0; // The highest-key checked start evicted. Eviction is not a key prefix, so a // group above it is checked against it as well as its cached neighbors; one at or below // it may have lost its lower bound. One entry, so nothing grows with the cache. @@ -1510,6 +1515,7 @@ export class Producer { resubscribe = (producer) => producer.#updateSubscription(); hooks.cacheTrack = (producer) => { producer.#wire = undefined; + producer.#placing++; // Nothing feeds the tail anymore, so it owes nothing and readers end at the boundary. for (const state of [producer.#state, ...producer.#sinks]) state.owed.set(false); producer.#endLargest(); @@ -1529,6 +1535,7 @@ export class Producer { : undefined; hooks.receiveTrack = (producer, starts = {}) => { producer.#wire = { starts, served: producer.#wire?.served }; + producer.#placing++; }; hooks.receiveGroup = (producer, { start: sequence, offset: start, fill }) => producer.#createGroup(sequence, start, fill); @@ -1548,12 +1555,15 @@ export class Producer { atKeys: producer.#startsAtKeys, starts: producer.#timeStarts, relative: producer.#relativeStarts, + placing: producer.#placing, }); hooks.startsAtKeys = (producer) => { producer.#startsAtKeys = true; + producer.#placing++; }; hooks.timeStarts = (producer, starts) => { producer.#timeStarts = new Map(starts); + producer.#placing++; }; hooks.relativeStarts = (producer, starts) => { producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); @@ -3590,17 +3600,16 @@ export class Subscriber { const producer = state.producer; if (this.#resume || !producer) return UNHELD; const keys = keysOf(producer); - // Whether drops of a kind `kind` accepts cover every key in `[start, end)`. An empty run is. - const covered = (start: bigint, end: bigint, kind: (run: DropRun) => boolean) => { - if (start >= end) return true; - let at = start; - for (const run of state.drops.overlapping(start, end)) { - if (!kind(run) || run.start > at) return false; - at = run.end; - } - return at >= end; - }; + const drops = state.drops; const anchors = timeAnchors(producer); + // A lite-07 copy holds only part of the track: a group held before a time contains it only + // once nothing between them is still to arrive. + const wire = receivedCopy(producer) && !state.dropsAnchors.peek(); + // The times from `from` on that the drops from there leave open. + const uncovered = (from: bigint): Run | undefined => { + const start = drops.coveredFrom(from); + return start < UNBOUNDED ? { start, end: UNBOUNDED } : undefined; + }; return { resolves: () => true, containing: (time) => { @@ -3612,22 +3621,43 @@ export class Subscriber { // A lite-07 copy holds only part of the track: a group held before `time` contains it // only once nothing between them is still to arrive. Mirrors the Rust // `TrackState::containing_received`. - if (receivedCopy(producer) && !state.dropsAnchors.peek()) { + if (wire) { if (below === time) return { group: time }; const start = anchors.starts.get(time); // The publisher serves nothing at or before it, as for a time older than every group. if (start !== undefined) return start <= time ? { group: start } : "before"; - if (below !== undefined && (covered(below + 1n, time + 1n, () => true) || index < keys.length)) { + if (below !== undefined && (drops.covers(below + 1n, time + 1n) || index < keys.length)) { return { group: below }; } - if (below === undefined && covered(0n, time + 1n, () => true)) return "before"; + if (below === undefined && drops.covers(0n, time + 1n)) return "before"; return "unknown"; } if (below !== undefined) return { group: below }; // Every key at or before it is gone, so whichever group held it will never arrive. - if (covered(0n, time + 1n, () => true)) return "before"; + if (drops.covers(0n, time + 1n)) return "before"; return keys.length === 0 ? "unknown" : "before"; }, + // The "unknown" times of `containing`, apart from those `anchors.starts` places, which the + // mark covers. Mirrors the Rust `Holdings::unplaced for TrackState`. + unplaced: () => { + const mark = anchors.placing * 4 + (state.dropsAnchors.peek() ? 2 : 0) + (state.fetch ? 1 : 0); + const runs: Run[] = []; + const first = keys[0]; + const last = keys.at(-1); + if (anchors.atKeys || state.fetch) { + // Every time starts a group. + } else if (first === undefined || last === undefined) { + const open = uncovered(0n); + if (open) runs.push(open); + } else if (wire) { + // Below every group, up to the first, and above the last, past the drops after it. + const below = uncovered(0n); + if (below && below.start < first) runs.push({ start: below.start, end: first }); + const above = uncovered(last + 1n); + if (above) runs.push(above); + } + return { runs, mark }; + }, nextStart: (from) => keys[keyIndex(keys, from)], // A lite-07 copy holds only part of the track, so it starts where its publisher did, // counting from the same latest group: a newer one moved the live edge. @@ -3648,7 +3678,15 @@ export class Subscriber { reachBack: (_latest, min) => this.#reachBack(min), // Skip runs coalesce, so a gap between two held groups is one run unless the cache // evicted a group inside it. - neverProduced: (start, end) => covered(start, end, (run) => run.skip === true), + neverProduced: (start, end) => { + if (start >= end) return true; + let at = start; + for (const run of drops.overlapping(start, end)) { + if (run.skip !== true || run.start > at) return false; + at = run.end; + } + return at >= end; + }, }; } @@ -4195,6 +4233,7 @@ export class Subscriber { makeSubscriber = (name, state) => new Subscriber(name, state); hooks.subscriberEnd = (subscriber) => subscriber.#resume ? subscriber.#resume.end() : subscriber.#state.end.peek()?.at; + hooks.subscriberHoldings = (subscriber) => subscriber.#holdings(); hooks.subscriberEndError = (subscriber) => subscriber.#resume ? subscriber.#resume.endError() : subscriber.#state.end.peek()?.error; hooks.askSubscriber = (subscriber, ask) => { diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 438b96d1e9f..1128221166f 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -17,6 +17,7 @@ import { rangesStart, span, UNHELD, + type Unplaced, union, Window, wireRanges, @@ -40,12 +41,16 @@ const groups = (start: bigint, end?: bigint): Range => const ascending = (a: bigint, b: bigint) => (a < b ? -1 : a > b ? 1 : 0); -/** The group starts a hop holds, the key runs its producer skipped, and whether a handler serves its history. */ +/** + * The group starts a hop holds, the key runs its producer skipped, whether a handler serves its + * history, and where the dropped prefix ends, which places a time below it before every group. + */ class Keys implements Holdings { held: Set; skipped: Run[] = []; done = false; looks = false; + dropped = 0n; constructor(held: Iterable = []) { this.held = new Set(held); @@ -63,7 +68,11 @@ class Keys implements Holdings { const sorted = this.#sorted(); const group = sorted.filter((key) => key <= at).at(-1); if (group !== undefined) return { group }; - return sorted.length === 0 ? "unknown" : "before"; + return sorted.length === 0 && at >= this.dropped ? "unknown" : "before"; + } + + unplaced(): Unplaced { + return { runs: this.held.size === 0 ? [{ start: this.dropped, end: UNBOUNDED }] : [], mark: 0 }; } nextStart(from: bigint): bigint | undefined { @@ -179,6 +188,19 @@ describe("Window", () => { expect(covered(ended, held.held)).toEqual(keys(0n, 8n)); }); + test("a relative start past every group asks from the oldest group held when placed", () => { + const held = new Keys([33n, 66n, 99n]); + const window = new Window([range(relative(10n))], 99n, held); + expect(window.missable()).toEqual([{ start: 33n, end: UNBOUNDED }]); + // Neither an older group arriving nor the oldest evicted moves it, as in Rust. + held.held.add(20n); + expect(window.settle(99n, held)).toBe(false); + held.held.delete(20n); + held.held.delete(33n); + expect(window.settle(99n, held)).toBe(false); + expect(window.missable()).toEqual([{ start: 33n, end: UNBOUNDED }]); + }); + test("relative anchors count groups back from the latest", () => { let held = new Keys([0n, 33n, 66n, 99n]); let window = new Window([range(relative(2n), relative(1n))], 99n, held); @@ -508,3 +530,68 @@ describe("ranges", () => { expect(joinsLive([range(relative(2n), relative(0n))])).toBe(false); }); }); + +/** {@link Keys} keeping its unplaced times to itself, so a window tries every start on each change. */ +function blind(keys: Keys): Holdings { + return { + resolves: () => true, + looksUp: () => keys.looksUp(), + neverProduced: (start, end) => keys.neverProduced(start, end), + complete: () => keys.complete(), + containing: (at) => keys.containing(at), + nextStart: (from) => keys.nextStart(from), + back: (latest, back) => keys.back(latest, back), + }; +} + +test("window: settling by steps matches trying every start and rebuilding from the spans", () => { + let seed = 11; + const random = (below: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + // The high bits: an LCG's low bits cycle. + return (seed >>> 16) % below; + }; + const seen = (window: Window) => ({ + runs: window.runs(), + missable: window.missable(), + bounds: [window.floor(), window.start(), window.end()], + resolved: window.resolved(), + lookups: window.lookups().map((lookup) => lookup.time), + state: [window.settled, window.pending], + }); + for (let round = 0; round < 3000; round++) { + const ranges = Array.from({ length: 1 + random(11) }, () => { + const start = random(5) === 0 ? relative(BigInt(random(6))) : time(BigInt(random(40))); + return range(start, random(3) === 0 ? undefined : time(BigInt(random(48)))); + }); + const keys = new Keys(); + keys.looks = random(2) === 0; + const fast = new Window(ranges, undefined, keys); + const full = new Window(ranges, undefined, blind(keys)); + for (let step = random(24); step > 0; step--) { + const value = BigInt(random(48)); + const op = random(5); + if (op === 0) keys.held.add(value); + else if (op === 1) { + if (value > keys.dropped) keys.dropped = value; + } else if (op === 2) { + const lookup = fast.lookups()[0]; + if (lookup === undefined) continue; + const answer = value % 2n === 0n ? value : undefined; + fast.found(lookup.time, answer, keys); + full.found(lookup.time, answer, blind(keys)); + } else if (op === 3) keys.done = true; + else if (op === 4) keys.held.delete(keys.nextStart(0n) ?? 0n); + const latest = [...keys.held].sort(ascending).at(-1); + fast.settle(latest, keys); + full.settle(latest, blind(keys)); + expect(seen(fast)).toEqual(seen(full)); + // A window waiting on a latest group reads everything, with no spans yet. + if (fast.pending) continue; + const fresh = fast.fresh(); + expect(fast.runs()).toEqual(fresh.runs); + expect(fast.missable()).toEqual(fresh.missable); + expect(fast.named()).toBe(fresh.named); + } + } +}); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 03596fa4498..670b473034b 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -359,8 +359,21 @@ export interface Holdings { * even in the history a handler serves. Absent for none. */ neverProduced?(start: bigint, end: bigint): boolean; + /** + * Runs of times {@link containing} can't place, so a window skips its time starts in them rather + * than try each again whenever the groups held change. Absent, or undefined, when it can't tell, + * so each is tried. Mirrors the Rust `Holdings::unplaced`. + */ + unplaced?(): Unplaced | undefined; } +/** + * The times a {@link Holdings} can't place: sorted, disjoint runs, apart from the times a placement + * that the groups and drops held don't decide places. `mark` changes whenever such a placement may + * have, so a window that sees it unchanged tries only the times outside `runs` again. + */ +export type Unplaced = { runs: readonly Run[]; mark: number }; + /** A hop that holds no groups of its own, such as a reader following route copies that resolve its ranges for it. */ export const UNHELD: Holdings = Object.freeze({ resolves: () => false, @@ -397,6 +410,11 @@ type Span = { looking?: Lookup; /** A time start the groups held can't place yet: the span reads only the groups from that time until it can. */ waiting: boolean; + /** + * A relative start counting back past every group held asks no miss below this: the oldest group + * held when it was placed, since which of the older ones it reaches is unknown. + */ + reach?: bigint; }; /** @@ -428,8 +446,18 @@ export class Window { #missable: Run[] = []; /** A relative anchor waits on a first group, so nothing is filtered yet. */ #pending = false; - /** The spans, by position, whose time start waits on a group arriving. */ - #waiting: number[] = []; + /** + * The spans, by position, whose time start waits on a group arriving, by that time: a change to + * the groups held tries only those its {@link Holdings.unplaced} leaves out. + */ + #waiting = new ByTime(); + /** The {@link Unplaced.mark} every waiting span was last tried under. */ + #waited: number | undefined; + /** + * The spans, by position, still looking up their time start, which read nothing meanwhile, by + * that time: kept apart so an answer finds them without visiting every span. + */ + #looking = new ByTime(); /** * The ranges, by index, of a live join whose reach back waits on the groups held to measure the * live edge, so they read nothing yet: a group held is no reason to start at it. @@ -437,6 +465,12 @@ export class Window { #reaching: number[] = []; /** Nothing is left to resolve as groups arrive. */ #settled = false; + /** + * How many spans name each start, lowest first, and how many name none: see {@link start}. + * Counted so a read, and a span starting, costs no walk of the spans. + */ + #named: { start: bigint; count: number }[] = []; + #unnamed = 0; /** * The subscriber's Min Age, which reaches a live join back from the live edge. Only a pending * window reads it, when it places its ranges, so a join already started stays where it started. @@ -473,49 +507,78 @@ export class Window { this.#place(latest, holdings); return !this.#pending; } - let moved = false; + // The spans placed or started here, each reading at least what it did before, unless one now + // looks up its start and reads nothing meanwhile. + const moved: number[] = []; if (this.#reaching.length > 0) { const placed = this.#spans.length; const reaching = this.#reaching; this.#reaching = []; for (const index of reaching) this.#placeRange(index, latest, holdings); - if (this.#spans.length > placed) moved = true; + for (let position = placed; position < this.#spans.length; position++) moved.push(position); } - if (this.#waiting.length > 0 && this.#startWaiting(holdings)) moved = true; - if (moved) this.#rebuild(holdings); - return moved; + const shrank = this.#waiting.size > 0 && this.#startWaiting(holdings, moved); + if (shrank) this.#rebuild(); + else if (moved.length === 0) return false; + else this.#grow(moved); + return true; } // Start the spans whose time start waits on a group, once one arrived, or once nothing can - // arrive any more. Returns whether any did. - #startWaiting(holdings: Holdings): boolean { - let started = false; - const waiting = this.#waiting; - this.#waiting = []; - for (const position of waiting) { - const span = this.#spans[position]; - // Only a time start waits on a group. - const at = time(span.anchor) ?? 0n; - const containing = holdings.containing(at); + // arrive any more, adding each one started to `started`. Only the times the holdings may now + // place are tried, so a change placing one of them costs that one, not every span still + // waiting. Returns whether one started reads less than before: it looks up its start, reading + // nothing meanwhile. + #startWaiting(holdings: Holdings, started: number[]): boolean { + const complete = holdings.complete(); + const unplaced = complete ? undefined : holdings.unplaced?.(); + let tried: Timed[]; + if (unplaced !== undefined && this.#waited === unplaced.mark) { + tried = []; + for (const gap of outside(unplaced.runs)) { + for (const entry of this.#waiting.takeIn(gap.start, gap.end)) tried.push(entry); + } + } else { + tried = this.#waiting.takeAll(); + this.#waited = unplaced?.mark; + } + let shrank = false; + for (const entry of tried) { + const containing = holdings.containing(entry.time); let start: bigint; let floored: boolean; if (typeof containing === "object") [start, floored] = [containing.group, true]; // The group holding it is older than any held, or nothing can arrive. - else if (containing === "before" || holdings.complete()) [start, floored] = [0n, false]; + else if (containing === "before" || complete) [start, floored] = [0n, false]; else { - this.#waiting.push(position); + this.#waiting.put(entry); continue; } - started = true; - span.start = start; - span.floored = floored; - span.waiting = false; - // Looked up where the groups held can't place it, as when first resolved. - const asked = time(this.ranges[span.index].start) !== undefined; - const below = asked ? unresolved(at, start, floored, holdings) : undefined; - if (below !== undefined) span.looking = { time: at, below }; + for (const position of entry.positions) { + if (this.#startSpan(position, entry.time, start, floored, holdings)) shrank = true; + started.push(position); + } } - return started; + return shrank; + } + + // Start the span at `position`, whose time start `at` waited, at `start`: looked up where the + // groups held can't place it, as when first resolved, and otherwise reading its groups. Returns + // whether it looks its start up. + #startSpan(position: number, at: bigint, start: bigint, floored: boolean, holdings: Holdings): boolean { + const span = this.#spans[position]; + const asked = time(this.ranges[span.index].start) !== undefined; + this.#unname(position); + span.start = start; + span.floored = floored; + span.waiting = false; + const below = asked ? unresolved(at, start, floored, holdings) : undefined; + if (below !== undefined) { + span.looking = { time: at, below }; + this.#looking.add(at, position); + } + this.#name(position); + return below !== undefined; } /** Whether group `key` is in range. Everything is while a relative anchor waits. */ @@ -543,13 +606,8 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.#pending || this.#reaching.length > 0) return undefined; - let start: bigint | undefined; - for (const span of this.#spans) { - if (!span.floored) return undefined; - start = min(start ?? span.start, span.start); - } - return start; + if (this.#pending || this.#reaching.length > 0 || this.#unnamed > 0) return undefined; + return this.#named[0]?.start; } /** The exclusive end past every group in range, undefined when one stays open or an anchor waits. Nothing in range ends at 0. */ @@ -569,7 +627,7 @@ export class Window { /** * The part of {@link runs} a miss may ask a fill for: all of it but what starts before a time * start that fell before every group held, or below the oldest group held when a relative start - * counted back past them all. Mirrors the Rust `Window::missable`. + * counting back past them all was placed. Mirrors the Rust `Window::missable`. */ missable(from = 0n, end = UNBOUNDED): Run[] { if (from >= end) return []; @@ -609,9 +667,16 @@ export class Window { }); } - /** The time starts still looked up. Mirrors the Rust `Window::lookups`. */ + /** The time starts still looked up, by time. Mirrors the Rust `Window::lookups`. */ lookups(): Lookup[] { - return this.#spans.flatMap((span) => (span.looking ? [span.looking] : [])); + const lookups: Lookup[] = []; + for (const { positions } of this.#looking.all) { + for (const position of positions) { + const looking = this.#spans[position].looking; + if (looking) lookups.push(looking); + } + } + return lookups; } /** @@ -622,46 +687,70 @@ export class Window { * `Window::found`. */ found(at: bigint, group: bigint | undefined, holdings: Holdings = UNHELD): boolean { - const found = this.#spans.filter((span) => span.looking?.time === at); - if (found.length === 0) return false; + const found = this.#looking.take(at); + if (found === undefined) return false; const held = group !== undefined && holdings.nextStart(group) === group ? group : undefined; const containing = holdings.containing(at); const start = held ?? (typeof containing === "object" ? containing.group : undefined); - for (const span of found) { + // A span looking up its start read nothing, so each one found only reads more. + for (const position of found) { + this.#unname(position); + const span = this.#spans[position]; span.looking = undefined; span.start = start ?? 0n; span.floored = start !== undefined; + this.#name(position); } - this.#rebuild(holdings); + this.#grow(found); return true; } + /** + * What rebuilding from the spans would read, may ask a miss for, and name as its start, for a test + * to check the runs grown span by span against. + */ + fresh(): { runs: Run[]; missable: Run[]; named: bigint | undefined } { + const groups: Run[] = []; + const missable: Run[] = []; + let named: bigint | undefined; + for (const span of this.#spans) { + const [reads, asks] = spanReads(span); + if (reads) groups.push(reads); + if (asks) missable.push(asks); + if (span.floored) named = min(named ?? span.start, span.start); + } + return { runs: merge(groups), missable: merge(missable), named }; + } + + /** The lowest start the spans name, counted. */ + named(): bigint | undefined { + return this.#named[0]?.start; + } + // Whether a time start or a join's reach back waits on the groups held to place it. #unplaced(): boolean { - return this.#waiting.length > 0 || this.#reaching.length > 0; + return this.#waiting.size > 0 || this.#reaching.length > 0; } // The first run ending past `key`. #runAt(key: bigint): Run | undefined { - let lo = 0; - let hi = this.#runs.length; - while (lo < hi) { - const mid = (lo + hi) >>> 1; - if (this.#runs[mid].end <= key) lo = mid + 1; - else hi = mid; - } - return this.#runs[lo]; + return this.#runs[above(this.#runs, key)]; } #place(latest: bigint | undefined, holdings: Holdings): void { this.#spans = []; this.#pending = false; - this.#waiting = []; + this.#waiting = new ByTime(); + this.#looking = new ByTime(); this.#reaching = []; + this.#named = []; + this.#unnamed = 0; for (let index = 0; index < this.ranges.length; index++) { if (!isEmpty(this.ranges[index])) this.#placeRange(index, latest, holdings); } - this.#rebuild(holdings); + // Each span waiting was just tried against these holdings. + this.#waited = holdings.unplaced?.()?.mark; + this.#rebuild(); } // Resolve the non-empty range at `index` into a span, or note what it waits on. @@ -692,8 +781,8 @@ export class Window { } if (start === "waiting") { // Reads only the groups from its time until the groups held place it; see #startWaiting. - this.#waiting.push(this.#spans.length); - this.#spans.push({ index, anchor, start: 0n, end: until, floored: false, waiting: true }); + this.#waiting.add(time(anchor) ?? 0n, this.#spans.length); + this.#push({ index, anchor, start: 0n, end: until, floored: false, waiting: true }); return; } // A time start the groups held can't place reads nothing until its lookup answers. A join's @@ -701,49 +790,208 @@ export class Window { const at = time(anchor); const asked = time(range.start) !== undefined && at === time(range.start); const below = at === undefined || !asked ? undefined : unresolved(at, start.group, start.floored, holdings); - this.#spans.push({ + const looking = at !== undefined && below !== undefined ? { time: at, below } : undefined; + if (looking) this.#looking.add(looking.time, this.#spans.length); + const counted = !start.floored && relative(anchor) !== undefined; + this.#push({ index, anchor, start: start.group, end: until, floored: start.floored, - looking: at !== undefined && below !== undefined ? { time: at, below } : undefined, + looking, waiting: false, + reach: counted ? (holdings.nextStart(0n) ?? start.group) : undefined, }); } + #push(span: Span): void { + this.#spans.push(span); + this.#name(this.#spans.length - 1); + } + + // Count where the span at `position` starts in {@link start}. + #name(position: number): void { + const span = this.#spans[position]; + if (!span.floored) { + this.#unnamed++; + return; + } + const named = this.#named; + const at = lowerBound(named.length, (i) => named[i].start >= span.start); + if (named[at]?.start === span.start) named[at].count++; + else named.splice(at, 0, { start: span.start, count: 1 }); + } + + // Stop counting where the span at `position` starts, before it moves. + #unname(position: number): void { + const span = this.#spans[position]; + if (!span.floored) { + this.#unnamed--; + return; + } + const named = this.#named; + const at = lowerBound(named.length, (i) => named[i].start >= span.start); + if (named[at]?.start !== span.start) return; + if (--named[at].count === 0) named.splice(at, 1); + } + + #updateSettled(): void { + this.#settled = !this.#pending && !this.#unplaced() && this.#looking.size === 0; + } + // The groups in range: every span that knows where it starts, through its end, and every span // waiting on a time start from that time. - #rebuild(holdings: Holdings): void { - this.#settled = !this.#pending && !this.#unplaced() && this.#spans.every((span) => !span.looking); + #rebuild(): void { + this.#updateSettled(); const groups: Run[] = []; const missable: Run[] = []; for (const span of this.#spans) { - if (span.looking) continue; - // A span waiting on its time start reads from that time meanwhile, and may ask a miss - // there, as one reaching back past every group held does: whichever group contains it - // starts at or before it, so every group from there on is in range. - if (span.waiting) { - const at = time(span.anchor) ?? 0n; - groups.push({ start: at, end: span.end }); - missable.push({ start: at, end: span.end }); - continue; - } - groups.push({ start: span.start, end: span.end }); - // A time start before every group held reaches back to the group containing it, which is - // unknown here, so it asks no miss below its time: every key from there on is in range, - // the key at the time itself being the containing group if it exists. A relative one - // counting back past every group held asks none below the oldest, since which of them it - // reaches is unknown. - let reach = span.start; - if (!span.floored && span.anchor !== undefined) { - reach = time(span.anchor) ?? holdings.nextStart(0n) ?? span.start; - } - missable.push({ start: reach, end: span.end }); + const [reads, asks] = spanReads(span); + if (reads) groups.push(reads); + if (asks) missable.push(asks); } this.#runs = merge(groups); this.#missable = merge(missable); } + + // Take in the spans at `positions`, each now reading at least what it did before: their runs + // are inserted into the sorted runs, so a change to a few spans costs them, not every range. + #grow(positions: readonly number[]): void { + this.#updateSettled(); + for (const position of positions) { + const [reads, asks] = spanReads(this.#spans[position]); + if (reads) insert(this.#runs, reads); + if (asks) insert(this.#missable, asks); + } + } +} + +/** Spans by position, keyed by the time each waits on or looks up, lowest first. */ +type Timed = { time: bigint; positions: number[] }; + +/** {@link Timed} entries kept sorted by time, so the times in a run are found without a walk. */ +class ByTime { + #entries: Timed[] = []; + + get size(): number { + return this.#entries.length; + } + + get all(): readonly Timed[] { + return this.#entries; + } + + // The index of the first entry at or after `time`. + #at(time: bigint): number { + const entries = this.#entries; + return lowerBound(entries.length, (i) => entries[i].time >= time); + } + + /** Add the span at `position` under `time`. */ + add(time: bigint, position: number): void { + const at = this.#at(time); + const entry = this.#entries[at]; + if (entry?.time === time) entry.positions.push(position); + else this.#entries.splice(at, 0, { time, positions: [position] }); + } + + /** Put back an entry taken out, whose time no entry holds. */ + put(entry: Timed): void { + this.#entries.splice(this.#at(entry.time), 0, entry); + } + + /** Take out the spans under `time`, if any. */ + take(time: bigint): number[] | undefined { + const at = this.#at(time); + if (this.#entries[at]?.time !== time) return undefined; + return this.#entries.splice(at, 1)[0].positions; + } + + /** Take out every entry whose time is in `[start, end)`, lowest first. */ + takeIn(start: bigint, end: bigint): Timed[] { + const from = this.#at(start); + const to = end >= UNBOUNDED ? this.#entries.length : this.#at(end); + return to > from ? this.#entries.splice(from, to - from) : []; + } + + /** Take out every entry, lowest first. */ + takeAll(): Timed[] { + const entries = this.#entries; + this.#entries = []; + return entries; + } +} + +// What `span` reads, and the part of it a miss may ask for, each undefined for none. +function spanReads(span: Span): [Run | undefined, Run | undefined] { + // A span still looking up its start reads nothing until it knows which group that is. + if (span.looking) return [undefined, undefined]; + // A span waiting on its time start reads from that time meanwhile, and may ask a miss there, as + // one reaching back past every group held does: whichever group contains it starts at or before + // it, so every group from there on is in range. + if (span.waiting) { + const at = time(span.anchor) ?? 0n; + if (at >= span.end) return [undefined, undefined]; + return [ + { start: at, end: span.end }, + { start: at, end: span.end }, + ]; + } + if (span.start >= span.end) return [undefined, undefined]; + // A time start before every group held reaches back to the group containing it, which is unknown + // here, so it asks no miss below its time: every key from there on is in range, the key at the + // time itself being the containing group if it exists. A relative one counting back past every + // group held asks none below its reach. + let reach = span.start; + if (!span.floored && span.anchor !== undefined) { + reach = time(span.anchor) ?? span.reach ?? span.start; + } + return [{ start: span.start, end: span.end }, reach < span.end ? { start: reach, end: span.end } : undefined]; +} + +// The first index in `[0, length)` for which `past` holds, `past` being false then true. +function lowerBound(length: number, past: (index: number) => boolean): number { + let lo = 0; + let hi = length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (past(mid)) hi = mid; + else lo = mid + 1; + } + return lo; +} + +// The index of the first of sorted, disjoint `runs` ending past `key`. +function above(runs: readonly Run[], key: bigint): number { + return lowerBound(runs.length, (i) => runs[i].end > key); +} + +// Insert `run` into sorted, disjoint `runs`, merging what it overlaps or touches. An empty one +// changes nothing. +function insert(runs: Run[], run: Run): void { + if (run.end <= run.start) return; + const from = lowerBound(runs.length, (i) => runs[i].end >= run.start); + const to = lowerBound(runs.length, (i) => runs[i].start > run.end); + if (from === to) { + runs.splice(from, 0, { ...run }); + return; + } + const merged = { start: min(runs[from].start, run.start), end: max(runs[to - 1].end, run.end) }; + runs.splice(from, to - from, merged); +} + +// The times outside sorted, disjoint `runs`, as runs lowest first. +function outside(runs: readonly Run[]): Run[] { + const gaps: Run[] = []; + let from = 0n; + for (const run of runs) { + if (run.end <= run.start) continue; + if (run.start > from) gaps.push({ start: from, end: run.start }); + from = max(from, run.end); + } + if (from < UNBOUNDED) gaps.push({ start: from, end: UNBOUNDED }); + return gaps; } // `runs` merged into sorted, disjoint runs, the empty ones dropped. diff --git a/quest/m0/nightly/publisher-drop-cost.md b/quest/m0/nightly/publisher-drop-cost.md deleted file mode 100644 index 4fdb3da9dee..00000000000 --- a/quest/m0/nightly/publisher-drop-cost.md +++ /dev/null @@ -1,13 +0,0 @@ -# [L] A dropped group costs a subscription O(1), not O(ranges) - -## Goal - -Dropping a group from a subscription with many ranges costs about the same as with one, so moq-net's `publisher` bench `wake/8192` finishes in seconds instead of running past 600 s and timing out the Nightly `Rust bench targets` job (55-minute limit). A new bench sweeps drop cost over ranges × drops, so a regression shows in Nightly's measured coverage. - -## Plan - -Caused by [moq-dev/moq#8638](https://github.com/moq-dev/moq/pull/8638): each dropped group makes the subscription rebuild state over every range, about 2.4 ms per drop at 8193 ranges. Four places rebuild instead of updating incrementally, in `rs/moq-net/src/model/track.rs` and `subscription/{window,runs}.rs`: the window, what is owed, the misses check, and the servable set. Make each incremental, and keep the JS counterpart in step where it has the same shape. - -Decided 2026-11-18 (maintainer, /quest-plan): ✅ no stopgap (rejected: having the bench fixture do all its drops before one step, which takes 6 ms but hides the bug from the only bench that catches it). The Rust bench job stays red until this lands. - -Public API: none. Wire: none. diff --git a/quest/m3/layered-runs.md b/quest/m3/layered-runs.md deleted file mode 100644 index deac17d5dd4..00000000000 --- a/quest/m3/layered-runs.md +++ /dev/null @@ -1,27 +0,0 @@ -# [S] Collapse a window's layered runs to one shared set - -## Goal - -`rs/moq-net`'s subscription window holds its groups as plain shared runs, -without the moving layer that time ends once walked, and every path that -diffed or searched that layer is gone, with no change in what readers get. - -## Plan - -Since [the model](https://github.com/moq-dev/moq/blob/7d1bee79cb8fcb4d816fc53f0547ef8b2b19756a/quest/m2/time-addressing/model.md), a time end settles at -once on keys, so `Window::rebuild` always builds `Layered` with an empty -layer. What still assumes one: - -- `model/subscription/runs.rs`: `Layered`, its `gained` and `minus`, and the - property tests over a moving layer. -- `model/track.rs`: `find_servable` leapfrogging the layer after the fixed - runs, `Admitted::gained`, and `Servable::layered` in its property test. -- `model/track/miss.rs`: the misses view that judges only what a moved layer - covered, and `shared_layer` in its property test. - -Replace `Layered` with `Arc`, comparing windows by pointer where they -compared fixed runs. A window that moves now rebuilds, so the incremental -paths become a full recompute of what is owed; keep the property tests that -check owed groups and servable finds against a scan. - -Public API: none. Wire: none. diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index 785a9d40a27..b4e824ee0b9 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -12,9 +12,16 @@ //! until parked; `steer` moves only the priority. A cost that grows with the live ranges //! rather than the edit shows up as a slope over `ranges`. //! +//! `publisher_drop` gives up `drops` ranges of a subscription to `ranges` ranges, plus one +//! left open, timing each drop and every wake it causes. Each drop starts the range it gives +//! up, so a drop costing every range shows as a slope over `ranges`, and one costing every +//! drop before it as a slope over `drops`. +//! //! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. -use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use std::time::Duration; + +use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use moq_net::fuzz::{CoverageBench, UpdateBench}; /// Ranges the source gave up before the wake. @@ -69,5 +76,34 @@ fn bench_update(c: &mut Criterion) { group.finish(); } -criterion_group!(benches, bench_covered_before_start, bench_update); +/// Ranges given up per measurement, and ranges subscribed besides the open one. +const SWEEP: [u64; 4] = [1, 32, 1024, 8192]; + +fn bench_drop(c: &mut Criterion) { + let mut group = c.benchmark_group("publisher_drop"); + // The largest cell gives up 8192 ranges per sample. + group.sample_size(10); + group.measurement_time(Duration::from_secs(2)); + for ranges in SWEEP { + for drops in SWEEP.into_iter().filter(|drops| *drops <= ranges) { + group.throughput(Throughput::Elements(drops)); + group.bench_function( + BenchmarkId::new(format!("ranges={ranges}"), format!("drops={drops}")), + |b| { + b.iter_batched( + || CoverageBench::spread(ranges + 1, drops), + |mut run| { + run.give_up(drops); + run + }, + BatchSize::PerIteration, + ) + }, + ); + } + } + group.finish(); +} + +criterion_group!(benches, bench_covered_before_start, bench_update, bench_drop); criterion_main!(benches); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 610c0ee775c..42e6567250b 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -6113,12 +6113,15 @@ impl TrackRun { } /// A bounded lite-07 subscription's run loop still waiting on SUBSCRIBE_START, for the -/// `publisher` bench: a wake that finds nothing new ends in the coverage check. +/// `publisher` bench: a wake that finds nothing new ends in the coverage check, and a drop +/// gives up one more of its ranges. #[cfg(any(test, feature = "fuzz"))] pub struct CoverageBench { run: TrackRun, writer: Writer, track: track::Producer, + /// The drops not given up yet, the next one last. + drops: Vec<(Range, StreamError)>, _peer: crate::transport::memory::Session, } @@ -6126,15 +6129,24 @@ pub struct CoverageBench { impl CoverageBench { /// Subscribe to `ranges` one-group ranges, then give up the first `skipped` of them, one /// wake each. A range is left open, so the start never resolves and nothing is covered. - /// - /// Each range starts at a time no group held places, so it may reach back to the group - /// before it: giving a range up gives that group up too. pub fn new(ranges: u64, skipped: u64) -> Self { assert!(skipped < ranges, "a range must stay open"); + let mut this = Self::spread(ranges, skipped); + this.give_up(skipped); + this.check(skipped); + this + } + + /// Subscribe to `ranges` one-group ranges, ready to give up the first `drops` of them + /// with [`Self::give_up`]. + /// + /// Each range starts at a time no group held places, so it may reach back to the group + /// before it: giving a range up gives that group up too, which starts the range. + pub fn spread(ranges: u64, drops: u64) -> Self { // Every other group, so no two ranges merge into one run. let wire = (0..ranges).map(|range| 2 * range..2 * range + 1); // Alternating errors keep adjacent drops separate entries in the skipped record. - let drops = (0..skipped) + let drops = (0..drops.min(ranges)) .map(|range| { let err = match range % 2 { 0 => StreamError::NotFound, @@ -6146,6 +6158,15 @@ impl CoverageBench { Self::build(wire, drops) } + /// Give up the next `count` drops, each followed by every wake it causes. + pub fn give_up(&mut self, count: u64) { + for _ in 0..count { + let (groups, err) = self.drops.pop().expect("a drop left to give up"); + self.track.drop_groups(groups, err); + while self.step().is_ready() {} + } + } + /// Subscribe to one range of `skipped + 1` groups, then give up all but the last, one /// wake each. Alternating errors keep each group its own entry in the skipped record. pub fn adjacent(skipped: u64) -> Self { @@ -6158,7 +6179,10 @@ impl CoverageBench { (group..group + 1, err) }) .collect(); - Self::build(std::iter::once(0..skipped + 1), drops) + let mut this = Self::build(std::iter::once(0..skipped + 1), drops); + this.give_up(skipped); + this.check(skipped); + this } fn build(wire: impl Iterator>, drops: Vec<(Range, StreamError)>) -> Self { @@ -6199,24 +6223,27 @@ impl CoverageBench { outcomes: Default::default(), object_records: false, }; + let mut drops = drops; + drops.reverse(); let mut this = Self { run: TrackRun::new(ctx, subscriber, bounds, 0, steer), writer: Writer::new(stream, version), track, + drops, _peer: peer, }; - let skipped = drops.len(); - for (groups, err) in drops { - this.track.drop_groups(groups, err); - while this.step().is_ready() {} - } - assert!(this.run.start.resolved().is_none(), "no group resolves a start"); + while this.step().is_ready() {} + this + } + + /// Each of the `skipped` drops is held as its own entry, and no start resolved. + fn check(&self, skipped: u64) { + assert!(self.run.start.resolved().is_none(), "no group resolves a start"); assert_eq!( - this.run.start.skipped().expect("unresolved").iter().count(), + self.run.start.skipped().expect("unresolved").iter().count() as u64, skipped, "each drop is its own entry" ); - this } /// One wake with nothing new to serve. diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index e8acd087dc7..d4a8d9c0411 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -8,7 +8,7 @@ mod window; pub(super) use cover::{Cover, minus}; pub(crate) use window::Accounted; pub(crate) use window::Resolved; -pub(super) use window::{Containing, Holdings, Unheld, Window}; +pub(super) use window::{Containing, Holdings, Unheld, Unplaced, Window}; /// Subscriber-side preferences for receiving a track. /// diff --git a/rs/moq-net/src/model/subscription/cover.rs b/rs/moq-net/src/model/subscription/cover.rs index c0e5e48a59a..f287078eb42 100644 --- a/rs/moq-net/src/model/subscription/cover.rs +++ b/rs/moq-net/src/model/subscription/cover.rs @@ -197,6 +197,19 @@ impl Cover { } } + /// Where the run reaching past every key starts, `None` when the last run ends. + pub fn open_from(&self) -> Option { + if sum(&self.root) <= 0 { + return None; + } + // The open run starts at the first covered key past the last gap edge, or at the + // first edge when every edge leaves a run open. + match last_in(&self.root, 0, Want::Gap) { + Some(gap) => self.first_key(gap, Want::Covered), + None => self.lowest(), + } + } + /// The spans where `self` and `old` may cover differently, lowest first: where their counts /// differ, a superset of where coverage does. /// @@ -442,6 +455,22 @@ fn first_in(link: &Link, offset: i64, want: Want) -> Option { first_in(&node.right, at, want) } +/// The last key of the subtree, entered with `offset` runs open, whose running sum is `want`. +fn last_in(link: &Link, offset: i64, want: Want) -> Option { + let node = link.as_ref()?; + if !want.within(offset, node) { + return None; + } + let at = offset + sum(&node.left) + node.delta; + if let Some(key) = last_in(&node.right, at, want) { + return Some(key); + } + if want.at(at) { + return Some(node.key); + } + last_in(&node.left, offset, want) +} + /// [`first_in`] over the keys at or after `from`. fn first_from(link: &Link, offset: i64, from: u64, want: Want) -> Option { let node = link.as_ref()?; @@ -575,6 +604,16 @@ mod tests { "{}", context() ); + assert_eq!( + cover.open_from(), + union + .iter() + .next_back() + .filter(|run| *run.end() == u64::MAX) + .map(|run| *run.start()), + "{}", + context() + ); for &lo in &all { assert_eq!(cover.contains(lo), union.contains(&lo), "{} at {lo}", context()); let from = union diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 8a481914123..6be5dfbc509 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -68,6 +68,13 @@ pub(in crate::model) trait Holdings { /// Where `time` falls among the groups held. fn containing(&self, time: u64) -> Containing; + /// Runs of times that [`Self::containing`] can't place, so a window skips its time starts + /// in them rather than try each again whenever the groups held change. `None` when it + /// can't tell, so each is tried. + fn unplaced(&self) -> Option { + None + } + /// The lowest group held starting at or after `from`. fn next_start(&self, from: u64) -> Option; @@ -115,6 +122,21 @@ pub(in crate::model) enum Containing { Unknown, } +/// The times a hop's [`Holdings::containing`] can't place, as up to two runs: every time in +/// them is [`Containing::Unknown`], except the few that `mark` places another way. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(in crate::model) struct Unplaced { + pub runs: [ops::RangeInclusive; 2], + /// Changes whenever a time inside the runs may have come to be placed by something other + /// than a group arriving or a drop, so a window that saw another mark tries every start. + pub mark: u64, +} + +impl Unplaced { + /// An empty run, for a hop that places every time on one side. + pub const NONE: ops::RangeInclusive = ops::RangeInclusive::new(1, 0); +} + /// A hop that holds no groups of its own, such as a reader following route copies that /// resolve its ranges for it. pub(in crate::model) struct Unheld; @@ -160,8 +182,11 @@ pub(in crate::model) struct Window { /// every span. looking: BTreeSet<(u64, u64)>, /// The spans whose time start waits on a group arriving, which read only the groups from - /// that time meanwhile. - waiting: BTreeSet, + /// that time meanwhile: by that time, then key, so a change to the groups held tries only + /// those its [`Holdings::unplaced`] leaves out, found without visiting the rest. + waiting: BTreeSet<(u64, u64)>, + /// The [`Unplaced::mark`] every waiting span was last tried under. + waited: Option, /// The ranges of a live join whose reach back waits on the groups held to measure the /// live edge, which read nothing meanwhile: a group held is no reason to start at it. reaching: BTreeSet, @@ -234,6 +259,7 @@ impl Window { pending: BTreeSet::new(), looking: BTreeSet::new(), waiting: BTreeSet::new(), + waited: None, reaching: BTreeSet::new(), groups: Cover::default(), missable: Cover::default(), @@ -279,7 +305,6 @@ impl Window { } self.pending.remove(&key); self.reaching.remove(&key); - self.waiting.remove(&key); if let Some(span) = self.spans.remove(&key) { self.uncount(key, &span); } @@ -341,22 +366,30 @@ impl Window { } /// Start the spans whose time start waits on a group, once one arrived, or once nothing - /// can arrive any more. Returns whether any did. + /// can arrive any more. Only the times the holdings may now place are tried, so a change + /// placing one of them costs that one, not every span still waiting. Returns whether any + /// started. fn start_waiting(&mut self, holdings: &impl Holdings) -> bool { + let complete = holdings.complete(); + let unplaced = holdings.unplaced().filter(|_| !complete); + let tried: Vec<(u64, u64)> = match unplaced { + Some(unplaced) if self.waited == Some(unplaced.mark) => outside(&unplaced.runs) + .flat_map(|gap| self.waiting.range((*gap.start(), 0)..=(*gap.end(), u64::MAX))) + .copied() + .collect(), + _ => { + self.waited = unplaced.map(|unplaced| unplaced.mark); + self.waiting.iter().copied().collect() + } + }; let mut started = false; - for key in std::mem::take(&mut self.waiting) { - let Anchor::Time(time) = self.spans[&key].anchor else { - unreachable!("only a time start waits on a group"); - }; + for (time, key) in tried { let (start, floored) = match holdings.containing(time) { Containing::Group(group) => (group, true), // The group holding it is older than any held, or nothing can arrive. Containing::Before => (0, false), - Containing::Unknown if holdings.complete() => (0, false), - Containing::Unknown => { - self.waiting.insert(key); - continue; - } + Containing::Unknown if complete => (0, false), + Containing::Unknown => continue, }; started = true; self.start_span(key, time, start, floored, holdings); @@ -588,6 +621,8 @@ impl Window { for key in keys { self.place_range(key, latest, holdings); } + // Each span waiting was just tried against these holdings. + self.waited = holdings.unplaced().map(|unplaced| unplaced.mark); Ok(()) } @@ -624,7 +659,6 @@ impl Window { // [`Self::start_waiting`]. Start::Waiting => { let span = Span::new(anchor, 0, end, false, None); - self.waiting.insert(key); self.count(key, Span { waiting: true, ..span }, holdings); return; } @@ -661,6 +695,9 @@ impl Window { if let Some(looking) = span.looking { self.looking.insert((looking.time, key)); } + if let Some(time) = span.waits_on() { + self.waiting.insert((time, key)); + } self.spans.insert(key, span); } @@ -685,10 +722,21 @@ impl Window { if let Some(looking) = span.looking { self.looking.remove(&(looking.time, key)); } + if let Some(time) = span.waits_on() { + self.waiting.remove(&(time, key)); + } } } impl Span { + /// The time start it waits on the groups held to place, if any. + fn waits_on(&self) -> Option { + match (self.waiting, self.anchor) { + (true, Anchor::Time(time)) => Some(time), + _ => None, + } + } + fn new(anchor: Anchor, start: u64, end: Option, floored: bool, looking: Option) -> Self { Self { anchor, @@ -751,6 +799,25 @@ fn check<'a>(mut ranges: impl Iterator + Clone, holdings: &imp Ok(()) } +/// The times outside `runs`, lowest first, as runs. +fn outside(runs: &[ops::RangeInclusive]) -> impl Iterator> + '_ { + let mut from = Some(0); + let mut runs = runs.iter().filter(|run| !run.is_empty()); + std::iter::from_fn(move || { + loop { + let at = from?; + let Some(run) = runs.next() else { + from = None; + return Some(at..=u64::MAX); + }; + from = run.end().checked_add(1).map(|next| next.max(at)); + if at < *run.start() { + return Some(at..=*run.start() - 1); + } + } + }) +} + /// The Min Age a live join reaches back by: `min`, for [`Range::live`], where this hop resolves /// its own ranges. Any other start names where it starts, the oldest one included, which asks for /// everything the budget keeps. A zero minimum starts at the latest group anyway. @@ -853,6 +920,9 @@ mod tests { skipped: Vec>, complete: bool, looks_up: bool, + /// Every key below this was dropped, so a time below it is placed before every + /// group even while none is held. + dropped: u64, } impl Keys { @@ -872,11 +942,22 @@ mod tests { fn containing(&self, time: u64) -> Containing { match self.held.range(..=time).next_back() { Some(key) => Containing::Group(*key), - None if self.held.is_empty() => Containing::Unknown, + None if self.held.is_empty() && time >= self.dropped => Containing::Unknown, None => Containing::Before, } } + fn unplaced(&self) -> Option { + let unknown = match self.held.is_empty() { + true => self.dropped..=u64::MAX, + false => Unplaced::NONE, + }; + Some(Unplaced { + runs: [unknown, Unplaced::NONE], + mark: 0, + }) + } + fn next_start(&self, from: u64) -> Option { self.held.range(from..).next().copied() } @@ -901,6 +982,105 @@ mod tests { } } + /// [`Keys`] keeping its [`Holdings::unplaced`] to itself, so a window tries every start + /// still waiting on each change. + struct Blind<'a>(&'a Keys); + + impl Holdings for Blind<'_> { + fn looks_up(&self) -> bool { + self.0.looks_up() + } + + fn containing(&self, time: u64) -> Containing { + self.0.containing(time) + } + + fn next_start(&self, from: u64) -> Option { + self.0.next_start(from) + } + + fn back(&self, latest: u64, back: u64) -> Option { + self.0.back(latest, back) + } + + fn complete(&self) -> bool { + self.0.complete() + } + + fn never_produced(&self, keys: ops::Range) -> bool { + self.0.never_produced(keys) + } + } + + /// Everything a reader of `window` sees. + fn seen(window: &Window) -> impl PartialEq + std::fmt::Debug { + let lookups: BTreeSet<_> = window.lookups().collect(); + ( + window.runs(0).collect::>(), + window.missable(0).collect::>(), + (window.floor(), window.start(), window.named(), window.end()), + (window.resolved(), lookups, window.is_settled(), window.is_waiting()), + ) + } + + proptest::proptest! { + /// A window that skips the starts its holdings can't place sees what one trying every + /// start sees, through groups arriving and leaving, drops, edits, lookups answering, + /// and the track completing. + #[test] + fn settling_by_steps_matches_trying_every_start( + ranges in proptest::collection::vec((proptest::bool::weighted(0.2), 0u64..40, proptest::option::of(0u64..48)), 1..12), + ops in proptest::collection::vec((0u8..6, 0u64..48), 0..24), + looks_up: bool, + ) { + let to_range = |(relative, start, end): (bool, u64, Option)| { + let start = match relative { + true => Anchor::Relative(start % 6), + false => time(start), + }; + range(start, end.map(time)) + }; + let ranges: Vec = ranges.into_iter().map(to_range).collect(); + let mut next_key = ranges.len() as u64; + let mut keys = Keys { + looks_up, + ..Keys::default() + }; + let mut fast = Window::new(&ranges, None, None, &keys).unwrap(); + let mut full = Window::new(&ranges, None, None, &Blind(&keys)).unwrap(); + for (op, value) in ops { + let latest = keys.held.last().copied(); + match op { + 0 => drop(keys.held.insert(value)), + 1 => keys.dropped = keys.dropped.max(value), + 2 => { + let Some((time, _)) = fast.lookups().next() else { + continue; + }; + let answer = (value % 2 == 0).then_some(value); + fast.found(time, answer, &keys); + full.found(time, answer, &Blind(&keys)); + } + 3 => keys.complete = true, + // Retire one range and add a time start, placed against what is held now. + 4 => { + let added = [(next_key, range(time(value), None))]; + next_key += 1; + let retired = [value % next_key]; + fast.edit(retired, &added, latest, &keys).unwrap(); + full.edit(retired, &added, latest, &Blind(&keys)).unwrap(); + } + // The oldest group held is evicted. + _ => drop(keys.held.pop_first()), + } + let latest = keys.held.last().copied(); + fast.settle(latest, &keys); + full.settle(latest, &Blind(&keys)); + proptest::prop_assert_eq!(seen(&fast), seen(&full)); + } + } + } + fn range(start: Anchor, end: impl Into>) -> Range { Range { start, end: end.into() } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 416e8fb28a3..7e240bf9f6f 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -23,7 +23,7 @@ use super::record::{Pending, Record, Report}; use crate::ledger::{self, Ledger}; pub(crate) use super::subscription::Resolved; -use super::subscription::{Accounted, Containing, Cover, Holdings, Window}; +use super::subscription::{Accounted, Containing, Cover, Holdings, Unplaced, Window}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; @@ -759,6 +759,9 @@ pub(crate) struct TrackState { // holds only part of the track, so it can't count back over its own groups. Replaced whole // as the ranges change, like `time_starts`. relative_starts: BTreeMap, + // Counts changes to how a time start is placed other than by groups arriving or drops: + // `time_starts`, `anchors`, and `starts_at_keys`. See [`Unplaced::mark`]. + placing: u64, // Keys the upstream publisher gave up, with why: an incoming SUBSCRIBE_SKIP or // SUBSCRIBE_DROP, or a producer cut. Kept apart from the cache so reclaiming a slot // cannot erase a disposition a relay must forward, and as ranges so a wide one costs @@ -1127,6 +1130,48 @@ impl Holdings for TrackState { } } + /// Exactly the times [`Self::containing`] can't place, but those `time_starts` places. + fn unplaced(&self) -> Option { + let none = Unplaced::NONE; + // The times at or after `from` whose keys from `from` through them drops do not all + // cover. As in `dropped_cover`, the last time checks the keys below it only, so a run + // from the last key covers nothing and leaves nothing unplaced. + let uncovered = |from: u64| match self.dropped.get(&from) { + _ if from == u64::MAX => none.clone(), + Some(run) if run.end == u64::MAX => none.clone(), + Some(run) => run.end..=u64::MAX, + None => from..=u64::MAX, + }; + let runs = if self.starts_at_keys { + [none.clone(), none] + } else { + match (self.lookup.first_key_value(), self.lookup.last_key_value()) { + // Before any group, only a time whose keys down to 0 were all dropped is placed. + (None, _) | (_, None) => [uncovered(0), none], + (Some(_), Some(_)) if self.anchors != Anchors::Wire => [none.clone(), none], + // A copy places a time below its groups only once the keys down to 0 were + // dropped, and one above them only once the keys down to the newest were. + (Some((&first, _)), Some((&last, _))) => { + let below = uncovered(0); + let below = *below.start()..=(*below.end()).min(first.saturating_sub(1)); + let below = match first { + 0 => none.clone(), + _ => below, + }; + let above = match last.checked_add(1) { + Some(next) => uncovered(next), + None => none, + }; + [below, above] + } + } + }; + Some(Unplaced { + runs, + mark: self.placing, + }) + } + fn next_start(&self, from: u64) -> Option { self.lookup.range(from..).next().map(|(&key, _)| key) } @@ -4711,6 +4756,7 @@ impl Producer { && state.anchors == Anchors::Local { state.anchors = Anchors::Refused; + state.placing += 1; } } @@ -4720,6 +4766,7 @@ impl Producer { pub(crate) fn anchored(&self) { if let Ok(mut state) = self.modify() { state.anchors = Anchors::Wire; + state.placing += 1; } } @@ -4742,6 +4789,7 @@ impl Producer { && state.time_starts != starts { state.time_starts = starts; + state.placing += 1; } } @@ -4774,6 +4822,7 @@ impl Producer { pub(crate) fn starts_at_keys(&self) { if let Ok(mut state) = self.modify() { state.starts_at_keys = true; + state.placing += 1; } } @@ -7616,6 +7665,29 @@ fn settles(budget: Duration, coverage: &Coverage, start: u64, hole: &Hole) -> bo budget <= stale && reads(above..u64::MAX) } +/// The starts at which a received stale run settles a reader whose coverage grew from `old` +/// to `new` under the same budget and did not before, or `None` inside for none: `None` when +/// the coverage changed other than by growing. See [`settles`]: only an uncapped reader is +/// settled, by a run whose next key the open run of its groups holds, and that run's start +/// only falls as the groups grow. +fn settling(old: &Coverage, new: &Coverage) -> Option>> { + let ((old, old_cap), (new, cap)) = match (old, new) { + (None, None) => return Some(None), + (Some(old), Some(new)) => (old, new), + _ => return None, + }; + if old_cap != cap || !old.gained(new, 0, u64::MAX).is_empty() { + return None; + } + if cap.is_some() { + return Some(None); + } + // The lowest start settled: the one just below the open run, or only the last key. + let settled = |groups: &Cover| groups.open_from().map_or(u64::MAX, |start| start.saturating_sub(1)); + let run = settled(new)..settled(old); + Some((!run.is_empty()).then_some(run)) +} + impl Drift { /// Whether `hole`, over a run from `start`, settles this reader; see [`settles`]. fn settles(&self, start: u64, hole: &Hole) -> bool { @@ -8395,7 +8467,7 @@ struct Within<'a> { /// The span, window revision, and the budget and coverage that settle received stale runs /// (see [`Drift::settles`]) a [`DropCursor`] last scanned the whole span under; between them -/// it folds in only the runs that changed. +/// it folds in only the runs that changed, and what the window gained. type Scan = (ops::Range, u64, Option<(Duration, Coverage)>); /// What a wire publisher's cursor gave up, found as it changes rather than by rescanning @@ -8410,6 +8482,10 @@ struct DropCursor { /// The [`TrackState::drops_changes`] revision [`Record::pending`] folded in. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] seen: u64, + /// The window's groups as of [`Self::scanned`]'s revision, so a window that only grew + /// since folds in what it gained rather than rescanning the span. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + groups: Option, /// The budget and coverage of the last drift judged, which settle received stale runs /// until the next: a queue-only poll judges none, and must not unsettle them. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -8424,6 +8500,7 @@ impl DropCursor { cuts: state.cache.watch_cuts(), scanned: None, seen: 0, + groups: None, drift: None, } } @@ -8452,6 +8529,45 @@ impl DropCursor { } } + /// What a scan of `span` would newly find since the last, when the span is the same and + /// the window and the reader's coverage only grew: the runs the window gained, and each + /// received stale run a wider coverage now settles. `None` when anything else moved. + fn grown( + &self, + state: &TrackState, + window: &Window, + old_drift: Option<&(Duration, Coverage)>, + span: &ops::Range, + ) -> Option>> { + let last = span.end.checked_sub(1).filter(|last| *last >= span.start); + let (groups, old) = (window.groups()?, self.groups.as_ref()?); + let mut grown: Vec<_> = match last { + // Losing a group of the span may leave a reported run no longer owed. + Some(last) if old.gained(groups, span.start, last).is_empty() => groups.gained(old, span.start, last), + Some(_) => return None, + None => Vec::new(), + } + .into_iter() + .map(|run| *run.start()..run.end().saturating_add(1)) + .collect(); + let settling = match (old_drift, &self.drift) { + (None, None) => None, + (Some((old_budget, old)), Some((budget, new))) if old_budget == budget => settling(old, new)?, + _ => return None, + }; + // A scan judges a drop at its start within the span, so each one starting where a + // stale run newly settles is folded in whole. + if let Some(settling) = settling { + for (run, _) in state.drops.overlapping(&settling) { + let start = run.start.max(span.start); + if settling.contains(&start) { + grown.push(start..run.end); + } + } + } + Some(grown) + } + /// The lowest unreported run of `within` given up, now marked reported, with why. fn next( &mut self, @@ -8467,18 +8583,35 @@ impl DropCursor { Self::cut(record, key, slot); } } - // Rescan the span only after the window moves or what settles a stale run does, since - // out-of-window and unsettling runs were skipped and may be owed now; otherwise fold in - // only the runs that changed, so a drop costs each cursor the runs it touches. + // Rescan the span only after the window moves other than by growing, or what settles a + // stale run does, since out-of-window and unsettling runs were skipped and may be owed + // now; otherwise fold in only the runs that changed and what the window gained, so a + // drop costs each cursor the runs it touches, even one that starts a range. if let Some(drift) = within.drift { self.drift = Some((drift.budget, drift.coverage.clone())); } let scan = (span.clone(), within.revision, self.drift.clone()); - let full = std::mem::take(&mut record.rescan) || self.scanned.as_ref() != Some(&scan); - let changed: Vec> = match full { - false => state.drops_changes.since(self.seen).collect(), - true => { - self.scanned = Some(scan); + let rescan = std::mem::take(&mut record.rescan); + let grown = match &self.scanned { + _ if rescan => None, + Some(scanned) if *scanned == scan => Some(Vec::new()), + Some((old_span, _, old_drift)) if old_span == span => self.grown(state, window, old_drift.as_ref(), span), + _ => None, + }; + let full = grown.is_none(); + self.scanned = Some(scan); + self.groups = window.groups().cloned(); + let changed: Vec> = match grown { + Some(grown) => { + // What the window newly reads may hold cuts the scan skipped. + for run in &grown { + for (&key, slot) in state.lookup.range(run.clone()) { + Self::cut(record, key, slot); + } + } + state.drops_changes.since(self.seen).chain(grown).collect() + } + None => { let received: Vec<_> = record .pending .iter() @@ -12157,6 +12290,44 @@ mod test { } proptest::proptest! { + /// The times a track reports unplaced are exactly those its `containing` can't place, + /// but for the ones a copy's reported time starts place, on its own groups or on a + /// lite-07 copy's, through groups arriving and every way keys are given up. + #[test] + fn unplaced_matches_containing( + ops in proptest::collection::vec((0u8..5, 0u64..40, 1u64..8), 0..24), + wire: bool, + starts in proptest::collection::vec((0u64..48, 0u64..48), 0..3), + ) { + let producer = track_producer("test", Info::default()); + if wire { + producer.anchored(); + producer.time_starts(starts.clone()); + } + for (op, start, len) in ops { + let groups = start..start + len; + match op { + 0 => producer.drop_groups(groups, StreamError::Cancel), + 1 => producer.skip_groups(groups), + 2 => drop(producer.create_group(group::Info { start })), + 3 => producer.modify().unwrap().forget_drops(groups), + // A group at the last key that can start one, past every time but the last. + _ => drop(producer.create_group(group::Info { start: u64::MAX - 1 })), + } + } + let state = producer.state.read(); + let unplaced = state.unplaced().expect("a track always tells"); + let reported: BTreeSet = starts.iter().map(|(time, _)| *time).collect(); + for time in (0..64).chain([u64::MAX - 1, u64::MAX]) { + let inside = unplaced.runs.iter().any(|run| run.contains(&time)); + let unknown = state.containing(time) == Containing::Unknown; + match wire && reported.contains(&time) { + true => proptest::prop_assert!(!unknown, "a reported start places {}", time), + false => proptest::prop_assert_eq!(inside, unknown, "at {}", time), + } + } + } + /// The coalesced drop set answers coverage as a walk of the drop runs would, through /// every way a run is given up, split, replaced, or forgotten. #[test] @@ -12809,6 +12980,29 @@ mod test { assert_eq!(read_groups(|waiter| groups_only(ordered.poll_next_group(waiter))), want); } + /// A drop outside a reader's ranges is reported once a group arriving places a time start + /// below it, widening its range over the drop, as a scan of the whole window would. + #[test] + fn a_range_widened_by_a_group_reports_an_earlier_drop() { + let producer = track_producer("test", Info::default()); + // The range at 0 keeps the floor where it is, so only the window moves. + let at = |time| Range { + start: Anchor::Time(time), + end: Some(Anchor::Time(time)), + }; + let mut reader = producer.subscribe(replay().with_ranges([at(0), at(8)])); + let waiter = kio::Waiter::noop(); + assert!(reader.poll_drop(&waiter).is_pending()); + producer.drop_groups(5..6, StreamError::Cancel); + assert!(reader.poll_drop(&waiter).is_pending(), "outside the range"); + let _group = producer.create_group(group::Info { start: 3 }).unwrap(); + assert_eq!( + reader.poll_drop(&waiter), + Poll::Ready((5..6, Hole::drop(StreamError::Cancel))), + "the group containing time 8 starts at 3" + ); + } + /// A group missing its head is never served short: reading it is [`Error::Lagged`], /// which a route copy recovers. #[test] @@ -27417,6 +27611,42 @@ mod test { ); } + /// A received stale run in a reader's range settles it once a group arriving places its + /// open range's time start low enough to read every group above the run, so it is + /// reported then, as a scan of the whole window would, though the window grew only above + /// the run. + #[test] + #[cfg(feature = "async")] + fn a_stale_run_settles_once_the_open_range_reaches_it() { + let producer = track_producer("test", Info::timed()); + // An unsettled run is asked of a handler rather than given up as unfillable. + let _dynamic = producer.requests(); + let budget = Duration::from_millis(500); + // Time 0 names group 0, so the first range reads through 5 at once; the open one waits + // on a group to place its time start. + let first = Range { + start: Anchor::Time(0), + end: Some(Anchor::Time(5)), + }; + let open = Range { + start: Anchor::Time(8), + end: None, + }; + let mut reader = producer.subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(budget)) + .with_ranges([first, open]), + ); + producer.give_up(5..6, Hole::stale(budget)); + let waiter = kio::Waiter::noop(); + assert!( + reader.poll_drop(&waiter).is_pending(), + "the groups above it are not all read" + ); + let _group = producer.create_group(group::Info { start: 6 }).unwrap(); + assert_eq!(reader.poll_drop(&waiter), Poll::Ready((5..6, Hole::stale(budget)))); + } + /// A queue-only poll, which judges no drift, still reports the received stale runs the /// last probing poll found settled, rather than forgetting them until the next probe. #[test] diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index 2249892b097..c96c3da3bda 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -177,7 +177,7 @@ case "$1" in done ;; js) - for benchmark in broadcasts requests fills fill-probe forward reader frames drift ledger bandwidth replay varint lite-varint track resume priority subscribe-drop refresh observed compression datagrams; do + for benchmark in broadcasts requests fills fill-probe forward reader frames drift ledger bandwidth replay varint lite-varint track resume priority subscribe-drop window-drop refresh observed compression datagrams; do javascript "$benchmark" just js bench "$benchmark" done javascript catalog just js catalog-bench