Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m2/perf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 0 additions & 20 deletions quest/m2/perf/covered-before-start.md

This file was deleted.

9 changes: 8 additions & 1 deletion rs/moq-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions rs/moq-net/benches/publisher.rs
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 3 additions & 0 deletions rs/moq-net/src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
2 changes: 2 additions & 0 deletions rs/moq-net/src/lite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
131 changes: 127 additions & 4 deletions rs/moq-net/src/lite/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = 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,
}
}
Expand Down Expand Up @@ -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<crate::transport::boxed::SendStream, Version>,
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<Item = Range<u64>>, drops: Vec<(Range<u64>, 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.
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions rs/moq-net/src/model/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<u64>, accounted: &RangeSet<u64>) -> bool {
pub(super) fn covered(&self, from: u64, cap: Option<u64>, accounted: &impl Accounted) -> bool {
let mut reader = self.reader();
reader.window();
reader.window.covered_by(from, cap, accounted)
Expand Down
1 change: 1 addition & 0 deletions rs/moq-net/src/model/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
71 changes: 62 additions & 9 deletions rs/moq-net/src/model/subscription/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ pub(in crate::model) struct Resolved {
pub end: Option<u64>,
}

/// 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<u64>) -> Option<u64>;
}

impl Accounted for rangemap::RangeSet<u64> {
fn first_gap(&self, range: &ops::Range<u64>) -> Option<u64> {
self.gaps(range).next().map(|gap| gap.start)
}
}

impl<V: Clone + Eq> Accounted for rangemap::RangeMap<u64, V> {
fn first_gap(&self, range: &ops::Range<u64>) -> Option<u64> {
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
Expand Down Expand Up @@ -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<u64>, accounted: &rangemap::RangeSet<u64>) -> 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<u64>, accounted: &impl Accounted) -> bool {
let Some(end) = self.end() else {
return false;
};
Expand All @@ -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
Expand Down Expand Up @@ -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!(
Expand Down
Loading