From 7e31b3e41362a69b81da1be0219cad0cb1da6541 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 10:31:36 -0700 Subject: [PATCH 1/9] chore(quest): claim quest/m1/idle-poll-exit.md Quest-Claim: 9ffaf03dce5b6bf1 From 3b612d25bc52f776f92b0e92336e1dcb904ff879 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 10:33:52 -0700 Subject: [PATCH 2/9] docs(quest): checkpoint idle poll profiling at build admission Co-Authored-By: GPT-6.1 Sol --- quest/m1/idle-poll-exit.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/quest/m1/idle-poll-exit.md b/quest/m1/idle-poll-exit.md index ca32d748d05..b3e460f112a 100644 --- a/quest/m1/idle-poll-exit.md +++ b/quest/m1/idle-poll-exit.md @@ -25,6 +25,22 @@ Decided 2026-10-28 (maintainer): m1, for relay fanout scale. - Measure the moq-mux catalog fanout benches and the moq-net session benches over tracks and readers, before and after. +Checkpoint 2026-10-29: no optimization implemented or measured. The requested +build of the existing `track`, `session`, and `catalog` benches never started: +`quest gate` kept waiting for host load to fall, so the attempt stopped at +build admission. Hardware instruction counters are accessible (`perf stat +--no-scale -e instructions true` succeeded), but that is only a tooling probe. +The 3,800-instruction baseline above has not been reproduced in this attempt. + +Resume with instruction attribution before choosing an early exit. The cursor +reads subscription preferences, fill answers, group starts, and wall deadlines +as well as track state; a track-only revision must not hide those changes or +retain registrations for a previous waiter. Existing regression surfaces include +`track_sparse_poll`, `catalog_estimate_fanout` (tracks x subscribers), and the +session scenarios. The transport waiter investigation is separately claimed by +[moq-dev/moq#7262](https://github.com/moq-dev/moq/pull/7262); no shared measured +cause or conflicting implementation scope has been established. + Public API: none. Wire: none. ## Related From 363e2557fced30f070964f5b0b3966b3b3273c46 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 21:15:45 -0700 Subject: [PATCH 3/9] perf(moq-net): answer a repeated idle group read without the walk Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 34 +++++++++ rs/moq-net/src/model/track/idle.rs | 113 +++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 rs/moq-net/src/model/track/idle.rs diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fb451d50464..3cac93912f8 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -31,6 +31,7 @@ pub(super) use demand::Preferences; use demand::Subscriptions; mod fill; +mod idle; mod miss; pub use fill::Fill; use fill::FillState; @@ -5593,6 +5594,10 @@ pub(super) struct Unread<'a> { } /// How a [`Subscriber`] reads: its track's own cache, or a front's routes. +#[expect( + clippy::large_enum_variant, + reason = "the plain cursor is the hot path; boxing it costs every read an indirection" +)] enum Inner { Plain(Cursor), /// A front's logical track, read straight from its routes' copies; see @@ -6018,6 +6023,9 @@ struct Cursor { drops: Option>, /// What this cursor misses and asks fills for, once it first reads. misses: Option>, + /// The group read that last parked, answered again without the walk until something + /// it parked on fires, once it first parks. + idle: Option>, } struct Peeked { @@ -6231,6 +6239,7 @@ impl Cursor { skipped: Skipped::default(), drops: None, misses: None, + idle: None, } } @@ -6279,6 +6288,8 @@ impl Cursor { /// Resolve [`Self::servable`] again once the window or cap moved, and publish it to the /// groups already handed out. Costs the ranges, never the cache. 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::new(self.filter.window.runs(0)); let (revision, cap) = (self.filter.resolved, self.end_sequence); self.servable = runs.below(cap); @@ -6742,6 +6753,17 @@ impl Cursor { peeked.group.cache_refresh(); return Poll::Ready(Ok(Some(self.deliver(peeked.group)))); } + if idle::Idle::parked(&self.idle, idle::Walk::Arrival, waiter) { + return Poll::Pending; + } + let idle = idle::Idle::start(&mut self.idle, waiter); + let polled = self.poll_recv_walk(take, idle.waiter()); + idle.finish(&mut self.idle, idle::Walk::Arrival, polled.is_pending()); + polled + } + + /// [`Self::poll_recv_group`] past a peeked group, parking on `waiter`. + fn poll_recv_walk(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { self.sync(); if let Some(err) = &self.filter.refused { return Poll::Ready(Err(err.clone())); @@ -6919,6 +6941,7 @@ impl Cursor { if next == self.next_sequence { return; } + idle::Idle::disarm(&mut self.idle); self.next_sequence = next; if self .peeked @@ -6941,6 +6964,17 @@ impl Cursor { self.sync(); return Poll::Ready(Ok(Some(self.advance_sequence(peeked.group)))); } + if idle::Idle::parked(&self.idle, idle::Walk::Sequence, waiter) { + return Poll::Pending; + } + let idle = idle::Idle::start(&mut self.idle, waiter); + let polled = self.poll_next_walk(take, idle.waiter()); + idle.finish(&mut self.idle, idle::Walk::Sequence, polled.is_pending()); + polled + } + + /// [`Self::poll_next_group`] past a peeked group, parking on `waiter`. + fn poll_next_walk(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { let Some(next) = self.next_sequence else { return Poll::Ready(Ok(None)); }; diff --git a/rs/moq-net/src/model/track/idle.rs b/rs/moq-net/src/model/track/idle.rs new file mode 100644 index 00000000000..e0aa7ed73e9 --- /dev/null +++ b/rs/moq-net/src/model/track/idle.rs @@ -0,0 +1,113 @@ +//! The shared early exit for a group read that found nothing. +//! +//! A read that parks registers on everything that can change its answer: the track, the +//! subscription, the fills it waits on, and the wall deadlines it judged against. Each of +//! those notifies on every change, so until one fires, the answer stays the same. [`Idle`] +//! parks reads through a waiter of its own that records any wake before passing it on to +//! the task, and answers a read repeated before that wake with the same `Pending`, rather +//! than walking the whole read path again to reach it. +//! +//! Only what notifies can be trusted this way. A change the reader makes to its own cursor, +//! such as moving its range, notifies nothing, so it disarms the gate itself. + +use std::{ + sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }, + task::{Wake, Waker}, +}; + +/// Which read parked: a [`Cursor`](super::Cursor) parks the arrival and sequence walks +/// separately, since one finding nothing says nothing about the other. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(super) enum Walk { + Arrival, + Sequence, +} + +/// A cursor's parked read, and the waiter it parked on. +pub(super) struct Idle { + /// The read that parked, or `None` while nothing is parked. + parked: Option, + woken: Arc, + /// Wraps [`Self::woken`]. Kept across reads, so parking again on a list that still + /// holds it is free, as it would be for the task's own waiter. + waiter: kio::Waiter, +} + +/// Records a wake, then wakes the task. +struct Woken { + woken: AtomicBool, + task: Waker, +} + +impl Wake for Woken { + fn wake(self: Arc) { + self.wake_by_ref(); + } + + fn wake_by_ref(self: &Arc) { + // Before the task's wake, so the read it triggers sees the change. + self.woken.store(true, Ordering::Release); + self.task.wake_by_ref(); + } +} + +impl Idle { + fn new(task: &Waker) -> Self { + let woken = Arc::new(Woken { + woken: AtomicBool::new(false), + task: task.clone(), + }); + Self { + parked: None, + waiter: kio::Waiter::new(Waker::from(woken.clone())), + woken, + } + } + + /// Whether `walk`, read again for `task`, would park exactly as it last did: it parked, + /// nothing it parked on has fired since, and it still wakes the same task. + pub(super) fn parked(this: &Option>, walk: Walk, task: &kio::Waiter) -> bool { + this.as_ref().is_some_and(|idle| { + idle.parked == Some(walk) + && !idle.woken.woken.load(Ordering::Acquire) + && idle.woken.task.will_wake(task.waker()) + }) + } + + /// Start a full read for `task`, returning the gate holding the waiter it must park + /// on. A wake from here on counts against the read about to happen. + pub(super) fn start(this: &mut Option>, task: &kio::Waiter) -> Box { + match this.take() { + Some(mut idle) if idle.woken.task.will_wake(task.waker()) => { + idle.parked = None; + // Cleared before the read looks at anything, so a change it misses wakes + // the waiter after this store and leaves the next read a full one. + idle.woken.woken.store(false, Ordering::Release); + idle + } + // Another task: parking anew drops the old waiter, whose registrations go dead. + _ => Box::new(Self::new(task.waker())), + } + } + + /// The waiter a full read parks on. + pub(super) fn waiter(&self) -> &kio::Waiter { + &self.waiter + } + + /// Put the gate back after a full read of `walk`, armed if it parked. + pub(super) fn finish(mut self: Box, this: &mut Option>, walk: Walk, pending: bool) { + self.parked = pending.then_some(walk); + *this = Some(self); + } + + /// Forget the parked read: the cursor moved in a way nothing notifies. + pub(super) fn disarm(this: &mut Option>) { + if let Some(idle) = this { + idle.parked = None; + } + } +} From 3fefde609414214f68a7ad05bd91884ab4cbef4a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 21:18:26 -0700 Subject: [PATCH 4/9] perf(moq-net): park a repeated idle read ahead of the subscriber's meter Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 3cac93912f8..b635ff32fca 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6538,6 +6538,12 @@ impl Cursor { }) } + /// Whether a group read in `walk` order would park again at once, with nothing peeked + /// or skipped for its owner to take; see [`idle`]. + fn parked(&self, walk: idle::Walk, waiter: &kio::Waiter) -> bool { + self.peeked.is_none() && self.stale == stats::Content::default() && idle::Idle::parked(&self.idle, walk, waiter) + } + /// Take the groups skipped since the last call, for the owner to meter. fn take_stale(&mut self) -> stats::Content { std::mem::take(&mut self.stale) @@ -7333,6 +7339,16 @@ impl Subscriber { /// The next group in arrival order, or in sequence order for `ordered`, skipping any /// already handed out. fn poll_group(&mut self, ordered: bool, waiter: &kio::Waiter) -> Poll>> { + let walk = match ordered { + true => idle::Walk::Sequence, + false => idle::Walk::Arrival, + }; + // Ahead of the meter: a read that parks again has nothing to count. + if let Inner::Plain(cursor) = &self.inner + && cursor.parked(walk, waiter) + { + return Poll::Pending; + } let meter = self.stats.meter(); let res = match &mut self.inner { Inner::Plain(cursor) => loop { From bfc2580b040ff3a9b6c4ef1d42cbf6fa0ec2fccb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 21:20:46 -0700 Subject: [PATCH 5/9] perf(moq-net): count only what a peek skipped on a repeated idle read Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 12 ++++++++---- rs/moq-net/src/stats.rs | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index b635ff32fca..fb3bc4dcb54 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6538,10 +6538,10 @@ impl Cursor { }) } - /// Whether a group read in `walk` order would park again at once, with nothing peeked - /// or skipped for its owner to take; see [`idle`]. + /// Whether a group read in `walk` order would park again at once, with nothing + /// peeked; see [`idle`]. fn parked(&self, walk: idle::Walk, waiter: &kio::Waiter) -> bool { - self.peeked.is_none() && self.stale == stats::Content::default() && idle::Idle::parked(&self.idle, walk, waiter) + self.peeked.is_none() && idle::Idle::parked(&self.idle, walk, waiter) } /// Take the groups skipped since the last call, for the owner to meter. @@ -7343,10 +7343,14 @@ impl Subscriber { true => idle::Walk::Sequence, false => idle::Walk::Arrival, }; - // Ahead of the meter: a read that parks again has nothing to count. + // Ahead of the meter: a read that parks again counts only what a peek skipped. if let Inner::Plain(cursor) = &self.inner && cursor.parked(walk, waiter) { + if cursor.stale != stats::Content::default() && self.stats.is_tagged() { + let meter = self.stats.meter(); + self.count_stale(&meter); + } return Poll::Pending; } let meter = self.stats.meter(); diff --git a/rs/moq-net/src/stats.rs b/rs/moq-net/src/stats.rs index 78efbf003a8..fa15d892b49 100644 --- a/rs/moq-net/src/stats.rs +++ b/rs/moq-net/src/stats.rs @@ -1970,6 +1970,12 @@ impl Clone for Scope { } impl Scope { + /// Whether a [`Meter`] from this scope can count anything, without resolving it: the + /// no-op context never does. + pub(crate) fn is_tagged(&self) -> bool { + self.session.inner.is_some() && self.resolved.is_some() + } + /// The counters for the session's current tier. Skips the registry unless the /// tier changed since the last call, so the per-group path stays cheap. fn counters(&self) -> Option> { From 2f3219d9109b2b4da8ce0769576009d594da4a16 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 21:25:43 -0700 Subject: [PATCH 6/9] test(moq-net): cover the idle read gate and bench repeated parks over readers Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/benches/track.rs | 73 +++++++++++++++++++++++++++++++++++ rs/moq-net/src/model/track.rs | 28 ++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/rs/moq-net/benches/track.rs b/rs/moq-net/benches/track.rs index 3e363a7a560..9fa1d34f563 100644 --- a/rs/moq-net/benches/track.rs +++ b/rs/moq-net/benches/track.rs @@ -29,6 +29,11 @@ //! `track_sparse_poll` repeats a parked read without changing the feed, so revisiting //! deferred gaps on an otherwise unchanged poll shows up independently of delivery. //! +//! `track_idle_poll` repeats every parked read over readers sharing one track, in arrival +//! and sequence order, with nothing new: a repeated read parks again without walking the +//! read path, so each costs a small constant, and a slope over the readers, or a jump in +//! that constant, means the early exit stopped applying. +//! //! `track_sparse_expire` opens one new gap while one old gap expires, so a full expiry //! rescan on each arrival shows up as a slope over the gaps still within budget. //! @@ -1314,6 +1319,73 @@ fn bench_sparse_poll(c: &mut Criterion) { group.finish(); } +/// Readers parked on one track with nothing new, each having taken the one cached group. +struct Idle { + _broadcast: broadcast::Producer, + _track: track::Producer, + _open: group::Producer, + recv: Vec, + next: Vec, + waiter: kio::Waiter, +} + +impl Idle { + fn new(readers: usize, ordered: bool) -> Self { + let broadcast = broadcast::Info::default().produce(); + let track = broadcast.create_track("bench", timed()).unwrap(); + let mut open = track.append_group().unwrap(); + open.write_frame(Timestamp::ZERO, Bytes::from_static(b"x")).unwrap(); + let waiter = kio::Waiter::noop(); + let (mut recv, mut next) = (Vec::new(), Vec::new()); + for _ in 0..readers { + let reader = track.subscribe(track::Subscription::default().with_max_delay(AGED_BUDGET)); + if ordered { + let mut reader = reader.ordered(); + assert!(matches!(reader.poll_next_group(&waiter), Poll::Ready(Ok(Some(_))))); + next.push(reader); + } else { + let mut reader = reader; + assert!(matches!(reader.poll_recv_group(&waiter), Poll::Ready(Ok(Some(_))))); + recv.push(reader); + } + } + let mut idle = Self { + _broadcast: broadcast, + _track: track, + _open: open, + recv, + next, + waiter, + }; + // The first park walks the read path; only the repeats are measured. + idle.poll(); + idle + } + + fn poll(&mut self) { + for reader in &mut self.recv { + assert!(black_box(reader.poll_recv_group(&self.waiter)).is_pending()); + } + for reader in &mut self.next { + assert!(black_box(reader.poll_next_group(&self.waiter)).is_pending()); + } + } +} + +fn bench_idle_poll(c: &mut Criterion) { + let mut group = c.benchmark_group("track_idle_poll"); + for (name, ordered) in [("recv", false), ("next", true)] { + for readers in FANOUT { + group.throughput(Throughput::Elements(readers as u64)); + group.bench_function(BenchmarkId::new(name, readers), |b| { + let mut idle = Idle::new(readers, ordered); + b.iter(|| idle.poll()); + }); + } + } + group.finish(); +} + /// A sparse feed that opens one gap per millisecond, keeping `gaps` within its budget. struct Expiring { sparse: Sparse, @@ -1963,6 +2035,7 @@ criterion_group!( bench_parked_read, bench_sparse_read, bench_sparse_poll, + bench_idle_poll, bench_sparse_expire, bench_gap_flush, bench_wall, diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fb3bc4dcb54..939e0d742e1 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -8907,6 +8907,34 @@ mod test { } } + /// A read that found nothing parks again at once until something it parked on fires, + /// but parks anew for another task, which the next group then wakes instead. + #[test] + fn an_idle_read_parks_for_the_task_polling_it() { + for ordered in [false, true] { + let producer = track_producer("test", Info::default()); + let mut first = framed(&producer, 0, 1); + first.finish().unwrap(); + let mut reader = producer.subscribe(replay()); + let mut read = |waiter: &kio::Waiter| match ordered { + true => reader.poll_group(true, waiter), + false => reader.poll_group(false, waiter), + }; + let (flag, waiter) = Flag::waiter(); + assert!(matches!(read(&waiter), Poll::Ready(Ok(Some(_))))); + assert!(read(&waiter).is_pending()); + assert!(read(&waiter).is_pending(), "nothing fired, so it parks again"); + assert!(!flag.take()); + + let (other, moved) = Flag::waiter(); + assert!(read(&moved).is_pending()); + let mut next = framed(&producer, 1, 1); + next.finish().unwrap(); + assert!(other.take(), "the next group wakes the task that polled last"); + assert!(matches!(read(&moved), Poll::Ready(Ok(Some(_))))); + } + } + /// Write group `sequence` with `frames` frames, left open. fn framed(producer: &Producer, sequence: u64, frames: usize) -> group::Producer { let mut group = producer.create_group(group::Info { sequence }).unwrap(); From 0ecdc8def367cdf7029603ca8b7a605ab333b540 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 21:59:15 -0700 Subject: [PATCH 7/9] chore(quest): retire the idle poll exit quest Co-Authored-By: Claude Opus 5.5 --- quest/m1/README.md | 1 - quest/m1/idle-poll-exit.md | 48 -------------------------------------- 2 files changed, 49 deletions(-) delete mode 100644 quest/m1/idle-poll-exit.md diff --git a/quest/m1/README.md b/quest/m1/README.md index cad79f53c8a..f018361aa1e 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -170,7 +170,6 @@ removed). - [moq-clock's tests pass under cargo test](/quest/m1/clock-test-process.md) - `paused_runtimes_own_their_anchors` stops depending on being first in its process - [The JS stalled-consumer eviction test ages its groups](/quest/m1/js-cache-age-test.md) - the test drives the clock the cache reads, so it can fail - [moq-transport-22 FETCH cost](/quest/m1/ietf22-fetch-poll-cost.md) - #7042's +12.8% instructions on draft-22 FETCH is fixed or attributed -- [An idle track poll exits cheaply](/quest/m1/idle-poll-exit.md) - a reader's poll on a track with nothing new costs a small constant, not ~3,800 instructions - [Main is no slower than release](/quest/m1/release-perf/README.md) - every benchmark where main is slower than release is fixed or accepted with a reason, checked by a final paired run before the cut - [Interop coverage](/quest/m1/interop-coverage/README.md) - the riskiest moq-transport features moq-net exposes get cross-implementation tests over drafts 18 and 22 - [Calibrate the paired nightly benchmarks](/quest/m1/bench-ci.md) - A/A and deliberately slowed Nightly runs set the paired comparison's threshold, so its regression alerts can be trusted diff --git a/quest/m1/idle-poll-exit.md b/quest/m1/idle-poll-exit.md deleted file mode 100644 index b3e460f112a..00000000000 --- a/quest/m1/idle-poll-exit.md +++ /dev/null @@ -1,48 +0,0 @@ -# [M] An idle track poll exits cheaply - -## Goal - -A reader's poll on a track with nothing new costs a small constant number of -instructions, not about 3,800, so catalog and media fanout scale with the -readers that have work rather than every pending reader. - -## Plan - -Found 2026-10-28 by [moq-dev/moq#7140](https://github.com/moq-dev/moq/pull/7140), -which moved the arrival-grace and clock checks behind the cheap "nothing -changed" checks in `Misses::judge`. Even at the old baseline, a pending catalog -poll costs about 3,800 instructions per reader, spread across the cursor's -poll path (`Cursor::probe` in `rs/moq-net/src/model/track.rs` and what it -calls) on a track with nothing new. - -Decided 2026-10-28 (maintainer): m1, for relay fanout scale. - -- Profile one idle poll with instruction counts (callgrind or - `perf stat --no-scale`) and find where the cost goes. -- Add one early exit the whole path shares, keyed on something every change - already bumps (for example a track revision the cursor last saw), rather - than a short-cut per function. -- Measure the moq-mux catalog fanout benches and the moq-net session benches - over tracks and readers, before and after. - -Checkpoint 2026-10-29: no optimization implemented or measured. The requested -build of the existing `track`, `session`, and `catalog` benches never started: -`quest gate` kept waiting for host load to fall, so the attempt stopped at -build admission. Hardware instruction counters are accessible (`perf stat ---no-scale -e instructions true` succeeded), but that is only a tooling probe. -The 3,800-instruction baseline above has not been reproduced in this attempt. - -Resume with instruction attribution before choosing an early exit. The cursor -reads subscription preferences, fill answers, group starts, and wall deadlines -as well as track state; a track-only revision must not hide those changes or -retain registrations for a previous waiter. Existing regression surfaces include -`track_sparse_poll`, `catalog_estimate_fanout` (tracks x subscribers), and the -session scenarios. The transport waiter investigation is separately claimed by -[moq-dev/moq#7262](https://github.com/moq-dev/moq/pull/7262); no shared measured -cause or conflicting implementation scope has been established. - -Public API: none. Wire: none. - -## Related - -- [Mesh session cost](/quest/m2/perf/mesh-session-cost.md) - per-session costs in the same model From 60789e2f236db14861f35ea4fda76cd52e3126fc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 22:03:01 -0700 Subject: [PATCH 8/9] fix(moq-net): retire the idle gate's waiter as a task's park would Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 4 +-- rs/moq-net/src/model/track/idle.rs | 45 ++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 939e0d742e1..1efef41d22c 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6762,7 +6762,7 @@ impl Cursor { if idle::Idle::parked(&self.idle, idle::Walk::Arrival, waiter) { return Poll::Pending; } - let idle = idle::Idle::start(&mut self.idle, waiter); + let mut idle = idle::Idle::start(&mut self.idle, waiter); let polled = self.poll_recv_walk(take, idle.waiter()); idle.finish(&mut self.idle, idle::Walk::Arrival, polled.is_pending()); polled @@ -6973,7 +6973,7 @@ impl Cursor { if idle::Idle::parked(&self.idle, idle::Walk::Sequence, waiter) { return Poll::Pending; } - let idle = idle::Idle::start(&mut self.idle, waiter); + let mut idle = idle::Idle::start(&mut self.idle, waiter); let polled = self.poll_next_walk(take, idle.waiter()); idle.finish(&mut self.idle, idle::Walk::Sequence, polled.is_pending()); polled diff --git a/rs/moq-net/src/model/track/idle.rs b/rs/moq-net/src/model/track/idle.rs index e0aa7ed73e9..0236b1cbbef 100644 --- a/rs/moq-net/src/model/track/idle.rs +++ b/rs/moq-net/src/model/track/idle.rs @@ -31,9 +31,13 @@ pub(super) struct Idle { /// The read that parked, or `None` while nothing is parked. parked: Option, woken: Arc, - /// Wraps [`Self::woken`]. Kept across reads, so parking again on a list that still - /// holds it is free, as it would be for the task's own waiter. - waiter: kio::Waiter, + /// Wakes [`Self::woken`]. + wake: Waker, + /// Holds the waiter reads park on with [`Self::wake`]. Kept across reads, so parking + /// again on a list that still holds it is free, as it would be for the task's own + /// waiter, and retired as a task's own park retires one that parked on more lists + /// than it can record. + park: kio::Park, } /// Records a wake, then wakes the task. @@ -62,8 +66,9 @@ impl Idle { }); Self { parked: None, - waiter: kio::Waiter::new(Waker::from(woken.clone())), + wake: Waker::from(woken.clone()), woken, + park: kio::Park::default(), } } @@ -94,8 +99,8 @@ impl Idle { } /// The waiter a full read parks on. - pub(super) fn waiter(&self) -> &kio::Waiter { - &self.waiter + pub(super) fn waiter(&mut self) -> &kio::Waiter { + self.park.hold(&std::task::Context::from_waker(&self.wake)) } /// Put the gate back after a full read of `walk`, armed if it parked. @@ -111,3 +116,31 @@ impl Idle { } } } + +#[cfg(test)] +mod test { + use super::*; + + /// The waiter reads park on is kept across reads, but retired once it parked on more + /// lists than it records, so re-parking cannot stack entries those lists never collect. + #[test] + fn a_waiter_parked_past_its_records_is_retired() { + let task = kio::Waiter::noop(); + let mut slot = None; + for (lists, retired) in [(1, false), (9, true)] { + let mut idle = Idle::start(&mut slot, &task); + let mut parked: Vec<_> = (0..lists).map(|_| kio::WaiterList::new()).collect(); + let waiter = idle.waiter(); + for list in &mut parked { + waiter.register(list); + } + idle.finish(&mut slot, Walk::Arrival, true); + assert!(Idle::parked(&slot, Walk::Arrival, &task)); + + let mut idle = Idle::start(&mut slot, &task); + idle.waiter(); + assert_eq!(parked.iter().all(kio::WaiterList::is_empty), retired); + idle.finish(&mut slot, Walk::Arrival, false); + } + } +} From 8ad758fe15b9ef1d3ac8685c9d73fcb2f6072fb4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 22:14:00 -0700 Subject: [PATCH 9/9] quest: plan follow-ups from the idle poll exit Adds the paired bench baseline wait-and-check quest and the adapter waker allocation quest, and notes the per-reader cost of a delivered group in Mesh session cost. Co-Authored-By: Claude Opus 5.5 --- quest/m0/nightly/README.md | 1 + quest/m0/nightly/bench-baseline-build.md | 35 +++++++++++++++++++++++ quest/m2/perf/README.md | 1 + quest/m2/perf/adapter-waker-alloc.md | 36 ++++++++++++++++++++++++ quest/m2/perf/mesh-session-cost.md | 9 ++++++ 5 files changed, 82 insertions(+) create mode 100644 quest/m0/nightly/bench-baseline-build.md create mode 100644 quest/m2/perf/adapter-waker-alloc.md diff --git a/quest/m0/nightly/README.md b/quest/m0/nightly/README.md index edab3d0a04c..fa38e9428e5 100644 --- a/quest/m0/nightly/README.md +++ b/quest/m0/nightly/README.md @@ -35,6 +35,7 @@ Public API: none. Wire: none. ## Required +- [The paired bench baseline builds](/quest/m0/nightly/bench-baseline-build.md) - the next scheduled Nightly's paired bench jobs build their baseline and compare, or the baseline moves outside the checkout - [The browser backend baseline completes on IETF22](/quest/m0/nightly/browser-baseline.md) - the WASM and UniFFI backend baseline completes every supported transport case without an internal error - [Probing beside Cubic](/quest/m0/nightly/competing-cubic.md) - the `competing_cubic` probe test stays within its deadline and p99 budgets - [OBS TSan passes without a wall-clock bound](/quest/m0/nightly/obs-tsan.md) - `destroySource` asserts ordering instead of `elapsed < 500ms` diff --git a/quest/m0/nightly/bench-baseline-build.md b/quest/m0/nightly/bench-baseline-build.md new file mode 100644 index 00000000000..a2e0914292d --- /dev/null +++ b/quest/m0/nightly/bench-baseline-build.md @@ -0,0 +1,35 @@ +# [XS] The paired bench baseline builds + +## Goal + +The scheduled Nightly's paired bench jobs build their baseline and get to the +comparison. Check with `gh run list --workflow nightly.yml --event schedule` +and the "Rust bench targets" step of the newest run (the other paired jobs, +such as "io_uring benchmarks", share the cause). Once a scheduled run gets past +the base build, delete this quest. + +## Plan + +Decided 2026-11-01 (maintainer, `/quest-plan`): a wait-and-check quest that +stays ready, since #7245 likely fixed it already and only the next scheduled +run can tell. + +Scheduled run +[41204380716](https://github.com/moq-dev/moq/actions/runs/41204380716) +(2026-10-31) failed every paired bench job before comparing: `bench/run.sh` +checks the baseline out to `$RUN/base`, under `target/bench/nightly/` inside +the main checkout, and Cargo could not merge that checkout's +`.cargo/config.toml` `build-ffi` alias with the current one ("failed to merge +key `build-ffi`"). + +That run's head already had +[moq-dev/moq#7245](https://github.com/moq-dev/moq/pull/7245), which makes the +aliases strings, so a nested checkout's alias overrides instead of merging. +Its baseline (`previous-nightly`) predated #7245 and still had the list form. +The next scheduled run's baseline includes #7245, so it should build. + +If it still fails on the merge, build the baseline outside the checkout (a +`git worktree` beside the repository instead of under `target/`), so no +enclosing `.cargo/config.toml` applies. + +Found by [moq-dev/moq#7253](https://github.com/moq-dev/moq/pull/7253). diff --git a/quest/m2/perf/README.md b/quest/m2/perf/README.md index 5086788bc2a..803ad4e2825 100644 --- a/quest/m2/perf/README.md +++ b/quest/m2/perf/README.md @@ -92,6 +92,7 @@ revisions on the same host and worker count, and profiles with - [io_uring buffer pool memory](/quest/m2/perf/uring-pool-memory.md) - measure reserved versus resident pool memory before any shrink policy - [An idle export poll is cheap](/quest/m2/perf/export-idle-poll.md) - an export poll nothing woke skips the track fill and catalog work it repeats today - [Mesh session cost](/quest/m2/perf/mesh-session-cost.md) - moq-net's per-session announce, route sync, and scope work drops, swept over sessions and announces +- [Adapter reads reuse their waker](/quest/m2/perf/adapter-waker-alloc.md) - a measured verdict on the per-read waker allocation moq-tokio and moq-uring reads pay since #7042, fixed if real - [Lite request allocations](/quest/m2/perf/lite-request-allocs.md) - `Path::concat` allocates once and the lite request handler keeps one track-name copy - [Coverage before START](/quest/m2/perf/covered-before-start.md) - the lite-07 publisher's coverage check costs O(1) per wake before SUBSCRIBE_START - [io_uring demux churn](/quest/m2/perf/uring-demux-churn.md) - the demux snapshots stats and builds its fed list once per completion, not per datagram diff --git a/quest/m2/perf/adapter-waker-alloc.md b/quest/m2/perf/adapter-waker-alloc.md new file mode 100644 index 00000000000..e8a3ffce37f --- /dev/null +++ b/quest/m2/perf/adapter-waker-alloc.md @@ -0,0 +1,36 @@ +# [S] Adapter reads reuse their waker + +## Goal + +A measured verdict on what a parking read costs in the production adapters +(moq-tokio, moq-uring) since +[moq-dev/moq#7042](https://github.com/moq-dev/moq/pull/7042), and a fix if +the cost is real: a stream read that has to wait stops allocating a fresh +shared waker each time. + +## Plan + +Decided 2026-11-01 (maintainer, `/quest-plan`): measure before fixing, since +no bench has shown the cost yet. + +Since #7042, transport polls take a `kio::Waiter` through `&self`, and every +`kio::wait` future (`rs/kio/src/waiter.rs`) holds a waiter of its own. The +moq-net coding layer (`rs/moq-net/src/coding/reader.rs`, `writer.rs`) wraps +each decode or flush in one, so a read that parks allocates the waiter's +shared `Arc` every time, where a per-stream `kio::Park` used to reuse +one. + +[moq-dev/moq#7262](https://github.com/moq-dev/moq/pull/7262) fixed the +in-memory transport's version (it registered on the session close per read). +The memory-transport benches rarely park, so they cannot see the adapters' +cost. + +Measure first: user-space instruction counts and allocations, pinned to one +core, on moq-uring's `session_lite` bench (`just rs bench-session`) and, for +moq-tokio, which has no crate-level session bench, the relay's +`rs/moq-relay/benches/session.rs` or `just bench BASE`. Measure a revision +before #7042 next to main. A measured no-win deletes this quest. + +If it is real, let a stream keep one waiter (or `kio::Park`) across reads +again, for example in `coding::Reader`, instead of one per `kio::wait` +future. diff --git a/quest/m2/perf/mesh-session-cost.md b/quest/m2/perf/mesh-session-cost.md index c0242e697e5..7d505c1e621 100644 --- a/quest/m2/perf/mesh-session-cost.md +++ b/quest/m2/perf/mesh-session-cost.md @@ -15,3 +15,12 @@ cost is production code). Cache `OriginScope::relative`'s patterns first; benchmark before changing the rest. Public API: none expected. + +Decided 2026-11-01 (maintainer, `/quest-plan`): the per-reader cost of a +delivered group belongs here, beside the rest of the per-session cost. +Measured by [moq-dev/moq#7263](https://github.com/moq-dev/moq/pull/7263): +a track read that does get a group still costs about 12k to 26k user-space +instructions per reader per group (1 to 16 readers per track). Part of it is +copying the large `group::Consumer` value, inside +`Poll>>`, through each layer that returns it; +consider boxing parts of `group::Consumer`, and measure it on the same read.