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
14 changes: 10 additions & 4 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ reconnects x 64 characters, and keeps its run directory as the `bench-browser`
artifact.

The in-memory frame, retained-history, gap ledger, bandwidth allocation, cached-group
replay, resume-history, and buffered-group paths also run directly in Chromium:
replay, resume-history, buffered-group, and peer grant paths also run directly in Chromium:

```bash
nix develop --command bun bench/browser/micro.ts --bench frames --out .tmp/frames.json base=/path/to/release current=. aa=.
Expand All @@ -507,6 +507,7 @@ nix develop --command bun bench/browser/micro.ts --bench bandwidth --out .tmp/ba
nix develop --command bun bench/browser/micro.ts --bench replay --out .tmp/replay.json before=/path/to/main current=. aa=.
nix develop --command bun bench/browser/micro.ts --bench resume-history --out .tmp/resume-history.json before=/path/to/main current=. aa=.
nix develop --command bun bench/browser/micro.ts --bench buffered --out .tmp/buffered.json before=/path/to/main current=. aa=.
nix develop --command bun bench/browser/micro.ts --bench pattern --out .tmp/pattern.json before=/path/to/main current=. aa=.
```

Each variant uses its own workspace source and a persistent, cross-origin isolated
Expand All @@ -527,6 +528,9 @@ change, a demand change and a ceiling change until the allocator republishes, an
peek of every reservation, checking every grant against a reference division. Its
Rust counterpart is `cargo bench -p moq-net --features fuzz --bench bandwidth`, whose
`BANDWIDTH_COUNTS=<iterations>` mode prints allocations and wakes per operation.
`pattern` runs `js/pattern/bench/algebra.ts` against every checkout's `@moq/pattern`:
the same peer grant shapes as the Rust `pattern-algebra` sweep below, reduced and
compared against a previous grant, up to a whole `AUTH_OK`.
The drift sweep includes untimed controls and timed groups with decode-order
B-frames, independently varying publisher and reader counts and retained history.
Its `time_mode` column is 0 for untimed, 1 for millisecond timestamps, and 2
Expand Down Expand Up @@ -570,14 +574,16 @@ route hold, from Chromium's precise memory info; `serve_us` and
`read_us` include no setup. Bun's collector scans its
stack conservatively, so the fixture refuses to run there.

The browser smoke lane preserves one measured round of all seven fixtures, and drift,
ledger, bandwidth, replay and buffered also run in the nightly JS benchmark leg. Timing is
The browser smoke lane preserves one measured round of all eight fixtures, and drift,
ledger, bandwidth, replay, buffered and pattern also run in the nightly JS benchmark leg. Timing is
observational, with the A/A spread showing the host's noise floor.

## Pattern and container coverage

