From 1120e36b6091081c2c8190f125cb33d842c0a33d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 00:37:45 -0700 Subject: [PATCH 1/3] claim quest/m1/admission-bench Co-Authored-By: Grok 4.7 From 05d08cdd3fad6b8d89c90f7a0509803cae4ab092 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 01:10:09 -0700 Subject: [PATCH 2/3] bench(net): sweep a front's route-swap copy walk Co-Authored-By: Grok 4.7 --- quest/m1/README.md | 1 - quest/m1/admission-bench.md | 31 -------- rs/moq-net/benches/origin.rs | 140 ++++++++++++++++++++++++++++++++++- 3 files changed, 137 insertions(+), 35 deletions(-) delete mode 100644 quest/m1/admission-bench.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 21d2307fea9..4908a6c705a 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -158,7 +158,6 @@ blocks. The quests that gated m0 lines moved under them. - [Papercuts](/quest/m1/papercuts.md) - JS refuses to serve a broadcast it did not produce, and a uring test stops sleeping - [Front parking](/quest/m1/origin-front-parks.md) - an unroutable request waits on a front instead of re-asking on every route-table move - [Route wakes](/quest/m1/route-wakes.md) - a route change wakes only the fronts it can move, so pool churn stops scaling with served paths -- [Copy-walk bench](/quest/m1/admission-bench.md) - a front's walk over its copies on a route change is benchmarked over tracks and copies - [Publish channel count](/quest/m1/publish-audio-channel-count.md) - forcing a channel count on an Audio.Capture stops costing the subscriber gaps of silence - [JS abandonment](/quest/m1/js-subscribe-abandonment.md) - a viewer returning during IETF subscribe setup keeps its track across microtasks - [E2EE](/quest/m1/e2ee/README.md) - TypeScript and Rust peers interoperate over encrypted broadcasts no relay can decrypt diff --git a/quest/m1/admission-bench.md b/quest/m1/admission-bench.md deleted file mode 100644 index 4d8cb614c32..00000000000 --- a/quest/m1/admission-bench.md +++ /dev/null @@ -1,31 +0,0 @@ -# [S] Front copy-walk benchmark - -## Goal - -A benchmark sweeps tracks per front against copies per track for the walks a -front makes over its copies when its route changes, so a cost that grows with -the front shows up as a slope. - -## Plan - -Decided in the 2026-10-05 audit: re-scoped from the wildcard line's admission -walk, which no longer exists (`Provenance::admit`, `front.admit()`, and -`io.copies()` are on neither `main` nor the line branch), to the copy walk -#4741 put on `main`. The Wildcard blocker is dropped, since this code is on -`main`. - -- Since #4741 a front resumes route changes by reading the routes' copies. - `attach` and `source_closed` call `drain_copies` - (`rs/moq-net/src/model/front.rs`), which walks every track, and the driver - keeps the serving source's copies until End. Nothing benchmarks either. -- Extend `rs/moq-net/benches/origin.rs` (or `session.rs` if a session is - needed to drive it) with a route swap swept over tracks per front and copies - per track. -- If the slope matters, say so rather than optimizing here; - [Front deadlines](/quest/m1/front-deadline-index.md) owns per-track wakes. - -Public API: none. Wire: none. - -## Related - -- [Front deadlines](/quest/m1/front-deadline-index.md) - per-track wakes on the same front diff --git a/rs/moq-net/benches/origin.rs b/rs/moq-net/benches/origin.rs index 3572263806e..368f06ae94b 100644 --- a/rs/moq-net/benches/origin.rs +++ b/rs/moq-net/benches/origin.rs @@ -12,14 +12,17 @@ //! An equal-cost pool is swept the same way, over its members and the paths it //! already serves. //! +//! A route swap on one front is swept over its tracks and the copies each track +//! still holds from earlier routes. +//! //! Run with `cargo bench -p moq-net --bench origin`. use std::task::Poll; use std::time::Duration; -use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::FutureExt; -use moq_net::{Hop, Hops, Pattern, Patterns, Timestamp, announce, broadcast, kio, origin}; +use moq_net::{Epoch, Hop, Hops, Pattern, Patterns, Timestamp, announce, broadcast, kio, origin, track}; /// `(publishers, subscribers)` shapes for the fan-out benchmarks. const SHAPES: [(usize, usize); 3] = [(100, 10), (1_000, 100), (1_000, 1_000)]; @@ -684,6 +687,136 @@ fn bench_parked(c: &mut Criterion) { group.finish(); } +/// `(tracks, copies)` for [`bench_copy_walk`]: tracks one front is serving, and +/// route copies each of those tracks still holds when the route swaps. +const COPY_WALK: [(usize, usize); 6] = [(1, 1), (1, 32), (32, 1), (8, 32), (32, 8), (64, 16)]; + +const COPY_PATH: &str = "room/live"; + +/// How long a splice may take before the bench treats it as stuck. +const COPY_WAIT: Duration = Duration::from_secs(5); + +/// One generation at [`COPY_PATH`], held so its copies stay subscribed. +struct Generation { + _broadcast: broadcast::Producer, + tracks: Vec, +} + +/// A front serving [`COPY_PATH`], with `copies` route copies on each of its tracks. +struct CopyWalk { + producer: origin::Producer, + epoch: Epoch, + tracks: usize, + _subscribers: Vec, + generations: Vec, +} + +/// Publish one local generation and leave a finished group on every track, so a +/// reader that does look has something to resume. +fn publish_generation(producer: &origin::Producer, epoch: &Epoch, tracks: usize) -> Generation { + let broadcast = producer.create_broadcast(COPY_PATH).unwrap(); + let mut held = Vec::with_capacity(tracks); + for i in 0..tracks { + let track = broadcast.create_track(format!("{i}"), None).unwrap(); + let mut written = track.append_group().unwrap(); + written.write_frame(Timestamp::ZERO, b"f".as_ref()).unwrap(); + written.finish().unwrap(); + held.push(track); + } + broadcast + .announce(origin::Route::default().with_epoch(epoch.clone())) + .unwrap(); + Generation { + _broadcast: broadcast, + tracks: held, + } +} + +async fn wait_spliced(tracks: &[track::Producer]) { + for track in tracks { + tokio::time::timeout(COPY_WAIT, track.demand().used()) + .await + .expect("route swap did not splice") + .expect("replacement track closed"); + } +} + +/// Build a front with `copies` already spliced onto each track. +/// +/// Subscribers are never polled. A poll drops a replaced copy once its groups +/// are delivered, and this measures the walk over the copies a swap still has. +/// The same is why every generation is kept: dropping one ends its copy. +async fn copy_walk(tracks: usize, copies: usize) -> CopyWalk { + assert!(copies >= 1, "a front holds at least the serving copy"); + let (producer, driver) = origin::Producer::new(origin::Config::default()); + tokio::spawn(moq_net::time::run(driver)); + let epoch = Epoch::mint(); + let first = publish_generation(&producer, &epoch, tracks); + let resolved = tokio::time::timeout(COPY_WAIT, producer.consume().request_broadcast(COPY_PATH)) + .await + .expect("front did not resolve") + .expect("front refused"); + let mut subscribers = Vec::with_capacity(tracks); + for i in 0..tracks { + let track = resolved.track(&format!("{i}")).unwrap(); + let subscriber = tokio::time::timeout(COPY_WAIT, track.subscribe(None)) + .await + .expect("track did not splice") + .expect("subscribe failed"); + subscribers.push(subscriber); + } + let mut generations = Vec::with_capacity(copies); + generations.push(first); + for _ in 1..copies { + let generation = publish_generation(&producer, &epoch, tracks); + wait_spliced(&generation.tracks).await; + generations.push(generation); + } + CopyWalk { + producer, + epoch, + tracks, + _subscribers: subscribers, + generations, + } +} + +impl CopyWalk { + /// Swap in one newer route at the same epoch. + /// + /// A local track's info is already known, so the front subscribes, walks + /// every track, and each reader walks its copies in one driver turn. Demand + /// flips at the subscribe, and this wait returns only after that turn. + async fn swap(&mut self) { + let generation = publish_generation(&self.producer, &self.epoch, self.tracks); + wait_spliced(&generation.tracks).await; + self.generations.push(generation); + } +} + +/// A route swap on one front. Setup holds `copies` per track and is not timed; +/// the swap is. Warm-up sizes the iteration count from wall time, which +/// includes that setup, so a large front does not multiply it by a fast routine. +fn bench_copy_walk(c: &mut Criterion) { + let mut group = c.benchmark_group("origin/copy_walk"); + group.sample_size(10); + for (tracks, copies) in COPY_WALK { + group.throughput(Throughput::Elements((tracks * copies) as u64)); + group.bench_function(BenchmarkId::from_parameter(format!("{tracks}t_{copies}c")), |b| { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + b.iter_batched_ref( + || runtime.block_on(copy_walk(tracks, copies)), + |rig| runtime.block_on(rig.swap()), + BatchSize::PerIteration, + ); + }); + } + group.finish(); +} + criterion_group!( benches, bench_announce, @@ -698,6 +831,7 @@ criterion_group!( bench_pool_churn, bench_handoff, bench_relay, - bench_parked + bench_parked, + bench_copy_walk ); criterion_main!(benches); From 33f1c87b93f0997de5bf88f5bb54dae6849999f6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 12:38:30 -0700 Subject: [PATCH 3/3] bench(net): time only the copy walk's announce and splice Build the next generation in setup, sweep single-copy fronts to 128 tracks, count throughput per track, and note the current-thread requirement behind the demand-based wait. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/benches/origin.rs | 117 ++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 49 deletions(-) diff --git a/rs/moq-net/benches/origin.rs b/rs/moq-net/benches/origin.rs index 368f06ae94b..252acec3397 100644 --- a/rs/moq-net/benches/origin.rs +++ b/rs/moq-net/benches/origin.rs @@ -689,7 +689,9 @@ fn bench_parked(c: &mut Criterion) { /// `(tracks, copies)` for [`bench_copy_walk`]: tracks one front is serving, and /// route copies each of those tracks still holds when the route swaps. -const COPY_WALK: [(usize, usize); 6] = [(1, 1), (1, 32), (32, 1), (8, 32), (32, 8), (64, 16)]; +/// +/// The single-copy points from 32 to 128 tracks show the per-track slope alone. +const COPY_WALK: [(usize, usize); 8] = [(1, 1), (1, 32), (32, 1), (64, 1), (128, 1), (8, 32), (32, 8), (64, 16)]; const COPY_PATH: &str = "room/live"; @@ -698,47 +700,61 @@ const COPY_WAIT: Duration = Duration::from_secs(5); /// One generation at [`COPY_PATH`], held so its copies stay subscribed. struct Generation { - _broadcast: broadcast::Producer, + broadcast: broadcast::Producer, tracks: Vec, } -/// A front serving [`COPY_PATH`], with `copies` route copies on each of its tracks. -struct CopyWalk { - producer: origin::Producer, - epoch: Epoch, - tracks: usize, - _subscribers: Vec, - generations: Vec, -} +impl Generation { + /// Create a hidden generation and leave a finished group on every track, so a + /// reader that does look has something to resume. + fn build(producer: &origin::Producer, tracks: usize) -> Self { + let broadcast = producer.create_broadcast(COPY_PATH).unwrap(); + let mut held = Vec::with_capacity(tracks); + for i in 0..tracks { + let track = broadcast.create_track(format!("{i}"), None).unwrap(); + let mut written = track.append_group().unwrap(); + written.write_frame(Timestamp::ZERO, b"f".as_ref()).unwrap(); + written.finish().unwrap(); + held.push(track); + } + Self { + broadcast, + tracks: held, + } + } -/// Publish one local generation and leave a finished group on every track, so a -/// reader that does look has something to resume. -fn publish_generation(producer: &origin::Producer, epoch: &Epoch, tracks: usize) -> Generation { - let broadcast = producer.create_broadcast(COPY_PATH).unwrap(); - let mut held = Vec::with_capacity(tracks); - for i in 0..tracks { - let track = broadcast.create_track(format!("{i}"), None).unwrap(); - let mut written = track.append_group().unwrap(); - written.write_frame(Timestamp::ZERO, b"f".as_ref()).unwrap(); - written.finish().unwrap(); - held.push(track); + /// Route the front to this generation without waiting. + fn route(&self, epoch: &Epoch) { + self.broadcast + .announce(origin::Route::default().with_epoch(epoch.clone())) + .unwrap(); } - broadcast - .announce(origin::Route::default().with_epoch(epoch.clone())) - .unwrap(); - Generation { - _broadcast: broadcast, - tracks: held, + + /// Route the front to this generation and wait until every track is spliced. + /// + /// The wait ends on demand, which flips when the front subscribes the new + /// copy, before its splice. That bounds the splice only on a current-thread + /// runtime, where every local query is ready within the front poll that + /// splices it. + async fn announce(&self, epoch: &Epoch) { + self.route(epoch); + for track in &self.tracks { + tokio::time::timeout(COPY_WAIT, track.demand().used()) + .await + .expect("route swap did not splice") + .expect("replacement track closed"); + } } } -async fn wait_spliced(tracks: &[track::Producer]) { - for track in tracks { - tokio::time::timeout(COPY_WAIT, track.demand().used()) - .await - .expect("route swap did not splice") - .expect("replacement track closed"); - } +/// A front serving [`COPY_PATH`], with `copies` route copies on each of its tracks. +struct CopyWalk { + epoch: Epoch, + _producer: origin::Producer, + _subscribers: Vec, + generations: Vec, + /// The generation the timed swap announces, built during setup. + next: Option, } /// Build a front with `copies` already spliced onto each track. @@ -751,7 +767,8 @@ async fn copy_walk(tracks: usize, copies: usize) -> CopyWalk { let (producer, driver) = origin::Producer::new(origin::Config::default()); tokio::spawn(moq_net::time::run(driver)); let epoch = Epoch::mint(); - let first = publish_generation(&producer, &epoch, tracks); + let first = Generation::build(&producer, tracks); + first.route(&epoch); let resolved = tokio::time::timeout(COPY_WAIT, producer.consume().request_broadcast(COPY_PATH)) .await .expect("front did not resolve") @@ -765,43 +782,45 @@ async fn copy_walk(tracks: usize, copies: usize) -> CopyWalk { .expect("subscribe failed"); subscribers.push(subscriber); } - let mut generations = Vec::with_capacity(copies); + let mut generations = Vec::with_capacity(copies + 1); generations.push(first); for _ in 1..copies { - let generation = publish_generation(&producer, &epoch, tracks); - wait_spliced(&generation.tracks).await; + let generation = Generation::build(&producer, tracks); + generation.announce(&epoch).await; generations.push(generation); } CopyWalk { - producer, + next: Some(Generation::build(&producer, tracks)), epoch, - tracks, + _producer: producer, _subscribers: subscribers, generations, } } impl CopyWalk { - /// Swap in one newer route at the same epoch. + /// Swap in the prebuilt newer route at the same epoch. /// /// A local track's info is already known, so the front subscribes, walks - /// every track, and each reader walks its copies in one driver turn. Demand - /// flips at the subscribe, and this wait returns only after that turn. + /// every track, and each reader walks its copies in one driver turn. async fn swap(&mut self) { - let generation = publish_generation(&self.producer, &self.epoch, self.tracks); - wait_spliced(&generation.tracks).await; + let generation = self.next.take().expect("one swap per setup"); + generation.announce(&self.epoch).await; self.generations.push(generation); } } -/// A route swap on one front. Setup holds `copies` per track and is not timed; -/// the swap is. Warm-up sizes the iteration count from wall time, which -/// includes that setup, so a large front does not multiply it by a fast routine. +/// A route swap on one front. Setup holds `copies` per track and builds the +/// next generation, untimed; the announce and splice are timed. Warm-up sizes +/// the iteration count from wall time, which includes that setup, so a large +/// front does not multiply it by a fast routine. +/// +/// The runtime must stay current-thread; see [`Generation::announce`]. fn bench_copy_walk(c: &mut Criterion) { let mut group = c.benchmark_group("origin/copy_walk"); group.sample_size(10); for (tracks, copies) in COPY_WALK { - group.throughput(Throughput::Elements((tracks * copies) as u64)); + group.throughput(Throughput::Elements(tracks as u64)); group.bench_function(BenchmarkId::from_parameter(format!("{tracks}t_{copies}c")), |b| { let runtime = tokio::runtime::Builder::new_current_thread() .enable_all()