Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test-changed list:
coverage package="":
python3 ../sh/js/coverage.py '{{ package }}'

# Run one net benchmark: broadcasts, forward, requests, fills, fill-probe, reader, frames, track, resume, observed, drift, ledger, bandwidth, replay, varint, lite-varint, priority, subscribe-drop, refresh, compression, or datagrams.
# Run one net benchmark: broadcasts, forward, requests, fills, fill-probe, reader, frames, track, resume, observed, drift, ledger, bandwidth, replay, varint, lite-varint, priority, subscribe-drop, window-drop, refresh, compression, or datagrams.
bench name:
uv run --no-project python ../sh/js/install.py frozen
bun net/bench/{{ name }}.ts
Expand Down
54 changes: 54 additions & 0 deletions js/net/bench/window-drop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Sweep what a drop costs a reader with many ranges: one-group time ranges a group apart, each
* waiting for the groups held to place it, given up lowest first so each drop places one more.
* Mirrors the Rust `publisher_drop` bench. A drop must cost about the same however many ranges
* the reader has, or the window has gone back to trying every range or rebuilding them all.
*/

import { StreamCode, StreamError } from "../src/error.ts";
import { hooks } from "../src/internal.ts";
import { Timescale } from "../src/time.ts";
import { Producer, type Range } from "../src/track.ts";

// `WINDOW_DROP_MAX` caps both axes, for a quick run.
const sweep = [1, 32, 1024, 8192].filter(
(count) => count <= Number(process.env.WINDOW_DROP_MAX ?? Number.POSITIVE_INFINITY),
);
// Each cell times at least this many drops, so a cell of one drop isn't one noisy sample.
const timed = 256;

function measure(ranges: number, drops: number): number {
const producer = new Producer("bench").accept({ timescale: Timescale.MILLI, maxAge: null });
// Every other group, so no two ranges merge into one run, and one open range past them all so
// the reader never settles.
const spans: Range[] = Array.from({ length: ranges }, (_, range) => {
const at = BigInt(2 * range);
return { start: { time: at }, end: { time: at } };
});
spans.push({ start: { time: BigInt(2 * ranges) } });
const subscriber = producer.subscribe({ ranges: spans });
subscriber.tryRecvGroup();
// Alternating errors keep adjacent drops separate runs.
const errors = [new StreamError(StreamCode.NotFound), new StreamError(StreamCode.Cancel)];
const start = performance.now();
for (let range = 0; range < drops; range++) {
const from = range === 0 ? 0n : BigInt(2 * range - 1);
hooks.dropTrackGroups(producer, from, BigInt(2 * range + 1), { error: errors[range % 2] });
subscriber.tryRecvGroup();
}
const elapsed = performance.now() - start;
producer.close();
return (elapsed * 1000) / drops;
}

// Untimed, so the first cell doesn't measure the JIT warming up.
measure(32, 32);
console.log("ranges,drops,drop_us");
for (const ranges of sweep) {
for (const drops of sweep) {
if (drops > ranges) continue;
const reps = Math.max(3, Math.ceil(timed / drops));
const times = Array.from({ length: reps }, () => measure(ranges, drops)).sort((a, b) => a - b);
console.log(`${ranges},${drops},${times[Math.floor(reps / 2)].toFixed(2)}`);
}
}
28 changes: 28 additions & 0 deletions js/net/src/drops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,31 @@ test("drops: what a change replaced or forgot is reported since its revision, un
expect(drops.removedSince(recent)).toBeUndefined();
expect(drops.removedSince(drops.revision)).toEqual([]);
});