Rust Criterion coverage includes `moq-pattern`'s
`matching` (pattern count and path depth, last match and miss) and `moq-mux`'s
`matching` (pattern count and path depth, last match and miss; and, in
`pattern-algebra`, whole-union operations, including the worst known peer grant
shapes swept over encoded size up to a whole `AUTH_OK`) and `moq-mux`'s
`containers` (CMAF and Annex-B import/export). Container cases vary frame count
and payload size; CMAF also varies track count. Separate frame and byte rates
use the same fixtures, with byte rates counting the encoded stream size.
Expand Down
5 changes: 4 additions & 1 deletion bench/browser/micro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const benchmarks: Record<string, { entry: string; rows: number }> = {
replay: { entry: "js/net/bench/replay.ts", rows: 72 },
"resume-history": { entry: "js/net/bench/resume-history.ts", rows: 72 },
buffered: { entry: "js/hang/bench/buffered.ts", rows: 36 },
pattern: { entry: "js/pattern/bench/algebra.ts", rows: 12 },
};
const selected = Object.hasOwn(benchmarks, benchmark) ? benchmarks[benchmark] : undefined;
if (!selected) throw new Error(`--bench must be one of: ${Object.keys(benchmarks).join(", ")}`);
Expand Down Expand Up @@ -55,7 +56,9 @@ for (const [name, root] of roots) {
importer.endsWith(`/${dir}/${benchmark}.ts`) ? { path: resolve(root, dir, path) } : undefined,
);
build.onLoad(
{ filter: /\/bench\/(frames|drift|ledger|bandwidth|replay|resume-history|buffered)\.ts$/ },
{
filter: /\/bench\/(frames|drift|ledger|bandwidth|replay|resume-history|buffered|algebra)\.ts$/,
},
async ({ path }) => {
const source = await Bun.file(path).text();
if (benchmark !== "frames")
Expand Down
2 changes: 1 addition & 1 deletion bench/browser/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bun install --frozen-lockfile

cd "$HERE"
# The in-memory paths share Chromium installation and artifact retention with this lane.
for benchmark in frames drift ledger bandwidth replay resume-history buffered; do
for benchmark in frames drift ledger bandwidth replay resume-history buffered pattern; do
bun micro.ts --bench "$benchmark" --rounds 1 --out "$HARNESS_RUN/$benchmark.json" \
current="$WORKSPACE" aa="$WORKSPACE"
done
Expand Down
6 changes: 6 additions & 0 deletions js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ arrivals-bench:
bun install --frozen-lockfile
bun watch/bench/arrivals.ts

# Reduce and compare the worst known peer grant shapes, swept over AUTH_OK size.
pattern-bench:
bun install --frozen-lockfile
bunx tsc --noEmit --project pattern/bench/tsconfig.json
bun pattern/bench/algebra.ts

# Sweep manual-clock armed timers x due timers, with independent and shared abort scopes.
clock-bench:
bun install --frozen-lockfile
Expand Down
2 changes: 1 addition & 1 deletion js/net/src/auth_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface AuthWire {
unsupported(stream: Stream, reason: string): Promise<void>;
}

/** The paths any of `grants` allows. Reduced once, so it costs about the grants' total size. */
/** The paths any of `grants` allows. Reduced once over all of them, not pairwise. */
function union(grants: Iterable<Grant>): Grant {
const publish: Path.Pattern[] = [];
const subscribe: Path.Pattern[] = [];
Expand Down
92 changes: 92 additions & 0 deletions js/pattern/bench/algebra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Reduce and compare a peer grant shaped against the union index, swept over its encoded size
* up to a whole AUTH_OK. Mirrors the Shape cases of the Rust `pattern-algebra` benchmark.
*/
import { Pattern, Patterns } from "@moq/pattern";

/** The largest moq-lite control message, and so the largest AUTH_OK. */
const MAX_MESSAGE = 65535;

/** Positions a mask chooses `*` or `y` for, half of them `*`. */
const FREE = 14;

/** A varint's size as AUTH_OK writes counts and lengths; QUIC's, never smaller than lite's. */
function varint(value: number): number {
return value < 64 ? 1 : value < 16384 ? 2 : 4;
}

/** A union's encoded size as AUTH_OK carries it: a count, then each text with its length. */
function wire(texts: string[]): number {
return texts.reduce((size, text) => size + varint(text.length) + text.length, varint(texts.length));
}

/**
* The grant's texts, filling at most `bytes` encoded, and a previous grant covering every one,
* as a regrant compares them. Every member is incomparable, so reduction keeps them all.
*
* `mask` pairs `*`/`y` masks of equal weight, each ending in `y`, with literal `y/…/y/xNNNN`
* queries that every mask matches but for the last segment. `chain` is the correlated partials
* `a^i*z^(n-i)`, each sharing prefixes and suffixes with all the others.
*/
function grant(shape: "mask" | "chain", bytes: number): { texts: string[]; previous: Patterns } {
const parse = (texts: string[]) => new Patterns(texts.map((text) => Pattern.parse(text)));
if (shape === "chain") {
const chain = (n: number) =>
Array.from({ length: n - 1 }, (_, i) => `${"a".repeat(i + 1)}*${"z".repeat(n - i - 1)}`);
let n = 2;
while (wire(chain(n + 1)) <= bytes) n++;
const texts = chain(n);
return { texts, previous: parse(texts) };
}
const masks: string[] = [];
const queries: string[] = [];
let size = 0;
for (let bits = 0, index = 0; bits < 1 << FREE; bits++) {
let weight = 0;
for (let at = 0; at < FREE; at++) weight += (bits >> at) & 1;
if (weight !== FREE / 2) continue;
const mask = [...Array.from({ length: FREE }, (_, at) => ((bits >> at) & 1 ? "*" : "y")), "y"].join("/");
const query = `${Array(FREE).fill("y").join("/")}/x${String(index++).padStart(4, "0")}`;
size += varint(mask.length) + mask.length + varint(query.length) + query.length;
if (varint(masks.length * 2 + 2) + size > bytes) break;
masks.push(mask);
queries.push(query);
}
// `*/…/*/x*` holds every query, and a walk tries a literal before `*`, so each query
// reaches it only after every mask.
const previous = parse([...masks, `${Array(FREE).fill("*").join("/")}/x*`]);
return { texts: [...masks, ...queries], previous };
}

/** Time each shape's reduction and regrant comparison, repeated to at least 100 ms per row. */
export function run(): void {
console.log("bytes,shape,op,members,ms_per_op");
for (const shape of ["mask", "chain"] as const) {
for (const bytes of [4096, 16384, MAX_MESSAGE]) {
const { texts, previous } = grant(shape, bytes);
const size = wire(texts);
if (size > bytes || size * 16 <= bytes * 15) throw new Error(`${shape} fills ${size} of ${bytes} bytes`);
const message = texts.map((text) => Pattern.parse(text));
const reduced = new Patterns(message);
if (reduced.size !== message.length) throw new Error(`${shape} members are not incomparable`);
if (!previous.covers(reduced)) throw new Error(`${shape} previous grant does not cover it`);
const ops: [string, () => unknown][] = [
["collect", () => new Patterns(message).size],
["covers", () => previous.covers(reduced)],
];
for (const [op, body] of ops) {
let iterations = 0;
const start = performance.now();
let elapsed = 0;
while (elapsed < 100 || iterations < 3) {
body();
iterations++;
elapsed = performance.now() - start;
}
console.log(`${bytes},${shape},${op},${message.length},${(elapsed / iterations).toFixed(3)}`);
}
}
}
}

if (import.meta.main) run();
8 changes: 8 additions & 0 deletions js/pattern/bench/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "..",
"noEmit": true
},
"include": ["*.ts"]
}
2 changes: 1 addition & 1 deletion js/pattern/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"scripts": {
"build": "rm -rf dist && tsc -b tsconfig.build.json && bun ../common/package.ts",
"check": "tsc --noEmit",
"check": "tsc --noEmit && tsc --noEmit --project bench/tsconfig.json",
"test": "bun test --only-failures",
"release": "bun ../common/release.ts"
},
Expand Down
5 changes: 3 additions & 2 deletions js/pattern/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,9 @@ function bounded(patterns: Iterable<Pattern>): Pattern[] {
*
* Operations over a whole union (construction, {@link Patterns.covers}, and
* {@link Patterns.intersect}) index one side by segment first, so their cost follows the
* members a query could match rather than every pair: a peer's large grant costs about its
* own size, not its square.
* members a query could match rather than every pair. That keeps common grants near their own
* size, but containment between wildcard patterns reduces to Orthogonal Vectors, so some shapes
* stay super-linear (the `mask` and `chain` cases in `bench/algebra.ts`).
*
* @public
*/
Expand Down
66 changes: 46 additions & 20 deletions quest/m2/peer-grant-bound.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,57 @@ A peer's `AUTH_OK` costs near-linear time in its bytes on every input shape,
in Rust and JS, including a regrant compared against the previous grant.
Measure JS in a browser, not node or bun.

Re-matching live routes after a regrant is out of scope; it is
[its own quest](/quest/m2/regrant-route-recheck.md).

## Plan

[moq-dev/moq#6356](https://github.com/moq-dev/moq/pull/6356) indexed
whole-union operations in `moq-pattern` and `@moq/pattern` by segment. That
made every benchmarked shape linear, but the worst case is still about
25 ms per 64 KiB message in Rust. The shape is 1024 incomparable `*`/`a`
masks against literal queries that fail only at the last segment. Containment
between patterns with wildcards reduces to Orthogonal Vectors, so no index
closes that gap. Correlated partials (`a^i*z^(n-i)`) cost 8.6 ms, growing as
bytes^1.5 (see the
[Codex thread](https://github.com/moq-dev/moq/pull/6356#discussion_r4352967969)).

Candidate approaches, needing a fresh `/quest-plan`:

- Keep peer grants unreduced: decode into a sorted, deduplicated list in
O(n log n), and make `Permit::covers` syntactic (its docs already allow a
spurious false). This changes what `Patterns` or `Grant` promise about
equality and canonical form, so it is an API decision.
- A joint prefix/suffix index for partial segments (rectangle stabbing on
trie traversal order). This removes the correlated-partial chain but not
the segment-level floor.

Prove it with the `pattern-algebra` benchmark extended by the mask and
partial-chain shapes, each sweeping message size.
made every benchmarked shape linear, but containment between patterns with
wildcards reduces to Orthogonal Vectors, so no index closes the gap.
[moq-dev/moq#7536](https://github.com/moq-dev/moq/pull/7536) added the
`pattern-algebra` cases (Rust Criterion) and `js/pattern/bench/algebra.ts`
(Chromium through `bench/browser/micro.ts --bench pattern`) that sweep the two
worst known shapes over message size up to a whole `AUTH_OK`:

- `mask`: equal-weight `*`/`y` masks beside literal queries that fail only at
the last segment. At 64 KiB one reduction costs about 40 ms in Rust and
250 ms in Chromium, growing as bytes^1.7.
- `chain`: correlated partials `a^i*z^(n-i)`. At 64 KiB about 9 ms in Rust and
24 ms in Chromium, growing as bytes^1.3.

Each `AUTH_OK` pays this more than once: Rust reduces on decode
(`Patterns::decode`) and again in `Grant::union`, then every moq-lite announce
stream's regrant runs `Permit::covers` (the same walk) against its previous
permit. JS reduces on decode (`decodePatterns`) and in `union`.

Decided 2026-11-01 (maintainer, on #7536):

- ✅ Keep received grants unreduced, in their own type. `AUTH_OK` decodes into
a sorted, deduplicated pattern list in O(n log n). The union of received
grants is a merge, and equality and `Permit::covers` become syntactic
(`Permit::covers` already allows a spurious false, which only re-reads the
origin once). Matching stays O(members) per path. `Patterns` keeps its
reduced, set-equality promise for the grants this side issues and verifies,
including moq-auth's `granted.covers(requested)`. Rejected: weakening
`Patterns` everywhere (loses set equality, or splits `covers` into two
behaviors), a joint prefix/suffix index alone (leaves `mask` at bytes^1.7),
and refusing an `AUTH_OK` past a work budget (a runtime refusal by constant
that changes wire behavior for valid grants).
- ✅ The regrant's route re-match is a follow-up quest, not this one.

Open, for a `/quest-plan` before implementation: the received-grant type's
name and crate, and whether `Grant` splits into issued and received types
(today one `Grant` serves `Handle::grant()` and `Credential::grant` as well as
`Request::accept`, `Issued::update`, and `Handle::authorize`, in Rust and in
`@moq/net`). Either is a breaking change to the received side's API.

Prove it with the same benches: the received-grant decode, union, and
`Permit::covers` swept over the `mask` and `chain` shapes, linear in bytes,
before and after.

## Related

- [A regrant re-checks only the routes it can change](/quest/m2/regrant-route-recheck.md) - the route fan-out after a regrant, which this leaves alone; whichever lands second adapts to the other
- [Fair share across broadcasts](/quest/m2/broadcast-fairness.md) - the other per-session CPU and bandwidth fairness concern
2 changes: 1 addition & 1 deletion rs/moq-net/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Grant {
}

/// The paths any of `grants` allows, lapsing at the earliest expiry, which is when
/// the union next shrinks. Reduced once, so it costs about the grants' total size.
/// the union next shrinks. Reduced once over all of them, not pairwise.
fn union<'a>(grants: impl IntoIterator<Item = &'a Self>) -> Self {
let mut publish = Vec::new();
let mut subscribe = Vec::new();
Expand Down
Loading
Loading