diff --git a/js/justfile b/js/justfile index fd6810300e..07164c55f1 100644 --- a/js/justfile +++ b/js/justfile @@ -77,7 +77,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, window-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, publisher-update, 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/publisher-update.ts b/js/net/bench/publisher-update.ts new file mode 100644 index 0000000000..e1158bb234 --- /dev/null +++ b/js/net/bench/publisher-update.ts @@ -0,0 +1,118 @@ +/** + * Sweep what a lite-07 SUBSCRIBE_UPDATE costs the publisher as the subscription's ranges grow. + * + * One subscription holds `ranges` one-group ranges on a track with no group yet, so nothing is + * served. Each update retires the `edited` oldest ranges and adds as many past the newest, so the + * union moves with every edit; `steer` moves only the priority. A cost that grows with + * the live ranges rather than the edit shows up as a slope over `ranges`. Mirrors the Rust + * `publisher_update` bench. + * + * Run with `just js bench publisher-update`. + */ +import { randomHop } from "../src/hop.ts"; +import { Publisher } from "../src/lite/publisher.ts"; +import { Subscribe, SubscribeUpdate, type WireRange } from "../src/lite/subscribe.ts"; +import { ALPN_07_WIP, Version } from "../src/lite/version.ts"; +import { createMockTransportPair } from "../src/mock.ts"; +import { Producer as OriginProducer } from "../src/origin.ts"; +import * as Path from "../src/path.ts"; +import { Stream } from "../src/stream.ts"; +import { Timescale } from "../src/time.ts"; + +/** Live ranges in the subscription. */ +const LIVE = [16, 1024, 8192]; +/** Ranges each update retires, and adds. */ +const EDITED = [1, 16]; +/** Updates timed per run, beyond the run with none that measures teardown. */ +const UPDATES = 1024; +const REPS = 7; + +// One group per range, cycling over twice the live ranges so every key fits the SUBSCRIBE's varints. +const range = (ranges: number, index: bigint): WireRange => { + const group = index % BigInt(2 * ranges); + return { start: { time: group }, end: { time: group } }; +}; + +// Serve one subscription of `ranges` ranges, send it `updates` updates, and time the whole run. +async function run(ranges: number, edited: number | "steer", updates: number): Promise { + const origin = new OriginProducer(); + const broadcast = origin.route(Path.from("bench")).broadcast; + broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null }); + const pair = createMockTransportPair(ALPN_07_WIP); + const publisher = new Publisher(pair.server, Version.DRAFT_07, randomHop(), origin.consume()); + const client = await Stream.open(pair.client, { version: Version.DRAFT_07 }); + const server = await Stream.accept(pair.server, Version.DRAFT_07); + if (!server) throw new Error("missing subscribe stream"); + const serving = publisher.runSubscribe( + new Subscribe({ + id: 0n, + broadcast: Path.from("bench"), + track: "video", + priority: 0, + maxDelay: 3_600_000, + order: "ascending", + ranges: Array.from({ length: ranges }, (_, index) => range(ranges, BigInt(index))), + }), + server, + ); + // Timed once the subscription is served and parked, so only the updates and teardown count. + await new Promise((resolve) => setTimeout(resolve, 100)); + const start = performance.now(); + // The Range IDs of the oldest live range and the next one added, and of the next range's group. + let oldest = 0n; + let next = BigInt(ranges); + for (let step = 0; step < updates; step++) { + const update = + edited === "steer" + ? new SubscribeUpdate({ + priority: step % 2, + order: "ascending", + maxDelay: 3_600_000, + remove: [], + ranges: [], + }) + : new SubscribeUpdate({ + priority: 0, + order: "ascending", + maxDelay: 3_600_000, + remove: Array.from({ length: edited }, (_, index) => oldest + BigInt(index)), + ranges: Array.from({ length: edited }, (_, index) => range(ranges, next + BigInt(index))), + }); + if (edited !== "steer") { + oldest += BigInt(edited); + next += BigInt(edited); + } + await update.encode(client.writer, Version.DRAFT_07); + } + client.writer.close(); + await serving; + const elapsed = performance.now() - start; + publisher.close(); + client.close(); + pair.client.close(); + pair.server.close(); + broadcast.close(); + origin.close(); + return elapsed; +} + +const debug = console.debug; +console.debug = () => {}; +try { + console.log("ranges,edit,update_us"); + for (const ranges of LIVE) { + for (const edited of [...EDITED, "steer" as const]) { + // The fastest of each, since a shared host only ever adds time. + let base = Number.POSITIVE_INFINITY; + let timed = Number.POSITIVE_INFINITY; + for (let rep = 0; rep < REPS; rep++) { + base = Math.min(base, await run(ranges, edited, 0)); + timed = Math.min(timed, await run(ranges, edited, UPDATES)); + } + const label = edited === "steer" ? "steer" : `edit${edited}`; + console.log(`${ranges},${label},${(((timed - base) * 1000) / UPDATES).toFixed(1)}`); + } + } +} finally { + console.debug = debug; +} diff --git a/js/net/src/cover.ts b/js/net/src/cover.ts new file mode 100644 index 0000000000..098cba39e1 --- /dev/null +++ b/js/net/src/cover.ts @@ -0,0 +1,354 @@ +/** + * Counted runs of group keys, so one edit and each query cost O(log n) however many runs are held. + * Mirrors the Rust `Cover`, without its persistence. + * + * A subscription may name thousands of ranges, and an update may edit one of them. Each run counts + * +1 at its start and -1 at its exclusive end, so a key is covered while the running sum through it + * stays positive. A treap keeps each subtree's sum and its lowest and highest running sums, so + * adding or removing a run touches two keys and every query is one descent. + * + * @module + */ + +import { min, UNBOUNDED } from "./util/key.ts"; + +/** A run of keys `[start, end)`; an open run ends at {@link UNBOUNDED}. */ +type Run = { start: bigint; end: bigint }; + +interface Node { + key: K; + // Runs starting here, minus runs ending here. Never zero. + delta: number; + // Random, so no choice of keys can unbalance the tree. + priority: number; + // The sum of every delta in this subtree. + sum: number; + // The lowest and highest running sums at any key of this subtree, counted from its first. + low: number; + high: number; + left?: Node; + right?: Node; +} + +type Link = Node | undefined; + +function sum(node: Link): number { + return node ? node.sum : 0; +} + +function low(node: Link): number { + return node ? node.low : Number.POSITIVE_INFINITY; +} + +function high(node: Link): number { + return node ? node.high : Number.NEGATIVE_INFINITY; +} + +function pull(node: Node): void { + const at = sum(node.left) + node.delta; + node.sum = at + sum(node.right); + node.low = Math.min(low(node.left), at, at + low(node.right)); + node.high = Math.max(high(node.left), at, at + high(node.right)); +} + +// Split into the keys below `key` (through it when `inclusive`) and the rest. +function split(node: Link, key: K, inclusive: boolean): [Link, Link] { + if (!node) return [undefined, undefined]; + if (node.key < key || (inclusive && node.key === key)) { + const [below, rest] = split(node.right, key, inclusive); + node.right = below; + pull(node); + return [node, rest]; + } + const [rest, above] = split(node.left, key, inclusive); + node.left = above; + pull(node); + return [rest, node]; +} + +// Join two treaps, every key of `left` below every key of `right`. +function merge(left: Link, right: Link): Link { + if (!left) return right; + if (!right) return left; + if (left.priority > right.priority) { + left.right = merge(left.right, right); + pull(left); + return left; + } + right.left = merge(left, right.left); + pull(right); + return right; +} + +// Add `delta` at `key`, dropping its node once its delta reaches zero: one descent to find the +// key, then one to adjust or insert it. +function shift(root: Link, key: K, delta: number): Link { + if (find(root, key)) return adjust(root as Node, key, delta); + return insert(root, { key, delta, priority: Math.random(), sum: delta, low: delta, high: delta }); +} + +// Add `delta` at `key`, which the subtree under `node` holds. +function adjust(node: Node, key: K, delta: number): Link { + if (key === node.key) { + node.delta += delta; + if (node.delta === 0) return merge(node.left, node.right); + } else if (key < node.key) node.left = adjust(node.left as Node, key, delta); + else node.right = adjust(node.right as Node, key, delta); + pull(node); + return node; +} + +// Insert `fresh`, whose key the subtree under `node` lacks, where its priority places it. +function insert(node: Link, fresh: Node): Node { + if (!node) return fresh; + if (fresh.priority > node.priority) { + [fresh.left, fresh.right] = split(node, fresh.key, false); + pull(fresh); + return fresh; + } + if (fresh.key < node.key) node.left = insert(node.left, fresh); + else node.right = insert(node.right, fresh); + pull(node); + return node; +} + +function find(node: Link, key: K): Node | undefined { + while (node && node.key !== key) node = key < node.key ? node.left : node.right; + return node; +} + +// The lowest running sum at any key of the subtree in `(lo, hi)`, entered with `offset` runs open. +function lowIn(node: Link, offset: number, lo: bigint, hi: bigint): number { + if (!node) return Number.POSITIVE_INFINITY; + const at = offset + sum(node.left) + node.delta; + if (node.key <= lo) return lowIn(node.right, at, lo, hi); + if (node.key >= hi) return lowIn(node.left, offset, lo, hi); + return Math.min(lowAbove(node.left, offset, lo), at, lowBelow(node.right, at, hi)); +} + +// {@link lowIn} for a subtree whose every key is below `hi`. +function lowAbove(node: Link, offset: number, lo: bigint): number { + if (!node) return Number.POSITIVE_INFINITY; + const at = offset + sum(node.left) + node.delta; + if (node.key <= lo) return lowAbove(node.right, at, lo); + return Math.min(lowAbove(node.left, offset, lo), at, at + low(node.right)); +} + +// {@link lowIn} for a subtree whose every key is above `lo`. +function lowBelow(node: Link, offset: number, hi: bigint): number { + if (!node) return Number.POSITIVE_INFINITY; + const at = offset + sum(node.left) + node.delta; + if (node.key >= hi) return lowBelow(node.left, offset, hi); + return Math.min(offset + low(node.left), at, lowBelow(node.right, at, hi)); +} + +function first(node: Link): K | undefined { + if (!node) return undefined; + while (node.left) node = node.left; + return node.key; +} + +function last(node: Link): K | undefined { + if (!node) return undefined; + while (node.right) node = node.right; + return node.key; +} + +// The running sum through `key`. +function count(root: Link, key: K): number { + let total = 0; + let node = root; + while (node) { + if (node.key <= key) { + total += sum(node.left) + node.delta; + node = node.right; + } else node = node.left; + } + return total; +} + +// Whether a running sum is what is searched for: covered (above zero), or a gap. +type Want = (sum: number) => boolean; +const COVERED: Want = (sum) => sum > 0; +const GAP: Want = (sum) => sum <= 0; + +// Whether a subtree entered with `offset` may hold a running sum `want` accepts. +function within(offset: number, node: Node, want: Want): boolean { + return want === COVERED ? offset + node.high > 0 : offset + node.low <= 0; +} + +// The first key of the subtree, entered with `offset` runs open, whose running sum is `want`. +function firstIn(node: Link, offset: number, want: Want): bigint | undefined { + if (!node || !within(offset, node, want)) return undefined; + const left = firstIn(node.left, offset, want); + if (left !== undefined) return left; + const at = offset + sum(node.left) + node.delta; + if (want(at)) return node.key; + return firstIn(node.right, at, want); +} + +// The last key of the subtree, entered with `offset` runs open, whose running sum is `want`. +function lastIn(node: Link, offset: number, want: Want): bigint | undefined { + if (!node || !within(offset, node, want)) return undefined; + const at = offset + sum(node.left) + node.delta; + const right = lastIn(node.right, at, want); + if (right !== undefined) return right; + if (want(at)) return node.key; + return lastIn(node.left, offset, want); +} + +// {@link firstIn} over the keys at or after `from`. +function firstFrom(node: Link, offset: number, from: bigint, want: Want): bigint | undefined { + if (!node) return undefined; + const at = offset + sum(node.left) + node.delta; + if (node.key < from) return firstFrom(node.right, at, from, want); + const left = firstFrom(node.left, offset, from, want); + if (left !== undefined) return left; + if (want(at)) return node.key; + return firstIn(node.right, at, want); +} + +/** A multiset of runs of keys, read as the set of keys any run covers. */ +export class Cover { + #root: Link; + + /** Count `delta` more of `[start, end)`, or fewer when negative; an empty run counts nothing. */ + add(start: bigint, end: bigint, delta: number): void { + if (end <= start || delta === 0) return; + this.#root = shift(this.#root, start, delta); + if (end < UNBOUNDED) this.#root = shift(this.#root, end, -delta); + } + + /** Whether any run covers `key`. */ + contains(key: bigint): boolean { + return count(this.#root, key) > 0; + } + + /** The fewest runs covering any key in `[start, end)`. */ + least(start: bigint, end: bigint): number { + return Math.min(count(this.#root, start), lowIn(this.#root, 0, start, end)); + } + + /** The lowest key covered at or after `from`. */ + next(from: bigint): bigint | undefined { + return this.contains(from) ? from : firstFrom(this.#root, 0, from, COVERED); + } + + /** The lowest key covered: where the first run starts. */ + lowest(): bigint | undefined { + return first(this.#root); + } + + /** The exclusive end past every key covered: {@link UNBOUNDED} while a run is open, 0 for none. */ + end(): bigint { + if (sum(this.#root) > 0) return UNBOUNDED; + return last(this.#root) ?? 0n; + } + + /** Where the run reaching past every key starts, undefined when the last run ends. */ + openFrom(): bigint | undefined { + if (sum(this.#root) <= 0) return undefined; + // Past the last gap edge, or from the first edge when every edge leaves a run open. + const gap = lastIn(this.#root, 0, GAP); + return gap === undefined ? this.lowest() : firstFrom(this.#root, 0, gap, COVERED); + } + + /** + * The runs reaching into `[from, end)`, lowest first, clipped to it. One walk in key order that + * skips every subtree holding no run's edge, so each run costs about one descent. + */ + runs(from = 0n, end = UNBOUNDED): Run[] { + const out: Run[] = []; + if (from >= end) return out; + // Every edge above `from`, each with how many runs were open entering it. + const stack: [Node, number][] = []; + let open: bigint | undefined; + let offset = 0; + let node = this.#root; + while (node) { + if (node.key <= from) { + offset += sum(node.left) + node.delta; + node = node.right; + } else { + stack.push([node, offset]); + node = node.left; + } + } + if (offset > 0) open = from; + // Queue the subtree under `link` down its left edge, leaving out each subtree whose running + // sum stays on the side it entered on. + const descend = (link: Link, offset: number) => { + while (link) { + const crosses = offset > 0 ? offset + link.low <= 0 : offset + link.high > 0; + if (!crosses) return; + stack.push([link, offset]); + link = link.left; + } + }; + for (let top = stack.pop(); top; top = stack.pop()) { + const [node, offset] = top; + if (open === undefined && node.key >= end) return out; + const at = offset + sum(node.left) + node.delta; + descend(node.right, at); + if (open !== undefined && at <= 0) { + out.push({ start: open, end: min(node.key, end) }); + open = undefined; + } else if (open === undefined && at > 0) open = node.key; + } + if (open !== undefined && open < end) out.push({ start: open, end }); + return out; + } +} + +/** A multiset of keys, counted so that adding, removing, and finding the lowest or highest cost O(log n). */ +export class Counts { + #root: Link; + + /** Count `key` once more, returning whether it is new. */ + add(key: K): boolean { + const fresh = !this.has(key); + this.#root = shift(this.#root, key, 1); + return fresh; + } + + /** Count `key` once less, returning whether that was its last. Nothing for a key not held. */ + remove(key: K): boolean { + const held = this.#find(key)?.delta; + if (held === undefined) return false; + this.#root = shift(this.#root, key, -1); + return held === 1; + } + + /** Whether `key` is held. */ + has(key: K): boolean { + return this.#find(key) !== undefined; + } + + /** The lowest key held. */ + first(): K | undefined { + return first(this.#root); + } + + /** The highest key held. */ + last(): K | undefined { + return last(this.#root); + } + + /** The keys held at or below `limit`, lowest first, costing those keys and one descent. */ + through(limit: K): K[] { + const keys: K[] = []; + const walk = (node: Link) => { + if (!node) return; + walk(node.left); + if (node.key > limit) return; + keys.push(node.key); + walk(node.right); + }; + walk(this.#root); + return keys; + } + + #find(key: K): Node | undefined { + return find(this.#root, key); + } +} diff --git a/js/net/src/demand.ts b/js/net/src/demand.ts new file mode 100644 index 0000000000..df1dc38ee8 --- /dev/null +++ b/js/net/src/demand.ts @@ -0,0 +1,315 @@ +/** + * Incremental demand: each subscriber's preferences keyed so an edit costs what it names, and the + * aggregate across subscribers counted so a change costs what it moved. Mirrors the Rust + * `track::demand`. + * + * @module + */ + +import { Duration } from "@moq/clock"; +import { type Dispose, type Getter, Signal } from "@moq/signals"; +import { Counts } from "./cover.ts"; +import { maxDelayOf } from "./internal.ts"; +import { Ranges } from "./ranges.ts"; +import type { Delay, Order, Range, Subscription } from "./track.ts"; +import { rangeKey, sameRanges } from "./window.ts"; + +/** The preferences besides the ranges, already checked and defaulted. */ +export interface Scalars { + priority: number; + delay: Delay; + order: Order; +} + +/** + * A change to a subscriber's preferences that edits its ranges by key: the ranges keyed `retire` + * go, then each of `add` joins under its key, and the rest stay as they resolved. A key added + * again replaces its range in place; any other key added is past every key held. Costs the ranges + * it names, however many the subscriber holds. Mirrors the Rust `Edit`. + */ +export interface Edit extends Scalars { + retire: readonly bigint[]; + add: readonly (readonly [bigint, Range])[]; +} + +/** What one subscriber adds to an aggregate besides its ranges. */ +export interface Contribution { + priority: number; + /** The max delay in milliseconds, see {@link maxDelayOf}. */ + maxDelay: number; + /** The min delay in milliseconds, undefined when not stated. */ + minDelay?: number; + descending: boolean; +} + +/** A change to one subscriber's preferences, as an aggregate counts it. */ +export interface Change { + before?: Contribution; + after?: Contribution; + /** The ranges `before` held and `after` doesn't, counted. */ + gone: readonly Range[]; + /** The ranges `after` holds and `before` doesn't, counted. */ + came: readonly Range[]; +} + +function contribution(scalars: Scalars): Contribution { + const min = scalars.delay.min; + return { + priority: scalars.priority, + maxDelay: maxDelayOf(scalars), + minDelay: min === null || min === undefined ? undefined : min.asMillis(), + descending: scalars.order === "descending", + }; +} + +function sameScalars(a: Scalars, b: Scalars): boolean { + const same = (x: Duration | null | undefined, y: Duration | null | undefined) => + x === y || (x instanceof Duration && y instanceof Duration && x.eq(y)); + return ( + a.priority === b.priority && + a.order === b.order && + same(a.delay.min, b.delay.min) && + same(a.delay.max, b.delay.max) + ); +} + +/** The ranges `old` holds more of than `next`, then those `next` holds more of than `old`. */ +function moved(old: readonly Range[], next: readonly Range[]): [Range[], Range[]] { + const counts = new Map(); + const tally = (range: Range, delta: number) => { + const key = rangeKey(range); + const held = counts.get(key); + if (held) held.count += delta; + else counts.set(key, { range, count: delta }); + }; + for (const range of old) tally(range, 1); + for (const range of next) tally(range, -1); + const gone: Range[] = []; + const came: Range[] = []; + for (const { range, count } of counts.values()) { + for (let left = Math.abs(count); left > 0; left--) (count > 0 ? gone : came).push(range); + } + return [gone, came]; +} + +/** + * A subscriber's preferences, the ranges keyed so an {@link Edit} costs only what it names. Reads + * as the {@link Subscription} last given, its ranges in key order, materialized only when read. + * Mirrors the Rust `Prefs`. + */ +export class Prefs implements Getter { + #scalars?: Scalars; + // In key order: a whole replacement keys by position, and each key an edit adds is past the rest. + #ranges = new Map(); + // The preferences as last read, until they change. + #value?: Subscription; + /** Bumped on every change, so a reader wakes without reading the ranges. */ + readonly revision = new Signal(0); + /** Told each change, so the aggregates this subscriber joins move by what changed. */ + watch?: (change: Change) => void; + + /** Start with `subscription`, already checked and defaulted, or none. */ + constructor(subscription?: Subscription) { + if (subscription) this.#replace(subscription); + } + + /** The preferences as a {@link Subscription}, costing every range once per change. */ + peek(): Subscription | undefined { + if (!this.#scalars) return undefined; + this.#value ??= { ...this.#scalars, ranges: [...this.#ranges.values()] }; + return this.#value; + } + + changed(): Promise; + changed(fn: (value: Subscription | undefined) => void): Dispose; + changed(fn?: (value: Subscription | undefined) => void): Promise | Dispose { + if (!fn) return this.revision.changed().then(() => this.peek()); + return this.revision.changed(() => fn(this.peek())); + } + + subscribe(fn: (value: Subscription | undefined) => void): Dispose { + return this.revision.subscribe(() => fn(this.peek())); + } + + /** The preferences besides the ranges, without reading the ranges. */ + scalars(): Scalars | undefined { + return this.#scalars; + } + + /** What these add to an aggregate besides the ranges. */ + contribution(): Contribution | undefined { + return this.#scalars && contribution(this.#scalars); + } + + /** Every range, in key order. */ + ranges(): IterableIterator { + return this.#ranges.values(); + } + + /** Every range with its key, in key order. */ + entries(): IterableIterator<[bigint, Range]> { + return this.#ranges.entries(); + } + + /** Wake every reader, as a change would, without changing anything. */ + touch(): void { + this.revision.update((revision) => revision + 1); + } + + /** + * Replace every preference, already checked and defaulted, rekeying the ranges by position when + * they change. Returns whether the ranges changed; nothing happens unless something did. + */ + set(next: Subscription): boolean { + const scalars = this.#scalars; + const old = [...this.#ranges.values()]; + const same = sameRanges(old, next.ranges ?? []); + if (scalars && same && sameScalars(scalars, next as Scalars)) return false; + const before = this.contribution(); + const [gone, came] = same ? [[], []] : moved(old, next.ranges ?? []); + this.#replace(next); + this.watch?.({ before, after: this.contribution(), gone, came }); + this.touch(); + return !same; + } + + /** Apply `edit`, costing the ranges it names. Returns whether the ranges changed. */ + edit(edit: Edit): boolean { + const before = this.contribution(); + const { retire, add, ...scalars } = edit; + const readded = new Set(add.map(([key]) => key)); + const gone: Range[] = []; + const came: Range[] = []; + for (const key of retire) { + const range = this.#ranges.get(key); + // One added again is replaced in place below, keeping its order. + if (range === undefined || readded.has(key)) continue; + this.#ranges.delete(key); + gone.push(range); + } + for (const [key, range] of add) { + const old = this.#ranges.get(key); + if (old !== undefined) gone.push(old); + this.#ranges.set(key, range); + came.push(range); + } + const moved = gone.length > 0 || came.length > 0; + if (!moved && this.#scalars && sameScalars(this.#scalars, scalars)) return false; + this.#scalars = scalars; + this.#value = undefined; + this.watch?.({ before, after: this.contribution(), gone, came }); + this.touch(); + return moved; + } + + #replace(next: Subscription): void { + const { ranges, ...rest } = next; + this.#scalars = rest as Scalars; + if (!sameRanges([...this.#ranges.values()], ranges ?? [])) { + this.#ranges = new Map((ranges ?? []).map((range, index) => [BigInt(index), range])); + } + // Read back as given, so its identity holds until the next change. + this.#value = next; + } +} + +/** + * The aggregate of every subscriber's preferences, counted so a change costs what it moved: + * the highest priority and max delay, the largest stated min delay, descending if any subscriber + * is, and the union of the ranges. Read as a {@link Subscription}, materialized only when read. + * Mirrors the Rust `Subscriptions`. + */ +export class Subscriptions implements Getter { + #count = 0; + #priority = new Counts(); + #maxDelay = new Counts(); + // Only stated minimums count, so an unstated one never reads as zero. + #minDelay = new Counts(); + #descending = 0; + #ranges = new Ranges(); + #revision = new Signal(0); + // The aggregate as last read, until it changes; null for none read yet. + #value: Subscription | undefined | null = null; + + /** + * Move one subscriber from `before` to `after`, either undefined for none, its ranges by + * `gone` and `came`. Returns whether the aggregate changed. + */ + replace({ before, after, gone, came }: Change): boolean { + const prev = this.#scalars(); + let reshaped = false; + // New ranges count before old ones leave, so a range both hold never reads as gone. + if (after) { + this.#priority.add(after.priority); + this.#maxDelay.add(after.maxDelay); + if (after.minDelay !== undefined) this.#minDelay.add(after.minDelay); + this.#descending += after.descending ? 1 : 0; + this.#count++; + } + for (const range of came) if (this.#ranges.add(range)) reshaped = true; + if (before) { + this.#priority.remove(before.priority); + this.#maxDelay.remove(before.maxDelay); + if (before.minDelay !== undefined) this.#minDelay.remove(before.minDelay); + this.#descending -= before.descending ? 1 : 0; + this.#count--; + } + for (const range of gone) if (this.#ranges.remove(range)) reshaped = true; + const next = this.#scalars(); + const changed = + reshaped || + prev?.priority !== next?.priority || + prev?.maxDelay !== next?.maxDelay || + prev?.minDelay !== next?.minDelay || + prev?.descending !== next?.descending; + if (changed) { + this.#value = null; + this.#revision.update((revision) => revision + 1); + } + return changed; + } + + /** The largest max delay in milliseconds, without reading the ranges. */ + maxDelay(): number { + return this.#maxDelay.last() ?? 0; + } + + #scalars(): Contribution | undefined { + if (this.#count === 0) return undefined; + return { + priority: this.#priority.last() ?? 0, + maxDelay: this.maxDelay(), + minDelay: this.#minDelay.last(), + descending: this.#descending > 0, + }; + } + + /** The aggregate, costing the union's length once per change. */ + peek(): Subscription | undefined { + if (this.#value !== null) return this.#value; + const scalars = this.#scalars(); + this.#value = scalars && { + priority: scalars.priority, + delay: { + max: Duration.millis(scalars.maxDelay), + // The largest stated, so an upstream starts far enough back for the reader that buffers most. + min: scalars.minDelay === undefined ? null : Duration.millis(scalars.minDelay), + }, + // One live subscriber held behind another's backlog is worse than a backlog sent late. + order: scalars.descending ? "descending" : "ascending", + ranges: [...this.#ranges.union()], + }; + return this.#value; + } + + changed(): Promise; + changed(fn: (value: Subscription | undefined) => void): Dispose; + changed(fn?: (value: Subscription | undefined) => void): Promise | Dispose { + if (!fn) return this.#revision.changed().then(() => this.peek()); + return this.#revision.changed(() => fn(this.peek())); + } + + subscribe(fn: (value: Subscription | undefined) => void): Dispose { + return this.#revision.subscribe(() => fn(this.peek())); + } +} diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index eb4718e351..6c9a638d62 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -11,6 +11,7 @@ import type { Consumer as BroadcastConsumer, Producer as BroadcastProducer } fro import type { Charge, Pool } from "./cache.ts"; import type * as datagram from "./datagram.ts"; import type { Delivered } from "./delivered.ts"; +import type { Edit, Prefs } from "./demand.ts"; import { StreamCode, StreamError, toStreamCode } from "./error.ts"; import type { Consumer as FrameConsumer } from "./frame.ts"; import type { Frame, Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; @@ -831,6 +832,24 @@ export const hooks: { * still waits: what lite-07 SUBSCRIBE_STARTED answers. Assigned by `track.ts`. */ servingResolved: (subscriber: Subscriber) => (Resolved | undefined)[]; + /** + * A subscriber's ranges keyed `keys` as resolved where they are served, as + * {@link servingResolved} reads them, undefined for one not held. Asked together, so a reader + * resolving by position on route copies builds its ranges once for them all. Assigned by + * `track.ts`. + */ + servingResolvedKeys: (subscriber: Subscriber, keys: readonly bigint[]) => (Resolved | undefined)[]; + /** + * Edit a subscriber's preferences by key, costing the ranges the edit names rather than every + * range: lite-07's SUBSCRIBE_UPDATE, by Range ID. Mirrors the Rust `Subscriber::edit`. Assigned + * by `track.ts`. + */ + editSubscriber: (subscriber: Subscriber, edit: Edit) => void; + /** + * A subscriber's preferences, whose scalars and revision read without materializing every + * range as {@link Subscriber.subscription} does. Assigned by `track.ts`. + */ + preferences: (subscriber: Subscriber) => Prefs; /** * `subscription`'s ranges resolved against the subscriber's track now, by index. Mirrors the * Rust `Subscriber::resolve_ranges`. Assigned by `track.ts`. @@ -1171,6 +1190,15 @@ export const hooks: { servingResolved: () => { throw new Error("track.ts not loaded"); }, + servingResolvedKeys: () => { + throw new Error("track.ts not loaded"); + }, + editSubscriber: () => { + throw new Error("track.ts not loaded"); + }, + preferences: () => { + throw new Error("track.ts not loaded"); + }, resolveRanges: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 3f016ffef7..030bc733e8 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -7,6 +7,7 @@ import { enforceGrant } from "../auth_session.ts"; import type * as broadcast from "../broadcast.ts"; import { statsOf } from "../connection/stats.ts"; import { Withdrawal } from "../connection/withdrawal.ts"; +import { Counts } from "../cover.ts"; import type * as datagram from "../datagram.ts"; import * as DatagramStream from "../datagram_stream.ts"; import { @@ -49,7 +50,7 @@ import { nextChange } from "../util/change.ts"; import { GivenUp } from "../util/given_up.ts"; import { max, min } from "../util/key.ts"; import { Budget } from "../util/yield.ts"; -import type { Resolved } from "../window.ts"; +import { isEmpty, type Resolved, rangeKey } from "../window.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; import { type Add, AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from "./announce.ts"; import { AnnounceStream } from "./announce_stream.ts"; @@ -60,9 +61,12 @@ import type { Fetch } from "./fetch.ts"; import { Group as GroupMessage, passedOver } from "./group.ts"; import { Probe } from "./probe.ts"; import { + type Applied, encodeSubscribeResponse, LiveRanges, + rangeBounds, rangesOf, + requestedRange, requestedRanges, type Subscribe, SubscribeDrop, @@ -73,7 +77,7 @@ import { SubscribeStart, SubscribeStarted, SubscribeUpdate, - spanOf, + spanBetween, type WireRange, } from "./subscribe.ts"; import { TrackInfo as TrackInfoMessage, type Track as TrackMessage } from "./track.ts"; @@ -317,9 +321,9 @@ type ServeFetch = Omit & { type Control = /** * The peer re-stated the subscription: priority, ordering, latency, and its ranges, with the - * Range ID of each on lite-07. + * Range IDs it retired and added on lite-07. */ - | { kind: "update"; update: SubscribeUpdate; ids: bigint[] } + | { kind: "update"; update: SubscribeUpdate; applied: Applied } /** The peer FIN'd, or our own half went away. Either way there is nobody left to serve. */ | { kind: "done" } /** The stream failed; the subscription goes down with it. */ @@ -332,11 +336,8 @@ type SubscriptionControlOptions = { version: Version; /** The SUBSCRIBE's ranges, which each lite-07 update edits. */ live: LiveRanges; - /** - * Apply an update, with every live range by Range ID on lite-07: the same array as the last - * update's while it left every range live. - */ - apply: (update: SubscribeUpdate, live: readonly { id: bigint; range: WireRange }[]) => void; + /** Apply an update, with the ranges it retired and added by Range ID on lite-07. */ + apply: (update: SubscribeUpdate, applied: Applied) => void; }; /** @@ -355,10 +356,9 @@ type SubscriptionControlOptions = { class SubscriptionControls { #writer: Writer; #live: LiveRanges; - #update?: { update: SubscribeUpdate; ids: bigint[] }; - // The live ranges last applied and their Range IDs, so an update leaving every range live hands - // the serving loop the same ids. - #applied?: { live: readonly { id: bigint; range: WireRange }[]; ids: bigint[] }; + // The latest update the loop has yet to take, with every edit since the last one it took: a + // range added then retired before then was never live, so it is in neither. + #update?: { update: SubscribeUpdate; retired: bigint[]; added: Map }; // Sticky, first one wins: null once the stream is over, an Error once it failed. #end?: Error | null; #ended: Promise; @@ -389,20 +389,30 @@ class SubscriptionControls { const update = this.#update; this.#update = undefined; // An update decoded before the stream ended still applies before the sticky end. - if (update) return { kind: "update", ...update }; + if (update) + return { + kind: "update", + update: update.update, + applied: { retired: update.retired, added: [...update.added] }, + }; if (this.#end === undefined) return undefined; return this.#end === null ? { kind: "done" } : { kind: "error", error: this.#end }; } /** - * Where each live Range ID sits among the track model's ranges. The decoder applies each - * update to the model as it decodes it, ahead of {@link take}, so this rather than the - * last control taken is what indexes the model. + * Where each live Range ID sits among the track model's ranges, which hold them in Range ID + * order. The decoder applies each update to the model as it decodes it, ahead of {@link take}, + * so this rather than the last control taken is what indexes the model. Costs every range. */ positions(): Map { return new Map(this.#live.ids().map((id, index) => [id, index])); } + /** Whether the track model holds Range ID `id`, as {@link positions} would index it. */ + holds(id: bigint): boolean { + return this.#live.get(id) !== undefined; + } + /** Whether {@link take} has a control to answer with now. */ pending(): boolean { return this.#update !== undefined || this.#end !== undefined; @@ -455,11 +465,18 @@ class SubscriptionControls { const update = await SubscribeUpdate.decodeMaybe(reader, version); if (!update) break; // Each lite-07 update edits the live ranges, which only the stream's history names. - const ranges = live.apply(update); - apply(update, ranges); - if (this.#applied?.live !== ranges) this.#applied = { live: ranges, ids: ranges.map(({ id }) => id) }; - // Resolved in full, so the latest one replaces any the loop has yet to take. - this.#update = { update, ids: this.#applied.ids }; + const edit = live.apply(update); + apply(update, edit); + // The latest one replaces any the loop has yet to take, its edit folded in. + const pending = this.#update ?? { + update, + retired: [] as bigint[], + added: new Map(), + }; + pending.update = update; + for (const id of edit.retired) if (!pending.added.delete(id)) pending.retired.push(id); + for (const [id, range] of edit.added) pending.added.set(id, range); + this.#update = pending; this.#changed.update((value) => value + 1); } } catch (err: unknown) { @@ -471,6 +488,117 @@ class SubscriptionControls { } } +/** + * A subscription's requested ranges by Range ID, as the model holds them, with the bounds the read + * cursor takes from them counted, so an edit moves them in what it costs. Mirrors the Rust + * `Requested`. + */ +class Requested { + #ranges = new Map(); + // The groups the ranges start at, and how many count back from the latest group instead. + #starts = new Counts(); + #unnamed = 0; + // The exclusive groups the ranges end before, and how many have no such end. + #ends = new Counts(); + #unbounded = 0; + + /** The ranges a SUBSCRIBE carried, by position. */ + constructor(ranges: readonly WireRange[]) { + for (const [id, range] of ranges.entries()) this.insert(BigInt(id), range); + } + + get(id: bigint): WireRange | undefined { + return this.#ranges.get(id); + } + + /** Hold `range` as Range ID `id`, replacing any it held. */ + insert(id: bigint, range: WireRange): void { + this.remove(id); + this.#count(range, true); + this.#ranges.set(id, range); + } + + remove(id: bigint): void { + const range = this.#ranges.get(id); + if (range === undefined) return; + this.#count(range, false); + this.#ranges.delete(id); + } + + #count(range: WireRange, add: boolean): void { + const [from, to] = rangeBounds(range); + if (from === undefined) this.#unnamed += add ? 1 : -1; + else if (add) this.#starts.add(from); + else this.#starts.remove(from); + if (to === undefined) this.#unbounded += add ? 1 : -1; + else if (add) this.#ends.add(to); + else this.#ends.remove(to); + } + + /** The lowest group the ranges start at, undefined unless each names its start; see {@link spanOf}. */ + start(): bigint | undefined { + return this.#unnamed > 0 ? undefined : this.#starts.first(); + } + + /** The exclusive end past every range, undefined when one has no such end; see {@link spanOf}. */ + end(): bigint | undefined { + return this.#unbounded > 0 ? undefined : this.#ends.last(); + } +} + +/** + * Lite-07 ranges whose relative or time anchors may still resolve, by Range ID, each due once the + * latest group reaches the lowest start that could resolve it. Mirrors the Rust `Unpinned`. + */ +class Unpinned { + #due = new Map(); + #byDue = new Map>(); + #dues = new Counts(); + + /** Hold range `id` until `due`, or let it go for undefined. */ + insert(id: bigint, due: bigint | undefined): void { + this.remove(id); + if (due === undefined) return; + this.#due.set(id, due); + let ids = this.#byDue.get(due); + if (!ids) { + ids = new Set(); + this.#byDue.set(due, ids); + this.#dues.add(due); + } + ids.add(id); + } + + remove(id: bigint): void { + const due = this.#due.get(id); + if (due === undefined) return; + this.#due.delete(id); + const ids = this.#byDue.get(due); + ids?.delete(id); + if (ids?.size === 0) { + this.#byDue.delete(due); + this.#dues.remove(due); + } + } + + /** The ranges due once the latest group starts at `latest`. */ + due(latest: bigint): bigint[] { + return this.#dues.through(latest).flatMap((due) => [...(this.#byDue.get(due) ?? [])]); + } +} + +/** + * The lowest latest group start that could resolve `range`'s next relative or time anchor: its + * start unless `started`, then its end. Undefined once neither can move. Mirrors the Rust `due`. + */ +function due(range: WireRange, started: boolean): bigint | undefined { + if (isEmpty(requestedRange(range))) return undefined; + if (!started && (range.start?.relative !== undefined || range.start?.time !== undefined)) return 0n; + if (range.end?.relative !== undefined) return 0n; + if (range.end?.time !== undefined) return range.end.time + 1n; + return undefined; +} + // `entry`, a range this subscription already held, resolved as it was: a range resolves once, so // an update that leaves it live never counts its relative anchors back from a newer latest group, // nor looks a time start up again in newer holdings. A start reaching back to the oldest group @@ -1084,27 +1212,54 @@ export class Publisher { } } - // The SUBSCRIBE's ranges, and the Range ID of each range the model holds, in its order. + // The SUBSCRIBE's ranges, whose positions are their Range IDs, which key them in the model. const asked = rangesOf(msg); - // Where each Range ID sits among the model's ranges, as of the last update applied. - let positions = new Map(asked.map((_, index) => [BigInt(index), index])); - // The live ranges the last update applied, what they resolved to, and the terms beside - // them, so an update leaving every range live costs nothing for them. - let applied: - | { - live: readonly { id: bigint; range: WireRange }[]; - span: ReturnType; - model: track.Subscription; - terms: Pick; - } - | undefined; + // The ranges as the model holds them, pinned, by Range ID, with the read cursor's bounds. + const requested = new Requested(asked); + // Lite-07: the ranges whose relative or time anchors may still resolve. + const unpinned = new Unpinned(); + if (hasRanges(this.version)) + for (const [id, range] of asked.entries()) unpinned.insert(BigInt(id), due(range, false)); + // Each live range due once the latest group reached where it may resolve, with its anchors + // replaced by the groups they resolved to: a range resolves once, so an update that leaves it + // live never counts its relative anchors back from a newer latest group, nor looks a time + // start up again in newer holdings, here or on a relay's copies. Only the ranges still + // waiting are asked, and each is let go once its anchors settle, so this costs those ranges + // rather than every range. Mirrors the Rust `TrackRun::pin`. + const pinDue = (): [bigint, WireRange][] => { + const latest = hooks.latestKey(track); + if (latest === undefined) return []; + const pins: [bigint, WireRange][] = []; + const asking: [bigint, WireRange][] = []; + for (const id of unpinned.due(latest)) { + const range = requested.get(id); + if (range === undefined) unpinned.remove(id); + else asking.push([id, range]); + } + const resolution = hooks.servingResolvedKeys( + track, + asking.map(([id]) => id), + ); + for (const [index, [id, range]] of asking.entries()) { + const resolved = resolution[index]; + if (!resolved) continue; + const pinned = pin(range, resolved); + // The start settled; the end is due again once it may settle. + unpinned.insert(id, resolved.end === undefined ? due(pinned, true) : undefined); + // One pinned to the groups it already names holds the same model range. + if (rangeKey(requestedRange(pinned)) === rangeKey(requestedRange(range))) + requested.insert(id, pinned); + else pins.push([id, pinned]); + } + return pins; + }; controls = new SubscriptionControls({ quic: this.#quic, reader: stream.reader, writer: stream.writer, version: this.version, live: new LiveRanges(this.version, asked), - apply: (update, live) => { + apply: (update, { retired, added }) => { legacyPriority.set(update.priority); if (wireOf(front).localDatagrams) legacy?.setPriority(update.priority); // Every update restates the wanted group. Past our largest it is a refresh, which @@ -1112,52 +1267,37 @@ export class Publisher { const largest = hooks.peekLargest(track).largest?.group; if (update.wantedGroup !== undefined && (largest === undefined || update.wantedGroup > largest)) hooks.askSubscriber(track, "refresh"); - const terms = { - priority: update.priority, - order: update.order, - maxDelay: update.maxDelay, - minDelay: update.minDelay, - }; const delay = { max: servingMaxDelay(update.maxDelay), min: update.minDelay === undefined ? null : Duration.millis(update.minDelay), }; - // Every range still live as last applied: the model's ranges stand, and only the - // terms beside them may move. - if (applied?.live === live) { - Object.assign(update, applied.span); - const was = applied.terms; - if ( - was.priority === terms.priority && - was.order === terms.order && - was.maxDelay === terms.maxDelay && - was.minDelay === terms.minDelay - ) - return; - applied.model = { ...applied.model, priority: update.priority, order: update.order, delay }; - applied.terms = terms; - track.update(applied.model); - return; - } - let span: ReturnType | undefined; - if (hasRanges(this.version)) { - const resolved = hooks.servingResolved(track); - const ranges = live.map(({ id, range }) => { - const index = positions.get(id); - return index === undefined ? range : pin(range, resolved[index]); + if (!hasRanges(this.version)) { + // An older update restates its one span, replacing every range. + track.update({ + priority: update.priority, + order: update.order, + delay, + ranges: requestedRanges(update), }); - span = spanOf(this.version, ranges); - Object.assign(update, span); - positions = new Map(live.map(({ id }, index) => [id, index])); + return; } - const model: track.Subscription = { + // Lite-07 hands the model the edit by Range ID, so it costs the ranges it names + // whatever else is live. A pinned range is replaced in place, keeping its Range ID. + for (const id of retired) unpinned.remove(id); + const pins = pinDue(); + for (const id of retired) requested.remove(id); + for (const [id, range] of [...pins, ...added]) requested.insert(id, range); + for (const [id, range] of added) unpinned.insert(id, due(range, false)); + // Refused before the model moves, as one SUBSCRIBE asking for nothing is. + Object.assign(update, spanBetween(this.version, requested.start(), requested.end())); + const add = [...pins, ...added].map(([id, range]) => [id, requestedRange(range)] as const); + hooks.editSubscriber(track, { + retire: [...retired, ...pins.map(([id]) => id)], + add, priority: update.priority, order: update.order, delay, - ranges: requestedRanges(update), - }; - track.update(model); - if (span) applied = { live, span, model, terms }; + }); }, }); const finished = await this.#runTrack(track, stream.writer, controls, { @@ -1387,10 +1527,8 @@ export class Publisher { const answered = (position: track.Position | undefined) => position && { position, ended: hooks.largestFinished(track, position) }; let endSent = false; - // The Range IDs of the last update taken, and those an update added whose start no - // SUBSCRIBE_STARTED reported yet. Lite-07 only. - let ids = Array.from({ length: Number(initial) }, (_, index) => BigInt(index)); - let unstarted: bigint[] = []; + // Lite-07: the Range IDs an update added whose start no SUBSCRIBE_STARTED reported yet. + const unstarted = new Set(); // Where each of the SUBSCRIBE's ranges starts once delivery starts at `from`, by Range ID, // undefined for one with no group there or an update retired: SUBSCRIBE_STARTED answers @@ -1408,22 +1546,15 @@ export class Publisher { // Report where each range an update added starts, once it resolves, or with no start once // nothing can resolve it (`finished`). SUBSCRIBE_START goes first. const reportStarted = async (finished: boolean): Promise => { - if (startKey === undefined || unstarted.length === 0) return true; - const resolved = hooks.servingResolved(track); - const positions = controls.positions(); - const waiting: bigint[] = []; - for (const id of unstarted) { - const index = positions.get(id); - // An update not taken yet retired it, and taking that update forgets it. - if (index === undefined) { - waiting.push(id); - continue; - } - const range = resolved[index]; - if (!range && !finished) { - waiting.push(id); - continue; - } + if (startKey === undefined || unstarted.size === 0) return true; + // An update not taken yet retired one, and taking that update forgets it. + const held = [...unstarted].filter((id) => controls.holds(id)); + const resolution = hooks.servingResolvedKeys(track, held); + for (const [index, id] of held.entries()) { + // Or one taken while an earlier start went out. + if (!controls.holds(id)) continue; + const range = resolution[index]; + if (!range && !finished) continue; let start: bigint | undefined; if (range) { start = max(range.start, startKey); @@ -1432,8 +1563,8 @@ export class Publisher { const started = new SubscribeStarted(id, start); if (!(await controls.response(encodeSubscribeResponse(stream, { started }, this.version)))) return false; + unstarted.delete(id); } - unstarted = waiting; return true; }; @@ -1627,14 +1758,10 @@ export class Publisher { // An update may reach the finished track's history, so it is served again. finished = false; const update = control.update; - // The same ids as last applied leave every range live, which owes nothing new. - if (hasRanges(this.version) && control.ids !== ids) { + if (hasRanges(this.version)) { // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. - const live = new Set(control.ids); - unstarted = unstarted.filter((id) => live.has(id)); - const held = new Set(ids); - for (const id of control.ids) if (id >= initial && !held.has(id)) unstarted.push(id); - ids = control.ids; + for (const id of control.applied.retired) unstarted.delete(id); + for (const [id] of control.applied.added) if (id >= initial) unstarted.add(id); } console.debug(`subscribe update: broadcast=${broadcast} track=${track.name}`); // `cursorFloor` pins an unfloored pre-06 SUBSCRIBE to the latest diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 8af416c3dd..6d9ecea858 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -610,8 +610,8 @@ test("lite-07 updates edit only the changed ranges", async () => { new Reader(undefined, await encodeMessage(version, update), version), version, ); - const resolved = live.apply(got).map(({ id, range }) => [id, range]); - expect(resolved).toEqual(sent.entries()); + live.apply(got); + expect(live.entries()).toEqual(sent.entries()); }; await step([b, a], [], []); await step([a, c], [1n], [c]); @@ -627,18 +627,25 @@ test("lite-07 updates edit only the changed ranges", async () => { expect(() => refused([0n], [])).toThrow(ProtocolViolation); }); -// An edit that changes nothing hands back the snapshot it last did, so a stream of empty updates -// copies nothing; one that changes a range takes a fresh one. Mirrors the Rust -// `lite07_an_empty_edit_reuses_the_snapshot`. -test("lite-07 an empty edit reuses the snapshot", () => { +// An edit resolves to the Range IDs it names. Mirrors the Rust `lite07_an_edit_names_its_range_ids`. +test("lite-07 an edit names its range ids", () => { const live = new LiveRanges(Version.DRAFT_07, [{}]); - const empty = () => new SubscribeUpdate({ priority: 0, remove: [], ranges: [] }); - const first = live.apply(empty()); - expect(live.apply(empty())).toBe(first); - const range: WireRange = { start: { absolute: 4n } }; - const edited = live.apply(new SubscribeUpdate({ priority: 0, remove: [0n], ranges: [range] })); - expect(edited).toEqual([{ id: 1n, range }]); - expect(live.apply(empty())).toBe(edited); + const range = (group: bigint): WireRange => ({ start: { absolute: group } }); + const edit = (remove: bigint[], ranges: WireRange[]) => + live.apply(new SubscribeUpdate({ priority: 0, remove, ranges })); + expect(edit([], [])).toEqual({ retired: [], added: [] }); + expect(edit([0n], [range(4n), range(5n)])).toEqual({ + retired: [0n], + added: [ + [1n, range(4n)], + [2n, range(5n)], + ], + }); + expect(edit([1n], [range(6n)])).toEqual({ retired: [1n], added: [[3n, range(6n)]] }); + expect(live.entries()).toEqual([ + [2n, range(5n)], + [3n, range(6n)], + ]); }); // An edit to ranges too large for a SUBSCRIBE is refused before anything changes, so the live diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index 9c0d6903cc..f933359135 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -215,26 +215,27 @@ async function encodeSpan(w: Writer, version: Version, span: Span, ranges?: read * starting at or after it, which the publisher's floor keeps (see `cursorFloor`). */ export function requestedRanges(msg: Span & { ranges?: WireRange[] }): Range[] { - if (msg.ranges) { - return msg.ranges.map((range) => { - // Empty, as group bounds say it: keep its place, covering no group. - const start = range.start?.absolute; - const end = range.end?.absolute; - if (start !== undefined && end !== undefined && end <= start) return { ...EMPTY }; - if (end === 0n) return { ...EMPTY }; - const out: Range = {}; - if (range.start) out.start = start === undefined ? (range.start as Anchor) : { time: start }; - // An absolute end is exclusive; every model end includes its group. - if (range.end) out.end = end === undefined ? (range.end as Anchor) : { time: end - 1n }; - return out; - }); - } + if (msg.ranges) return msg.ranges.map(requestedRange); const range: Range = {}; if (msg.startGroup !== undefined) range.start = { time: msg.startGroup }; if (msg.endGroup !== undefined) range.end = { time: msg.endGroup }; return [range]; } +/** The model range one lite-07 range asks for; see {@link requestedRanges}. */ +export function requestedRange(range: WireRange): Range { + // Empty, as group bounds say it: keep its place, covering no group. + const start = range.start?.absolute; + const end = range.end?.absolute; + if (start !== undefined && end !== undefined && end <= start) return { ...EMPTY }; + if (end === 0n) return { ...EMPTY }; + const out: Range = {}; + if (range.start) out.start = start === undefined ? (range.start as Anchor) : { time: start }; + // An absolute end is exclusive; every model end includes its group. + if (range.end) out.end = end === undefined ? (range.end as Anchor) : { time: end - 1n }; + return out; +} + /** Decode one lite-07 range end, `undefined` for none: a time, inclusive as an end, or a count back. */ async function decodeAnchor(r: Reader): Promise { const kind = await r.u53(); @@ -278,20 +279,31 @@ export function spanOf(version: Version, ranges: readonly WireRange[]): Span & { let floor = true; let live = false; for (const range of ranges) { - // A time is the start of the group it names, inclusive as an end. - const from = range.start?.absolute ?? range.start?.time; - const to = range.end?.absolute ?? (range.end?.time === undefined ? undefined : range.end.time + 1n); + const [from, to] = rangeBounds(range); if (from === undefined) floor = false; else if (start === undefined || from < start) start = from; if (to === undefined) live = true; else if (end === undefined || end < to) end = to; } - if (!floor) start = undefined; - if (live) end = undefined; + return { ranges: [...ranges], ...spanBetween(version, floor ? start : undefined, live ? undefined : end) }; +} + +/** + * The group a lite-07 range starts at and the exclusive group it ends before, either undefined + * when it counts back from the latest group or has no such end: a time is the start of the group + * it names, inclusive as an end. + */ +export function rangeBounds(range: WireRange): [bigint | undefined, bigint | undefined] { + const from = range.start?.absolute ?? range.start?.time; + const to = range.end?.absolute ?? (range.end?.time === undefined ? undefined : range.end.time + 1n); + return [from, to]; +} + +/** The span from group `start` up to the exclusive group `end`, either undefined for none. */ +export function spanBetween(version: Version, start: bigint | undefined, end: bigint | undefined): Span { // An exclusive end at the very first group names nothing. if (end === 0n) throw new ProtocolViolation(EMPTY_RANGE); return { - ranges: [...ranges], startGroup: canonicalStartGroup(version, start, 0), startFrame: 0, endGroup: end === undefined ? undefined : end - 1n, @@ -357,6 +369,12 @@ function rangeKey(range: WireRange): string { return `${anchor(range.start)}:${anchor(range.end)}`; } +/** A received update's ranges resolved by Range ID: the ones retired, then each one added. Mirrors the Rust `Applied`. */ +export interface Applied { + retired: bigint[]; + added: [bigint, WireRange][]; +} + /** * A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the * subscriber to write each update as an edit, the publisher to resolve one. Mirrors the Rust @@ -372,9 +390,6 @@ export class LiveRanges { readonly #live = new Map(); // The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries. #size = 0; - // The live ranges `apply` last returned, until one changes, so an edit that changes nothing - // costs nothing. - #snapshot?: readonly { id: bigint; range: WireRange }[]; /** How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. */ readonly initial: bigint; @@ -386,7 +401,6 @@ export class LiveRanges { } #add(ranges: readonly WireRange[]) { - if (ranges.length > 0) this.#snapshot = undefined; for (const range of ranges) { // Only lite-07 numbers ranges on the wire, and only it is held to one SUBSCRIBE. if (hasRanges(this.#version)) this.#size += rangeSize(range); @@ -397,7 +411,6 @@ export class LiveRanges { #remove(id: bigint): boolean { const range = this.#live.get(id); if (!range) return false; - this.#snapshot = undefined; if (hasRanges(this.#version)) this.#size -= rangeSize(range); this.#live.delete(id); return true; @@ -434,23 +447,26 @@ export class LiveRanges { } /** - * Apply a received lite-07 `update`'s edit, returning every live range by Range ID: the same - * array as last time while nothing changed. Throws {@link ProtocolViolation} for an edit it - * cannot apply. Before lite-07 an update restates its ranges, so nothing is numbered. + * Apply a received lite-07 `update`'s edit, returning it by Range ID: the ranges retired, then + * each added with the Range ID it took, costing the ranges it names. Throws + * {@link ProtocolViolation} for an edit it cannot apply. Before lite-07 an update restates its + * ranges, so nothing is numbered and the edit is empty. Mirrors the Rust `LiveRanges::apply`. */ - apply(update: SubscribeUpdate): readonly { id: bigint; range: WireRange }[] { - if (!hasRanges(this.#version)) return []; - for (const id of update.remove ?? []) { + apply(update: SubscribeUpdate): Applied { + if (!hasRanges(this.#version)) return { retired: [], added: [] }; + const retired = update.remove ?? []; + for (const id of retired) { // An id never assigned, or already retired (including earlier in this list). if (!this.#remove(id)) throw new ProtocolViolation(`unknown range id: ${id}`); } - this.#add(update.ranges ?? []); + const first = this.#next; + const add = update.ranges ?? []; + this.#add(add); // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE // could carry like one too large to send. if (this.#live.size === 0) throw new ProtocolViolation("a subscription asks for no range"); if (this.#size > Message.MAX_MESSAGE_SIZE) throw new ProtocolViolation("too many ranges"); - this.#snapshot ??= [...this.#live].map(([id, range]) => ({ id, range })); - return this.#snapshot; + return { retired: [...retired], added: add.map((range, index) => [first + BigInt(index), range]) }; } /** diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 61ccfadff1..4ce0b398d0 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -85,7 +85,7 @@ import { EMPTY_RANGE, inclusiveGroupEnd, LiveRanges, - requestedRanges, + requestedRange, Subscribe, SubscribeUpdate, toWireRanges, @@ -2689,7 +2689,7 @@ export class Subscriber { if (!entry.starts.has(id)) return false; const at = entry.starts.get(id); if (at === undefined) continue; - const [model] = requestedRanges({ startFrame: 0, ranges: [range] }); + const model = requestedRange(range); owed.push({ ...model, start: { time: at } }); } // Each starts at the group its publisher reported starting it at. diff --git a/js/net/src/priority.ts b/js/net/src/priority.ts index 197e99c75d..c6a83263e9 100644 --- a/js/net/src/priority.ts +++ b/js/net/src/priority.ts @@ -6,6 +6,7 @@ */ import type { Dispose } from "@moq/signals"; +import { hooks } from "./internal.ts"; import type { Writer } from "./stream.ts"; import type * as track from "./track.ts"; @@ -110,12 +111,12 @@ export class Priority { constructor(track: track.Subscriber, publisher = 0) { this.#track = track; this.#publisher = publisher; - this.#dispose = track.subscription.subscribe(() => this.#rerank()); + this.#dispose = hooks.preferences(track).revision.subscribe(() => this.#rerank()); } /** The send order for a stream starting at this group and object, before it opens. */ rank(key: bigint, object: number): number { - const subscription = this.#track.subscription.peek(); + const subscription = hooks.preferences(this.#track).scalars(); return sendOrder({ priority: subscription?.priority ?? 0, publisher: this.#publisher, @@ -179,7 +180,7 @@ export class Priority { // Sorted once, so ranking every stream costs one sort rather than a scan per stream: a // stream's position is how many sort strictly ahead of it. #rerank() { - const subscription = this.#track.subscription.peek(); + const subscription = hooks.preferences(this.#track).scalars(); const priority = subscription?.priority ?? 0; const order = subscription?.order ?? "descending"; const queue = [...this.#streams].sort(([, a], [, b]) => compareStarts(a, b, order)); diff --git a/js/net/src/ranges.test.ts b/js/net/src/ranges.test.ts new file mode 100644 index 0000000000..0252b598f1 --- /dev/null +++ b/js/net/src/ranges.test.ts @@ -0,0 +1,118 @@ +import { expect, test } from "bun:test"; +import { Counts, Cover } from "./cover.ts"; +import { Ranges } from "./ranges.ts"; +import type { Anchor, Range } from "./track.ts"; +import { max, min, UNBOUNDED } from "./util/key.ts"; +import { union } from "./window.ts"; + +// A seeded generator, so a failure reproduces. +function rng(seed: number): (below: number) => number { + let state = seed >>> 0; + return (below) => { + state = (state + 0x6d2b79f5) >>> 0; + let t = state; + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + return (((t ^ (t >>> 14)) >>> 0) / 2 ** 32) * below; + }; +} + +// Each change reports exactly whether the union moved, and the union matches a full fold of what +// is held. +test("the incremental union matches a full fold", () => { + const random = rng(0x72616e67); + const int = (below: number) => Math.floor(random(below)); + const anchor = (): Anchor | undefined => { + const kind = int(6); + if (kind === 0) return { relative: BigInt(int(4)) }; + if (kind === 1) return undefined; + return { time: BigInt(int(12)) }; + }; + const ranges = new Ranges(); + const held: Range[] = []; + for (let step = 0; step < 50_000; step++) { + const grow = held.length < 24 ? 0.65 : 0.35; + const before = union(held); + let changed: boolean; + if (held.length === 0 || random(1) < grow) { + const range: Range = {}; + const start = anchor(); + if (start) range.start = start; + const end = random(1) < 0.6 ? anchor() : undefined; + if (end) range.end = end; + held.push(range); + changed = ranges.add(range); + } else { + const index = int(held.length); + const [range] = held.splice(index, 1); + changed = ranges.remove(range); + } + const after = union(held); + expect(ranges.union()).toEqual(after); + expect(changed).toBe(!Bun.deepEquals(before, after)); + } +}); + +// Every query matches a count kept per key. +test("a cover answers as its counts do", () => { + const random = rng(0x636f76); + const int = (below: number) => Math.floor(random(below)); + const keys = 40; + const cover = new Cover(); + const counts = new Array(keys + 1).fill(0); + const held: [number, number][] = []; + for (let step = 0; step < 5_000; step++) { + if (held.length === 0 || random(1) < 0.55) { + const start = int(keys); + // The last key stands for open. + const end = start + 1 + int(keys - start); + held.push([start, end]); + for (let key = start; key < (end === keys ? keys + 1 : end); key++) counts[key]++; + cover.add(BigInt(start), end === keys ? UNBOUNDED : BigInt(end), 1); + } else { + const [[start, end]] = held.splice(int(held.length), 1); + for (let key = start; key < (end === keys ? keys + 1 : end); key++) counts[key]--; + cover.add(BigInt(start), end === keys ? UNBOUNDED : BigInt(end), -1); + } + const key = (index: number) => (index >= keys ? UNBOUNDED : BigInt(index)); + const runs: { start: bigint; end: bigint }[] = []; + for (let index = 0; index < keys; index++) { + if (counts[index] === 0) continue; + const last = runs.at(-1); + if (last && last.end === BigInt(index)) last.end = key(index + 1); + else runs.push({ start: BigInt(index), end: key(index + 1) }); + } + const from = int(keys); + const to = from + int(keys - from + 1); + const clipped = runs + .filter((run) => run.end > BigInt(from) && run.start < key(to)) + .map((run) => ({ + start: max(run.start, BigInt(from)), + end: min(run.end, key(to)), + })) + .filter((run) => run.start < run.end); + expect(cover.runs(BigInt(from), key(to))).toEqual(clipped); + expect(cover.contains(BigInt(from))).toBe(counts[from] > 0); + expect(cover.next(BigInt(from))).toEqual( + runs.find((run) => run.end > BigInt(from)) && + (counts[from] > 0 ? BigInt(from) : runs.find((run) => run.start > BigInt(from))?.start), + ); + expect(cover.lowest()).toEqual(runs[0]?.start); + expect(cover.end()).toBe(runs.at(-1)?.end ?? 0n); + expect(cover.openFrom()).toEqual(runs.at(-1)?.end === UNBOUNDED ? runs.at(-1)?.start : undefined); + const least = Math.min(...counts.slice(from, Math.max(to, from + 1))); + expect(cover.least(BigInt(from), key(Math.max(to, from + 1)))).toBe(least); + } +}); + +test("counts track their lowest and highest key", () => { + const counts = new Counts(); + expect(counts.add(3)).toBe(true); + expect(counts.add(3)).toBe(false); + expect(counts.add(-1)).toBe(true); + expect([counts.first(), counts.last()]).toEqual([-1, 3]); + expect(counts.remove(3)).toBe(false); + expect(counts.remove(3)).toBe(true); + expect(counts.remove(3)).toBe(false); + expect([counts.first(), counts.last()]).toEqual([-1, -1]); +}); diff --git a/js/net/src/ranges.ts b/js/net/src/ranges.ts new file mode 100644 index 0000000000..d6fe86a034 --- /dev/null +++ b/js/net/src/ranges.ts @@ -0,0 +1,161 @@ +/** + * The union of every subscriber's ranges, kept current per change rather than refolded. Mirrors + * the Rust `demand::Ranges`. + * + * Time ranges count as runs of group starts in a {@link Cover}, so a change touches two boundaries + * and asks one range-minimum question, whatever else the audience holds. A start between groups + * stands for the group containing it, which a range starting at or before it covers too, so spans + * merge as they do over starts. Ranges anchored to the latest group can't be placed on that line + * and are counted by value instead. + * + * @module + */ + +import { Counts, Cover } from "./cover.ts"; +import type { Range } from "./track.ts"; +import { UNBOUNDED } from "./util/key.ts"; +import { compare, isEmpty, rangeKey } from "./window.ts"; + +/** How {@link Ranges} counts one range. */ +type Shape = + | { kind: "empty" } + | { kind: "oldest" } + /** A time range, as the exclusive span of starts it covers, {@link UNBOUNDED} when open. */ + | { kind: "time"; start: bigint; end: bigint } + | { kind: "relative"; back: bigint } + | { kind: "mixed" }; + +function shapeOf(range: Range): Shape { + if (isEmpty(range)) return { kind: "empty" }; + const { start, end } = range; + if (start === undefined) return end === undefined ? { kind: "oldest" } : { kind: "mixed" }; + if (start.time !== undefined) { + if (end === undefined) return { kind: "time", start: start.time, end: UNBOUNDED }; + // An end at the last start covers every group, as an open one does. + if (end.time !== undefined) return { kind: "time", start: start.time, end: end.time + 1n }; + return { kind: "mixed" }; + } + return end === undefined ? { kind: "relative", back: start.relative } : { kind: "mixed" }; +} + +/** Every subscriber's ranges, counted so that one change costs O(log n). */ +export class Ranges { + // Holders of the open oldest range, which covers every other range. + #oldest = 0; + // Time ranges, as spans of starts. + #time = new Cover(); + // Open relative ranges by how far back they start: only the farthest matters. + #relative = new Counts(); + // Ranges with one relative end, or a bounded relative span, kept as they are. + #mixed = new Map(); + // The last union read, until a change moves it. + #union?: Range[]; + + /** Count `range`, returning whether the union changed. */ + add(range: Range): boolean { + const shape = shapeOf(range); + let changed = false; + switch (shape.kind) { + case "empty": + break; + case "oldest": + changed = ++this.#oldest === 1; + break; + case "time": { + const uncovered = this.#time.least(shape.start, shape.end) <= 0; + this.#time.add(shape.start, shape.end, 1); + changed = uncovered && this.#oldest === 0; + break; + } + case "relative": { + const farthest = this.#relative.last(); + this.#relative.add(shape.back); + changed = this.#oldest === 0 && this.#relative.last() !== farthest; + break; + } + case "mixed": { + const key = rangeKey(range); + const held = this.#mixed.get(key); + if (held) held.count++; + else this.#mixed.set(key, { range, count: 1 }); + changed = !held && this.#oldest === 0 && !this.#covered(range); + break; + } + } + return this.#moved(changed); + } + + /** Uncount `range`, one {@link add} counted, returning whether the union changed. */ + remove(range: Range): boolean { + const shape = shapeOf(range); + let changed = false; + switch (shape.kind) { + case "empty": + break; + case "oldest": + changed = --this.#oldest === 0; + break; + case "time": + this.#time.add(shape.start, shape.end, -1); + changed = this.#oldest === 0 && this.#time.least(shape.start, shape.end) <= 0; + break; + case "relative": { + const farthest = this.#relative.last(); + this.#relative.remove(shape.back); + changed = this.#oldest === 0 && this.#relative.last() !== farthest; + break; + } + case "mixed": { + const key = rangeKey(range); + const held = this.#mixed.get(key); + if (!held) break; + const last = --held.count === 0; + if (last) this.#mixed.delete(key); + changed = last && this.#oldest === 0 && !this.#covered(range); + break; + } + } + return this.#moved(changed); + } + + // Drop the cached union when `changed`, passing it through. + #moved(changed: boolean): boolean { + if (changed) this.#union = undefined; + return changed; + } + + /** The union in a canonical order, costing its own length rather than the audience's. Read only. */ + union(): readonly Range[] { + this.#union ??= this.#fold(); + return this.#union; + } + + #fold(): Range[] { + if (this.#oldest > 0) return [{}]; + const union: Range[] = this.#time + .runs() + .map((run) => + run.end === UNBOUNDED + ? { start: { time: run.start } } + : { start: { time: run.start }, end: { time: run.end - 1n } }, + ); + for (const { range } of this.#mixed.values()) if (!this.#covered(range)) union.push(range); + const farthest = this.#relative.last(); + if (farthest !== undefined) union.push({ start: { relative: farthest } }); + return union.sort(compare); + } + + // Whether an open range already delivers everything a mixed `range` could. + #covered(range: Range): boolean { + const start = range.start; + // Only the open oldest range, which hides every other, covers the oldest group. + if (start === undefined) return false; + if (start.time !== undefined) { + // The group containing a start at or before this one's comes no later. + const tail = this.#time.openFrom(); + return tail !== undefined && tail <= start.time; + } + const farthest = this.#relative.last(); + return farthest !== undefined && start.relative <= farthest; + } +} diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index bc70a80ed3..d925383297 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -6,13 +6,21 @@ import { DEFAULT_EXPIRY } from "./cache.ts"; import { ProtocolViolation, Skipped, StreamCode, StreamError, TimestampMismatch, TooFarBehind } from "./error.ts"; import type { Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; import { Version as IetfVersion } from "./ietf/version.ts"; -import { hooks, skipHole, staleHole } from "./internal.ts"; +import { hooks, maxDelayOf, skipHole, staleHole } from "./internal.ts"; import { textFrame } from "./mock.ts"; import { rescale, rescaleIn } from "./rescale.ts"; import { Timescale, Timestamp } from "./time.ts"; -import { authoredInfo, Subscription, Producer as TrackProducer } from "./track.ts"; +import { + type Anchor, + authoredInfo, + type Delay, + type Order, + type Range, + Subscription, + Producer as TrackProducer, +} from "./track.ts"; import { unwanted } from "./util/demand.ts"; -import { LIVE, rangesStart } from "./window.ts"; +import { LIVE, rangesStart, union } from "./window.ts"; const enc = new TextEncoder(); const dec = new TextDecoder(); @@ -3497,7 +3505,8 @@ test("a late subscriber to a closed unlimited track is released once the cache a producer.close(); // Counts the map entries subscribing leaves behind, rather than the heap: any entry - // keyed by the subscriber that outlives the cache window would pin it. + // keyed by the subscriber that outlives the cache window would pin it. Only an object key + // can be, so the subscriber's own ranges, keyed by number, don't count. const added: [Map, unknown][] = []; const set = Map.prototype.set; const spy = spyOn(Map.prototype, "set").mockImplementation(function (this: Map, key, value) { @@ -3519,7 +3528,7 @@ test("a late subscriber to a closed unlimited track is released once the cache a // A closed track protects nothing, so the next scan past the window reclaims it all. clock.set(10_000 + CACHE_WINDOW_MS + 200); producer.subscribe().close(); - expect(added.filter(([map, key]) => map.has(key)).length).toBe(0); + expect(added.filter(([map, key]) => typeof key === "object" && map.has(key)).length).toBe(0); } finally { clock.installed.close(); } @@ -5657,3 +5666,125 @@ test("track: a reader's unplaced times are the times its holdings can't place", producer.close(); } }); + +// The aggregate as it was folded from every subscriber before it was counted, kept to check the +// counted one against: Rust's Subscription::poll_combined. +function foldSubscriptions(subscriptions: Iterable): Subscription | undefined { + const largest = (a: Duration | null | undefined, b: Duration | null | undefined): Duration | null => { + if (a === null || a === undefined) return b ?? null; + if (b === null || b === undefined) return a; + return a.max(b); + }; + let combined: Subscription | undefined; + for (const subscription of subscriptions) { + if (!subscription) continue; + if (!combined) { + combined = { ...subscription }; + continue; + } + combined.priority = Math.max(combined.priority ?? 0, subscription.priority ?? 0); + combined.delay = { + max: Duration.millis(Math.max(maxDelayOf(combined), maxDelayOf(subscription))), + min: largest(combined.delay?.min, subscription.delay?.min), + }; + combined.order = + combined.order === "ascending" && subscription.order === "ascending" ? "ascending" : "descending"; + combined.ranges = [...(combined.ranges ?? []), ...(subscription.ranges ?? [])]; + } + if (combined) combined.ranges = union(combined.ranges ?? []); + return combined; +} + +// Joins, whole replacements, edits by key, closes, and fetches each move the counted aggregates +// to exactly what folding every subscriber gives. +test("the counted aggregates match a full fold of every subscriber", async () => { + 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; + }; + const anchor = (): Anchor | undefined => { + const kind = random(6); + if (kind === 0) return { relative: BigInt(random(4)) }; + if (kind === 1) return undefined; + return { time: BigInt(random(24)) }; + }; + const range = (): Range => { + const out: Range = {}; + const start = anchor(); + if (start) out.start = start; + const end = random(3) === 0 ? undefined : anchor(); + if (end) out.end = end; + return out; + }; + const delay = (): Delay => ({ + max: random(5) === 0 ? null : Duration.millis(random(4) * 100), + min: random(3) === 0 ? null : Duration.millis(random(4) * 50), + }); + const order = (): Order => (random(2) === 0 ? "ascending" : "descending"); + type Held = { + subscriber: ReturnType; + keys: Map; + next: bigint; + fetch: boolean; + }; + for (let round = 0; round < 200; round++) { + const producer = new TrackProducer("test"); + const held: Held[] = []; + for (let step = 0; step < 60; step++) { + const op = held.length === 0 ? 0 : random(8); + const pick = held[random(held.length)]; + if (op === 0 && held.length < 6) { + const ranges = Array.from({ length: random(6) }, range); + const subscriber = producer.subscribe({ priority: random(4), order: order(), delay: delay(), ranges }); + held.push({ + subscriber, + keys: new Map(ranges.map((r, i) => [BigInt(i), r])), + next: BigInt(ranges.length), + fetch: false, + }); + } else if (op === 1 && !pick.fetch) { + const ranges = Array.from({ length: random(6) }, range); + const before = [...pick.keys.values()]; + pick.subscriber.update({ priority: random(4), order: order(), delay: delay(), ranges }); + // A whole replacement rekeys by position only when the ranges change. + if (!Bun.deepEquals(before, ranges)) { + pick.keys = new Map(ranges.map((r, i) => [BigInt(i), r])); + if (pick.next < BigInt(ranges.length)) pick.next = BigInt(ranges.length); + } + } else if (op <= 4 && !pick.fetch) { + const retire: bigint[] = []; + const add: [bigint, Range][] = []; + for (const key of pick.keys.keys()) if (random(3) === 0) retire.push(key); + for (const key of pick.keys.keys()) if (random(5) === 0) add.push([key, range()]); + for (let count = random(3); count > 0; count--) add.push([pick.next++, range()]); + const readded = new Set(add.map(([key]) => key)); + for (const key of retire) if (!readded.has(key)) pick.keys.delete(key); + for (const [key, r] of add) pick.keys.set(key, r); + hooks.editSubscriber(pick.subscriber, { + retire, + add, + priority: random(4), + order: order(), + delay: delay(), + }); + } else if (op === 5 && !pick.fetch) { + hooks.fetchBudget(pick.subscriber, Duration.millis(random(4) * 100)); + pick.fetch = true; + } else if (op >= 6) { + pick.subscriber.close(); + held.splice(held.indexOf(pick), 1); + } + // A close leaves the aggregates once its signal fires. + await yieldNow(); + for (const { subscriber, keys } of held) + expect(subscriber.subscription.peek()?.ranges).toEqual([...keys.values()]); + const all = held.map(({ subscriber }) => subscriber.subscription.peek()); + const live = held.filter(({ fetch }) => !fetch).map(({ subscriber }) => subscriber.subscription.peek()); + expect(hooks.requested(producer).peek()).toEqual(foldSubscriptions(all)); + expect(producer.subscription.peek()).toEqual(foldSubscriptions(live)); + } + producer.close(); + } +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 37ecd48b37..6741b11d08 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -8,6 +8,7 @@ import { Duration, Instant, now, sleepUntil } from "@moq/clock"; import { Derived, type Dispose, type GetPromise, type Getter, Once, Signal } from "@moq/signals"; import * as cache from "./cache.ts"; import { Delivered } from "./delivered.ts"; +import { type Change, type Edit, Prefs, Subscriptions } from "./demand.ts"; import { DropSet, Runs } from "./drops.ts"; import { GroupTooLarge, @@ -56,18 +57,7 @@ import { rescale } from "./rescale.ts"; import { floorAt, Timescale, type Timestamp } from "./time.ts"; import { checkEnd, checkKey, max, min, UNBOUNDED } from "./util/key.ts"; import { checkName } from "./util/utf8.ts"; -import { - bounded, - type Holdings, - isWire, - joinsLive, - rangesOf, - sameRanges, - UNHELD, - union, - Window, - wireSpan, -} from "./window.ts"; +import { bounded, type Holdings, joinsLive, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; import { type Broadcast as BroadcastWire, registerTrackConsumer } from "./wire.ts"; export { type Fill, FillGroups, FillLookup, Requests } from "./fill.ts"; @@ -487,41 +477,6 @@ function orderOf(order: Order | undefined): Order { return order; } -// The larger of two minimums, ignoring one that went unstated (`null`). -function largest(a: Duration | null | undefined, b: Duration | null | undefined): Duration | null { - if (a === null || a === undefined) return b ?? null; - if (b === null || b === undefined) return a; - return a.max(b); -} - -// Aggregate the preferences of `states`, matching Rust's Subscription::poll_combined. -function combineSubscriptions(states: Iterable): Subscription | undefined { - let combined: Subscription | undefined; - for (const state of states) { - const subscription = state.update.peek(); - if (!subscription) continue; - if (!combined) { - combined = { ...subscription }; - continue; - } - - combined.priority = Math.max(combined.priority ?? 0, subscription.priority ?? 0); - combined.delay = { - max: Duration.millis(Math.max(maxDelayOf(combined), maxDelayOf(subscription))), - // The largest stated, so an upstream starts far enough back for the reader that buffers most. - min: largest(combined.delay?.min, subscription.delay?.min), - }; - // One live subscriber held behind another's backlog is worse than a backlog sent late. - combined.order = - combined.order === "ascending" && subscription.order === "ascending" ? "ascending" : "descending"; - - // A range above the live edge never hides the latest group from a merged live subscriber. - combined.ranges = [...(combined.ranges ?? []), ...(subscription.ranges ?? [])]; - } - if (combined) combined.ranges = union(combined.ranges ?? []); - return combined; -} - /** * A request for a track the peer wants, delivered to the publishing wire layer. * @@ -865,7 +820,7 @@ class TrackState { // The producer's outcome, settled by its close or by a reached and settled end, whichever // is first. Only the producer's own state uses it. outcome = new Once(); - update: Signal; + update: Prefs; /** Resolved once the producer commits the immutable properties. */ info = new Signal(undefined); /** Newest group head, frame, or location reported by the publisher. */ @@ -880,7 +835,7 @@ class TrackState { largestFinished = false; constructor(subscription?: Subscription) { - this.update = new Signal(subscription === undefined ? undefined : subscriptionDefaults(subscription)); + this.update = new Prefs(subscription === undefined ? undefined : subscriptionDefaults(subscription)); } } @@ -1032,9 +987,9 @@ let reofferCached: ( let reofferKeys: (producer: Producer, sink: TrackState, keys: readonly bigint[]) => void; // Whether a group may still land on the producer's live feed; assigned in its static block. let mayLand: (producer: Producer, land: Land) => boolean; -// Recompute the producer's aggregates now, for a sink that just became a fetch; assigned in its +// Take a sink that just became a fetch out of the producer's live aggregate; assigned in its // static block. -let resubscribe: (producer: Producer) => void; +let unlive: (producer: Producer, sink: TrackState) => void; // Where a session's live feed starts owing groups, if known: it owes none below. let feedFloor: (producer: Producer) => bigint | undefined; let owedRuns: (producer: Producer, start: bigint, end: bigint) => Iterable; @@ -1291,10 +1246,14 @@ export class Producer { // `max_sequence`, and where the next append lands. #received = 0n; #subscribing = new Signal(false); + // The aggregate across every subscriber, fetches included: what the wire asks for. + #requested = new Subscriptions(); // The aggregate across live subscribers only. A fetch's preferences judge just the group it // asks for, so a publisher sizing what it serves (such as how much retained media to replay) - // must not see them. The wire's aggregate, `#state.update`, keeps them. - #live = new Signal(undefined); + // must not see them. The wire's aggregate, `#requested`, keeps them. + #live = new Subscriptions(); + // The sinks counted in `#live`. + #counted = new Set(); // Recently written source groups, in arrival order, retained for replay to late subscribers // until the cache pool evicts them once idle. A set, so evicting a run of @@ -1511,8 +1470,8 @@ export class Producer { producer.#account = producer.#openAccount(pool); }; hooks.subscribing = (producer) => producer.#subscribing; - hooks.requested = (producer) => producer.#state.update; - resubscribe = (producer) => producer.#updateSubscription(); + hooks.requested = (producer) => producer.#requested; + unlive = (producer, sink) => producer.#unlive(sink); hooks.cacheTrack = (producer) => { producer.#wire = undefined; producer.#placing++; @@ -1809,7 +1768,9 @@ export class Producer { this.#state.info.set(resolved); // Propagate to any sink handed out before accept (the on-demand path). for (const sink of this.#sinks) sink.info.set(resolved); - this.#updateSubscription(); + // The info bounds the tail's grace, which judges what it still owes. + this.#settle(); + this.#owe(); return this; } @@ -2010,10 +1971,9 @@ export class Producer { this.#sinks.add(sink); this.#reportWanted(); - // Forward subscription updates from the sink's Subscriber to the producer's own - // state, which the wire layer (or the serving application) watches. - const forward = sink.update.subscribe(() => this.#updateSubscription()); - this.#updateSubscription(); + // Count the sink's preferences into the aggregates, which the wire layer (or the serving + // application) watches, and move them by each change it makes. + this.#join(sink); // Drop the sink once its consumer goes away, closing its mirrors so source // groups stop teeing into them, so a long-lived producer doesn't leak. This @@ -2022,10 +1982,9 @@ export class Producer { const dispose = sink.closed.subscribe((c) => { if (c === undefined) return; const abort = c instanceof Error ? c : undefined; - forward(); this.#sinks.delete(sink); if (sink.asker) this.#fills.leave(sink.asker); - this.#updateSubscription(); + this.#leave(sink); // Update demand: once the last subscriber leaves, the consumer wire (watching // {@link Demand.wanted}) tears the upstream down instead of downloading to nobody. this.#reportWanted(); @@ -2060,21 +2019,48 @@ export class Producer { } } - // Recompute from every sink because an update or close can narrow as well as widen the - // aggregates. The wire layer observes `#state.update` and emits SUBSCRIBE_UPDATE. + // Count `sink`'s preferences into the aggregates, then move them by each change it makes, so a + // change costs what it moved rather than every subscriber's ranges. The wire layer observes + // `#requested` and emits SUBSCRIBE_UPDATE. // // The max delay is the subscribers' own, not clamped to the track's maxAge: the publisher // applies its own, as each reader's budget does, so the demand asked upstream is the same // before and after the info lands. - #updateSubscription(): void { - const live = [...this.#sinks].filter((sink) => !sink.fetch); - this.#subscribing.set(live.some((sink) => sink.closed.peek() === undefined)); - const combined = combineSubscriptions(this.#sinks); + #join(sink: TrackState): void { + this.#moved(sink, { after: sink.update.contribution(), gone: [], came: [...sink.update.ranges()] }, true); + sink.update.watch = (change) => this.#moved(sink, change); + this.#subscribed(); + } + + // Take `sink` out of the aggregates. + #leave(sink: TrackState): void { + sink.update.watch = undefined; + this.#moved(sink, { before: sink.update.contribution(), gone: [...sink.update.ranges()], came: [] }); + this.#counted.delete(sink); + this.#subscribed(); + } + + // Take a sink that became a fetch out of the live aggregate, before anyone sees it there. + #unlive(sink: TrackState): void { + if (!this.#counted.delete(sink)) return; + this.#live.replace({ before: sink.update.contribution(), gone: [...sink.update.ranges()], came: [] }); + this.#subscribed(); + } + + // Move the aggregates by `sink`'s `change`, counting it live when `joining` a live sink. + #moved(sink: TrackState, change: Change, joining = false): void { + // Settled before the demand moves, which may reopen the outcome. this.#settle(); - this.#state.update.set(combined); - this.#live.set(combineSubscriptions(live)); + const requested = this.#requested.replace(change); + if (joining && !sink.fetch) this.#counted.add(sink); + const live = this.#counted.has(sink) && this.#live.replace(change); // The requested floor bounds what a pending tail owes. - this.#owe(); + if (requested || live) this.#owe(); + } + + // Whether a reader beyond one-shot fetches subscribes. + #subscribed(): void { + this.#subscribing.set([...this.#counted].some((sink) => sink.closed.peek() === undefined)); } // Mirror a cached source group into a sink. The mirror fills synchronously as the @@ -2645,8 +2631,7 @@ export class Producer { // The grace the session's tail uses, from the max delay the track's subscribers ask for, // bounded by how long the publisher keeps a superseded group fresh. The demand asked upstream is not. #grace(): Duration { - const update = this.#state.update.peek(); - const maxDelay = Duration.millis(update ? maxDelayOf(update) : 0); + const maxDelay = Duration.millis(this.#requested.maxDelay()); const maxAge = this.#state.info.peek()?.maxAge; return grace(maxAge == null ? maxDelay : maxDelay.min(maxAge)); } @@ -2677,7 +2662,7 @@ export class Producer { // back restores the START. Without either, the first arrival stands in for it. // Until the START arrives, the feed start is pending; the requested floor still bounds it. #feedStart(): bigint | "pending" | undefined { - const ranges = this.#state.update.peek()?.ranges; + const ranges = this.#requested.peek()?.ranges; const requested = ranges && wireSpan(ranges).start; const served = this.#wire?.served; if (!served || served.requested !== requested) return requested; @@ -2700,7 +2685,7 @@ export class Producer { if (start === undefined || start === 0n) return undefined; if (wire.starts.fillsFromStart) return start; // The START applies only while the demand keeps the floor it answered, as `#feedStart`. - const ranges = this.#state.update.peek()?.ranges; + const ranges = this.#requested.peek()?.ranges; const served = wire.served; const resolved = wire.starts.resolvesStart === true && @@ -3156,7 +3141,7 @@ export class Subscriber { break; } - const update = this.#state.update.peek(); + const update = this.#state.update.scalars(); const requested = this.#fetchBudget ?? (update ? maxDelayOf(update) : 0); const fresh = info?.maxAge?.asMillis(); return { @@ -3396,7 +3381,7 @@ export class Subscriber { this.#state.groups, this.#state.timelineChanged, this.#wall.revision, - this.#state.update, + this.#state.update.revision, this.#state.info, this.#cursor, this.#state.closed, @@ -3762,8 +3747,7 @@ export class Subscriber { // one exists, and a time start as groups arrive. Returns the refusal of ranges this copy cannot // resolve, which every read reports. #settle(): Error | undefined { - const ranges = this.#window.ranges; - if (this.#state.dropsAnchors.peek() && !ranges.every(isWire)) { + if (this.#state.dropsAnchors.peek() && !this.#window.wired) { return new Error("a relative or time anchor other than the live start needs moq-lite-07"); } if (this.#resume || this.#window.settled) return undefined; @@ -3872,9 +3856,9 @@ export class Subscriber { #askerOf(): Asker { this.#state.asker ??= { wants: (start, end) => this.#wants(start, end), - priority: () => this.#state.update.peek()?.priority ?? 0, + priority: () => this.#state.update.scalars()?.priority ?? 0, maxDelay: () => this.#drift().budget, - order: () => this.#state.update.peek()?.order ?? "descending", + order: () => this.#state.update.scalars()?.order ?? "descending", refuse: (start, end, error, stale) => { // A budget's verdict is no eviction: a widening asks it afresh, never through the // one refill an evicted answer is allowed, as the Rust `Misses::resolve` retries it. @@ -4315,6 +4299,25 @@ export class Subscriber { subscriber.#settle(); return subscriber.#window.resolved(); }; + hooks.servingResolvedKeys = (subscriber, keys) => { + if (keys.length === 0) return []; + const resumed = subscriber.#resume?.resolved(); + if (resumed) { + // A reader following route copies resolves its ranges by position, in key order, so + // they are built and walked once for every key asked. + const at = new Map(); + let index = 0; + for (const [held] of subscriber.#state.update.entries()) at.set(held, index++); + return keys.map((key) => { + const index = at.get(key); + return index === undefined ? undefined : resumed[index]; + }); + } + subscriber.#settle(); + return keys.map((key) => subscriber.#window.resolvedKey(key)); + }; + hooks.editSubscriber = (subscriber, edit) => subscriber.#edit(edit); + hooks.preferences = (subscriber) => subscriber.#state.update; hooks.resolveRanges = (subscriber, subscription) => new Window( subscription.ranges ?? [{}], @@ -4485,9 +4488,8 @@ export class Subscriber { // Its group resolves by start now; see `#holdings`. subscriber.#resolve(); // The sink joined as a live subscriber; leave the live aggregate before anyone sees it. - if (subscriber.#state.producer) resubscribe(subscriber.#state.producer); - const update = subscriber.#state.update.peek(); - if (update) subscriber.#state.update.set(update, true); + if (subscriber.#state.producer) unlive(subscriber.#state.producer, subscriber.#state); + if (subscriber.#state.update.scalars()) subscriber.#state.update.touch(); }; // The key cursor lives here (it shares the buffer and the drift anchor with // the arrival cursor); `Ordered` is the handle that reaches it. @@ -5137,7 +5139,7 @@ export class Subscriber { this.#state.dropsAnchors.changed(fn), this.#cursor.changed(fn), // A widened budget re-offers the groups it skipped as stale. - this.#state.update.changed(fn), + this.#state.update.revision.changed(fn), this.#state.closed.changed(fn), this.#state.end.changed(fn), this.#state.received.changed(fn), @@ -5655,6 +5657,28 @@ export class Subscriber { return producer ? hooks.requestProducer(producer, options?.signal) : Promise.resolve(); } + // Edit the ranges by key, as `hooks.editSubscriber` does: only the ranges it adds resolve, and + // every other stays where it resolved. Mirrors the Rust `Subscriber::edit`. + #edit(edit: Edit): void { + const { ranges: _, ...scalars } = subscriptionDefaults({ + priority: edit.priority, + delay: edit.delay, + order: edit.order, + ranges: [], + }) as Required; + const add = edit.add.map(([key, range]) => [key, rangesOf([range])[0]] as const); + const reordered = scalars.order !== this.#state.update.scalars()?.order; + const moved = this.#state.update.edit({ retire: edit.retire, add, ...scalars }); + // A live join not yet started reaches back by the Min Age stated when it starts. + this.#window.min = scalars.delay.min ?? undefined; + if (moved) { + this.#window.edit(edit.retire, add, this.#latestGroup(), this.#holdings()); + // Leaves the lookups the retired ranges asked, which a settled window never reads again. + if (this.#lookups.length > 0) this.#lookUp(); + this.#moved(); + } else if (reordered) this.#fillsMoved(); + } + /** * Update this subscription's options (e.g. priority), triggering a SUBSCRIBE_UPDATE to the * publisher. Replacing the ranges resolves them again, without redelivering groups already @@ -5664,9 +5688,8 @@ export class Subscriber { */ update(options: Subscription) { const subscription = subscriptionDefaults(options); - const replaced = !sameRanges(subscription.ranges ?? [], this.#window.ranges); - const reordered = subscription.order !== this.#state.update.peek()?.order; - this.#state.update.set(subscription); + const reordered = subscription.order !== this.#state.update.scalars()?.order; + const replaced = this.#state.update.set(subscription); if (replaced) this.#resolve(); // A live join not yet started reaches back by the Min Age stated when it starts. else this.#window.min = subscription.delay?.min ?? undefined; diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 1128221166..131e5207e6 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -595,3 +595,50 @@ test("window: settling by steps matches trying every start and rebuilding from t } } }); + +test("window: an edit by key matches building the edited ranges afresh", () => { + let seed = 23; + 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 any = (): Range => { + const kind = random(6); + const start = kind === 0 ? relative(BigInt(random(6))) : kind === 1 ? undefined : time(BigInt(random(40))); + const end = + random(3) === 0 ? undefined : random(5) === 0 ? relative(BigInt(random(4))) : time(BigInt(random(48))); + return range(start, end); + }; + const seen = (window: Window) => ({ + ranges: window.ranges, + 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, window.wired], + }); + for (let round = 0; round < 2000; round++) { + // Fixed holdings, so ranges left alone resolve as a fresh build resolves them. + const keys = new Keys(Array.from({ length: random(8) }, () => BigInt(random(48)))); + keys.looks = random(2) === 0; + const latest = [...keys.held].sort(ascending).at(-1); + const held = new Map(); + let next = 0n; + for (let count = random(8); count > 0; count--) held.set(next++, any()); + const window = Window.keyed(held, latest, keys); + for (let step = random(16); step > 0; step--) { + const retire: bigint[] = []; + const add: [bigint, Range][] = []; + for (const key of held.keys()) if (random(4) === 0) retire.push(key); + for (const key of held.keys()) if (random(6) === 0) add.push([key, any()]); + for (let count = random(3); count > 0; count--) add.push([next++, any()]); + const readded = new Set(add.map(([key]) => key)); + for (const key of retire) if (!readded.has(key)) held.delete(key); + for (const [key, range] of add) held.set(key, range); + window.edit(retire, add, latest, keys); + expect(seen(window)).toEqual(seen(Window.keyed(held, latest, keys))); + } + } +}); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 670b473034..a8b90bb0fe 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -10,8 +10,10 @@ */ import type { Duration } from "@moq/clock"; +import { Counts, Cover } from "./cover.ts"; import type { Anchor, Range } from "./track.ts"; import { max, min, UNBOUNDED } from "./util/key.ts"; +import { Sorted } from "./util/sorted.ts"; /** A run of group starts `[start, end)`; an open run ends at {@link UNBOUNDED}. */ export type Run = { start: bigint; end: bigint }; @@ -87,7 +89,7 @@ function anchorKey(anchor: Anchor | undefined, none: [number, bigint]): [number, * A canonical order, as the Rust `Range` sorts: by start, relative ones first, then the oldest, * then time ones; then by end, open first. */ -function compare(a: Range, b: Range): number { +export function compare(a: Range, b: Range): number { const [x, y] = [anchorKey(a.start, [1, 0n]), anchorKey(b.start, [1, 0n])]; const [p, q] = [anchorKey(a.end, [-1, 0n]), anchorKey(b.end, [-1, 0n])]; return x[0] - y[0] || order(x[1], y[1]) || p[0] - q[0] || order(p[1], q[1]); @@ -103,8 +105,8 @@ export function sameRanges(a: readonly Range[], b: readonly Range[]): boolean { return a.length === b.length && a.every((range, index) => rangeKey(range) === rangeKey(b[index])); } -// A range as a string, equal exactly when the ranges are; JSON can't hold a bigint. -function rangeKey(range: Range): string { +/** A range as a string, equal exactly when the ranges are; JSON can't hold a bigint. */ +export function rangeKey(range: Range): string { return `${anchorText(range.start)}..${anchorText(range.end)}`; } @@ -394,8 +396,6 @@ export const AT_KEYS: Holdings = Object.freeze({ /** One non-empty range, resolved. */ type Span = { - /** Which of the ranges it came from. */ - index: number; /** Where it starts: its range's start, or where a live join's Min Age reached it back to. */ anchor: Anchor | undefined; start: bigint; @@ -415,6 +415,9 @@ type Span = { * held when it was placed, since which of the older ones it reaches is unknown. */ reach?: bigint; + /** What it counts in the window's groups and missable groups, so taking it out uncounts exactly that. */ + reads?: Run; + asks?: Run; }; /** @@ -428,73 +431,138 @@ export interface Lookup { } /** - * A subscriber's ranges resolved to the group starts it reads. Mirrors the Rust `Window`. + * A subscriber's ranges resolved to the group starts it reads, each by the key its caller gave + * it, so an edit resolves only the ranges it adds and leaves every other where it resolved. + * Mirrors the Rust `Window`. * - * A relative or time anchor resolves when the window is built, and again only when the ranges - * are replaced. Before any group exists a relative anchor waits, reading everything, until - * {@link settle} names the first one. A time start the groups held can't place yet waits too, - * reading only the groups from its time meanwhile, since whichever group contains it starts at or - * before it. + * A relative or time anchor resolves when its range is placed, and again only when it is replaced. + * Before any group exists a relative anchor waits, reading everything, until {@link settle} names + * the first one. A time start the groups held can't place yet waits too, reading only the groups + * from its time meanwhile, since whichever group contains it starts at or before it. */ export class Window { - /** What these were resolved from, so only replacing the ranges resolves again. */ - readonly ranges: readonly Range[]; - #spans: Span[] = []; - /** The groups the spans cover: merged, sorted, and disjoint. */ - #runs: Run[] = []; - /** The part of {@link #runs} a miss may ask for: see {@link missable}. */ - #missable: Run[] = []; - /** A relative anchor waits on a first group, so nothing is filtered yet. */ - #pending = false; + // What these were resolved from, by key, in key order. + #ranges = new Map(); + // How many ranges a wire without ranges can't carry; see {@link isWire}. + #unwired = 0; + // Each non-empty range placed, by key. + #spans = new Map(); + // The groups the spans cover, counted so an edit moves only its own runs. + #groups = new Cover(); + // The part of {@link #groups} a miss may ask for: see {@link missable}. + #missable = new Cover(); + // The ranges whose relative anchor waits on a first group, by key: everything is in range meanwhile. + #pending = new Set(); /** - * 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. + * The spans, by key, 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. + * The spans, by key, 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 + * The ranges, by key, 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. */ - #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 }[] = []; + #reaching = new Set(); + // How many spans name each start, and how many name none: see {@link start}. + #named = new Counts(); #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. + * The subscriber's Min Age, which reaches a live join back from the live edge. Only a range + * placed reads it, so a join already started stays where it started. */ min: Duration | undefined; /** - * Resolve `ranges`, counting relative anchors back from `latest` and time anchors from - * `holdings`, and reaching a live join back by `min` (see {@link joins}). + * Resolve `ranges`, keyed by position, counting relative anchors back from `latest` and time + * anchors from `holdings`, and reaching a live join back by `min` (see {@link joins}). */ constructor(ranges: readonly Range[], latest: bigint | undefined, holdings: Holdings = UNHELD, min?: Duration) { - this.ranges = ranges; this.min = min; + for (const [index, range] of ranges.entries()) this.#hold(BigInt(index), range); this.#place(latest, holdings); } + /** {@link Window} for ranges the caller keys itself, in key order. */ + static keyed( + ranges: Iterable, + latest: bigint | undefined, + holdings: Holdings = UNHELD, + min?: Duration, + ): Window { + const window = new Window([], undefined, holdings, min); + for (const [key, range] of ranges) window.#hold(key, range); + window.#place(latest, holdings); + return window; + } + + /** What these were resolved from, in key order. Costs every range. */ + get ranges(): Range[] { + return [...this.#ranges.values()]; + } + + /** Whether a wire without ranges carries every range; see {@link isWire}. */ + get wired(): boolean { + return this.#unwired === 0; + } + + /** + * Retire the ranges keyed `retire`, then resolve `add`, leaving every other range where it + * resolved. A key added again replaces its range in place; any other key added is past every + * key held. Costs the ranges edited, never the rest. Mirrors the Rust `Window::edit`. + */ + edit( + retire: Iterable, + add: readonly (readonly [bigint, Range])[], + latest: bigint | undefined, + holdings: Holdings = UNHELD, + ): void { + const readded = new Set(add.map(([key]) => key)); + for (const key of retire) { + this.#retire(key); + const range = this.#ranges.get(key); + // One added again is replaced in place below, keeping its order. + if (range === undefined || readded.has(key)) continue; + this.#unwired -= isWire(range) ? 0 : 1; + this.#ranges.delete(key); + } + for (const [key, range] of add) { + this.#retire(key); + this.#hold(key, range); + if (!isEmpty(range)) this.#placeRange(key, latest, holdings); + } + } + + // Hold `range` under `key`, in place of any range it held. + #hold(key: bigint, range: Range): void { + const old = this.#ranges.get(key); + if (old !== undefined) this.#unwired -= isWire(old) ? 0 : 1; + this.#unwired += isWire(range) ? 0 : 1; + this.#ranges.set(key, range); + } + + // Take out what the range keyed `key` placed, leaving the range itself. + #retire(key: bigint): void { + this.#pending.delete(key); + this.#reaching.delete(key); + const span = this.#spans.get(key); + if (span) this.#uncount(key, span); + } + /** Whether a relative anchor still waits on a first group. */ get pending(): boolean { - return this.#pending; + return this.#pending.size > 0; } /** Whether nothing is left to resolve as groups arrive. */ get settled(): boolean { - return this.#settled; + return this.#pending.size === 0 && !this.#unplaced() && this.#looking.size === 0; } /** @@ -502,34 +570,28 @@ export class Window { * starts once a group arrives. Returns whether the groups in range moved. */ settle(latest: bigint | undefined, holdings: Holdings = UNHELD): boolean { - if (this.#pending) { + if (this.pending) { if (latest === undefined) return false; + // Every range places again, as when first resolved: the first group arrives once per track. this.#place(latest, holdings); - return !this.#pending; + return !this.pending; } - // 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); - for (let position = placed; position < this.#spans.length; position++) moved.push(position); + let moved = false; + if (this.#reaching.size > 0) { + const placed = this.#spans.size; + const reaching = [...this.#reaching]; + this.#reaching.clear(); + for (const key of reaching) this.#placeRange(key, latest, holdings); + moved = this.#spans.size > placed; } - 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; + if (this.#waiting.size > 0 && this.#startWaiting(holdings)) moved = true; + return moved; } // Start the spans whose time start waits on a group, once one arrived, or once nothing can - // 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 { + // 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. + #startWaiting(holdings: Holdings): boolean { const complete = holdings.complete(); const unplaced = complete ? undefined : holdings.unplaced?.(); let tried: Timed[]; @@ -542,7 +604,7 @@ export class Window { tried = this.#waiting.takeAll(); this.#waited = unplaced?.mark; } - let shrank = false; + let started = false; for (const entry of tried) { const containing = holdings.containing(entry.time); let start: bigint; @@ -554,51 +616,41 @@ export class Window { this.#waiting.put(entry); continue; } - for (const position of entry.positions) { - if (this.#startSpan(position, entry.time, start, floored, holdings)) shrank = true; - started.push(position); - } + started = true; + for (const key of entry.keys) this.#startSpan(key, entry.time, start, floored, holdings); } - return shrank; + return started; } - // 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); + // Start the span keyed `key`, 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. + #startSpan(key: bigint, at: bigint, start: bigint, floored: boolean, holdings: Holdings): void { + const span = this.#spans.get(key); + if (!span) return; + const asked = time(this.#ranges.get(key)?.start) !== undefined; + this.#uncount(key, span); 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; + if (below !== undefined) span.looking = { time: at, below }; + this.#count(key, span); } /** Whether group `key` is in range. Everything is while a relative anchor waits. */ contains(key: bigint): boolean { - if (this.#pending) return true; - const run = this.#runAt(key); - return run !== undefined && key >= run.start; + return this.pending || this.#groups.contains(key); } /** The lowest group in range at or above `from`, undefined when none is. `from` itself while a relative anchor waits. */ next(from: bigint): bigint | undefined { - if (this.#pending) return from; - const run = this.#runAt(from); - return run === undefined ? undefined : max(run.start, from); + return this.pending ? from : this.#groups.next(from); } /** The lowest group in range, undefined while an anchor waits. Nothing in range floors at group 0. */ floor(): bigint | undefined { - if (this.#pending || this.#unplaced()) return undefined; - return this.#runs[0]?.start ?? 0n; + if (this.pending || this.#unplaced()) return undefined; + return this.#groups.lowest() ?? 0n; } /** @@ -606,22 +658,22 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.#pending || this.#reaching.length > 0 || this.#unnamed > 0) return undefined; - return this.#named[0]?.start; + if (this.pending || this.#reaching.size > 0 || this.#unnamed > 0) return undefined; + return this.#named.first(); } /** The exclusive end past every group in range, undefined when one stays open or an anchor waits. Nothing in range ends at 0. */ end(): bigint | undefined { - if (!this.#settled) return undefined; - const end = this.#runs.at(-1)?.end ?? 0n; + if (!this.settled) return undefined; + const end = this.#groups.end(); return end === UNBOUNDED ? undefined : end; } /** The runs in range from `from` up to the exclusive `end`, lowest first. */ runs(from = 0n, end = UNBOUNDED): Run[] { if (from >= end) return []; - if (this.#pending) return [{ start: from, end }]; - return intersect(this.#runs, from, end); + if (this.pending) return [{ start: from, end }]; + return this.#groups.runs(from, end); } /** @@ -631,33 +683,33 @@ export class Window { */ missable(from = 0n, end = UNBOUNDED): Run[] { if (from >= end) return []; - if (this.#pending) return [{ start: from, end }]; - return intersect(this.#missable, from, end); + if (this.pending) return [{ start: from, end }]; + return this.#missable.runs(from, end); } /** - * Each range resolved, in the order given: undefined for one whose start still waits, or is - * still looked up. One empty as given ends at its start. Mirrors the Rust `Window::resolved`. + * The range keyed `key` resolved: undefined for one whose start still waits, or is still looked + * up, or that isn't held. One empty as given ends at its start. Mirrors the Rust + * `Window::resolved_key`. */ + resolvedKey(key: bigint): Resolved | undefined { + const range = this.#ranges.get(key); + if (range === undefined) return undefined; + const start = time(range.start); + if (start !== undefined && isEmpty(range)) return { start, end: start, floored: true }; + const span = this.#spans.get(key); + if (!span || span.looking || span.waiting) return undefined; + return { start: span.start, end: span.end === UNBOUNDED ? undefined : span.end, floored: span.floored }; + } + + /** Each range resolved, in key order, as {@link resolvedKey} reads it. Mirrors the Rust `Window::resolved`. */ resolved(): (Resolved | undefined)[] { - const resolved: (Resolved | undefined)[] = this.ranges.map((range) => { - const start = time(range.start); - return start !== undefined && isEmpty(range) ? { start, end: start, floored: true } : undefined; - }); - for (const span of this.#spans) { - if (span.looking || span.waiting) continue; - resolved[span.index] = { - start: span.start, - end: span.end === UNBOUNDED ? undefined : span.end, - floored: span.floored, - }; - } - return resolved; + return [...this.#ranges.keys()].map((key) => this.resolvedKey(key)); } /** - * Where each range starts once delivery starts at `from`, in the order given: undefined for - * one with no group there, or whose start still waits. Mirrors the Rust `Window::starts`. + * Where each range starts once delivery starts at `from`, in key order: undefined for one with + * no group there, or whose start still waits. Mirrors the Rust `Window::starts`. */ starts(from: bigint): (bigint | undefined)[] { return this.resolved().map((range) => { @@ -670,9 +722,9 @@ export class Window { /** The time starts still looked up, by time. Mirrors the Rust `Window::lookups`. */ lookups(): Lookup[] { const lookups: Lookup[] = []; - for (const { positions } of this.#looking.all) { - for (const position of positions) { - const looking = this.#spans[position].looking; + for (const { keys } of this.#looking.all()) { + for (const key of keys) { + const looking = this.#spans.get(key)?.looking; if (looking) lookups.push(looking); } } @@ -692,28 +744,27 @@ export class Window { const held = group !== undefined && holdings.nextStart(group) === group ? group : undefined; const containing = holdings.containing(at); const start = held ?? (typeof containing === "object" ? containing.group : undefined); - // 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]; + for (const key of found) { + const span = this.#spans.get(key); + if (!span) continue; + this.#uncount(key, span); span.looking = undefined; span.start = start ?? 0n; span.floored = start !== undefined; - this.#name(position); + this.#count(key, span); } - 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. + * to check the counted runs 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) { + for (const span of this.#spans.values()) { const [reads, asks] = spanReads(span); if (reads) groups.push(reads); if (asks) missable.push(asks); @@ -724,41 +775,39 @@ export class Window { /** The lowest start the spans name, counted. */ named(): bigint | undefined { - return this.#named[0]?.start; + return this.#named.first(); } // Whether a time start or a join's reach back waits on the groups held to place it. #unplaced(): boolean { - return this.#waiting.size > 0 || this.#reaching.length > 0; - } - - // The first run ending past `key`. - #runAt(key: bigint): Run | undefined { - return this.#runs[above(this.#runs, key)]; + return this.#waiting.size > 0 || this.#reaching.size > 0; } + // Resolve every range again from scratch. #place(latest: bigint | undefined, holdings: Holdings): void { - this.#spans = []; - this.#pending = false; + this.#spans.clear(); + this.#pending.clear(); this.#waiting = new ByTime(); this.#looking = new ByTime(); - this.#reaching = []; - this.#named = []; + this.#reaching.clear(); + this.#groups = new Cover(); + this.#missable = new Cover(); + this.#named = new Counts(); this.#unnamed = 0; - for (let index = 0; index < this.ranges.length; index++) { - if (!isEmpty(this.ranges[index])) this.#placeRange(index, latest, holdings); + for (const [key, range] of this.#ranges) { + if (!isEmpty(range)) this.#placeRange(key, latest, 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. - #placeRange(index: number, latest: bigint | undefined, holdings: Holdings): void { - const range = this.ranges[index]; + // Resolve the non-empty range keyed `key` into a span, or note what it waits on. + #placeRange(key: bigint, latest: bigint | undefined, holdings: Holdings): void { + const range = this.#ranges.get(key); + if (range === undefined) return; const until = endOf(range.end, latest, holdings); if (until === undefined) { - this.#pending = true; + this.#pending.add(key); return; } let anchor = range.start; @@ -768,21 +817,20 @@ export class Window { if (!reached) { // Nothing to measure the live edge by yet: with groups held, read none of them until one // measures it; with none, wait as a relative start does. - if (latest === undefined) this.#pending = true; - else this.#reaching.push(index); + if (latest === undefined) this.#pending.add(key); + else this.#reaching.add(key); return; } anchor = reached.anchor; } const start = startOf(anchor, latest, holdings); if (start === "pending") { - this.#pending = true; + this.#pending.add(key); return; } if (start === "waiting") { // Reads only the groups from its time until the groups held place it; see #startWaiting. - this.#waiting.add(time(anchor) ?? 0n, this.#spans.length); - this.#push({ index, anchor, start: 0n, end: until, floored: false, waiting: true }); + this.#count(key, { 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 @@ -791,10 +839,8 @@ export class Window { const asked = time(range.start) !== undefined && at === time(range.start); const below = at === undefined || !asked ? undefined : unresolved(at, start.group, start.floored, holdings); 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, + this.#count(key, { anchor, start: start.group, end: until, @@ -805,121 +851,95 @@ export class Window { }); } - #push(span: Span): void { - this.#spans.push(span); - this.#name(this.#spans.length - 1); + // Hold `span` under `key`, counting the groups it reads and where it starts. + #count(key: bigint, span: Span): void { + [span.reads, span.asks] = spanReads(span); + if (span.reads) this.#groups.add(span.reads.start, span.reads.end, 1); + if (span.asks) this.#missable.add(span.asks.start, span.asks.end, 1); + if (span.floored) this.#named.add(span.start); + else this.#unnamed++; + if (span.looking) this.#looking.add(span.looking.time, key); + if (span.waiting) this.#waiting.add(time(span.anchor) ?? 0n, key); + this.#spans.set(key, span); } - // 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(): void { - this.#updateSettled(); - const groups: Run[] = []; - const missable: Run[] = []; - for (const span of this.#spans) { - 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); - } + // Take back what {@link #count} counted for `span`, and take it out of the spans. + #uncount(key: bigint, span: Span): void { + if (span.reads) this.#groups.add(span.reads.start, span.reads.end, -1); + if (span.asks) this.#missable.add(span.asks.start, span.asks.end, -1); + if (span.floored) this.#named.remove(span.start); + else this.#unnamed--; + if (span.looking) this.#looking.remove(span.looking.time, key); + if (span.waiting) this.#waiting.remove(time(span.anchor) ?? 0n, key); + this.#spans.delete(key); } } -/** Spans by position, keyed by the time each waits on or looks up, lowest first. */ -type Timed = { time: bigint; positions: number[] }; +/** Spans by key, keyed by the time each waits on or looks up, lowest first. */ +type Timed = { time: bigint; keys: Set }; + +const compareTimes = (a: bigint, b: bigint): number => (a < b ? -1 : a > b ? 1 : 0); -/** {@link Timed} entries kept sorted by time, so the times in a run are found without a walk. */ +/** + * {@link Timed} entries ordered by time, so the times in a run are found without a walk, and a + * span joins or leaves its time without shifting every other. + */ class ByTime { - #entries: Timed[] = []; + #entries = new Sorted((entry) => entry.time, compareTimes); get size(): number { - return this.#entries.length; + return this.#entries.size; } - get all(): readonly Timed[] { - return this.#entries; + /** Every entry, lowest first. */ + *all(): Generator { + const first = this.#entries.first(); + if (first) yield* this.#entries.from(first.time); } - // 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 keyed `key` under `time`. */ + add(time: bigint, key: bigint): void { + const entry = this.#entries.ceil(time); + if (entry?.time === time) entry.keys.add(key); + else this.#entries.insert({ time, keys: new Set([key]) }); } - /** 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] }); + /** Take the span keyed `key` out from under `time`, if it is there. */ + remove(time: bigint, key: bigint): void { + const entry = this.#entries.ceil(time); + if (entry?.time !== time || !entry.keys.delete(key)) return; + if (entry.keys.size === 0) this.#entries.delete(time); } /** Put back an entry taken out, whose time no entry holds. */ put(entry: Timed): void { - this.#entries.splice(this.#at(entry.time), 0, entry); + this.#entries.insert(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(time: bigint): Set | undefined { + const entry = this.#entries.ceil(time); + if (entry?.time !== time) return undefined; + this.#entries.delete(time); + return entry.keys; } /** 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) : []; + const taken: Timed[] = []; + for (const entry of this.#entries.from(start)) { + if (end < UNBOUNDED && entry.time >= end) break; + taken.push(entry); + } + for (const entry of taken) this.#entries.delete(entry.time); + return taken; } /** Take out every entry, lowest first. */ takeAll(): Timed[] { - const entries = this.#entries; - this.#entries = []; - return entries; + const taken = [...this.all()]; + this.#entries = new Sorted((entry) => entry.time, compareTimes); + return taken; } } @@ -950,37 +970,6 @@ function spanReads(span: Span): [Run | undefined, Run | undefined] { 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[] = []; @@ -1005,17 +994,6 @@ function merge(runs: Run[]): Run[] { return merged; } -// The parts of sorted, disjoint `runs` inside `[from, end)`. -function intersect(runs: readonly Run[], from: bigint, end: bigint): Run[] { - const out: Run[] = []; - for (const run of runs) { - if (run.end <= from) continue; - if (run.start >= end) break; - out.push({ start: max(run.start, from), end: min(run.end, end) }); - } - return out; -} - /** * The Min Age a live join reaches back by: `min`, for {@link LIVE}, where the hop resolves its own * ranges. Any other start names where it starts, the oldest one included, which asks for everything diff --git a/quest/m2/lite07-update-cost.md b/quest/m2/lite07-update-cost.md deleted file mode 100644 index a977d16864..0000000000 --- a/quest/m2/lite07-update-cost.md +++ /dev/null @@ -1,46 +0,0 @@ -# [M] A lite-07 SUBSCRIBE_UPDATE costs its edit on a relay's front and in JS too - -## Goal - -A SUBSCRIBE_UPDATE that edits k ranges of an n-range subscription costs -O(k log n) on the publisher, in the session's own task as well as under the -lock every subscriber of the track shares, in Rust and JS alike. - -## Plan - -Left by [#9134](https://github.com/moq-dev/moq/pull/9134), which made the -Rust publisher hand the model the edit by Range ID -(`track::Subscriber::edit`), keyed each subscriber's ranges with a change -log (`model/track/demand.rs` `Prefs`), and made the plain cursor, its -window, the misses, and the shared aggregate move only what an edit touched -(`model/subscription/cover.rs`). `publisher_update` measures it. What -remains: - -- Rust, a relay's front (`rs/moq-net/src/model/resume.rs`): `Reader::sync` - materializes the whole `Subscription` on each change, rebuilds the reader's - window, and hands every copy a whole `update`, whose own cursor then - rebuilds. The demand fold (`poll_combined`) materializes each reader's - preferences too. Replay `Prefs::changes` into the reader's window and pass - each copy the edit, clamped per copy, through `track::Subscriber::edit`; the - copy's positional `resolved_ranges` and `Copy::from` become keyed. Add a - routed case to `publisher_update`. -- Rust, the servable index (`model/track.rs` `Live`): rebuilt from the cache - arrivals whenever the groups' cover moves, so an edit costs the cache held, - not the ranges. -- JS: `combineSubscriptions` in `js/net/src/track.ts` rebuilds the union over - every subscriber on each `Subscriber.update`; give it the incremental union - `rs/moq-net/src/model/track/demand/ranges.rs` keeps, and mirror the Rust - edit path in `js/net/src/lite/publisher.ts` and `lite/subscribe.ts`. - -The rework is internal-only: no public API changes in `moq-net`, `moq-ffi`, or -`@moq/net`. Keep it that way: - -- Rust: the edit entry point stays crate-private; the public `update` stays a - whole-set replacement and `subscription()` keeps returning ranges in the - order last given. -- JS: reach the edit through an internal `hooks.*` accessor, as the lite - publisher already does for the track, and keep the per-producer union - incremental behind the existing `Subscription` signal. `Subscriber.update` - and the exported types stay as they are. -- If the work turns up a need for a public change after all, stop and ask: - the maintainer's condition was that this rework stays internal. diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index b4e824ee0b..7b869906b1 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -9,7 +9,8 @@ //! //! `publisher_update` feeds a lite-07 subscription of `ranges` one-group ranges a stream of //! SUBSCRIBE_UPDATEs, each retiring the `edited` oldest ranges and adding as many, then serving -//! until parked; `steer` moves only the priority. A cost that grows with the live ranges +//! until parked; `steer` moves only the priority, and `routed_edit` edits on a relay's front, +//! which mirrors each update into its route's copy. 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 @@ -72,6 +73,14 @@ fn bench_update(c: &mut Criterion) { let run = run.get_or_insert_with(|| UpdateBench::new(ranges)); b.iter(|| run.steer()); }); + // The same on a relay's front, which mirrors each update into its route's copy. + for edited in EDITED { + let mut run = None; + group.bench_function(BenchmarkId::new(format!("routed_edit{edited}"), ranges), |b| { + let run = run.get_or_insert_with(|| UpdateBench::routed(ranges)); + b.iter(|| run.edit(edited)); + }); + } } group.finish(); } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 42e6567250..9786d3c259 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -6272,6 +6272,7 @@ pub struct UpdateBench { oldest: u64, priority: u8, _track: track::Producer, + _routed: Option, _peer: crate::transport::memory::Session, } @@ -6280,6 +6281,16 @@ impl UpdateBench { /// Subscribe to `ranges` one-group time ranges on a track whose groups start at their keys, /// so every range resolves without a group held and nothing is served. pub fn new(ranges: u64) -> Self { + Self::with(ranges, false) + } + + /// [`Self::new`] on a relay's front: the subscription reads a logical track straight from + /// one route's copy, so each update reaches the copy too. + pub fn routed(ranges: u64) -> Self { + Self::with(ranges, true) + } + + fn with(ranges: u64, routed: bool) -> Self { let version = Version::Lite07; let (local, peer) = crate::transport::memory::pair(Default::default()); let session = Transport::new(local); @@ -6298,7 +6309,21 @@ impl UpdateBench { .with_max_age(None), ); track.starts_at_keys(); - let subscriber = track.subscribe(bounds.groups()); + let routed = routed.then(|| { + let routed = track::Routed::new("bench", track.info().clone()); + routed.copy.starts_at_keys(); + routed + }); + let subscriber = match &routed { + Some(routed) => { + let pending = routed.logical.consume().subscribe(bounds.groups()); + let Poll::Ready(Ok(subscriber)) = pending.poll_ok(&kio::Waiter::noop()) else { + panic!("an accepted logical track answers at once"); + }; + subscriber + } + None => track.subscribe(bounds.groups()), + }; let steer = kio::Producer::new(Steer::default()); let ctx = Subscription { session, @@ -6321,6 +6346,7 @@ impl UpdateBench { oldest: 0, priority: 0, _track: track, + _routed: routed, _peer: peer, }; this.settle(); diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index d4f61e910c..c3d3a0d35a 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -4346,7 +4346,7 @@ struct Asked { /// SUBSCRIBE_OK), and read the demand only then, so the readers' demand rides the /// query. Kept once spliced until the last reader leaves, so the copy is never left /// with nobody subscribed while readers are still subscribing to it on their own. - sub: Option>, + sub: Option, /// The copy staged for fetches while its info is in flight, withdrawn if it is never /// spliced. staged: super::resume::Staged, @@ -4370,7 +4370,7 @@ struct TrackIo { /// The source whose copy serves the track, the copy, and the query holding it; plus the /// subscription that query made. copy: Option<(u64, track::Consumer, track::Resolving)>, - held: Option>, + held: Option, /// Whether the track had a reader as of the last demand edge. wanted: bool, } @@ -4487,7 +4487,7 @@ struct InFlight { // subscription, which follows their demand as it did while the front served it. logical: Logical, query: track::Resolving, - held: Option>, + held: Option, } /// Keep the query's subscription on a copy following the readers' demand, and let it go @@ -4495,7 +4495,7 @@ struct InFlight { /// aggregate from the copy's source: a shorter budget, or a demoted priority. With `copy`, /// a reader arriving subscribes it too. fn follow_demand( - held: &mut Option>, + held: &mut Option, copy: Option<&track::Consumer>, accepted: Option<&mut track::Producer>, weak: &track::TrackWeak, @@ -4505,17 +4505,22 @@ fn follow_demand( if held.is_none() && copy.is_none() { return; } + // Woken by the demand changing, which `follow` reads from each reader's own edits. if let Some(accepted) = accepted { - while let Poll::Ready(Ok(_)) = accepted.poll_subscription_changed(waiter) {} + accepted.watch_subscription(waiter); } - match (weak.subscription(), held.as_mut()) { - (Some((subscription, subscribers)), Some(hold)) => { - let _ = hold.update(routes.subscription(subscription, subscribers)); + match held { + Some(hold) => { + if !routes.follow(hold, weak) { + *held = None; + } } - (Some((subscription, subscribers)), None) => { - *held = copy.map(|copy| copy.subscribe(routes.subscription(subscription, subscribers))); + None => { + *held = weak.subscription().and_then(|(subscription, subscribers)| { + let sub = copy?.subscribe(routes.subscription(subscription, subscribers)); + Some(super::resume::Held::new(sub)) + }); } - (None, _) => *held = None, } } @@ -4616,7 +4621,7 @@ impl kio::Task for FrontServeRun { } for mut io in std::mem::take(in_flight) { if let Logical::Requested(request) = &mut io.logical { - while let Poll::Ready(Some(_)) = request.poll_subscription_changed(waiter) {} + request.watch_subscription(waiter); match io.query.poll_ok(waiter) { Poll::Ready(Ok(info)) => io.logical.accept(info), Poll::Ready(Err(err)) => { @@ -5216,7 +5221,9 @@ impl kio::Task for FrontServe { // Subscribe before the query exposes upstream demand, and query // that same instance even if the broadcast changes meanwhile. let sub = io.weak.subscription().map(|(demand, subscribers)| { - lookup.subscribe(io.routes.subscription(demand, subscribers)) + super::resume::Held::new( + lookup.subscribe(io.routes.subscription(demand, subscribers)), + ) }); let query = match &sub { Some(sub) => sub.info(), diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 59fa557161..5021844915 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -42,6 +42,8 @@ use rangemap::{RangeMap, RangeSet}; +use std::collections::{BTreeMap, BTreeSet}; + use std::ops::Bound; use std::sync::{Arc, PoisonError, Weak}; use std::sync::{Mutex, MutexGuard}; @@ -164,8 +166,9 @@ impl Producer { } } - /// The readers' demand on a new copy, with each reader resuming where it got to. - pub(super) fn subscription(&self, mut demand: Subscription, subscribers: usize) -> Subscription { + /// The readers, or `None` until every one of `subscribers` is here: a subscription not + /// yet polled has demand but no reader. + fn readers(&self, subscribers: usize) -> Option>>> { let readers: Vec<_> = self .readers .lock() @@ -173,46 +176,92 @@ impl Producer { .iter() .filter_map(Weak::upgrade) .collect(); - // A subscription not yet polled has demand but no reader. It still needs the - // original ranges, so never narrow the aggregate until every contribution is here. - if readers.len() != subscribers || readers.is_empty() { + (readers.len() == subscribers && !readers.is_empty()).then_some(readers) + } + + /// The readers' demand on a new copy, with each reader resuming where it got to. + pub(super) fn subscription(&self, mut demand: Subscription, subscribers: usize) -> Subscription { + // A subscription not yet polled still needs the original ranges, so never narrow the + // aggregate until every contribution is here. + let Some(readers) = self.readers(subscribers) else { return demand; - } + }; let mut ranges = Vec::new(); for reader in readers { let mut reader = reader.lock().unwrap_or_else(PoisonError::into_inner); - let preferences = reader.subscription.read().subscription(); - // A failed copy's resolution is no placement, as in `Reader::window`: the reader's - // own stands, or the anchors do for ranges it has yet to take up. - let failed = reader.route.end().is_none() - && reader - .copies - .last() - .is_some_and(|copy| matches!(copy.sub, Sub::Ready(_)) && copy_failed(©.track)); - let placed = match failed { - true if reader.mirrored.ranges == preferences.ranges => Some(reader.window().resolved()), - true => Some(Vec::new()), - false => None, - }; - let resolved = match reader.copies.last_mut() { - _ if placed.is_some() => placed.unwrap_or_default(), - Some(Copy { - sub: Sub::Ready(sub), - from, - .. - }) if *from == preferences.ranges => sub.resolved_ranges(), - Some(Copy { - sub: Sub::Ready(sub), .. - }) => sub.resolve_ranges(&preferences), - Some(copy) if copy.from == preferences.ranges => copy.resolved.clone(), - _ => Vec::new(), - }; - ranges.extend(preferences.clamped(reader.newest, None, &resolved).ranges); + let preferences: Vec<_> = reader.subscription.read().ranges().collect(); + ranges.extend(reader.resumed(&preferences).into_iter().map(|(_, range)| range)); } demand.ranges = ranges; demand } + /// Keep `held` asking for the readers' demand of `weak`, as [`Self::subscription`] folds + /// it. Returns `false` once no reader is left, to let it go. Once every reader is here, + /// a change costs the ranges it touched: a reader's edit reaches `held` as an edit too. + pub(super) fn follow(&self, held: &mut Held, weak: &track::TrackWeak) -> bool { + let Some((terms, subscribers)) = weak.terms() else { + return false; + }; + let Some(readers) = self.readers(subscribers) else { + held.fold = None; + if let Some((demand, _)) = weak.subscription() { + let _ = held.sub.update(demand); + } + return true; + }; + let fold = match &mut held.fold { + Some(fold) if fold.feed == terms.feed => fold, + _ => { + // Asked whole, every range of every reader keyed by its place in the fold. + let mut fold = Fold { + feed: terms.feed, + ..Default::default() + }; + let mut ranges = Vec::new(); + for reader in &readers { + ranges.extend(fold.fold(reader, None).into_iter().map(|(_, range)| range)); + } + let _ = held.sub.update(Subscription { ranges, ..terms }); + let keyed = held + .sub + .preferences() + .read() + .ranges() + .map(|(key, _)| key) + .eq(0..fold.next); + if !keyed { + let _ = held.sub.preferences().rekey(0..fold.next); + } + held.fold = Some(fold); + return true; + } + }; + let mut edit = track::Edit { + priority: terms.priority, + delay: terms.delay, + order: terms.order, + ..Default::default() + }; + let mut seen = std::collections::HashSet::with_capacity(readers.len()); + for reader in &readers { + seen.insert(Arc::as_ptr(reader) as usize); + let (retire, add) = fold.refold(reader); + edit.retire.extend(retire); + edit.add.extend(add); + } + // Readers that left take their ranges with them. + fold.readers.retain(|id, folded| { + let kept = seen.contains(id); + if !kept { + edit.retire.extend(folded.keys.values().copied()); + } + kept + }); + let _ = held.sub.preferences().edit(edit); + true + } + /// Stage `copy`, whose info is still in flight, for fetches until the returned /// [`Staged`] is spliced with [`Self::splice`] or dropped. pub(super) fn stage(&self, copy: track::Consumer) -> Staged { @@ -232,7 +281,7 @@ impl Producer { } /// Serve the track from `copy` from now on, subscribing every reader to it. - #[cfg(test)] + #[cfg(any(test, feature = "fuzz"))] pub(super) fn serve(&self, copy: track::Consumer) { self.serve_from(copy, None); } @@ -462,22 +511,27 @@ impl Consumer { /// Subscribe to the logical track with the reader's own preferences. pub(super) fn subscribe(&self, subscription: track::Preferences) -> Subscriber { - let (mirrored, keys, revision) = { + let (mirrored, revision) = { let prefs = subscription.read(); - ( - prefs.subscription(), - prefs.ranges().map(|(key, _)| key).collect::>(), - prefs.revision, - ) + let mut mirrored = Mirrored { + priority: prefs.priority, + delay: prefs.delay, + order: prefs.order, + feed: prefs.feed, + ..Default::default() + }; + for (key, range) in prefs.ranges() { + mirrored.insert(key, range); + } + (mirrored, prefs.revision) }; - let window = keyed(&keys, &mirrored.ranges, &Unheld); + let window = keyed(mirrored.ranges.iter().map(|(key, range)| (*key, *range)), &Unheld); let reader = Arc::new_cyclic(|this| { Mutex::new(Reader { this: this.clone(), route: self.clone(), subscription: subscription.clone(), mirrored, - keys, revision, generation: None, copies: Vec::new(), @@ -609,11 +663,11 @@ struct Copy { sub: Sub, /// Where the subscription was asked to start, so demand updates never ask for less. floor: Option, - /// The reader's ranges [`Self::resolved`] are for. - from: Vec, - /// Each of those ranges resolved, by index, so clamping never widens one: as the - /// serving copy had them when this one was subscribed, until this one answers. - resolved: Vec>, + /// The reader's ranges resolved, by the reader's key, so clamping never widens one: as + /// the serving copy had them when this one was subscribed, until this one answers. A + /// range left out is not resolved. The copy's own subscription keys each range as the + /// reader does, so a change to the reader's ranges reaches it as an edit of those alone. + resolved: BTreeMap, /// Replaced by a newer route. replaced: bool, /// Bounded once the newer copy delivered its first group: nothing past this group is @@ -642,25 +696,31 @@ enum Sub { } impl Copy { - /// What this copy is asked for: the reader's own preferences, from no earlier than - /// where it was asked to start, and through no later than where it was replaced. - fn wire(&mut self, reader: &Subscription) -> Subscription { - match &mut self.sub { - // Its own resolution, which capping its end must not move. - Sub::Ready(sub) if reader.ranges == self.from => self.resolved = sub.resolved_ranges(), - // Replaced ranges resolve against this copy's cache now, as any subscriber's do. - Sub::Ready(sub) => { - self.resolved = sub.resolve_ranges(reader); - self.from = reader.ranges.clone(); - } - Sub::Pending(_) if reader.ranges == self.from => {} - // Its own resolution, once it answers, is for these. - Sub::Pending(_) => { - self.resolved.clear(); - self.from = reader.ranges.clone(); - } + /// Subscribe to `track` for `reader`, from `floor` up to the exclusive `end`, each range + /// clamped by where `resolved` places it, keyed as the reader keys it. + fn subscribe( + track: &track::Consumer, + reader: &Mirrored, + floor: Option, + end: Option, + resolved: &BTreeMap, + ) -> Sub { + // The wire request carries the floor from the first message: some sessions read the + // demand only once. + let pending = track.subscribe(reader.wire(floor, end, resolved)); + // Keyed by position as subscribed: the reader's own keys from then on. + if !reader.ranges.keys().copied().eq(0..reader.ranges.len() as u64) { + let _ = pending.preferences().rekey(reader.ranges.keys().copied()); + } + Sub::Pending(Box::new(pending)) + } + + /// The copy's own subscription preferences. + fn preferences(&self) -> &track::Preferences { + match &self.sub { + Sub::Pending(pending) => pending.preferences(), + Sub::Ready(sub) => sub.preferences(), } - reader.clamped(self.floor, self.end(), &self.resolved) } /// The first group past where this copy was replaced, if any: one replaced before @@ -670,20 +730,72 @@ impl Copy { .and_then(|last| last.map_or(Some(0), |last| last.checked_add(1))) } - fn update(&mut self, reader: &Subscription) { + /// Mirror `change` to the reader's preferences, which now read as `reader`. + fn update(&mut self, reader: &Mirrored, change: &Change) { self.reopen(); - let wire = self.wire(reader); - self.send(wire); + match change { + Change::Scalars => self.send(reader, []), + Change::Edit(keys) => { + let added: Vec<(u64, track::Range)> = keys + .iter() + .filter_map(|key| Some((*key, *reader.ranges.get(key)?))) + .collect(); + for key in keys { + self.resolved.remove(key); + } + // Added ranges resolve against this copy's cache now, as any subscriber's do; + // a copy still unanswered resolves them once it answers. + if let Sub::Ready(sub) = &mut self.sub { + let subscription = reader.with_ranges(added.iter().map(|(_, range)| *range).collect()); + for ((key, _), resolved) in added.iter().zip(sub.resolve_ranges(&subscription)) { + if let Some(resolved) = resolved { + self.resolved.insert(*key, resolved); + } + } + } + self.send(reader, keys.iter().copied()); + } + Change::All => { + // Replaced ranges resolve against this copy's cache now, as any subscriber's + // do, and the copy resolves them all again too. + self.resolved = match &mut self.sub { + Sub::Ready(sub) => keyed_resolution( + reader.ranges.keys().copied(), + sub.resolve_ranges(&reader.subscription()), + ), + Sub::Pending(_) => BTreeMap::new(), + }; + let wire = reader.wire(self.floor, self.end(), &self.resolved); + let _ = match &mut self.sub { + Sub::Pending(pending) => pending.update(wire), + Sub::Ready(sub) => sub.update(wire), + }; + // Keyed by position as replaced: the reader's own keys from then on. + let keyed = self + .preferences() + .read() + .ranges() + .map(|(key, _)| key) + .eq(reader.ranges.keys().copied()); + if !keyed { + let _ = self.preferences().rekey(reader.ranges.keys().copied()); + } + } + } } /// Take nothing past `until` from this replaced copy, superseded from now. A bound only /// narrows what it may deliver, so a copy that ran out stays out: reopened, it would be /// waited on with nothing left to wake the reader. - fn bound(&mut self, until: Option, reader: &Subscription) { + fn bound(&mut self, until: Option, reader: &Mirrored) { self.until = Some(until); self.since = Some(self.track.now()); - let wire = self.wire(reader); - self.send(wire); + // Its own resolution, which capping its end must not move. + if let Sub::Ready(sub) = &mut self.sub { + let keys = reader.ranges.keys().copied(); + self.resolved = keyed_resolution(reader.ranges.keys().copied(), sub.resolved_ranges(keys)); + } + self.send(reader, reader.ranges.keys().copied()); } /// Read this copy again: one that ran out under the reader's old ranges, budget, or @@ -695,11 +807,45 @@ impl Copy { } } - /// Ask for `wire` instead, answered or not. - fn send(&mut self, wire: Subscription) { + /// The range keyed `key` as this copy is asked for it: the reader's own, from no earlier + /// than where it was asked to start, and through no later than where it was replaced. + fn wire(&self, reader: &Mirrored, key: u64) -> Option { + let range = reader.ranges.get(&key)?; + Some(range.clamped(self.floor, self.end(), self.resolved.get(&key).copied())) + } + + /// Ask for the reader's current priority, delay, and order, and for the ranges keyed in + /// `keys` as [`Self::wire`] has them, answered or not: an edit of the ranges that moved. + fn send(&mut self, reader: &Mirrored, keys: impl IntoIterator) { + let (mut retire, mut add) = (Vec::new(), Vec::new()); + { + let held = self.preferences().read(); + // A feed an edit can't carry is asked whole, which keys the ranges by position. + if held.feed != reader.feed { + drop(held); + return self.update(reader, &Change::All); + } + for key in keys { + let wire = self.wire(reader, key); + if wire == held.range(key) { + continue; + } + match wire { + Some(range) => add.push((key, range)), + None => retire.push(key), + } + } + } + let edit = track::Edit { + retire, + add, + priority: reader.priority, + delay: reader.delay, + order: reader.order, + }; let _ = match &mut self.sub { - Sub::Pending(pending) => pending.update(wire), - Sub::Ready(sub) => sub.update(wire), + Sub::Pending(pending) => pending.preferences().edit(edit), + Sub::Ready(sub) => sub.edit(edit), }; } @@ -828,7 +974,7 @@ struct Retain<'a> { groups_read: bool, /// The newest group the serving copy has. edge: Option, - mirrored: &'a Subscription, + mirrored: &'a Mirrored, delivered: &'a Delivered, out: &'a [(u64, Weak<()>)], waiter: &'a kio::Waiter, @@ -1216,6 +1362,224 @@ impl Candidate { } } +/// A subscription on a copy asking for its readers' demand, as [`Producer::subscription`] +/// folds it, kept current by [`Producer::follow`]. +pub(super) struct Held { + sub: kio::Pending, + /// How the subscription's ranges map to the readers' once every reader is here, so a + /// reader's change reaches it as an edit. `None` while it was last asked whole. + fold: Option, +} + +impl Held { + pub(super) fn new(sub: kio::Pending) -> Self { + Self { sub, fold: None } + } + + /// Resolve the copy's info with this subscription registered. + pub(super) fn info(&self) -> track::Resolving { + self.sub.info() + } +} + +/// Each reader's ranges as [`Held`] asks for them: by reader, the revision of its preferences +/// folded and the key each of its ranges has in the held subscription. +#[derive(Default)] +struct Fold { + readers: std::collections::HashMap, + /// The next key the held subscription gives a range. + next: u64, + /// The feed the held subscription was asked with, which only asking it whole changes. + feed: bool, +} + +struct Folded { + /// Keeps the reader's allocation, so no other reader takes its address, the entry's key, + /// until the entry leaves. + _reader: Weak>, + revision: u64, + /// The held key of each of the reader's ranges, by the reader's key. + keys: BTreeMap, +} + +impl Fold { + /// Fold in every range of `reader`, or the ranges keyed `keys` it still holds, returning + /// each under the held key it now has. + fn fold(&mut self, reader: &Arc>, keys: Option>) -> Vec<(u64, track::Range)> { + let mut locked = reader.lock().unwrap_or_else(PoisonError::into_inner); + let (preferences, revision) = { + let current = locked.subscription.read(); + let preferences: Vec<_> = match &keys { + Some(keys) => keys + .iter() + .filter_map(|key| Some((*key, current.range(*key)?))) + .collect(), + None => current.ranges().collect(), + }; + (preferences, current.revision) + }; + let resumed = locked.resumed(&preferences); + drop(locked); + let folded = self + .readers + .entry(Arc::as_ptr(reader) as usize) + .or_insert_with(|| Folded { + _reader: Arc::downgrade(reader), + revision, + keys: BTreeMap::new(), + }); + folded.revision = revision; + resumed + .into_iter() + .map(|(key, range)| { + let held = self.next; + self.next += 1; + folded.keys.insert(key, held); + (held, range) + }) + .collect() + } + + /// What `reader` changed since it was last folded in: the held keys of the ranges it + /// retired or replaced, and the ranges it added, under new held keys. + fn refold(&mut self, reader: &Arc>) -> (Vec, Vec<(u64, track::Range)>) { + let id = Arc::as_ptr(reader) as usize; + let Some(folded) = self.readers.get_mut(&id) else { + return (Vec::new(), self.fold(reader, None)); + }; + let changes = { + let locked = reader.lock().unwrap_or_else(PoisonError::into_inner); + let current = locked.subscription.read(); + if current.revision == folded.revision { + return Default::default(); + } + current.changes(folded.revision) + }; + let retire = match &changes { + Some(keys) => keys.iter().filter_map(|key| folded.keys.remove(key)).collect(), + None => std::mem::take(&mut folded.keys).into_values().collect(), + }; + (retire, self.fold(reader, changes)) + } +} + +/// A reader's preferences as its copies are asked for them, each range by the key the +/// reader's own preferences give it. +#[derive(Clone, Default)] +struct Mirrored { + priority: i8, + delay: track::Delay, + order: track::Order, + feed: bool, + ranges: BTreeMap, + /// How many ranges end at the oldest group, which names no group; see [`Reader::refusal`]. + oldest_ends: usize, +} + +impl Mirrored { + fn insert(&mut self, key: u64, range: track::Range) { + self.oldest_ends += usize::from(range.end == Some(Anchor::Oldest)); + if let Some(old) = self.ranges.insert(key, range) { + self.oldest_ends -= usize::from(old.end == Some(Anchor::Oldest)); + } + } + + fn remove(&mut self, key: u64) { + if let Some(old) = self.ranges.remove(&key) { + self.oldest_ends -= usize::from(old.end == Some(Anchor::Oldest)); + } + } + + fn budget(&self) -> Duration { + self.delay.budget() + } + + /// These preferences with `ranges` instead. + fn with_ranges(&self, ranges: Vec) -> Subscription { + Subscription { + priority: self.priority, + delay: self.delay, + ranges, + order: self.order, + feed: self.feed, + } + } + + /// These preferences, the ranges in key order. Costs every range. + fn subscription(&self) -> Subscription { + self.with_ranges(self.ranges.values().copied().collect()) + } + + /// [`Self::subscription`] from no earlier than `floor` and through no later than the + /// exclusive `end`, each range clamped where `resolved` places it. + fn wire(&self, floor: Option, end: Option, resolved: &BTreeMap) -> Subscription { + self.with_ranges( + self.ranges + .iter() + .map(|(key, range)| range.clamped(floor, end, resolved.get(key).copied())) + .collect(), + ) + } +} + +#[cfg(test)] +impl Mirrored { + /// `subscription`, its ranges keyed by position. + fn of(subscription: &Subscription) -> Self { + let mut mirrored = Self { + priority: subscription.priority, + delay: subscription.delay, + order: subscription.order, + feed: subscription.feed, + ..Default::default() + }; + for (key, range) in (0..).zip(&subscription.ranges) { + mirrored.insert(key, *range); + } + mirrored + } +} + +#[cfg(test)] +impl Copy { + /// What this copy is asked for, every range as [`Self::wire`] has it, a ready copy's + /// resolution its own. + fn wire_all(&mut self, reader: &Mirrored) -> Subscription { + if let Sub::Ready(sub) = &mut self.sub { + let keys = reader.ranges.keys().copied(); + self.resolved = keyed_resolution(keys.clone(), sub.resolved_ranges(keys)); + } + reader.wire(self.floor, self.end(), &self.resolved) + } +} + +/// What changed in a reader's preferences, for its copies to mirror. +enum Change { + /// Only the priority, delay, order, or feed. + Scalars, + /// Those, and the ranges keyed here: each one retired, added, or both. + Edit(BTreeSet), + /// Every range, keyed afresh. + All, +} + +/// Why the serving copy's resume point may pass over groups the reader lacks. +enum Resumed { + /// The route changed: the floor the copy serving before was asked from, and where it + /// placed each range, by key. + Route(Option, BTreeMap), + /// The ranges keyed here were added, and may cover groups below the resume point. + Ranges(BTreeSet), +} + +/// Each of `keys` with what `resolved` holds in its place, leaving out the unresolved. +fn keyed_resolution(keys: impl IntoIterator, resolved: Vec>) -> BTreeMap { + keys.into_iter() + .zip(resolved) + .filter_map(|(key, resolved)| Some((key, resolved?))) + .collect() +} + /// A [`Subscriber`]'s state. struct Reader { /// The first exclusive end any copy declared, refined by a matching mid-group end. A copy @@ -1229,10 +1593,7 @@ struct Reader { route: Consumer, /// The reader's own preferences, mirrored into every copy's subscription. subscription: track::Preferences, - mirrored: Subscription, - /// The key of each of [`Self::mirrored`]'s ranges, in order, so the reader's window answers - /// a range by the key an edit names it with. - keys: Vec, + mirrored: Mirrored, /// The preferences' revision [`Self::mirrored`] holds. revision: u64, /// The route generation last read. @@ -1243,10 +1604,9 @@ struct Reader { /// The newest group handed out: where a new copy is asked to start. newest: Option, /// Set once the serving copy's resume point may pass over groups the reader lacks: the - /// route changed, or the ranges did. Where the copy serving before placed the ranges, if - /// the route changed: the floor it was asked from, and its resolution. Kept until the gaps - /// are backfilled: see [`Reader::backfill_gaps`]. - resumed: Option<(Option, Vec>)>, + /// route changed, or the ranges did. Kept until the gaps are backfilled: see + /// [`Reader::backfill_gaps`]. + resumed: Option, /// The reader's ranges resolved; see [`Reader::window`]. window: Window, /// The local floor and exclusive cap; see [`track::Subscriber::with_range`]. @@ -1268,10 +1628,57 @@ struct Reader { } impl Reader { + /// The ranges in `preferences`, from the reader's own, each as a new copy is asked for + /// it: from where the reader got to, its anchors where the serving copy placed them. + fn resumed(&mut self, preferences: &[(u64, track::Range)]) -> Vec<(u64, track::Range)> { + let (revision, delay) = { + let current = self.subscription.read(); + (current.revision, current.delay) + }; + // Whether the reader mirrored these ranges, and so its copies resolved them. + let current = revision == self.revision; + let keys = preferences.iter().map(|(key, _)| *key); + // A failed copy's resolution is no placement, as in `Reader::window`: the reader's + // own stands, or the anchors do for ranges it has yet to take up. + let failed = self.route.end().is_none() + && self + .copies + .last() + .is_some_and(|copy| matches!(copy.sub, Sub::Ready(_)) && copy_failed(©.track)); + let resolved: Vec> = match self.copies.last_mut() { + _ if failed && current => { + let window = self.window(); + keys.map(|key| window.resolved_key(key)).collect() + } + _ if failed => Vec::new(), + Some(Copy { + sub: Sub::Ready(sub), .. + }) if current => sub.resolved_ranges(keys), + Some(Copy { + sub: Sub::Ready(sub), .. + }) => sub.resolve_ranges(&Subscription { + ranges: preferences.iter().map(|(_, range)| *range).collect(), + delay, + ..Default::default() + }), + Some(copy) if current => keys.map(|key| copy.resolved.get(&key).copied()).collect(), + _ => Vec::new(), + }; + let floor = self.newest; + preferences + .iter() + .enumerate() + .map(|(index, (key, range))| { + let resolved = resolved.get(index).copied().flatten(); + (*key, range.clamped(floor, None, resolved)) + }) + .collect() + } + /// The reader's ranges resolved, a relative anchor as the first ready copy to answer for /// them resolved it, whatever its failover cap: that bounds what the copy delivers, not /// where the ranges resolve. Kept once resolved, past that copy being let go. Pending - /// while a relative anchor waits on a copy to resolve it. + /// while a relative anchor waits on a copy to resolve it. Costs the ranges still unsettled. fn window(&mut self) -> &Window { let concluded = self.route.end().is_some(); for index in 0..self.copies.len() { @@ -1286,22 +1693,27 @@ impl Reader { continue; } let cap = copy.end(); - let resolved = match &mut copy.sub { - Sub::Ready(sub) => sub.resolved_ranges(), - Sub::Pending(_) => continue, + let Sub::Ready(sub) = &mut copy.sub else { + continue; }; + let keys = self.window.unsettled(); // A failover cap at or below where a range starts empties the copy's range, which // then says nothing of where the reader's own range starts. One at or below where // it ends cut that end short, so it says nothing of where the reader's range ends. - let resolved: Vec<_> = resolved - .into_iter() - .map(|resolved| { + let resolved: BTreeMap = keys + .iter() + .copied() + .zip(sub.resolved_ranges(keys.iter().copied())) + .filter_map(|(key, resolved)| { let resolved = resolved .filter(|resolved| cap.is_none() || resolved.end.is_none_or(|end| end > resolved.start))?; - Some(Resolved { - end: resolved.end.filter(|end| cap.is_none_or(|cap| *end < cap)), - ..resolved - }) + Some(( + key, + Resolved { + end: resolved.end.filter(|end| cap.is_none_or(|cap| *end < cap)), + ..resolved + }, + )) }) .collect(); self.resolve(&resolved); @@ -1309,51 +1721,59 @@ impl Reader { &self.window } - /// Resolve unsettled [`Self::window`] anchors with a copy's resolution of the reader's ranges, - /// unless a relative anchor still waits there too. - fn resolve(&mut self, resolved: &[Option]) { - // A range the copy left unresolved keeps its anchors, which only a copy can place. - let mut unplaced = false; - let ranges: Vec<_> = self - .mirrored - .ranges - .iter() - .enumerate() - .map(|(index, range)| { - let Some(resolved) = resolved.get(index).copied().flatten() else { - unplaced |= !range.is_empty() - && (matches!(range.start, Anchor::Relative(_) | Anchor::Time(1..)) - || matches!(range.end, Some(Anchor::Relative(_)))); - return *range; - }; - track::Range { - start: match range.start { - Anchor::Relative(_) | Anchor::Time(_) if resolved.floored => Anchor::Time(resolved.start), - // One that reached back to the oldest group still does, as on a plain reader. - Anchor::Relative(_) | Anchor::Time(_) => Anchor::Oldest, - start => start, - }, - end: match (range.end, resolved.end) { - (Some(Anchor::Relative(_) | Anchor::Time(_)), Some(end)) => match end.checked_sub(1) { - Some(last) => Some(Anchor::Time(last)), - None => return track::Range::EMPTY, - }, - (end, _) => end, - }, + /// Place the unsettled [`Self::window`] ranges where a copy resolved them, unless one it + /// leaves unresolved, or a relative anchor, still waits there too. + fn resolve(&mut self, resolved: &BTreeMap) { + let mut placed = Vec::new(); + for key in self.window.unsettled() { + let Some(range) = self.mirrored.ranges.get(&key).copied() else { + continue; + }; + let Some(resolved) = resolved.get(&key).copied() else { + // A range the copy left unresolved keeps its anchors, which only a copy can place. + if !range.is_empty() + && (matches!(range.start, Anchor::Relative(_) | Anchor::Time(1..)) + || matches!(range.end, Some(Anchor::Relative(_)))) + { + return; } - }) - .collect(); - if unplaced { - return; + // Placed as it is: nothing of it waits on a copy. + placed.push((key, range)); + continue; + }; + let end = match (range.end, resolved.end) { + (Some(Anchor::Relative(_) | Anchor::Time(_)), Some(end)) => match end.checked_sub(1) { + Some(last) => Some(Anchor::Time(last)), + None => { + placed.push((key, track::Range::EMPTY)); + continue; + } + }, + (end, _) => end, + }; + let start = match range.start { + Anchor::Relative(_) | Anchor::Time(_) if resolved.floored => Anchor::Time(resolved.start), + // One that reached back to the oldest group still does, as on a plain reader. + Anchor::Relative(_) | Anchor::Time(_) => Anchor::Oldest, + start => start, + }; + placed.push((key, track::Range { start, end })); } - let window = keyed(&self.keys, &ranges, &Placed); - if !window.is_pending() { - self.window = window; + // A relative end still open leaves the window waiting on a first group it never gets. + if placed.iter().any(|(_, range)| { + !range.is_empty() + && (matches!(range.start, Anchor::Relative(_)) || matches!(range.end, Some(Anchor::Relative(_)))) + }) { + return; } + self.window + .edit([], &placed, None, &Placed) + .expect("a hop that holds it placed refuses nothing"); } /// Follow the route: subscribe to a new copy, mark the ones it replaced, and mirror - /// the reader's preferences into each. + /// the reader's preferences into each. A change to the ranges costs the ranges it + /// touched, in the reader and in each copy alike. fn sync(&mut self, waiter: &kio::Waiter) { // Learn before a swap or retention drops the only copy that knows the end. self.learn_end(None); @@ -1363,34 +1783,88 @@ impl Reader { let mirrored = &self.mirrored; let revision = self.revision; let _ = self.subscription.poll_ref(waiter, |sub| { - let moved = (sub.priority, sub.delay, sub.order, sub.feed) - != (mirrored.priority, mirrored.delay, mirrored.order, mirrored.feed); - if moved || sub.revision != revision { - let keys = (sub.revision != revision).then(|| sub.ranges().map(|(key, _)| key).collect()); - changed = Some((sub.subscription(), keys, sub.revision)); + let scalars = (sub.priority, sub.delay, sub.order, sub.feed); + let moved = scalars != (mirrored.priority, mirrored.delay, mirrored.order, mirrored.feed); + if sub.revision != revision { + // The keys the changes since touched, each with its range now, or every range + // once the log no longer reaches back that far. + let ranges = match sub.changes(revision) { + Some(keys) => Ok(keys.into_iter().map(|key| (key, sub.range(key))).collect::>()), + None => Err(sub.ranges().collect::>()), + }; + changed = Some((scalars, Some(ranges), sub.revision)); + } else if moved { + changed = Some((scalars, None, revision)); } Poll::<()>::Pending }); - if let Some((sub, keys, revision)) = changed { + if let Some((scalars, ranges, revision)) = changed { self.revision = revision; - let replaced = keys.is_some_and(|keys: Vec| { - let replaced = sub.ranges != self.mirrored.ranges || keys != self.keys; - self.keys = keys; - replaced - }); - if replaced { - self.window = keyed(&self.keys, &sub.ranges, &Unheld); - // The serving copy's resume point may cut off groups the new ranges cover, judged - // by the reader's window. - self.resumed = Some((None, Vec::new())); - } - self.mirrored = sub; + let mirrored = &mut self.mirrored; + (mirrored.priority, mirrored.delay, mirrored.order, mirrored.feed) = scalars; + let change = match ranges { + None => Change::Scalars, + Some(Ok(edited)) => { + for (key, range) in &edited { + match range { + Some(range) => self.mirrored.insert(*key, *range), + None => self.mirrored.remove(*key), + } + } + let added: Vec<(u64, track::Range)> = edited + .iter() + .filter_map(|(key, range)| Some((*key, (*range)?))) + .collect(); + self.window + .edit(edited.iter().map(|(key, _)| *key), &added, None, &Unheld) + .expect("a hop that holds nothing refuses nothing"); + // A retired range has nothing left to backfill, so a peer that keeps retiring + // and adding ranges before the floor resolves holds only its live ones here. + if let Some(Resumed::Ranges(keys)) = &mut self.resumed { + for (key, _) in edited.iter().filter(|(_, range)| range.is_none()) { + keys.remove(key); + } + } + // The serving copy's resume point may cut off groups the added ranges cover, + // judged by the reader's window. A route change still to judge is judged + // against every range instead, since the ones it placed moved. + if !added.is_empty() { + let added = added.iter().map(|(key, _)| *key); + self.resumed = Some(Resumed::Ranges(match self.resumed.take() { + Some(Resumed::Ranges(mut keys)) => { + keys.extend(added); + keys + } + Some(Resumed::Route(..)) => self.mirrored.ranges.keys().copied().collect(), + None => added.collect(), + })); + } + Change::Edit(edited.into_iter().map(|(key, _)| key).collect()) + } + Some(Err(ranges)) if ranges == self.mirrored.ranges => Change::Scalars, + Some(Err(ranges)) => { + let mut mirrored = Mirrored { + ranges: BTreeMap::new(), + oldest_ends: 0, + ..std::mem::take(&mut self.mirrored) + }; + for (key, range) in ranges { + mirrored.insert(key, range); + } + self.mirrored = mirrored; + self.window = keyed(self.mirrored.ranges.iter().map(|(key, range)| (*key, *range)), &Unheld); + // The serving copy's resume point may cut off groups the new ranges cover, + // judged by the reader's window. + self.resumed = Some(Resumed::Ranges(self.mirrored.ranges.keys().copied().collect())); + Change::All + } + }; for copy in &mut self.copies { - copy.update(&self.mirrored); + copy.update(&self.mirrored, &change); } - // A ready copy just resolved the replaced ranges against its cache, before its + // A ready copy just resolved the changed ranges against its cache, before its // failover cap raised them, so the reader takes that resolution now. - if replaced { + if !matches!(change, Change::Scalars) { let concluded = self.route.end().is_some(); for index in (0..self.copies.len()).rev() { if self.window.is_settled() { @@ -1401,7 +1875,13 @@ impl Reader { continue; } if matches!(self.copies[index].sub, Sub::Ready(_)) { - let resolved = self.copies[index].resolved.clone(); + let copy = &self.copies[index].resolved; + let resolved: BTreeMap = self + .window + .unsettled() + .into_iter() + .filter_map(|key| Some((key, *copy.get(&key)?))) + .collect(); self.resolve(&resolved); } } @@ -1439,22 +1919,26 @@ impl Reader { .copies .last() .is_some_and(|copy| matches!(copy.sub, Sub::Ready(_)) && copy_failed(©.track)); + let keys = self.mirrored.ranges.keys().copied(); let resolved = match self.copies.last_mut() { - Some(_) if failed => self.window().resolved(), + Some(_) if failed => self.window().resolved_by_key(), Some(Copy { sub: Sub::Ready(sub), .. - }) => sub.resolved_ranges(), + }) => keyed_resolution(keys.clone(), sub.resolved_ranges(keys)), // One still unanswered passes on what it was handed. Some(Copy { sub: Sub::Pending(_), resolved, .. }) => resolved.clone(), - None => Vec::new(), + None => BTreeMap::new(), }; // The new copy starts at the newest group handed out, so a lower one still missing is // asked of its route once the reader says which; see `backfill_gaps`. - self.resumed = Some((self.copies.last().and_then(|copy| copy.floor), resolved.clone())); + self.resumed = Some(Resumed::Route( + self.copies.last().and_then(|copy| copy.floor), + resolved.clone(), + )); // The replaced copies keep delivering until the new one delivers its first group: a // group they get meanwhile may be one the new route never has. See `observe`. for copy in &mut self.copies { @@ -1465,7 +1949,7 @@ impl Reader { // newest is the route's own copy, not a backfill of it. if let Some(index) = self.copies.iter().rposition(|copy| copy.track.same_channel(&track)) { // The copy's own resolution ends at its old cap; the reader's does not. - let resolved = self.window().resolved(); + let resolved = self.window().resolved_by_key(); let mut copy = self.copies.remove(index); copy.generation = generation; copy.replaced = false; @@ -1483,21 +1967,16 @@ impl Reader { )); } copy.resolved = resolved; - let wire = self.mirrored.clamped(copy.floor, None, ©.resolved); - copy.send(wire); + copy.send(&self.mirrored, self.mirrored.ranges.keys().copied()); self.copies.push(copy); return; } let floor = self.newest; - // The wire request carries the floor from the first message: some sessions read - // the demand only once. - let wire = self.mirrored.clamped(floor, None, &resolved); self.copies.push(Copy { generation, - sub: Sub::Pending(Box::new(track.subscribe(wire))), + sub: Copy::subscribe(&track, &self.mirrored, floor, None, &resolved), track, floor, - from: self.mirrored.ranges.clone(), resolved, replaced: false, until: None, @@ -1549,11 +2028,7 @@ impl Reader { /// end is refused here, before waiting on a copy to refuse it, since the demand asked /// upstream leaves it out. fn refusal(&self) -> Option { - self.mirrored - .ranges - .iter() - .any(|range| range.end == Some(Anchor::Oldest)) - .then_some(Error::InvalidRange) + (self.mirrored.oldest_ends > 0).then_some(Error::InvalidRange) } /// Drop the replaced copies nothing needs any more; see [`Copy::retains`]. Whether one @@ -1854,7 +2329,8 @@ impl Reader { // a backfill that already ran out was let go: ask its route again. let copy = &self.copies[index]; if late && !copy.replaced && copy.floor.is_some() && Some(copy.generation) == self.generation { - self.resumed.get_or_insert_with(|| (None, Vec::new())); + self.resumed + .get_or_insert_with(|| Resumed::Route(None, BTreeMap::new())); } } // A later copy placed the window this pass: a group held back above may now be the lowest. @@ -2208,7 +2684,7 @@ impl Reader { } // Resolved as the reader first had them: the newest copy's resolution starts at its // resume point, and a bounded copy's own ends at its earlier cap. - let resolved = self.window().resolved(); + let resolved = self.window().resolved_by_key(); // A later resume point widens the route's backfill rather than asking again, so flaps // over a gap keep one per route. Waited for as long as a new one would be. let last = self.copies.len() - 1; @@ -2218,19 +2694,15 @@ impl Reader { { copy.until = Some(lowered.end.checked_sub(1)); copy.since = Some(copy.track.now()); - copy.from = self.mirrored.ranges.clone(); copy.resolved = resolved; - let wire = self.mirrored.clamped(copy.floor, copy.end(), ©.resolved); - copy.send(wire); + copy.send(&self.mirrored, self.mirrored.ranges.keys().copied()); return; } let floor = Some(lowered.start); - let wire = self.mirrored.clamped(floor, Some(lowered.end), &resolved); let copy = Copy { generation, - sub: Sub::Pending(Box::new(track.subscribe(wire))), + sub: Copy::subscribe(&track, &self.mirrored, floor, Some(lowered.end), &resolved), floor, - from: self.mirrored.ranges.clone(), resolved, // Bounded from the start: everything it may deliver is older than what the reader // already read, so it is waited for no longer than the reader's budget. @@ -2259,26 +2731,43 @@ impl Reader { let Some(floor) = self.window().floor() else { return; }; - let Some((replaced, resolved)) = self.resumed.take() else { + let Some(resumed_by) = self.resumed.take() else { return; }; let Some(resumed) = self.copies.last().and_then(|copy| copy.floor) else { return; }; // Each range from where the copy it replaced placed it, else where the reader did: a - // start at that copy's own resume point was only raised there. - let from = self - .mirrored - .clamped(Some(0), None, &resolved) - .ranges - .iter() - .map(|range| match range.start { - Anchor::Time(start) if replaced.is_none_or(|replaced| start > replaced) => start, - _ => floor, - }) - .min() - .unwrap_or(floor) - .max(self.groups.0); + // start at that copy's own resume point was only raised there. After an edit, only the + // ranges it added: the rest were judged when they came. + let start = |range: &track::Range, replaced: Option, resolved: Option| match range + .clamped(Some(0), None, resolved) + .start + { + Anchor::Time(start) if replaced.is_none_or(|replaced| start > replaced) => start, + _ => floor, + }; + let from = match &resumed_by { + Resumed::Route(replaced, resolved) => self + .mirrored + .ranges + .iter() + .map(|(key, range)| start(range, *replaced, resolved.get(key).copied())) + .min(), + Resumed::Ranges(keys) => { + let starts: Vec = keys + .iter() + .filter_map(|key| self.mirrored.ranges.get(key)) + .map(|range| start(range, None, None)) + .collect(); + // Retired since, so nothing new is covered. + if starts.is_empty() { + return; + } + starts.into_iter().min() + } + }; + let from = from.unwrap_or(floor).max(self.groups.0); if let Some(gap) = delivered.gaps(from..resumed).first() { self.backfill(gap.start..resumed, delivered); } @@ -2955,10 +3444,9 @@ impl kio::Task for Fetching { } } -/// A window over `ranges`, each keyed as `keys` names it, on a hop that refuses nothing. -fn keyed(keys: &[u64], ranges: &[track::Range], holdings: &impl Holdings) -> Window { - Window::keyed(keys.iter().copied().zip(ranges.iter().copied()), None, None, holdings) - .expect("a hop that holds nothing or holds it placed refuses nothing") +/// A window over `ranges`, each under its key, on a hop that refuses nothing. +fn keyed(ranges: impl IntoIterator, holdings: &impl Holdings) -> Window { + Window::keyed(ranges, None, None, holdings).expect("a hop that holds nothing or holds it placed refuses nothing") } #[cfg(test)] @@ -7495,6 +7983,191 @@ mod test { assert_eq!(rest, vec![10_000, 11_000]); } + /// Ranges a front's reader edits one key at a time reach each of its copies as an edit, + /// keyed as the reader keys them, and place the reader's window where the same ranges + /// given whole do, across a route switch and copies answered or not. + #[test] + fn edited_ranges_reach_every_copy_as_given_whole() { + use rand::{RngExt, SeedableRng}; + let mut rng = rand::rngs::StdRng::seed_from_u64(0x66726f6e74); + let routes = Producer::new(); + let groups = || { + let copy = copy(); + for key in (0..20_000).step_by(1000) { + write(©, key); + } + copy + }; + let (a, b) = (groups(), groups()); + routes.serve(a.consume()); + let range = |rng: &mut rand::rngs::StdRng| { + let start = match rng.random_range(0..4) { + 0 => Anchor::Relative(rng.random_range(0..5)), + _ => Anchor::Time(rng.random_range(0..24_000)), + }; + let end = rng.random_bool(0.6).then(|| Anchor::Time(rng.random_range(0..24_000))); + track::Range { start, end } + }; + let answer = |sub: &Subscriber| { + let mut reader = sub.reader(); + let groups = reader.groups; + for copy in &mut reader.copies { + let _ = copy.poll_ready(groups, &kio::Waiter::noop()); + } + }; + // Key 0 is the default subscription's one range. + let mut live = BTreeMap::from([(0, track::Range::oldest())]); + let (mut next, mut placed) = (0, 0); + let edited = routes.consume().subscribe(Subscription::default().into()); + for step in 0..300 { + match step { + 50 => answer(&edited), + 150 => routes.serve(b.consume()), + 200 => answer(&edited), + _ => {} + } + let retire: Vec = live.keys().copied().filter(|_| rng.random_bool(0.2)).collect(); + let add: Vec<(u64, track::Range)> = (0..rng.random_range(0..3)) + .map(|_| { + next += 1; + (next, range(&mut rng)) + }) + .collect(); + for key in &retire { + live.remove(key); + } + live.extend(add.iter().copied()); + edited + .subscription() + .edit(track::Edit { + retire, + add, + priority: 0, + delay: Delay::default(), + order: track::Order::default(), + }) + .unwrap(); + edited.mirror().sync(); + { + let reader = edited.reader(); + assert!(reader.mirrored.ranges.iter().eq(live.iter()), "step {step}"); + if let Some(Resumed::Ranges(keys)) = &reader.resumed { + assert!( + keys.iter().all(|key| live.contains_key(key)), + "step {step}: only live ranges wait to be backfilled" + ); + } + for copy in &reader.copies { + let held = copy.preferences().read(); + let keys: Vec = held.ranges().map(|(key, _)| key).collect(); + assert!( + keys.iter().eq(live.keys()), + "step {step}: the copy keys as the reader does" + ); + for key in keys { + assert_eq!( + held.range(key), + copy.wire(&reader.mirrored, key), + "step {step}, key {key}" + ); + } + } + } + if step < 50 { + continue; + } + let whole = routes + .consume() + .subscribe(Subscription::default().with_ranges(live.values().copied()).into()); + answer(&whole); + let resolved = edited.resolved_keys(live.keys().copied()); + assert_eq!(resolved, whole.resolved_keys(0..live.len() as u64), "step {step}"); + assert_eq!(edited.runs(0), whole.runs(0), "step {step}"); + placed += resolved.iter().flatten().count(); + } + assert!(placed > 1000, "the copies placed the ranges: {placed}"); + } + + /// A held subscription following its readers' demand asks, edit by edit, for the ranges + /// the whole fold does, as readers edit their ranges, arrive, and leave, and keeps an + /// entry only for each reader still here. + #[test] + fn a_held_subscription_follows_each_edit_as_the_whole_fold() { + use rand::{RngExt, SeedableRng}; + let mut rng = rand::rngs::StdRng::seed_from_u64(0x68656c64); + let routes = Producer::new(); + let a = copy(); + for key in (0..20_000).step_by(1000) { + write(&a, key); + } + let logical = logical(&routes); + routes.serve(a.consume()); + let weak = logical.weak(); + let range = |rng: &mut rand::rngs::StdRng| { + let start = match rng.random_range(0..4) { + 0 => Anchor::Relative(rng.random_range(0..5)), + _ => Anchor::Time(rng.random_range(0..24_000)), + }; + let end = rng.random_bool(0.6).then(|| Anchor::Time(rng.random_range(0..24_000))); + track::Range { start, end } + }; + let reader = || { + logical + .consume() + .subscribe(Subscription::default()) + .now_or_never() + .unwrap() + .unwrap() + }; + // Each reader with its live keys; key 0 is the default subscription's one range. + let mut readers: Vec<(track::Subscriber, Vec, u64)> = (0..3).map(|_| (reader(), vec![0], 0)).collect(); + let mut held = super::Held::new(a.consume().subscribe(Subscription::default())); + let sorted = |mut ranges: Vec| { + ranges.sort_unstable(); + ranges + }; + for step in 0..400 { + match rng.random_range(0..20) { + 0 => readers.push((reader(), vec![0], 0)), + 1 if readers.len() > 1 => { + readers.swap_remove(rng.random_range(0..readers.len())); + } + _ => { + let index = rng.random_range(0..readers.len()); + let (sub, live, next) = &mut readers[index]; + let retire: Vec = live.iter().copied().filter(|_| rng.random_bool(0.2)).collect(); + live.retain(|key| !retire.contains(key)); + let add: Vec<(u64, track::Range)> = (0..rng.random_range(0..3)) + .map(|_| { + *next += 1; + live.push(*next); + (*next, range(&mut rng)) + }) + .collect(); + sub.edit(track::Edit { + retire, + add, + priority: rng.random_range(0..3), + delay: Delay::default(), + order: track::Order::default(), + }) + .unwrap(); + } + } + assert!(routes.follow(&mut held, &weak)); + let (demand, subscribers) = weak.subscription().unwrap(); + let whole = routes.subscription(demand.clone(), subscribers); + let asked = held.sub.preferences().read().subscription(); + assert_eq!(sorted(asked.ranges), sorted(whole.ranges), "step {step}"); + assert_eq!(asked.priority, demand.priority, "step {step}"); + // A reader that left takes its entry, and the allocation that entry kept, with it. + let folded = held.fold.as_ref().map_or(0, |fold| fold.readers.len()); + assert_eq!(folded, readers.len(), "step {step}"); + } + readers.clear(); + assert!(!routes.follow(&mut held, &weak), "the last reader lets it go"); + } + /// A replacement updated to a time range it cannot resolve yet asks for that range, /// not everything from the floor: before it answers, and once it answers with nothing /// to resolve it against. @@ -7513,10 +8186,11 @@ mod test { }]; let updated = Subscription::default().with_ranges(ranges.clone()); assert!(matches!(copy.sub, Sub::Pending(_))); - copy.update(&updated); - assert_eq!(copy.wire(&updated).ranges, ranges); + let updated = Mirrored::of(&updated); + copy.update(&updated, &Change::All); + assert_eq!(copy.wire_all(&updated).ranges, ranges); assert!(copy.poll_ready((0, None), &kio::Waiter::noop()).is_ready()); - assert_eq!(copy.wire(&updated).ranges, ranges); + assert_eq!(copy.wire_all(&updated).ranges, ranges); } /// A copy that already answered resolves ranges the reader replaced against its cache, @@ -7538,8 +8212,9 @@ mod test { start: Anchor::Relative(2), end: None, }]); - copy.update(&updated); - assert_eq!(copy.wire(&updated).ranges[0].start, Anchor::Time(2)); + let updated = Mirrored::of(&updated); + copy.update(&updated, &Change::All); + assert_eq!(copy.wire_all(&updated).ranges[0].start, Anchor::Time(2)); } /// A replacement asked for a range reaching back past where the reader got to, while @@ -7886,9 +8561,9 @@ mod test { let old = &mut reader.copies[0]; old.replaced = true; old.until = Some(Some(3800)); - old.update(&mirrored); + old.update(&mirrored, &Change::Scalars); assert_eq!( - old.wire(&mirrored).ranges, + old.wire_all(&mirrored).ranges, vec![range], "the time end is inside the cap" ); @@ -7976,7 +8651,7 @@ mod test { old.replaced = true; old.until = Some(Some(12)); assert_eq!( - old.wire(&mirrored).ranges, + old.wire_all(&mirrored).ranges, [track::Range { start: track::Anchor::Time(8), end: Some(track::Anchor::Time(12)), @@ -7994,10 +8669,10 @@ mod test { let mut reader = sub.reader.lock().unwrap(); let old = &mut reader.copies[0]; old.until = Some(last); - assert_eq!(old.wire(&Subscription::default()).end(), end); + assert_eq!(old.wire_all(&Mirrored::of(&Subscription::default())).end(), end); // A reader's narrower bound survives an unbounded replacement cap. - let requested = Subscription::default().with_end(7); - assert_eq!(old.wire(&requested).end(), Some(end.unwrap_or(7))); + let requested = Mirrored::of(&Subscription::default().with_end(7)); + assert_eq!(old.wire_all(&requested).end(), Some(end.unwrap_or(7))); } } diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index d4a8d9c041..8bc6984c9f 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -458,49 +458,40 @@ impl Subscription { fn with_span(self, start: Option, end: Option) -> Self { self.with_ranges(Range::span(start, end)) } +} - /// These preferences with every range raised to start no earlier than `floor` and - /// stop before `end`, the exclusive end, without ever widening one. +impl Range { + /// This range raised to start no earlier than `floor` and stop before `end`, the + /// exclusive end, without ever widening it. /// - /// `resolved` names where each range resolved, by index, so a relative or time anchor - /// becomes its own group, raised to `floor` or capped at `end`. A start not resolved - /// keeps its anchor, since the floor may lie before it, so the caller drops what it - /// resolves to below `floor`; an end not resolved becomes the bound. - pub(super) fn clamped(&self, floor: Option, end: Option, resolved: &[Option]) -> Self { - let clamping = floor.is_some() || end.is_some(); - let ranges = self - .ranges - .iter() - .enumerate() - .map(|(index, range)| { - let resolved = resolved.get(index).copied().flatten().filter(|_| clamping); - let start = match (range.start, resolved) { - (Anchor::Oldest | Anchor::Relative(_) | Anchor::Time(_), Some(resolved)) => { - Anchor::Time(resolved.start) - } - (start, _) => start, - }; - let start = match (start, floor) { - (Anchor::Time(start), Some(floor)) => Anchor::Time(start.max(floor)), - (start, _) => start, - }; - let requested = match (range.end, resolved.and_then(|resolved| resolved.end)) { - (Some(Anchor::Relative(_) | Anchor::Time(_)), Some(resolved)) => match resolved.checked_sub(1) { - Some(last) => Some(Anchor::Time(last)), - None => return Range::EMPTY, - }, - (requested, _) => requested, - }; - let end = match (requested, end.map(|end| end.checked_sub(1))) { - (requested, None) => requested, - (_, Some(None)) => return Range::EMPTY, - (Some(Anchor::Time(requested)), Some(Some(last))) => Some(Anchor::Time(requested.min(last))), - (_, Some(Some(last))) => Some(Anchor::Time(last)), - }; - Range { start, end } - }) - .collect(); - Self { ranges, ..self.clone() } + /// `resolved` names where it resolved, so a relative or time anchor becomes its own + /// group, raised to `floor` or capped at `end`. A start not resolved keeps its anchor, + /// since the floor may lie before it, so the caller drops what it resolves to below + /// `floor`; an end not resolved becomes the bound. + pub(super) fn clamped(&self, floor: Option, end: Option, resolved: Option) -> Self { + let resolved = resolved.filter(|_| floor.is_some() || end.is_some()); + let start = match (self.start, resolved) { + (Anchor::Oldest | Anchor::Relative(_) | Anchor::Time(_), Some(resolved)) => Anchor::Time(resolved.start), + (start, _) => start, + }; + let start = match (start, floor) { + (Anchor::Time(start), Some(floor)) => Anchor::Time(start.max(floor)), + (start, _) => start, + }; + let requested = match (self.end, resolved.and_then(|resolved| resolved.end)) { + (Some(Anchor::Relative(_) | Anchor::Time(_)), Some(resolved)) => match resolved.checked_sub(1) { + Some(last) => Some(Anchor::Time(last)), + None => return Self::EMPTY, + }, + (requested, _) => requested, + }; + let end = match (requested, end.map(|end| end.checked_sub(1))) { + (requested, None) => requested, + (_, Some(None)) => return Self::EMPTY, + (Some(Anchor::Time(requested)), Some(Some(last))) => Some(Anchor::Time(requested.min(last))), + (_, Some(Some(last))) => Some(Anchor::Time(last)), + }; + Self { start, end } } } diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 6be5dfbc50..47c1a5ce3e 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -476,6 +476,24 @@ impl Window { self.ranges.keys().map(|key| self.resolved_key(*key)).collect() } + /// Each range resolved, by key, leaving out those [`Self::resolved_key`] can't place. + pub fn resolved_by_key(&self) -> BTreeMap { + self.ranges + .keys() + .filter_map(|key| Some((*key, self.resolved_key(*key)?))) + .collect() + } + + /// The keys of the ranges not yet settled, which keep [`Self::is_settled`] false: waiting + /// on a first group, on the groups held to place a start, or on a lookup. Costs those. + pub fn unsettled(&self) -> BTreeSet { + let mut keys = self.pending.clone(); + keys.extend(self.reaching.iter().copied()); + keys.extend(self.waiting.iter().map(|(_, key)| *key)); + keys.extend(self.looking.iter().map(|(_, key)| *key)); + keys + } + /// The exclusive end past every group in range, `None` when one stays open or an anchor /// waits. pub fn end(&self) -> Option { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 92f1dfb6b4..e4413de5d3 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, Unplaced, Window}; +use super::subscription::{Accounted, Containing, Cover, Holdings, Unplaced, Window, minus}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; @@ -5408,6 +5408,14 @@ impl Producer { snapshot_subscription(&subs, bound) } + /// Arm `waiter` for the next change to the subscribers' demand, without reading it: a + /// caller that follows each subscriber's own edits needs the wake, not the aggregate, + /// whose ranges cost every subscriber's to build. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn watch_subscription(&self, waiter: &kio::Waiter) { + watch_combined(&self.state, waiter); + } + /// The aggregate subscription once it changes: the most demanding request across all /// live subscribers, or `None` once the last one drops. Errors once the track is aborted. /// @@ -5784,6 +5792,12 @@ fn poll_combined_changed( subs.lock().poll_changed(bound, prev, waiter) } +/// Arm `waiter` for the next change to the counted aggregate, reading none of it. +fn watch_combined(state: &kio::Producer, waiter: &kio::Waiter) { + let subs = state.read().subscriptions.clone(); + subs.lock().watch(waiter); +} + /// Read the counted aggregate without walking the audience. fn snapshot_subscription(subs: &kio::Lock, bound: Option) -> Option { clamp_combined(subs.lock().combined(), bound) @@ -5821,6 +5835,36 @@ fn clamp_combined(combined: Option, bound: Option) -> Op Some(combined) } +/// A front's logical track served from one route's copy, for benches. +#[cfg(any(feature = "fuzz", all(test, feature = "async")))] +pub(crate) struct Routed { + /// The logical track readers subscribe to. + pub logical: Producer, + /// The route's copy, which its source writes. + pub copy: Producer, + /// Concludes the logical track once dropped. + _routes: super::resume::Producer, +} + +#[cfg(any(feature = "fuzz", all(test, feature = "async")))] +impl Routed { + /// A logical track named `name`, and a copy of it serving it, both with `info`. + pub(crate) fn new(name: &str, info: Info) -> Self { + let broadcast = Arc::new(broadcast::Info::default()); + let routes = super::resume::Producer::new(); + let logical = Request::new(broadcast.clone(), name) + .routes(routes.consume()) + .accept(info.clone()); + let copy = Producer::new(broadcast, name, info); + routes.serve(copy.consume()); + Self { + logical, + copy, + _routes: routes, + } + } +} + /// Aborts a track without keeping it open, from [`Producer::abort_handle`]. /// /// Holding one neither keeps the track alive nor counts as its producer, so a track whose @@ -5934,6 +5978,15 @@ impl TrackWeak { Demand::new(self.name.clone(), self.state.clone()) } + /// [`Self::subscription`] without its ranges, costing none of them. + pub(super) fn terms(&self) -> Option<(Subscription, usize)> { + let state = self.state.read(); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); + drop(state); + let subs = subs.lock(); + Some((clamp_combined(subs.terms(), bound)?, subs.len())) + } + /// The readers' aggregate demand and contribution count, or `None` without one. pub(super) fn subscription(&self) -> Option<(Subscription, usize)> { let state = self.state.read(); @@ -7172,6 +7225,11 @@ impl Subscribing { pub fn update(&mut self, subscription: Subscription) -> Result<()> { self.subscription.update(subscription) } + + /// The subscription's preferences, which [`Preferences::edit`] changes by key. + pub(super) fn preferences(&self) -> &Preferences { + &self.subscription + } } impl kio::Task for Subscribing { @@ -8089,8 +8147,8 @@ enum Servable { /// `cap`, with the reader's index of the live groups inside that cover. /// /// Built again whenever the window or cap moves, which a time end does on each group. The -/// snapshot is a pointer clone, and the index is kept while the cover is unchanged -/// (pointer-equal), so a move of only the cap rebuilds nothing. +/// snapshot is a pointer clone, and the index carries over: a move of only the cap changes +/// nothing in it, and a move of the cover costs the runs that moved. struct Several { groups: Cover, cap: Option, @@ -8115,6 +8173,9 @@ impl PartialEq for Several { /// One index per track searched: a group's expiry that moved to a replacement copy (see /// [`GroupExpiry::for_track`]) searches its reader's servable on that copy's track, whose /// arrival order and stamps are its own. +/// +/// Each index is for the cover it last searched: a search under an edited cover moves only +/// the runs the edit moved, so a SUBSCRIBE_UPDATE costs its own ranges, never the cache. #[derive(Default)] struct Live(Mutex>); @@ -8122,6 +8183,8 @@ struct Indexed { /// The track indexed, by its cache account. Held weakly, so a track that went away /// leaves an entry another track's can't be mistaken for. track: Weak, + /// The cover the entries were taken in for. + cover: Cover, /// How far into the arrival order this has taken in, as an absolute index. taken: usize, /// The stamp of each live group inside the cover, by key, as it arrived. @@ -8138,8 +8201,15 @@ impl Indexed { std::ptr::eq(self.track.as_ptr(), Arc::as_ptr(&state.cache)) } - /// Take in what arrived since the last call. + /// Take in what arrived since the last call, under `groups`. fn catch_up(&mut self, state: &TrackState, groups: &Cover) { + if self.taken == 0 { + // Nothing taken in yet: every live group has an arrival entry, so the walk below + // takes in all of them under `groups`. + self.cover = groups.clone(); + } else if self.cover != *groups { + self.recover(state, groups); + } for entry in state.arrivals_from(self.taken) { if groups.contains(entry.key) { self.groups.insert(entry.key, entry.stamp); @@ -8152,6 +8222,31 @@ impl Indexed { self.groups.retain(|key, stamp| current(state, *key, *stamp).is_some()); } } + + /// Move the entries to `groups` from the cover they were taken in for: only where the + /// two differ, which for an edited copy costs the runs the edit moved and the live groups + /// in them. A group not yet taken in from the arrival order still is, as an entry again. + fn recover(&mut self, state: &TrackState, groups: &Cover) { + let old = std::mem::replace(&mut self.cover, groups.clone()); + for span in groups.moved(&old) { + let (lo, hi) = (*span.start(), *span.end()); + for run in old.within(lo, hi) { + for lost in minus(run, groups) { + let keys: Vec = self.groups.range(lost).map(|(key, _)| *key).collect(); + for key in keys { + self.groups.remove(&key); + } + } + } + for run in groups.within(lo, hi) { + for gained in minus(run, &old) { + for (key, slot) in state.live(gained) { + self.groups.insert(*key, slot.stamp); + } + } + } + } + } } /// The live slot at `key`, if it is still the incarnation stamped `stamp`. @@ -8172,6 +8267,7 @@ impl Live { indexes.retain(|indexed| indexed.track.strong_count() > 0); indexes.push(Indexed { track: Arc::downgrade(&state.cache), + cover: Cover::default(), taken: 0, groups: BTreeMap::new(), }); @@ -8265,18 +8361,18 @@ impl Servable { } } - /// The groups `window` reads. `previous`, what the same reader was served before, lends - /// its index of the live groups while the cover is unchanged. - fn of(window: &Window, previous: &Self) -> Self { + /// The groups `window` reads, searched through `live`, the reader's index of the live + /// groups, which follows the cover it is searched under. + fn of(window: &Window, live: &Arc) -> Self { match window.groups() { None => Self::ALL, - Some(groups) => Self::covering(groups.clone(), None, previous), + Some(groups) => Self::covering(groups.clone(), None, live), } } /// The groups in `groups` below the exclusive `cap`. Only several runs allocate: every /// subscriber resolves one, so the common single run stays off the heap. - fn covering(groups: Cover, cap: Option, previous: &Self) -> Self { + fn covering(groups: Cover, cap: Option, live: &Arc) -> Self { let last = match cap { Some(cap) => match cap.checked_sub(1) { Some(last) => last, @@ -8297,11 +8393,11 @@ impl Servable { end: (*first.end()).min(last).checked_add(1), }; } - let live = match previous { - Self::Runs(several) if several.groups == groups => several.live.clone(), - _ => Arc::default(), - }; - Self::Runs(Arc::new(Several { groups, cap, live })) + Self::Runs(Arc::new(Several { + groups, + cap, + live: live.clone(), + })) } /// These groups below the exclusive `cap`. @@ -8322,7 +8418,7 @@ impl Servable { } Self::Runs(several) => { let cap = several.cap.map_or(cap, |own| own.min(cap)); - Self::covering(several.groups.clone(), Some(cap), self) + Self::covering(several.groups.clone(), Some(cap), &several.live) } } } @@ -8394,6 +8490,9 @@ struct Cursor { /// measures drift against the same groups after the cap or ranges move, and with its /// [`Control`]s, which republish it when they replace the ranges. published: kio::Producer, + /// This cursor's index of the live groups its servable window covers, kept across every + /// move of the window, so an edit to the ranges costs the runs it moved. + live: Arc, /// Groups the drift budget skipped since the count was last drained. Accumulated /// here rather than metered in place because the stats scope lives on the owning /// [`Subscriber`], which drains this after every read. @@ -8774,7 +8873,8 @@ impl Cursor { }; (filter, state.cache.clone()) }; - let published = Published::new(Servable::of(&filter.window, &Servable::NONE), filter.resolved, None); + let live = Arc::::default(); + let published = Published::new(Servable::of(&filter.window, &live), filter.resolved, None); Self { state, cache, @@ -8788,6 +8888,7 @@ impl Cursor { // Uncapped, the servable groups are the window's. servable: published.servable.clone(), published: kio::Producer::new(published), + live, stale: stats::Content::default(), seek_pending: BTreeMap::new(), record: Record::default(), @@ -8876,7 +8977,7 @@ impl Cursor { fn update_servable(&mut self) { // A moved window or cap notifies nothing this cursor parked on. idle::Idle::disarm(&mut self.idle); - let runs = Servable::of(&self.filter.window, &self.published.read().runs); + let runs = Servable::of(&self.filter.window, &self.live); let (revision, cap) = (self.filter.resolved, self.end_key); self.servable = runs.below(cap); let next = |current: &Published| { @@ -10176,7 +10277,7 @@ impl Republish { return; }; drop(state); - let runs = Servable::of(&window, &Servable::NONE); + let runs = Servable::of(&window, &Arc::default()); if let Ok(mut current) = published.write() && current.revision < revision { @@ -10937,15 +11038,18 @@ impl Subscriber { } } - /// This subscriber's own ranges, by index, as resolved here: `None` for one empty or - /// still waiting, and for every range of a front's reader. - pub(super) fn resolved_ranges(&mut self) -> Vec> { + /// This subscriber's own ranges keyed in `keys`, in that order, as resolved here: `None` + /// for one empty, still waiting, or not held, and for every range of a front's reader, + /// whose copies resolve them. + pub(super) fn resolved_ranges(&mut self, keys: impl IntoIterator) -> Vec> { match &mut self.inner { Inner::Plain(cursor) => { cursor.sync(); - cursor.filter.window.resolved() + keys.into_iter() + .map(|key| cursor.filter.window.resolved_key(key)) + .collect() } - Inner::Resume(..) => Vec::new(), + Inner::Resume(..) => keys.into_iter().map(|_| None).collect(), } } @@ -11129,6 +11233,14 @@ impl Subscriber { self.control().subscription() } + /// This subscriber's preferences, read by key without materializing them. + pub(super) fn preferences(&self) -> &Preferences { + match &self.inner { + Inner::Plain(cursor) => &cursor.subscription, + Inner::Resume(resume, _) => resume.subscription(), + } + } + /// Replace this subscriber's delivery preferences. /// /// Stored verbatim; the publisher's max age window is applied to the aggregate, not @@ -12183,6 +12295,13 @@ impl Request { snapshot_subscription(&subs, bound) } + /// Arm `waiter` for the next change to the demand of everyone waiting, without reading + /// it; see [`Producer::watch_subscription`]. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn watch_subscription(&self, waiter: &kio::Waiter) { + watch_combined(&self.state, waiter); + } + /// The aggregate [`subscription`](Self::subscription) once it changes, or `None` once /// nobody is waiting. #[cfg_attr(feature = "async", kio::wrapper)] @@ -17383,7 +17502,7 @@ mod test { /// A search over a servable window of several runs, through the reader's index of the /// live groups, finds what a scan of every live group would, in either direction, as /// groups arrive, start, abort, leave the cache, or arrive again between searches that - /// keep the same index, and as the window is edited. Each window is searched on two tracks, as a + /// keep the same index, and as the window is edited or replaced. Each window is searched on two tracks, as a /// group's expiry that moved to a replacement copy searches its reader's. #[test] fn live_index_searches_match_a_scan_of_every_live_group() { @@ -17416,6 +17535,7 @@ mod test { let mut layer = Vec::new(); let mut groups = [Vec::new(), Vec::new()]; let mut servable = Servable::NONE; + let live = Arc::::default(); for step in 0..30 { let key = next(KEYS); let track = next(2) as usize; @@ -17452,8 +17572,12 @@ mod test { for run in &layer { cover.add(run.clone(), 1); } + } else if next(8) == 0 { + // Ranges replaced whole: a cover of another lineage. + layer.clear(); + cover = Cover::new(runs(8)); } - servable = Servable::covering(cover.clone(), cap, &servable); + servable = Servable::covering(cover.clone(), cap, &live); } for (track, producer) in producers.iter().enumerate() { let state = producer.state.read(); @@ -17492,7 +17616,7 @@ mod test { .map(|key| producer.create_group(group::Info { start: key }).unwrap()) .collect(); let runs = Cover::new([0..=999, 1001..=u64::MAX]); - let servable = Servable::covering(runs, None, &Servable::NONE); + let servable = Servable::covering(runs, None, &Arc::default()); let Servable::Runs(several) = &servable else { panic!("several runs"); }; @@ -17559,7 +17683,7 @@ mod test { // The runs counted once, and again with the first counted twice. let covers = [Cover::new(runs()), Cover::new(runs().chain(runs().take(1)))]; for groups in covers { - let servable = Servable::covering(groups, None, &Servable::NONE); + let servable = Servable::covering(groups, None, &Arc::default()); if let [(first, last)] = bounds { assert!( servable @@ -28884,7 +29008,7 @@ mod test { }])); assert_eq!(drain(&mut subscriber), vec![0, 400]); assert_eq!( - subscriber.resolved_ranges(), + subscriber.resolved_ranges([0]), vec![Some(Resolved { start: 0, end: Some(501), diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 9863430ed1..8c482dbe8b 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -74,6 +74,18 @@ impl Subscriptions { }) } + /// [`Self::combined`] without its ranges, costing none of them. + pub(super) fn terms(&self) -> Option { + let (priority, delay, order, feed) = self.scalars()?; + Some(Subscription { + priority, + delay, + ranges: Vec::new(), + order, + feed, + }) + } + /// The largest delay budget without materializing the subscription's ranges. pub(super) fn max_delay(&self) -> Duration { self.max_delay @@ -473,6 +485,18 @@ impl Prefs { } } + /// Key the ranges by `keys`, one per range in the order held, keeping each range: every + /// reader resolves them all again. + fn rekey(&mut self, keys: impl IntoIterator) { + let ranges = std::mem::take(&mut self.ranges); + let count = ranges.len(); + self.ranges = keys.into_iter().zip(ranges.into_values()).collect(); + debug_assert_eq!(self.ranges.len(), count, "one key per range"); + self.revision += 1; + self.log.clear(); + self.base = self.revision; + } + /// Replace every preference, rekeying the ranges by position when they change: every /// reader resolves them all again. fn replace(&mut self, next: Subscription) { @@ -599,6 +623,14 @@ impl Preferences { } impl Preferences { + /// Key the ranges by `keys`, one per range in the order held, as [`Edit`]s then name + /// them. The ranges stay as they are, so the aggregate does too. + pub(in crate::model) fn rekey(&self, keys: impl IntoIterator) -> crate::Result<()> { + let mut value = self.0.value.write().map_err(|_| crate::Error::Closed)?; + value.rekey(keys); + Ok(()) + } + pub(in crate::model) fn same_channel(&self, other: &Self) -> bool { self.0.value.same_channel(&other.0.value) } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index 517a5d8f78..c1ae7c4369 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -2211,7 +2211,7 @@ mod tests { _ => Servable::covering( Cover::new(runs(6).into_iter().chain(runs(2))), (next(3) == 0).then(|| next(KEYS + 2)), - &Servable::NONE, + &Default::default(), ), }; let mut misses = Misses::default(); diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index c96c3da3bd..f5fdf6c299 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 window-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 publisher-update refresh observed compression datagrams; do javascript "$benchmark" just js bench "$benchmark" done javascript catalog just js catalog-bench