test("drops: the keys given up from a key on stop where a walk of the runs stops", () => {
const drops = new DropSet();
const errors = [new NotFound("a"), new NotFound("b")];
let seed = 7;
const random = (below: number) => {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
// The high bits: an LCG's low bits cycle.
return (seed >>> 16) % below;
};
for (let step = 0; step < 400; step++) {
const start = BigInt(random(64));
const end = start + BigInt(1 + random(6));
if (random(5) === 0) drops.clear([{ start, end }]);
else if (random(4) === 0) drops.set(start, end);
else drops.set(start, end, { error: errors[random(2)], skip: random(3) === 0 || undefined });
for (let from = 0n; from < 72n; from++) {
let at = from;
for (const run of drops.overlapping(from, 1000n)) {
if (run.start > at) break;
at = run.end;
}
expect(drops.coveredFrom(from)).toBe(at);
expect(drops.covers(from, at)).toBe(true);
expect(drops.covers(from, at + 1n)).toBe(false);
}
}
});
15 changes: 15 additions & 0 deletions js/net/src/drops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ const LOG_SLACK = 64;
*/
export class DropSet {
readonly #runs = new Runs<DropRun>(sameHole);
// The keys in `#runs`, whatever the reason, as coalesced runs, so whether drops cover a span is
// one lookup rather than a walk of every run in it. Mirrors the Rust `TrackState::dropped`.
readonly #keys = new Runs<Run>(() => true);
// Each run's last change, oldest first. An entry whose run was since replaced is dead, and
// the log is rebuilt once dead entries clearly outnumber live ones.
#log: { revision: number; run: DropRun }[] = [];
Expand Down Expand Up @@ -169,13 +172,25 @@ export class DropSet {
return this.#runs.overlapping(start, end);
}

/** Where the keys given up from `from` on, whatever the reason, stop: `from` when it isn't given up. */
coveredFrom(from: bigint): bigint {
return this.#keys.at(from)?.end ?? from;
}

/** Whether every key in `[start, end)` is given up, whatever the reason. An empty run is. */
covers(start: bigint, end: bigint): boolean {
return start >= end || this.coveredFrom(start) >= end;
}

/** Give up `[start, end)` as `run` says, or forget it when `run` is undefined. */
set(start: bigint, end: bigint, run?: Omit<DropRun, "start" | "end">): void {
this.#keys.set(start, end, run && { start, end });
this.#apply(this.#runs.set(start, end, run && { ...run, start, end }));
}

/** Forget each of `ranges`, sorted and disjoint, in one pass. */
clear(ranges: readonly Run[]): void {
this.#keys.clear(ranges);
this.#apply(this.#runs.clear(ranges));
}

Expand Down
7 changes: 6 additions & 1 deletion js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type {
} from "./track.ts";
import { max, min } from "./util/key.ts";
import * as Varint from "./varint.ts";
import type { Resolved, Window } from "./window.ts";
import type { Holdings, Resolved, Window } from "./window.ts";
import type { Advertised, Advertisements } from "./wire.ts";

/** A null {@link Delay.max} in milliseconds, which the model holds as the largest value every peer can read. */
Expand Down Expand Up @@ -703,6 +703,8 @@ export const hooks: {
subscriberEnd: (subscriber: Subscriber) => Position | undefined;
/** The error that end carries, if the track does not end cleanly. Assigned by `track.ts`. */
subscriberEndError: (subscriber: Subscriber) => Error | undefined;
/** What this subscriber resolves its ranges against, for a test. Assigned by `track.ts`. */
subscriberHoldings: (subscriber: Subscriber) => Holdings;
/**
* How many keys just below `key` the publisher declared it never produced: the skip run
* ending at `key`, which a moq-transport Prior Group ID Gap names. Mirrors the Rust
Expand Down Expand Up @@ -1131,6 +1133,9 @@ export const hooks: {
subscriberEndError: () => {
throw new Error("track.ts not loaded");
},
subscriberHoldings: () => {
throw new Error("track.ts not loaded");
},
skippedBelow: () => {
throw new Error("track.ts not loaded");
},
Expand Down
33 changes: 33 additions & 0 deletions js/net/src/track.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5624,3 +5624,36 @@ test("finishing returns handles that write nothing new but fill a late group", a
expect(hooks.tryRecvGroup(subscriber, false)).toEqual({ kind: "done" });
finished.close();
});

test("track: a reader's unplaced times are the times its holdings can't place", () => {
let seed = 5;
const random = (below: number) => {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;
// The high bits: an LCG's low bits cycle.
return (seed >>> 16) % below;
};
const errors = [new StreamError(StreamCode.NotFound), new StreamError(StreamCode.Cancel)];
for (let round = 0; round < 400; round++) {
const received = random(2) === 0;
const producer = received ? receivedTrack() : new TrackProducer("test").accept({ maxAge: null });
const reader = producer.subscribe();
for (let step = random(16); step >= 0; step--) {
const holdings = hooks.subscriberHoldings(reader);
const unplaced = holdings.unplaced?.();
expect(unplaced).toBeDefined();
for (let at = 0n; at < 64n; at++) {
const open = unplaced?.runs.some((run) => run.start <= at && at < run.end) ?? false;
expect([received, at, holdings.containing(at) === "unknown"]).toEqual([received, at, open]);
}
const key = BigInt(random(48));
if (random(2) === 0) {
try {
putGroup(producer, key);
} catch {
// A key this copy refuses, such as one a skip covers.
}
} else hooks.dropTrackGroups(producer, key, key + BigInt(1 + random(4)), { error: errors[random(2)] });
}
producer.close();
}
});
69 changes: 54 additions & 15 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,8 @@ let timeAnchors: (producer: Producer) => {
atKeys: boolean;
starts: ReadonlyMap<bigint, bigint>;
relative: ReadonlyMap<bigint, { start: bigint; latest?: bigint }>;
/** Bumped whenever the others, or whether the producer is a received copy, change. */
placing: number;
};

/** The groups a reader reads, and the exclusive end it stops at; undefined before its ranges resolve. */
Expand Down Expand Up @@ -1318,6 +1320,9 @@ export class Producer {
#timeStarts = new Map<bigint, bigint>();
// On a lite-07 copy, where the publisher started each live range counting back, by its count.
#relativeStarts = new Map<bigint, { start: bigint; latest?: bigint }>();
// Counts changes to how a time start is placed other than by groups arriving or drops: the wire,
// `#timeStarts`, and `#startsAtKeys`. Mirrors the Rust `TrackState::placing`.
#placing = 0;
// The highest-key checked start evicted. Eviction is not a key prefix, so a
// group above it is checked against it as well as its cached neighbors; one at or below
// it may have lost its lower bound. One entry, so nothing grows with the cache.
Expand Down Expand Up @@ -1510,6 +1515,7 @@ export class Producer {
resubscribe = (producer) => producer.#updateSubscription();
hooks.cacheTrack = (producer) => {
producer.#wire = undefined;
producer.#placing++;
// Nothing feeds the tail anymore, so it owes nothing and readers end at the boundary.
for (const state of [producer.#state, ...producer.#sinks]) state.owed.set(false);
producer.#endLargest();
Expand All @@ -1529,6 +1535,7 @@ export class Producer {
: undefined;
hooks.receiveTrack = (producer, starts = {}) => {
producer.#wire = { starts, served: producer.#wire?.served };
producer.#placing++;
};
hooks.receiveGroup = (producer, { start: sequence, offset: start, fill }) =>
producer.#createGroup(sequence, start, fill);
Expand All @@ -1548,12 +1555,15 @@ export class Producer {
atKeys: producer.#startsAtKeys,
starts: producer.#timeStarts,
relative: producer.#relativeStarts,
placing: producer.#placing,
});
hooks.startsAtKeys = (producer) => {
producer.#startsAtKeys = true;
producer.#placing++;
};
hooks.timeStarts = (producer, starts) => {
producer.#timeStarts = new Map(starts);
producer.#placing++;
};
hooks.relativeStarts = (producer, starts) => {
producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }]));
Expand Down Expand Up @@ -3590,17 +3600,16 @@ export class Subscriber {
const producer = state.producer;
if (this.#resume || !producer) return UNHELD;
const keys = keysOf(producer);
// Whether drops of a kind `kind` accepts cover every key in `[start, end)`. An empty run is.
const covered = (start: bigint, end: bigint, kind: (run: DropRun) => boolean) => {
if (start >= end) return true;
let at = start;
for (const run of state.drops.overlapping(start, end)) {
if (!kind(run) || run.start > at) return false;
at = run.end;
}
return at >= end;
};
const drops = state.drops;
const anchors = timeAnchors(producer);
// A lite-07 copy holds only part of the track: a group held before a time contains it only
// once nothing between them is still to arrive.
const wire = receivedCopy(producer) && !state.dropsAnchors.peek();
// The times from `from` on that the drops from there leave open.
const uncovered = (from: bigint): Run | undefined => {
const start = drops.coveredFrom(from);
return start < UNBOUNDED ? { start, end: UNBOUNDED } : undefined;
};
return {
resolves: () => true,
containing: (time) => {
Expand All @@ -3612,22 +3621,43 @@ export class Subscriber {
// A lite-07 copy holds only part of the track: a group held before `time` contains it
// only once nothing between them is still to arrive. Mirrors the Rust
// `TrackState::containing_received`.
if (receivedCopy(producer) && !state.dropsAnchors.peek()) {
if (wire) {
if (below === time) return { group: time };
const start = anchors.starts.get(time);
// The publisher serves nothing at or before it, as for a time older than every group.
if (start !== undefined) return start <= time ? { group: start } : "before";
if (below !== undefined && (covered(below + 1n, time + 1n, () => true) || index < keys.length)) {
if (below !== undefined && (drops.covers(below + 1n, time + 1n) || index < keys.length)) {
return { group: below };
}
if (below === undefined && covered(0n, time + 1n, () => true)) return "before";
if (below === undefined && drops.covers(0n, time + 1n)) return "before";
return "unknown";
}
if (below !== undefined) return { group: below };
// Every key at or before it is gone, so whichever group held it will never arrive.
if (covered(0n, time + 1n, () => true)) return "before";
if (drops.covers(0n, time + 1n)) return "before";
return keys.length === 0 ? "unknown" : "before";
},
// The "unknown" times of `containing`, apart from those `anchors.starts` places, which the
// mark covers. Mirrors the Rust `Holdings::unplaced for TrackState`.
unplaced: () => {
const mark = anchors.placing * 4 + (state.dropsAnchors.peek() ? 2 : 0) + (state.fetch ? 1 : 0);
const runs: Run[] = [];
const first = keys[0];
const last = keys.at(-1);
if (anchors.atKeys || state.fetch) {
// Every time starts a group.
} else if (first === undefined || last === undefined) {
const open = uncovered(0n);
if (open) runs.push(open);
} else if (wire) {
// Below every group, up to the first, and above the last, past the drops after it.
const below = uncovered(0n);
if (below && below.start < first) runs.push({ start: below.start, end: first });
const above = uncovered(last + 1n);
if (above) runs.push(above);
}
return { runs, mark };
},
nextStart: (from) => keys[keyIndex(keys, from)],
// A lite-07 copy holds only part of the track, so it starts where its publisher did,
// counting from the same latest group: a newer one moved the live edge.
Expand All @@ -3648,7 +3678,15 @@ export class Subscriber {
reachBack: (_latest, min) => this.#reachBack(min),
// Skip runs coalesce, so a gap between two held groups is one run unless the cache
// evicted a group inside it.
neverProduced: (start, end) => covered(start, end, (run) => run.skip === true),
neverProduced: (start, end) => {
if (start >= end) return true;
let at = start;
for (const run of drops.overlapping(start, end)) {
if (run.skip !== true || run.start > at) return false;
at = run.end;
}
return at >= end;
},
};
}

Expand Down Expand Up @@ -4195,6 +4233,7 @@ export class Subscriber {
makeSubscriber = (name, state) => new Subscriber(name, state);
hooks.subscriberEnd = (subscriber) =>
subscriber.#resume ? subscriber.#resume.end() : subscriber.#state.end.peek()?.at;
hooks.subscriberHoldings = (subscriber) => subscriber.#holdings();
hooks.subscriberEndError = (subscriber) =>
subscriber.#resume ? subscriber.#resume.endError() : subscriber.#state.end.peek()?.error;
hooks.askSubscriber = (subscriber, ask) => {
Expand Down
Loading