diff --git a/quest/m2/perf/README.md b/quest/m2/perf/README.md index 35f1970a844..42a9608cce8 100644 --- a/quest/m2/perf/README.md +++ b/quest/m2/perf/README.md @@ -98,6 +98,5 @@ revisions on the same host and worker count, and profiles with - [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 - [Cluster route memory](/quest/m2/perf/cluster-route-memory.md) - a full-mesh cluster's ~3.7 KB per route per broadcast shrinks, the in-memory hop is profiled, and `bench-profile lock` tolerates lost records - [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 - [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 - [Small hot-path waste](/quest/m2/perf/hot-path-waste.md) - one track-name copy per lite subscription, no full-history `#drift` walk, and one header buffer per JS object 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/Cargo.toml b/rs/moq-net/Cargo.toml index 405dc55c83e..fb55be30215 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -18,7 +18,8 @@ default = ["async"] async = ["dep:futures", "kio/async", "kio/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"] @@ -143,6 +144,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..8babc54acc2 --- /dev/null +++ b/rs/moq-net/benches/publisher.rs @@ -0,0 +1,39 @@ +//! The lite publisher's coverage check before SUBSCRIBE_START. +//! +//! `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`. `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`. + +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use moq_net::fuzz::CoverageBench; + +/// 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(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(); +} + +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 c223068bb1b..0e2f00310aa 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..97cd9aae167 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(all(feature = "async", 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 1393f68957c..28c335ade3a 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4334,10 +4334,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, } } @@ -4568,6 +4565,112 @@ 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"); + // 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"); + }; + let group = |group| Some(lite::Anchor::Absolute(track::Position::group(group))); + let wire: Vec<_> = wire + .map(|range| lite::Range { + start: group(range.start), + end: group(range.end), + }) + .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(kio::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, + }; + 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(), skipped, "each drop is its own entry"); + 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. @@ -6941,6 +7044,26 @@ 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. + #[kio::test] + async fn giving_up_every_range_before_the_start_ends_it() { + // 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()); + } + } + /// A SUBSCRIBE_UPDATE that lowers the start below SUBSCRIBE_START asks for those groups /// too, so the drops at the end count from there. #[kio::test] diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 7419d9fb8a2..c5a05bdedc9 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. @@ -797,7 +797,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 { let mut reader = self.reader(); reader.window(); 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 4a237cefe92..48683800611 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 { + /// 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 first_gap(&self, range: &ops::Range) -> Option { + self.gaps(range).next().map(|gap| gap.start) + } +} + +impl Accounted for rangemap::RangeMap { + fn first_gap(&self, range: &ops::Range) -> Option { + self.gaps(range).next().map(|gap| gap.start) + } +} + /// 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 @@ -510,11 +529,12 @@ 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. - pub fn covered_by(&mut self, from: u64, cap: Option, accounted: &rangemap::RangeSet) -> bool { + /// 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; }; @@ -525,11 +545,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.gaps(&(*run.start()..run_end)).next().is_none(); - 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 @@ -1144,6 +1169,34 @@ 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() { + 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!( diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 3392bdda9ab..88ff29b4c7f 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}; +use super::subscription::{Accounted, Containing, Held, Holdings, Resolved, Window, later}; pub use super::subscription::{Anchor, Order, Position, Range, Subscription}; -use super::subscription::{Containing, Held, Holdings, Resolved, Window, later}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; mod demand; @@ -7854,7 +7854,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 {