From e8d251525407ade444c2f42b70b057945e7f27f5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 23:00:11 -0700 Subject: [PATCH 1/7] chore(quest): claim quest/m2/perf/covered-before-start.md Quest-Claim: 21297f0c40fc685f From 79300d88d73184f95ca4ce8c5911a62900a9e89d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 23:20:23 -0700 Subject: [PATCH 2/7] perf(net): check lite coverage before START against the skipped record in place The lite-07 publisher rebuilt a RangeSet from every range given up before SUBSCRIBE_START on each wake, only to ask whether it covers the window. Window::covered_by now reads any Accounted record, a RangeSet or a RangeMap keyed by the groups, so the skipped map is checked as it is. Adds the publisher bench, which wakes a bounded lite-07 subscription with no start resolved, swept over the ranges already given up. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/Cargo.toml | 9 +- rs/moq-net/benches/publisher.rs | 29 ++++++ rs/moq-net/src/fuzz.rs | 3 + rs/moq-net/src/lite/mod.rs | 2 + rs/moq-net/src/lite/publisher.rs | 104 +++++++++++++++++++- rs/moq-net/src/model/resume.rs | 4 +- rs/moq-net/src/model/subscription.rs | 1 + rs/moq-net/src/model/subscription/window.rs | 23 ++++- rs/moq-net/src/model/track.rs | 4 +- 9 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 rs/moq-net/benches/publisher.rs diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index ddaff4c7a5f..07cf1e7f926 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -18,7 +18,8 @@ default = ["async"] async = ["dep:futures", "moq-clock/runtime"] # Exposes the wire codecs to the `fuzz/` harness and the announce, codec, path, varint, frame, and canonical storage benches, -# and session views of an origin to the viewers bench, through the hidden `fuzz` module. Off by +# session views of an origin to the viewers bench, and the lite publisher's run loop to the +# publisher bench, through the hidden `fuzz` module. Off by # default and hidden from the docs. fuzz = ["async"] @@ -144,6 +145,12 @@ name = "tail" harness = false required-features = ["fuzz"] +# Drives the lite publisher's run loop through the hidden `fuzz` module. +[[bench]] +name = "publisher" +harness = false +required-features = ["fuzz"] + [[bench]] name = "priority" harness = false diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs new file mode 100644 index 00000000000..37f48345c22 --- /dev/null +++ b/rs/moq-net/benches/publisher.rs @@ -0,0 +1,29 @@ +//! The lite publisher's coverage check before SUBSCRIBE_START. +//! +//! `publisher_covered_before_start` holds a bounded lite-07 subscription whose source gave +//! up `skipped` of its ranges and has yet to resolve a start, then wakes it with nothing new +//! to serve. Each wake ends in the check of whether every range was given up, so a cost +//! that grows with the ranges already given up shows up as a slope over `skipped`. +//! +//! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. + +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use moq_net::fuzz::CoverageBench; + +/// Ranges the source gave up before the wake; one more stays open. +const SKIPPED: [u64; 4] = [1, 32, 1024, 8192]; + +fn bench_covered_before_start(c: &mut Criterion) { + let mut group = c.benchmark_group("publisher_covered_before_start"); + group.throughput(Throughput::Elements(1)); + for skipped in SKIPPED { + group.bench_function(BenchmarkId::new("wake", skipped), |b| { + let mut run = CoverageBench::new(skipped + 1, skipped); + b.iter(|| run.wake()); + }); + } + group.finish(); +} + +criterion_group!(benches, bench_covered_before_start); +criterion_main!(benches); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 052d2d938ce..cbb1b62f4b1 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -16,6 +16,9 @@ mod request; pub use ingest::ingest; pub use request::request; +/// A lite-07 publisher run loop waiting on SUBSCRIBE_START, for the publisher bench. +pub use crate::lite::CoverageBench; + use bytes::Bytes; use crate::{ diff --git a/rs/moq-net/src/lite/mod.rs b/rs/moq-net/src/lite/mod.rs index 651b06abca6..f92d8567532 100644 --- a/rs/moq-net/src/lite/mod.rs +++ b/rs/moq-net/src/lite/mod.rs @@ -30,6 +30,8 @@ pub(crate) mod priority; mod probe; #[cfg(feature = "async")] mod publisher; +#[cfg(any(test, feature = "fuzz"))] +pub use publisher::CoverageBench; #[cfg(feature = "async")] mod replay; #[cfg(feature = "async")] diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index cc7a76e29c4..879c341f83d 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4317,10 +4317,7 @@ impl TrackRun { // `SubStream::owed`), so only a range given up whole is covered. Datagrams // resolve no start, and a FIN would overtake them, so a range they served // keeps the subscription open. - (None, _) => { - let skipped: RangeSet = self.skipped.iter().map(|(range, _)| range.clone()).collect(); - self.track.covered(self.floor, &skipped) - } + (None, _) => self.track.covered(self.floor, &self.skipped), (Some(_), None) => false, } } @@ -4551,6 +4548,88 @@ impl TrackRun { } } +/// A bounded lite-07 subscription's run loop still waiting on SUBSCRIBE_START, for the +/// `publisher` bench: a wake that finds nothing new ends in the coverage check. +#[cfg(any(test, feature = "fuzz"))] +pub struct CoverageBench { + run: TrackRun, + writer: Writer, + track: track::Producer, + _peer: crate::transport::memory::Session, +} + +#[cfg(any(test, feature = "fuzz"))] +impl CoverageBench { + /// Subscribe to `ranges` one-group ranges, then give up the first `skipped` of them, one + /// wake each. A range is left open, so the start never resolves and nothing is covered. + pub fn new(ranges: u64, skipped: u64) -> Self { + assert!(skipped < ranges, "a range must stay open"); + let version = Version::Lite07; + let (local, peer) = crate::transport::memory::pair(Default::default()); + let session = Transport::new(local); + let Poll::Ready(Ok(stream)) = session.poll_open_uni(&kio::Waiter::noop(), SendOptions::default()) else { + panic!("a memory stream opens at once"); + }; + // Every other group, so no two ranges merge into one run. + let group = |group| Some(lite::Anchor::Absolute(track::Position::group(group))); + let wire: Vec<_> = (0..ranges) + .map(|range| lite::Range { + start: group(2 * range), + end: group(2 * range + 1), + }) + .collect(); + let bounds = Bounds::new(&wire, None).unwrap(); + let track = track::Producer::new( + Arc::new(crate::broadcast::Info::default()), + "bench", + track::Info::default(), + ); + let subscriber = track.subscribe(bounds.groups()); + let steer = kio::Producer::new(Steer::default()); + let ctx = Subscription { + session, + id: 0, + track_name: "bench".into(), + priority: PriorityQueue::default(), + publisher: 0, + group: None, + steer: steer.consume(), + steer_seen: Steer::default(), + version, + stamp: None, + runtime: crate::time::Deadlines::new(moq_clock::now()), + outcomes: Default::default(), + ietf: false, + }; + let mut this = Self { + run: TrackRun::new(ctx, subscriber, bounds, 0, steer), + writer: Writer::new(stream, version), + track, + _peer: peer, + }; + for range in 0..skipped { + this.track.drop_groups(2 * range..2 * range + 1, StreamError::NotFound); + while this.step().is_ready() {} + } + assert!(this.run.start.is_none(), "no group resolves a start"); + assert_eq!(this.run.skipped.iter().count() as u64, skipped); + this + } + + /// One wake with nothing new to serve. + pub fn wake(&mut self) { + assert!(self.step().is_pending(), "the open range keeps the subscription parked"); + } + + fn step(&mut self) -> Poll<()> { + match self.run.poll_step(&mut self.writer, &kio::Waiter::noop()) { + Poll::Ready(Ok(ControlFlow::Continue(()))) => Poll::Ready(()), + Poll::Ready(res) => panic!("the subscription ended: {res:?}"), + Poll::Pending => Poll::Pending, + } + } +} + /// Where a group stream is ranked. enum Rank { /// In the subscription's send group, by its sequence in the subscription's order. @@ -6927,6 +7006,23 @@ mod serve_group_test { } } + /// A bounded lite-07 subscription ends once its source gives up every range, even with no + /// start resolved, and not while one range is still open. + #[moq_clock::test] + async fn giving_up_every_range_before_the_start_ends_it() { + let mut bench = CoverageBench::new(3, 2); + bench.wake(); + bench.track.drop_groups(4..5, StreamError::NotFound); + loop { + match bench.run.poll_step(&mut bench.writer, &kio::Waiter::noop()) { + Poll::Ready(Ok(ControlFlow::Continue(()))) => continue, + Poll::Ready(Ok(ControlFlow::Break(()))) => break, + other => panic!("the subscription should end, got {other:?}"), + } + } + assert!(bench.run.start.is_none()); + } + /// A SUBSCRIBE_UPDATE that lowers the start below SUBSCRIBE_START asks for those groups /// too, so the drops at the end count from there. #[moq_clock::test] diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 4d212c4858f..7e9a4a0c161 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -51,7 +51,7 @@ use crate::time::Instant; use crate::{Error, Result, StreamError, group, stats, track}; use super::delivered::Delivered; -use super::subscription::{Anchor, Position, Resolved, Subscription, Unheld, Window}; +use super::subscription::{Accounted, Anchor, Position, Resolved, Subscription, Unheld, Window}; use crate::ledger; /// What the front says about a logical track: the copy to read now, and how it ended. @@ -751,7 +751,7 @@ impl Subscriber { /// Whether `accounted` holds every group the reader's bounded ranges name from `from` /// up to `cap`; see [`track::Subscriber::covered`]. - pub(super) fn covered(&self, from: u64, cap: Option, accounted: &RangeSet) -> bool { + pub(super) fn covered(&self, from: u64, cap: Option, accounted: &impl Accounted) -> bool { self.reader().window().covered_by(from, cap, accounted) } diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index c75b4945d55..addaeb24cdd 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -6,6 +6,7 @@ use std::{ use crate::Timestamp; mod window; +pub(crate) use window::Accounted; pub(super) use window::{Containing, Held, Holdings, Resolved, Unheld, Window, later}; /// Subscriber-side preferences for receiving a track. diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 2fc66d49297..a4df67f788a 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -19,6 +19,25 @@ pub(in crate::model) struct Resolved { pub end: Option, } +/// The groups a publisher accounted for, which [`Window::covered_by`] checks against: a +/// set, or a map keyed by them, read in place so a check copies nothing. +pub(crate) trait Accounted { + /// Whether every group in `range` is accounted for. + fn holds(&self, range: &ops::Range) -> bool; +} + +impl Accounted for rangemap::RangeSet { + fn holds(&self, range: &ops::Range) -> bool { + self.gaps(range).next().is_none() + } +} + +impl Accounted for rangemap::RangeMap { + fn holds(&self, range: &ops::Range) -> bool { + self.gaps(range).next().is_none() + } +} + /// When a serving hop's groups start, for resolving time anchors. /// /// Each query answers from ordered indexes, so a time end that walks them forward a step @@ -508,14 +527,14 @@ impl Window { /// Whether `accounted` holds every group in range from `from` up to the exclusive `cap`, /// `false` while a range stays open (see [`Self::end`]). - pub fn covered_by(&self, from: u64, cap: Option, accounted: &rangemap::RangeSet) -> bool { + pub fn covered_by(&self, from: u64, cap: Option, accounted: &impl Accounted) -> bool { let Some(end) = self.end() else { return false; }; let end = cap.map_or(end, |cap| cap.min(end)); self.runs(from).take_while(|run| *run.start() < end).all(|run| { let run_end = run.end().checked_add(1).map_or(end, |next| next.min(end)); - accounted.gaps(&(*run.start()..run_end)).next().is_none() + accounted.holds(&(*run.start()..run_end)) }) } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fb451d50464..f51b01748e3 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -23,7 +23,7 @@ use super::skipped::Skipped; use crate::ledger::{self, Ledger}; pub use super::subscription::{Anchor, Order, Position, Range, Subscription}; -use super::subscription::{Containing, Held, Holdings, Resolved, Window, later}; +use super::subscription::{Accounted, Containing, Held, Holdings, Resolved, Window, later}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; mod demand; @@ -7668,7 +7668,7 @@ impl Subscriber { /// A publisher that accounts for each group it serves or drops learns from it that a /// bounded subscription has nothing more to deliver. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn covered(&mut self, from: u64, accounted: &rangemap::RangeSet) -> bool { + pub(crate) fn covered(&mut self, from: u64, accounted: &impl Accounted) -> bool { let bounds = self.bounds(); let from = from.max(bounds.start); match &mut self.inner { From 160bc581f5b373edfddf374f557e1072487ce72a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 23:43:03 -0700 Subject: [PATCH 3/7] perf(net): sweep the publisher bench over skipped ranges at a fixed range count Holding the range count fixed isolates the cost that grows with the ranges already given up, and removes the finished quest. Co-Authored-By: Claude Opus 5.5 --- quest/m2/perf/README.md | 1 - quest/m2/perf/covered-before-start.md | 20 -------------------- rs/moq-net/benches/publisher.rs | 16 ++++++++++------ 3 files changed, 10 insertions(+), 27 deletions(-) delete mode 100644 quest/m2/perf/covered-before-start.md diff --git a/quest/m2/perf/README.md b/quest/m2/perf/README.md index 5086788bc2a..0397d376b38 100644 --- a/quest/m2/perf/README.md +++ b/quest/m2/perf/README.md @@ -93,7 +93,6 @@ revisions on the same host and worker count, and profiles with - [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 - [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 - [Lock-free single-delivery writes](/quest/m2/perf/single-delivery-write.md) - a frame with one delivery writes without a lock, recovering #6922's remaining FETCH cost - [JS publisher per-frame cost](/quest/m2/perf/js-publisher-frame-cost.md) - fewer promise races per frame, and a per-frame cost that stays flat as streams grow diff --git a/quest/m2/perf/covered-before-start.md b/quest/m2/perf/covered-before-start.md deleted file mode 100644 index d7348da88eb..00000000000 --- a/quest/m2/perf/covered-before-start.md +++ /dev/null @@ -1,20 +0,0 @@ -# [S] Lite coverage checks before START are cheap - -## Goal - -Before SUBSCRIBE_START, the lite-07 publisher's coverage check stops -rebuilding a `RangeSet` from every skipped range and rescanning the covered -runs on each wake. A wake costs work proportional to what changed since the -last one, not to the skipped record's size. - -## Plan - -Found by [moq-dev/moq#6995](https://github.com/moq-dev/moq/pull/6995), which -made `covered` incremental after the start resolves. `self.skipped` is already -updated incrementally; the cost is the set rebuilt from it and the scan in -`track::Subscriber::covered`. Keep an incremental coverage summary or cursor -across wakes instead. `track_range_filling` never builds a `TrackRun` or -reaches this branch, so add a benchmark that drives the lite publisher with -no start resolved, swept over skipped ranges. - -Public API: none. diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index 37f48345c22..a92ccda247d 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -1,24 +1,28 @@ //! The lite publisher's coverage check before SUBSCRIBE_START. //! -//! `publisher_covered_before_start` holds a bounded lite-07 subscription whose source gave -//! up `skipped` of its ranges and has yet to resolve a start, then wakes it with nothing new -//! to serve. Each wake ends in the check of whether every range was given up, so a cost -//! that grows with the ranges already given up shows up as a slope over `skipped`. +//! `publisher_covered_before_start` holds a bounded lite-07 subscription to `RANGES` ranges +//! whose source gave up `skipped` of them and has yet to resolve a start, then wakes it with +//! nothing new to serve. Each wake ends in the check of whether every range was given up. +//! The range count stays fixed, so a cost that grows with the ranges already given up shows +//! up as a slope over `skipped`. //! //! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use moq_net::fuzz::CoverageBench; -/// Ranges the source gave up before the wake; one more stays open. +/// Ranges the source gave up before the wake. const SKIPPED: [u64; 4] = [1, 32, 1024, 8192]; +/// Ranges subscribed: one more than the most given up, so one stays open. +const RANGES: u64 = 8193; + fn bench_covered_before_start(c: &mut Criterion) { let mut group = c.benchmark_group("publisher_covered_before_start"); group.throughput(Throughput::Elements(1)); for skipped in SKIPPED { group.bench_function(BenchmarkId::new("wake", skipped), |b| { - let mut run = CoverageBench::new(skipped + 1, skipped); + let mut run = CoverageBench::new(RANGES, skipped); b.iter(|| run.wake()); }); } From ae7f0d4dd21ded2385bc52a60cdbe0c7314889c1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 00:09:24 -0700 Subject: [PATCH 4/7] style(net): order the subscription imports as rustfmt does Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index f51b01748e3..ae7e67aadba 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -22,8 +22,8 @@ use super::delivered::Delivered; use super::skipped::Skipped; use crate::ledger::{self, Ledger}; -pub use super::subscription::{Anchor, Order, Position, Range, Subscription}; use super::subscription::{Accounted, Containing, Held, Holdings, Resolved, Window, later}; +pub use super::subscription::{Anchor, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; mod demand; From bab52009dc333e729e11ebf48850777d888b4bf6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 00:16:54 -0700 Subject: [PATCH 5/7] perf(net): resume a coverage check where the last one stopped Each wake before SUBSCRIBE_START rescanned every range already given up. Window::covered_by keeps where its last check stopped and resumes there while the floor holds, resetting when the window rebuilds. This is the memo from moq-dev/moq#6995 as it stands, so the quest's goal holds on main whichever lands first. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/resume.rs | 4 +- rs/moq-net/src/model/subscription/window.rs | 61 +++++++++++++++++++-- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 7e9a4a0c161..40f7763a87f 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -752,7 +752,9 @@ impl Subscriber { /// Whether `accounted` holds every group the reader's bounded ranges name from `from` /// up to `cap`; see [`track::Subscriber::covered`]. pub(super) fn covered(&self, from: u64, cap: Option, accounted: &impl Accounted) -> bool { - self.reader().window().covered_by(from, cap, accounted) + let mut reader = self.reader(); + reader.window(); + reader.window.covered_by(from, cap, accounted) } /// Poll for sequences the serving copy gave up that no copy may still deliver; see diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index a4df67f788a..25c48a8c741 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -144,6 +144,9 @@ pub(in crate::model) struct Window { /// Nothing is left to resolve: see [`Self::is_settled`]. Kept so a read checks it once /// per group, not once per range. settled: bool, + /// The floor the last [`Self::covered_by`] checked from, and where it stopped finding the + /// groups accounted for. + covered: Option<(u64, u64)>, } /// One non-empty range, resolved. @@ -351,6 +354,7 @@ impl Window { missable: RangeInclusiveSet::new(), pending: false, settled: false, + covered: None, }; window.place(latest, holdings)?; Ok(window) @@ -527,15 +531,30 @@ impl Window { /// Whether `accounted` holds every group in range from `from` up to the exclusive `cap`, /// `false` while a range stays open (see [`Self::end`]). - pub fn covered_by(&self, from: u64, cap: Option, accounted: &impl Accounted) -> bool { + /// + /// The caller only grows `accounted` between checks, so each resumes where the last + /// stopped unless `from` fell below its floor: one made on every wake visits each range + /// once over the subscription, not once per wake. The last stop never passes its cap, so + /// a wider one is checked past it anyway. + pub fn covered_by(&mut self, from: u64, cap: Option, accounted: &impl Accounted) -> bool { let Some(end) = self.end() else { return false; }; let end = cap.map_or(end, |cap| cap.min(end)); - self.runs(from).take_while(|run| *run.start() < end).all(|run| { + let mut through = match self.covered { + Some((floor, through)) if floor <= from => through.max(from), + _ => from, + }; + let covered = self.runs(through).take_while(|run| *run.start() < end).all(|run| { let run_end = run.end().checked_add(1).map_or(end, |next| next.min(end)); - accounted.holds(&(*run.start()..run_end)) - }) + let whole = accounted.holds(&(*run.start()..run_end)); + if whole { + through = run_end; + } + whole + }); + self.covered = Some((from, through)); + covered } /// The groups in range from `from` on, lowest first. @@ -622,6 +641,7 @@ impl Window { } fn rebuild(&mut self) { + self.covered = None; self.settled = !self.pending && !self.spans.iter().any(|span| matches!(span.end, End::Time(_))); self.groups = RangeInclusiveSet::new(); self.missable = RangeInclusiveSet::new(); @@ -1125,6 +1145,39 @@ mod tests { assert!(window.admits_datagram(6, Some(at(5000)))); } + /// A coverage check resumes where the last stopped, but not below its floor or past its + /// cap: a lower floor or a wider cap reaches groups no check has seen. + #[test] + fn coverage_resumes_within_what_was_checked() { + let mut window = Window::new(&[Range::groups(0..2), Range::groups(4..6)], None, &Untimed).unwrap(); + let accounted = rangemap::RangeSet::from_iter([0..1, 4..6]); + assert!(window.covered_by(4, None, &accounted)); + assert!( + !window.covered_by(0, None, &accounted), + "group 1 is owed below the old floor" + ); + + let accounted = rangemap::RangeSet::from_iter([0..2, 4..5]); + assert!(window.covered_by(0, Some(5), &accounted)); + assert!( + !window.covered_by(0, None, &accounted), + "group 5 is owed past the old cap" + ); + assert!(window.covered_by(1, Some(5), &accounted), "a raised floor resumes"); + } + + /// A map keyed by the groups accounted for counts as a set of them, whatever its values. + #[test] + fn coverage_reads_a_map_of_the_groups_accounted_for() { + let mut window = Window::new(&[Range::groups(0..2), Range::groups(4..6)], None, &Untimed).unwrap(); + let mut accounted = rangemap::RangeMap::new(); + accounted.insert(0..2, 'a'); + accounted.insert(4..5, 'b'); + assert!(!window.covered_by(0, None, &accounted), "group 5 is owed"); + accounted.insert(5..6, 'a'); + assert!(window.covered_by(0, None, &accounted)); + } + #[test] fn an_untimed_track_refuses_time_anchors() { assert!(matches!( From 3e5ed017f76ab3d34e5d684947b321569543afbd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 00:39:12 -0700 Subject: [PATCH 6/7] perf(net): resume a coverage check at the first group it found missing A check that stopped inside a range rescanned the range from its start on the next wake, so a range given up a group at a time with alternating errors, one skipped entry per group, cost a walk of every entry per wake. Accounted now names the first missing group, and the check resumes there. The publisher bench gains an adjacent case for it. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/benches/publisher.rs | 8 ++- rs/moq-net/src/lite/publisher.rs | 59 +++++++++++++++------ rs/moq-net/src/model/subscription/window.rs | 50 ++++++++++++----- 3 files changed, 86 insertions(+), 31 deletions(-) diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index a92ccda247d..8babc54acc2 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -4,7 +4,8 @@ //! whose source gave up `skipped` of them and has yet to resolve a start, then wakes it with //! nothing new to serve. Each wake ends in the check of whether every range was given up. //! The range count stays fixed, so a cost that grows with the ranges already given up shows -//! up as a slope over `skipped`. +//! up as a slope over `skipped`. `adjacent` gives up the groups of one range instead, each +//! with a different error from its neighbor, so the record holds one entry per group. //! //! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. @@ -25,6 +26,11 @@ fn bench_covered_before_start(c: &mut Criterion) { let mut run = CoverageBench::new(RANGES, skipped); b.iter(|| run.wake()); }); + // The same record within one range, each group its own entry. + group.bench_function(BenchmarkId::new("adjacent", skipped), |b| { + let mut run = CoverageBench::adjacent(skipped); + b.iter(|| run.wake()); + }); } group.finish(); } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index fbbc27540a4..92265770579 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4573,18 +4573,41 @@ impl CoverageBench { /// wake each. A range is left open, so the start never resolves and nothing is covered. pub fn new(ranges: u64, skipped: u64) -> Self { assert!(skipped < ranges, "a range must stay open"); + // Every other group, so no two ranges merge into one run. + let wire = (0..ranges).map(|range| 2 * range..2 * range + 1); + let drops = (0..skipped) + .map(|range| (2 * range..2 * range + 1, StreamError::NotFound)) + .collect(); + Self::build(wire, drops) + } + + /// Subscribe to one range of `skipped + 1` groups, then give up all but the last, one + /// wake each. Alternating errors keep each group its own entry in the skipped record. + pub fn adjacent(skipped: u64) -> Self { + let drops = (0..skipped) + .map(|group| { + let err = match group % 2 { + 0 => StreamError::NotFound, + _ => StreamError::Cancel, + }; + (group..group + 1, err) + }) + .collect(); + Self::build(std::iter::once(0..skipped + 1), drops) + } + + fn build(wire: impl Iterator>, drops: Vec<(Range, StreamError)>) -> Self { let version = Version::Lite07; let (local, peer) = crate::transport::memory::pair(Default::default()); let session = Transport::new(local); let Poll::Ready(Ok(stream)) = session.poll_open_uni(&kio::Waiter::noop(), SendOptions::default()) else { panic!("a memory stream opens at once"); }; - // Every other group, so no two ranges merge into one run. let group = |group| Some(lite::Anchor::Absolute(track::Position::group(group))); - let wire: Vec<_> = (0..ranges) + let wire: Vec<_> = wire .map(|range| lite::Range { - start: group(2 * range), - end: group(2 * range + 1), + start: group(range.start), + end: group(range.end), }) .collect(); let bounds = Bounds::new(&wire, None).unwrap(); @@ -4616,12 +4639,13 @@ impl CoverageBench { track, _peer: peer, }; - for range in 0..skipped { - this.track.drop_groups(2 * range..2 * range + 1, StreamError::NotFound); + let skipped = drops.len(); + for (groups, err) in drops { + this.track.drop_groups(groups, err); while this.step().is_ready() {} } assert!(this.run.start.is_none(), "no group resolves a start"); - assert_eq!(this.run.skipped.iter().count() as u64, skipped); + assert_eq!(this.run.skipped.iter().count(), skipped, "each drop is its own entry"); this } @@ -7019,17 +7043,20 @@ mod serve_group_test { /// start resolved, and not while one range is still open. #[moq_clock::test] async fn giving_up_every_range_before_the_start_ends_it() { - let mut bench = CoverageBench::new(3, 2); - bench.wake(); - bench.track.drop_groups(4..5, StreamError::NotFound); - loop { - match bench.run.poll_step(&mut bench.writer, &kio::Waiter::noop()) { - Poll::Ready(Ok(ControlFlow::Continue(()))) => continue, - Poll::Ready(Ok(ControlFlow::Break(()))) => break, - other => panic!("the subscription should end, got {other:?}"), + // Disjoint ranges, then one range given up a group at a time with alternating errors. + for (mut bench, last) in [(CoverageBench::new(3, 2), 4), (CoverageBench::adjacent(4), 4)] { + bench.wake(); + bench.wake(); + bench.track.drop_groups(last..last + 1, StreamError::NotFound); + loop { + match bench.run.poll_step(&mut bench.writer, &kio::Waiter::noop()) { + Poll::Ready(Ok(ControlFlow::Continue(()))) => continue, + Poll::Ready(Ok(ControlFlow::Break(()))) => break, + other => panic!("the subscription should end, got {other:?}"), + } } + assert!(bench.run.start.is_none()); } - assert!(bench.run.start.is_none()); } /// A SUBSCRIBE_UPDATE that lowers the start below SUBSCRIBE_START asks for those groups diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 25c48a8c741..9cb934abb40 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -22,19 +22,19 @@ pub(in crate::model) struct Resolved { /// The groups a publisher accounted for, which [`Window::covered_by`] checks against: a /// set, or a map keyed by them, read in place so a check copies nothing. pub(crate) trait Accounted { - /// Whether every group in `range` is accounted for. - fn holds(&self, range: &ops::Range) -> bool; + /// The first group in `range` not accounted for, `None` when every one is. + fn first_gap(&self, range: &ops::Range) -> Option; } impl Accounted for rangemap::RangeSet { - fn holds(&self, range: &ops::Range) -> bool { - self.gaps(range).next().is_none() + fn first_gap(&self, range: &ops::Range) -> Option { + self.gaps(range).next().map(|gap| gap.start) } } impl Accounted for rangemap::RangeMap { - fn holds(&self, range: &ops::Range) -> bool { - self.gaps(range).next().is_none() + fn first_gap(&self, range: &ops::Range) -> Option { + self.gaps(range).next().map(|gap| gap.start) } } @@ -532,10 +532,11 @@ impl Window { /// Whether `accounted` holds every group in range from `from` up to the exclusive `cap`, /// `false` while a range stays open (see [`Self::end`]). /// - /// The caller only grows `accounted` between checks, so each resumes where the last - /// stopped unless `from` fell below its floor: one made on every wake visits each range - /// once over the subscription, not once per wake. The last stop never passes its cap, so - /// a wider one is checked past it anyway. + /// The caller only grows `accounted` between checks, so each resumes at the first group + /// the last found missing unless `from` fell below its floor: one made on every wake + /// visits each accounted entry once over the subscription, not once per wake, even + /// within one range. The last stop never passes its cap, so a wider one is checked past + /// it anyway. pub fn covered_by(&mut self, from: u64, cap: Option, accounted: &impl Accounted) -> bool { let Some(end) = self.end() else { return false; @@ -547,11 +548,16 @@ impl Window { }; let covered = self.runs(through).take_while(|run| *run.start() < end).all(|run| { let run_end = run.end().checked_add(1).map_or(end, |next| next.min(end)); - let whole = accounted.holds(&(*run.start()..run_end)); - if whole { - through = run_end; + match accounted.first_gap(&(*run.start()..run_end)) { + None => { + through = run_end; + true + } + Some(gap) => { + through = gap; + false + } } - whole }); self.covered = Some((from, through)); covered @@ -1166,6 +1172,22 @@ mod tests { assert!(window.covered_by(1, Some(5), &accounted), "a raised floor resumes"); } + /// A check that stops partway into a range resumes there, and still sees a gap filled + /// behind where it stopped. + #[test] + fn coverage_resumes_within_a_range() { + let mut window = Window::new(&[Range::groups(0..8)], None, &Untimed).unwrap(); + let mut accounted = rangemap::RangeSet::from_iter(std::iter::once(0..3)); + assert!(!window.covered_by(0, None, &accounted)); + assert_eq!(window.covered, Some((0, 3)), "stops at the first missing group"); + accounted.insert(3..5); + accounted.insert(6..8); + assert!(!window.covered_by(0, None, &accounted)); + assert_eq!(window.covered, Some((0, 5))); + accounted.insert(5..6); + assert!(window.covered_by(0, None, &accounted)); + } + /// A map keyed by the groups accounted for counts as a set of them, whatever its values. #[test] fn coverage_reads_a_map_of_the_groups_accounted_for() { From 02580c203ea48ab61b9009f4b35094a4ca27df17 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 09:31:05 -0800 Subject: [PATCH 7/7] fix(net): gate the CoverageBench re-export on async, as its module is The no-default-features test lane sets cfg(test) without async, so the re-export named a module that was not compiled. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/src/lite/mod.rs b/rs/moq-net/src/lite/mod.rs index f92d8567532..97cd9aae167 100644 --- a/rs/moq-net/src/lite/mod.rs +++ b/rs/moq-net/src/lite/mod.rs @@ -30,7 +30,7 @@ pub(crate) mod priority; mod probe; #[cfg(feature = "async")] mod publisher; -#[cfg(any(test, feature = "fuzz"))] +#[cfg(all(feature = "async", any(test, feature = "fuzz")))] pub use publisher::CoverageBench; #[cfg(feature = "async")] mod replay;