From 7ba8b46c75c1b7902f212bd8b0dfd0c50a181deb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:28:56 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/m2/peer-grant-bound.md Quest-Claim: 1e1976c1ccdcc87c From 209760e49dd74d94e6cfcf61c9b50cdbf2d9683d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 01:44:16 -0800 Subject: [PATCH 2/5] bench(pattern): sweep the worst known peer grant shapes over AUTH_OK size Adds the mask (Orthogonal Vectors) and correlated partial-chain shapes to the Rust pattern-algebra benchmark and a matching @moq/pattern sweep that runs in Chromium through bench/browser/micro.ts, nightly, and the browser smoke lane. Co-Authored-By: Claude Opus 5.5 --- bench/README.md | 16 ++-- bench/browser/micro.ts | 12 +-- bench/browser/run.sh | 2 +- js/justfile | 6 ++ js/pattern/bench/algebra.ts | 92 +++++++++++++++++++++++ js/pattern/bench/tsconfig.json | 8 ++ js/pattern/package.json | 2 +- quest/m2/peer-grant-bound.md | 28 ++++--- rs/moq-pattern/benches/matching.rs | 115 +++++++++++++++++++++++++++++ sh/gh/nightly.sh | 1 + 10 files changed, 259 insertions(+), 23 deletions(-) create mode 100644 js/pattern/bench/algebra.ts create mode 100644 js/pattern/bench/tsconfig.json diff --git a/bench/README.md b/bench/README.md index 82427ba0cb9..3718d9c733e 100644 --- a/bench/README.md +++ b/bench/README.md @@ -409,12 +409,13 @@ Crashes, wrong bytes, missing frames, a window that presents nothing, or an unavailable CPU clock fail the run; timing never does. The nightly runs `--smoke` and keeps its run directory as the `bench-browser` artifact. -The in-memory frame, retained-history, and gap ledger paths also run directly in Chromium: +The in-memory frame, retained-history, gap ledger, 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=. nix develop --command bun bench/browser/micro.ts --bench drift --out .tmp/drift.json before=/path/to/main current=. aa=. nix develop --command bun bench/browser/micro.ts --bench ledger --out .tmp/ledger.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 page, with one full @@ -425,21 +426,26 @@ against every checkout, sweeping retained groups, publishers, and readers. Repla and group setup stay outside drift timing; each parked read checks its live payload. `ledger` uses one fixture against every checkout's internal ledger, sweeping ascending, descending, shuffled, and rolling-expiration arrivals over the retained -gap count. It includes each arrival's expiration and coverage checks. +gap count. It includes each arrival's expiration and coverage checks. `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 for mixed millisecond/microsecond timestamps, which exercise exact cross-scale comparison. -The browser smoke lane preserves one measured round of all three fixtures, and drift -and ledger also run in the nightly JS benchmark leg. Timing is +The browser smoke lane preserves one measured round of all four fixtures, and drift, +ledger, 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. diff --git a/bench/browser/micro.ts b/bench/browser/micro.ts index 1bf4d138034..0bddac8995f 100644 --- a/bench/browser/micro.ts +++ b/bench/browser/micro.ts @@ -14,8 +14,8 @@ const { values, positionals } = parseArgs({ }, }); const benchmark = values.bench; -if (benchmark !== "frames" && benchmark !== "drift" && benchmark !== "ledger") - throw new Error("--bench must be frames, drift, or ledger"); +if (benchmark !== "frames" && benchmark !== "drift" && benchmark !== "ledger" && benchmark !== "pattern") + throw new Error("--bench must be frames, drift, ledger, or pattern"); const rounds = Number(values.rounds); if (!Number.isSafeInteger(rounds) || rounds < 1) throw new Error("--rounds must be a positive integer"); const roots = new Map(); @@ -30,7 +30,9 @@ for (const [name, root] of roots) { const entry = benchmark === "frames" ? resolve(root, "js/net/bench/frames.ts") - : resolve(import.meta.dir, `../../js/net/bench/${benchmark}.ts`); + : benchmark === "pattern" + ? resolve(import.meta.dir, "../../js/pattern/bench/algebra.ts") + : resolve(import.meta.dir, `../../js/net/bench/${benchmark}.ts`); const result = await Bun.build({ entrypoints: [entry], target: "browser", @@ -43,7 +45,7 @@ for (const [name, root] of roots) { build.onResolve({ filter: /^\.\.\/src\/ledger\.ts$/ }, () => ({ path: resolve(root, "js/net/src/ledger.ts"), })); - build.onLoad({ filter: /\/bench\/(frames|drift|ledger)\.ts$/ }, async ({ path }) => { + build.onLoad({ filter: /\/bench\/(frames|drift|ledger|algebra)\.ts$/ }, async ({ path }) => { const source = await Bun.file(path).text(); if (benchmark !== "frames") return { loader: "ts", contents: `${source}\nglobalThis.measure = run;` }; @@ -109,7 +111,7 @@ try { rows.set(variant, []); await pages.get(variant)?.evaluate("globalThis.measure()"); const measured = rows.get(variant); - if (measured?.length !== (benchmark === "frames" ? 26 : benchmark === "drift" ? 36 : 12)) + if (measured?.length !== { frames: 26, drift: 36, ledger: 12, pattern: 12 }[benchmark]) throw new Error(`incomplete rows: ${variant}`); samples.push({ round, variant, rows: measured }); await Bun.write( diff --git a/bench/browser/run.sh b/bench/browser/run.sh index 6c16ba98a76..83c61dc8e91 100755 --- a/bench/browser/run.sh +++ b/bench/browser/run.sh @@ -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; do +for benchmark in frames drift ledger pattern; do bun micro.ts --bench "$benchmark" --rounds 1 --out "$HARNESS_RUN/$benchmark.json" \ current="$WORKSPACE" aa="$WORKSPACE" done diff --git a/js/justfile b/js/justfile index 5f4bcb5691f..3064566725a 100644 --- a/js/justfile +++ b/js/justfile @@ -117,6 +117,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 diff --git a/js/pattern/bench/algebra.ts b/js/pattern/bench/algebra.ts new file mode 100644 index 00000000000..3d90ffa2f64 --- /dev/null +++ b/js/pattern/bench/algebra.ts @@ -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(); diff --git a/js/pattern/bench/tsconfig.json b/js/pattern/bench/tsconfig.json new file mode 100644 index 00000000000..127a2e04490 --- /dev/null +++ b/js/pattern/bench/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "rootDir": "..", + "noEmit": true + }, + "include": ["*.ts"] +} diff --git a/js/pattern/package.json b/js/pattern/package.json index f8ae148edbd..af6ad72f576 100644 --- a/js/pattern/package.json +++ b/js/pattern/package.json @@ -15,7 +15,7 @@ }, "scripts": { "build": "rimraf 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" }, diff --git a/quest/m2/peer-grant-bound.md b/quest/m2/peer-grant-bound.md index 3adb55dc01b..c3cb569b79e 100644 --- a/quest/m2/peer-grant-bound.md +++ b/quest/m2/peer-grant-bound.md @@ -10,15 +10,24 @@ Measure JS in a browser, not node or bun. [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)). +made every benchmarked shape linear, but containment between patterns with +wildcards reduces to Orthogonal Vectors, so no index closes the gap. The +`pattern-algebra` benchmark (Rust Criterion) and `js/pattern/bench/algebra.ts` +(Chromium through `bench/browser/micro.ts --bench pattern`) sweep the two worst +known shapes over message size up to a whole `AUTH_OK`: -Candidate approaches, needing a fresh `/quest-plan`: +- `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 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 and in +`union`. + +Candidate approaches, needing a decision: - 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 @@ -28,9 +37,6 @@ Candidate approaches, needing a fresh `/quest-plan`: 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. - ## Related - [Fair share across broadcasts](/quest/m2/broadcast-fairness.md) - the other per-session CPU and bandwidth fairness concern diff --git a/rs/moq-pattern/benches/matching.rs b/rs/moq-pattern/benches/matching.rs index c99b851ac8a..2b2f605bb39 100644 --- a/rs/moq-pattern/benches/matching.rs +++ b/rs/moq-pattern/benches/matching.rs @@ -49,6 +49,11 @@ fn matching(c: &mut Criterion) { /// succeed on every query to visit them all: per kind it queries a subset of the held /// union, and `covers-subtree` holds `sNNNN/**` members that cover any number of /// literals beneath them, so it sweeps both sizes. +/// +/// The [`Shape`] cases sweep one peer grant's encoded size up to a whole `AUTH_OK`: +/// `collect` reduces it as decoding does, and `covers` compares it against a previous +/// grant as a regrant does. Their throughput is bytes, so a cost that grows faster than +/// the message shows as falling throughput. fn algebra(c: &mut Criterion) { let mut group = c.benchmark_group("pattern-algebra"); let union = |kind: &str, name: &str, count: usize| -> Patterns { @@ -114,8 +119,118 @@ fn algebra(c: &mut Criterion) { b.iter(|| std::hint::black_box(subtrees.covers(&literals))); }); } + // Each iteration reduces a whole message, so fewer samples keep the sweep short. + group.sample_size(10); + for shape in [Shape::Mask, Shape::Chain] { + for bytes in [4096, 16384, MAX_MESSAGE] { + let (message, previous) = shape.grant(bytes); + let size = wire(&message); + assert!( + size <= bytes && size * 16 > bytes * 15, + "{shape:?} fills {size} of {bytes} bytes" + ); + let message: Vec = message.iter().map(|text| text.parse().unwrap()).collect(); + let reduced: Patterns = message.iter().cloned().collect(); + assert_eq!(reduced.len(), message.len(), "{shape:?} members are incomparable"); + assert!(previous.covers(&reduced)); + group.throughput(Throughput::Bytes(size as u64)); + group.bench_function(BenchmarkId::new(format!("collect-{}", shape.name()), bytes), |b| { + b.iter(|| std::hint::black_box(message.iter().cloned().collect::().len())); + }); + group.bench_function(BenchmarkId::new(format!("covers-{}", shape.name()), bytes), |b| { + b.iter(|| std::hint::black_box(previous.covers(&reduced))); + }); + } + } group.finish(); } +/// The largest moq-lite control message, and so the largest `AUTH_OK`. +const MAX_MESSAGE: usize = u16::MAX as usize; + +/// A QUIC varint's size, as `AUTH_OK` writes counts and lengths. +fn varint(value: usize) -> usize { + match value { + ..64 => 1, + ..16384 => 2, + _ => 4, + } +} + +/// A union's encoded size as `AUTH_OK` carries it: a count, then each text with its length. +fn wire(texts: &[String]) -> usize { + varint(texts.len()) + texts.iter().map(|text| varint(text.len()) + text.len()).sum::() +} + +/// A peer grant shaped against the union index, filling one message. +/// +/// Containment between patterns with wildcards reduces to Orthogonal Vectors, so some shape +/// costs more than linear time against any index; these are the two worst known against +/// this one. Every member is incomparable, so reduction keeps them all and visits each. +#[derive(Clone, Copy, Debug)] +enum Shape { + /// `*`/`y` masks of equal weight, each ending in `y`, beside literal `y/…/y/xNNNN` + /// queries that every mask matches but for the last segment. + Mask, + /// The correlated partials `a^i*z^(n-i)`, each sharing prefixes and suffixes with all + /// the others. + Chain, +} + +impl Shape { + /// Positions a mask chooses `*` or `y` for, half of them `*`. + const FREE: usize = 14; + + fn name(self) -> &'static str { + match self { + Self::Mask => "mask", + Self::Chain => "chain", + } + } + + /// The grant's texts, filling at most `bytes` encoded, and a previous grant covering + /// every one, as a regrant compares them. + fn grant(self, bytes: usize) -> (Vec, Patterns) { + let parse = |texts: &[String]| texts.iter().map(|text| text.parse::().unwrap()).collect(); + match self { + Self::Mask => { + let masks = (0u32..1 << Self::FREE) + .filter(|bits| bits.count_ones() as usize == Self::FREE / 2) + .map(|bits| { + let segments = (0..Self::FREE).map(|at| if bits >> at & 1 == 1 { "*" } else { "y" }); + segments.chain(["y"]).collect::>().join("/") + }); + let queries = (0..).map(|index| format!("{}/x{index:04}", ["y"; Self::FREE].join("/"))); + let (mut kept, mut asked) = (Vec::new(), Vec::new()); + let mut texts = 0; + for (mask, query) in masks.zip(queries) { + texts += varint(mask.len()) + mask.len() + varint(query.len()) + query.len(); + if varint(2 * kept.len() + 2) + texts > bytes { + break; + } + kept.push(mask); + asked.push(query); + } + // `*/…/*/x*` holds every query, and a walk tries a literal before `*`, so each + // query reaches it only after every mask. + let previous = parse(&[kept.clone(), vec![format!("{}/x*", ["*"; Self::FREE].join("/"))]].concat()); + kept.extend(asked); + (kept, previous) + } + Self::Chain => { + let chain = |n: usize| -> Vec { + (1..n) + .map(|i| format!("{}*{}", "a".repeat(i), "z".repeat(n - i))) + .collect() + }; + let n = (2..).take_while(|&n| wire(&chain(n)) <= bytes).last().unwrap(); + let texts = chain(n); + let previous = parse(&texts); + (texts, previous) + } + } + } +} + criterion_group!(benches, matching, algebra); criterion_main!(benches); diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index ae8312bc9dd..7956b4d338e 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -153,6 +153,7 @@ case "$1" in javascript slow-group just js slow-group-bench javascript jitter just js jitter-bench javascript audio-warmup just js audio-warmup-bench + javascript pattern just js pattern-bench javascript clock just js clock-bench ;; # The binding benchmark's runners at a token budget, so they keep working at this From bc17d07aa58553a0a56427c981ca28200eeed93c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:06:08 -0800 Subject: [PATCH 3/5] quest(peer-grant-bound): record the decided design, pending a plan The maintainer picked keeping received grants unreduced in their own type, and left the regrant's route re-match to regrant-route-recheck. The type's name and whether Grant splits wait on a /quest-plan. Also correct the Patterns and union docs, which promised near-linear cost that the new mask and chain benches refute. Co-Authored-By: Claude Opus 5.5 --- js/net/src/auth_session.ts | 2 +- js/pattern/src/index.ts | 5 +-- quest/m2/peer-grant-bound.md | 56 +++++++++++++++++++++++----------- rs/moq-net/src/auth.rs | 2 +- rs/moq-pattern/src/patterns.rs | 6 ++-- 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/js/net/src/auth_session.ts b/js/net/src/auth_session.ts index 8df4df55918..5a1af021d2b 100644 --- a/js/net/src/auth_session.ts +++ b/js/net/src/auth_session.ts @@ -105,7 +105,7 @@ export interface AuthWire { unsupported(stream: Stream, reason: string): Promise; } -/** 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 { const publish: Path.Pattern[] = []; const subscribe: Path.Pattern[] = []; diff --git a/js/pattern/src/index.ts b/js/pattern/src/index.ts index 3d736b618f9..baf6b1a3f4a 100644 --- a/js/pattern/src/index.ts +++ b/js/pattern/src/index.ts @@ -1054,8 +1054,9 @@ function bounded(patterns: Iterable): 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 */ diff --git a/quest/m2/peer-grant-bound.md b/quest/m2/peer-grant-bound.md index c3cb569b79e..db47bc0a0ef 100644 --- a/quest/m2/peer-grant-bound.md +++ b/quest/m2/peer-grant-bound.md @@ -6,15 +6,19 @@ 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 containment between patterns with -wildcards reduces to Orthogonal Vectors, so no index closes the gap. The -`pattern-algebra` benchmark (Rust Criterion) and `js/pattern/bench/algebra.ts` -(Chromium through `bench/browser/micro.ts --bench pattern`) sweep the two worst -known shapes over message size up to a whole `AUTH_OK`: +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 @@ -22,21 +26,37 @@ known shapes over message size up to a whole `AUTH_OK`: - `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 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 and in -`union`. - -Candidate approaches, needing a decision: - -- 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. +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 diff --git a/rs/moq-net/src/auth.rs b/rs/moq-net/src/auth.rs index ee6eeff6d5a..c535c0416ff 100644 --- a/rs/moq-net/src/auth.rs +++ b/rs/moq-net/src/auth.rs @@ -105,7 +105,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) -> Self { let mut publish = Vec::new(); let mut subscribe = Vec::new(); diff --git a/rs/moq-pattern/src/patterns.rs b/rs/moq-pattern/src/patterns.rs index 51524133088..af5bb0bac1a 100644 --- a/rs/moq-pattern/src/patterns.rs +++ b/rs/moq-pattern/src/patterns.rs @@ -13,8 +13,10 @@ use super::{IntersectionError, InvalidPattern, Pattern, index::Index}; /// /// Operations over a whole union (collecting, extending, [`covers`](Self::covers), and /// [`intersect`](Self::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. +/// follows the 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 `pattern-algebra` bench's +/// `mask` and `chain` cases). #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] pub struct Patterns(Vec); From 9db02fcf5e15923911caf6657352f4ba4773def2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 2 Nov 2026 22:34:26 -0800 Subject: [PATCH 4/5] test(nightly): map the pattern smoke fixture to @moq/pattern's bench Main's smoke-lane coverage check assumed every micro.ts fixture lives in js/net/bench; the pattern fixture runs js/pattern/bench/algebra.ts, as micro.ts resolves it. Co-Authored-By: Claude Opus 5.5 --- sh/gh/nightly.test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sh/gh/nightly.test.py b/sh/gh/nightly.test.py index a3e88f90fe2..9adba0e9ed9 100644 --- a/sh/gh/nightly.test.py +++ b/sh/gh/nightly.test.py @@ -199,7 +199,9 @@ def test_every_js_bench_runs_nightly(self): r"^for benchmark in (.+); do\n\s+bun micro\.ts ", (ROOT / "bench/browser/run.sh").read_text(), re.M ) self.assertIsNotNone(smoke) - entries |= {ROOT / "js/net/bench" / f"{name}.ts" for name in smoke[1].split()} + # As micro.ts resolves them: `pattern` is @moq/pattern's, the rest are @moq/net's. + outside = {"pattern": ROOT / "js/pattern/bench/algebra.ts"} + entries |= {outside.get(name, ROOT / "js/net/bench" / f"{name}.ts") for name in smoke[1].split()} # Bundled helpers only run through the entry that names them. helpers = { path for entry in entries for path in entry.parent.glob("*.ts") if f"/{path.name}" in entry.read_text() From e959f2c4d631f528f9cc6c5e62fd317e244f4368 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 09:40:48 -0800 Subject: [PATCH 5/5] style(bench): wrap the browser bundle filter after the merge Co-Authored-By: Claude Opus 5.5 --- bench/browser/micro.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bench/browser/micro.ts b/bench/browser/micro.ts index 073bd3bab90..18a7327b7d1 100644 --- a/bench/browser/micro.ts +++ b/bench/browser/micro.ts @@ -56,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|algebra)\.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")