diff --git a/quest/m1/release-perf/README.md b/quest/m1/release-perf/README.md index 7f1f0991309..22563537e70 100644 --- a/quest/m1/release-perf/README.md +++ b/quest/m1/release-perf/README.md @@ -60,6 +60,13 @@ noise. [#5548](https://github.com/moq-dev/moq/pull/5548) needs. An earliest-deadline atomic cannot prove a replacement driver is registered, and a lock-free notification primitive is not worth ~8 ns. +- `track_gap_flush` is accepted at O(log gaps + retired entries) per shared + successor, after [#6287](https://github.com/moq-dev/moq/pull/6287) indexed + the gap ledger and retired a shared verdict's gaps with one split. Each + retired entry must be freed exactly once, so the batch is already amortized + optimal; a representation that hides that reclamation would widen scope for + no measured benefit. Sparse arrivals cost O(log gaps) amortized, and at 4096 + gaps flush, read, and expiry instructions fell 80 to 95%. ### Release as a baseline @@ -74,9 +81,9 @@ usually pays for a feature. | Bench | release | main | Ratio | Owner | |---|---|---|---|---| -| `track_gap_flush/4096` | 5.7 us | 551 us | 96x | [Sparse gaps](/quest/m1/release-perf/sparse-gaps.md) | -| `track_gap_flush/512` | 1.8 us | 48 us | 27x | same | -| `track_gap_flush/64` | 1.5 us | 6.2 us | 4.1x | same | +| `track_gap_flush/4096` | 5.7 us | 551 us | 96x | accepted (above) | +| `track_gap_flush/512` | 1.8 us | 48 us | 27x | accepted (above) | +| `track_gap_flush/64` | 1.5 us | 6.2 us | 4.1x | accepted (above) | | `stats/session-report/1000x10` | 11.0 us | 86.8 us | 7.9x | accepted (above) | | `track_gc/idle/16384` | 11.6 ns | 73.7 ns | 6.3x | accepted (above) | | `origin/resume_overlap/sequence/1r_64g_32c` | 93 us | 426 us | 4.6x | fixed in [moq-dev/moq#6292](https://github.com/moq-dev/moq/pull/6292) | @@ -110,7 +117,6 @@ outside the worktree to keep the next one. ## Required -- [Sparse gaps](/quest/m1/release-perf/sparse-gaps.md) - an arrival and a gap flush cost O(log gaps), not O(gaps) - [Re-check](/quest/m1/release-perf/recheck.md) - the screen-only moves are re-measured with instruction counts, and the ones that hold are fixed or accepted - [JS frames](/quest/m1/release-perf/js-frames.md) - the js/net frame path is no slower than release in a browser, or the gap is accepted diff --git a/quest/m1/release-perf/sparse-gaps.md b/quest/m1/release-perf/sparse-gaps.md deleted file mode 100644 index 8b7a74e03d1..00000000000 --- a/quest/m1/release-perf/sparse-gaps.md +++ /dev/null @@ -1,37 +0,0 @@ -# [M] Sparse gaps - -## Goal - -On a track with many open gaps, recording an arrival and flushing gaps cost -O(log gaps). `track_sparse_read/timed` and `track_gap_flush` stop growing -faster than their size parameter. - -## Plan - -Found by reading code on 2026-10-21. Confirm each step with callgrind -instruction counts at two sizes before and after the change. - -- `Ledger` (`rs/moq-net/src/model/ledger.rs`) stores its runs in a `Vec`. - [#5802](https://github.com/moq-dev/moq/pull/5802) moved it from the session - tail ([#4533](https://github.com/moq-dev/moq/pull/4533), which only ever - held a few gaps) onto every track's live arrivals. Each arrival now pays - O(gaps) twice. `account` uses `Vec::splice`: closing the lowest gap moves - every run, about 100 KB at 4096 gaps. And while gaps exist, `expire` runs a - `dedup_by` over all runs plus `snapshot_subscription`. Under the 3600 s - delay in `track_sparse_read/timed`, gaps never fold, so a sparse feed is - quadratic overall. Fix: key the runs in a `BTreeMap`, - as `Delivered` already does. Cache the earliest fold instant (the smallest - `since` plus grace), so `expire` returns in O(1) when nothing is due. -- `track_gap_flush` (4.1x at 64 gaps, 96x at 4096): `Cursor::forget_past_budget` - in `track.rs` loops once per gap. Each pass calls `delivered.forget_below` - (a `BTreeMap::split_off` plus an insert) and reads the wall clock again in - `state.aged()`. Fix: read `now` once. Every gap below the shared successor - gets the same verdict, so look up the largest run start below it with - `runs.range(..s).next_back()` and call `forget_below` once. - `Delivered` comes from [#5446](https://github.com/moq-dev/moq/pull/5446) and - the wall-clock check from [#5548](https://github.com/moq-dev/moq/pull/5548). - Both are intended features, so only the cost per gap is wrong. - -Release forgot nothing here because it had no per-reader delivery record. -Don't aim for its absolute numbers. Aim for flat cost per gap across the -`track_gap_flush` and `track_sparse_read/timed` sweeps (16, 256, 4096). diff --git a/rs/moq-net/benches/track.rs b/rs/moq-net/benches/track.rs index 7404db02b30..ea21b5d851f 100644 --- a/rs/moq-net/benches/track.rs +++ b/rs/moq-net/benches/track.rs @@ -27,6 +27,9 @@ //! holding a fixed number of gaps the reader still remembers: each hand-out must cost the //! same however many gaps it holds, so a walk over them shows up as a slope. //! +//! `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. +//! //! `track_gap_flush` forgets every remembered gap in one hand-out, each below an aborted //! group still cached: the successor they share must be found once, not once per gap. //! @@ -1019,6 +1022,64 @@ fn bench_parked_read(c: &mut Criterion) { /// opening a gap at the top, and the lowest missing one, closing the gap at the bottom, so /// the count stays fixed. Swept over the gaps so a per-hand-out cost that grows with them /// shows up as a slope. +struct Sparse { + _broadcast: broadcast::Producer, + track: track::Producer, + sub: track::Subscriber, + next: u64, + missing: u64, + timed: bool, +} + +impl Sparse { + fn new(info: track::Info, gaps: u64) -> Self { + let broadcast = broadcast::Info::default().produce(); + let track = broadcast.create_track("bench", info.clone()).unwrap(); + let sub = track.subscribe(track::Subscription::default().with_max_delay(Duration::from_secs(3600))); + let mut sparse = Self { + _broadcast: broadcast, + track, + sub, + next: gaps * 2, + missing: 1, + timed: info.timescale.is_some(), + }; + for gap in 0..gaps { + sparse.hand_out(gap * 2); + } + // Fill beyond the cache queue's initial power-of-two capacity outside timing. + // Its amortized growth is independent of how many ledger gaps remain open. + if gaps != 0 { + sparse.advance(); + } + sparse + } + + fn hand_out(&mut self, sequence: u64) { + let timestamp = self.timed.then(|| Timestamp::from_micros(sequence).unwrap()); + let mut group = self.track.create_group(moq_net::group::Info { sequence }).unwrap(); + group.write_frame(timestamp, Bytes::from_static(b"x")).unwrap(); + group.finish().unwrap(); + let Poll::Ready(Ok(Some(read))) = self.sub.poll_recv_group(&kio::Waiter::noop()) else { + panic!("the group is cached"); + }; + black_box(read); + } + + // A profiling boundary excludes filling the initial sparse backlog. + #[inline(never)] + fn read_pair(&mut self) { + self.advance(); + } + + fn advance(&mut self) { + self.hand_out(self.next); + self.hand_out(self.missing); + self.next += 2; + self.missing += 2; + } +} + fn bench_sparse_read(c: &mut Criterion) { let mut group = c.benchmark_group("track_sparse_read"); group.throughput(Throughput::Elements(2)); @@ -1026,31 +1087,10 @@ fn bench_sparse_read(c: &mut Criterion) { for gaps in [16u64, 256, 4096] { group.bench_function(BenchmarkId::new(name, gaps), |b| { b.iter_custom(|iterations| { - let broadcast = broadcast::Info::default().produce(); - let track = broadcast.create_track("bench", info.clone()).unwrap(); - let mut sub = - track.subscribe(track::Subscription::default().with_max_delay(Duration::from_secs(3600))); - // Microseconds, so no run lasts long enough to cross the budget. - let timestamp = |sequence: u64| info.timescale.map(|_| Timestamp::from_micros(sequence).unwrap()); - let mut hand_out = |sequence: u64| { - let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); - group - .write_frame(timestamp(sequence), Bytes::from_static(b"x")) - .unwrap(); - group.finish().unwrap(); - let Poll::Ready(Ok(Some(read))) = sub.poll_recv_group(&kio::Waiter::noop()) else { - panic!("the group is cached"); - }; - black_box(read); - }; - // Every odd sequence is missing until the bottom gap closes. - for gap in 0..gaps { - hand_out(gap * 2); - } + let mut sparse = Sparse::new(info.clone(), gaps); let start = WallInstant::now(); - for iteration in 0..iterations { - hand_out((gaps + iteration) * 2); - hand_out(iteration * 2 + 1); + for _ in 0..iterations { + sparse.read_pair(); } start.elapsed() }); @@ -1060,6 +1100,71 @@ fn bench_sparse_read(c: &mut Criterion) { group.finish(); } +/// A sparse feed that opens one gap per millisecond, keeping `gaps` within its budget. +struct Expiring { + sparse: Sparse, + pool: cache::Pool, + now: Instant, +} + +impl Expiring { + fn new(gaps: u64) -> Self { + let mut sparse = Sparse::new(track::Info::default(), 0); + sparse + .sub + .update(track::Subscription::default().with_max_delay(Duration::from_millis(gaps))) + .unwrap(); + let pool = sparse._broadcast.info().pool.clone(); + // Supply time ahead of the host clock, so only these steps age the gaps even + // under an instruction profiler. No cache capacity or idle expiry is enabled. + let mut fixture = Self { + sparse, + pool, + now: Instant::now() + Duration::from_secs(3600), + }; + for gap in 0..gaps { + fixture.pool.gc(fixture.now, &kio::Waiter::noop()); + fixture.sparse.hand_out(gap * 2); + fixture.now += Duration::from_millis(1); + } + fixture.sparse.next = gaps * 2; + // The eviction queue trails arrivals by one entry: warm both capacities. + fixture.step(); + fixture.step(); + fixture + } + + // One new arrival expires one old gap; excludes building the initial ledger. + #[inline(never)] + fn arrive(&mut self) { + self.step(); + } + + fn step(&mut self) { + self.now += Duration::from_millis(1); + self.pool.gc(self.now, &kio::Waiter::noop()); + self.sparse.hand_out(self.sparse.next); + self.sparse.next += 2; + } +} + +fn bench_sparse_expire(c: &mut Criterion) { + let mut group = c.benchmark_group("track_sparse_expire"); + for gaps in [16u64, 256, 4096] { + group.bench_function(BenchmarkId::from_parameter(gaps), |b| { + b.iter_custom(|iterations| { + let mut expiring = Expiring::new(gaps); + let start = WallInstant::now(); + for _ in 0..iterations { + expiring.arrive(); + } + start.elapsed() + }); + }); + } + group.finish(); +} + /// A reader remembering `gaps` gaps, each below a handed-out group since aborted but still /// cached, then a stamped survivor after them all. struct GapFlush { @@ -1100,6 +1205,8 @@ impl GapFlush { } /// Hand out a group two hours on, which puts every remembered gap past the budget. + // A profiling boundary excludes publishing the groups and their aborts. + #[inline(never)] fn flush(&mut self) { let mut group = self .track @@ -1123,7 +1230,7 @@ impl GapFlush { fn bench_gap_flush(c: &mut Criterion) { let mut group = c.benchmark_group("track_gap_flush"); group.sample_size(10); - for gaps in [64u64, 512, 4096] { + for gaps in [16u64, 64, 256, 512, 4096] { group.throughput(Throughput::Elements(gaps)); group.bench_function(BenchmarkId::from_parameter(gaps), |b| { b.iter_batched_ref(|| GapFlush::new(gaps), GapFlush::flush, BatchSize::PerIteration); @@ -1375,6 +1482,7 @@ criterion_group!( bench_datagram_subscriber_join, bench_parked_read, bench_sparse_read, + bench_sparse_expire, bench_gap_flush, bench_wall, bench_empty_run, diff --git a/rs/moq-net/src/ledger.rs b/rs/moq-net/src/ledger.rs index ab6be5a69c8..7c71fde293e 100644 --- a/rs/moq-net/src/ledger.rs +++ b/rs/moq-net/src/ledger.rs @@ -3,7 +3,11 @@ //! A subscription's tail (the session's `tail::Tail`) and a track's record of live arrivals //! both keep one, so they give up on a missing group by the same rule. -use std::{ops::Range, time::Duration}; +use std::{ + collections::{BTreeMap, BTreeSet}, + ops::Range, + time::Duration, +}; use crate::time::Instant; @@ -23,10 +27,10 @@ pub(crate) fn grace(max_delay: Duration) -> Duration { } } -/// A run of accounted sequences. +/// A run of accounted sequences, keyed by its inclusive start. #[derive(Debug, Clone, PartialEq, Eq)] struct Run { - groups: Range, + end: u64, /// When the gap below this run opened, which is when its missing groups became late. since: Instant, } @@ -38,7 +42,10 @@ pub(crate) struct Ledger { // can no longer be waited for, so it folds into the runs around it, which bounds this by // the gaps opened within the grace rather than every gap ever seen. A fold is final: a // later, longer grace or a lowered floor cannot reopen it. - runs: Vec, + runs: BTreeMap, + // Demand can restart a gap below an older one, so sequence order is not age order. + // Index both: one due gap must not make every arrival scan all the younger ones. + ages: BTreeSet<(Instant, u64)>, grace: Duration, } @@ -52,7 +59,8 @@ impl Ledger { /// A ledger that gives up on a missing group after `grace`. pub fn new(grace: Duration) -> Self { Self { - runs: Vec::new(), + runs: BTreeMap::new(), + ages: BTreeSet::new(), grace, } } @@ -62,43 +70,69 @@ impl Ledger { self.grace = grace; } + fn remove(&mut self, start: u64) -> Run { + let run = self.runs.remove(&start).expect("an indexed run"); + self.ages.remove(&(run.since, start)); + run + } + /// Record `groups` as accounted for. Gaps fold only on [`Self::expire`]. pub fn account(&mut self, groups: Range, now: Instant) { if groups.is_empty() { return; } - // Every run the insert overlaps or touches merges with it into one. - let first = self.runs.partition_point(|run| run.groups.end < groups.start); - let last = self.runs.partition_point(|run| run.groups.start <= groups.end); - let merged = match &self.runs[first..last] { - [] => Run { - groups, - // Splitting a gap leaves both halves as late as it was. A run past every - // other one opens a new gap. - since: self.runs.get(first).map_or(now, |above| above.since), - }, - [head, .., tail] => Run { - groups: head.groups.start.min(groups.start)..tail.groups.end.max(groups.end), - since: head.since, - }, - [only] => Run { - groups: only.groups.start.min(groups.start)..only.groups.end.max(groups.end), - since: only.since, - }, + // Keep the lower run in place when extending it: the in-order path changes + // only its end, without reallocating either index or changing its gap's age. + let below = self + .runs + .range(..=groups.start) + .next_back() + .filter(|(_, run)| run.end >= groups.start) + .map(|(&start, run)| (start, run.end)); + if below.is_some_and(|(_, end)| groups.end <= end) { + return; + } + let first = below.map_or(groups.start, |(start, _)| start); + // Splitting a gap inherits the upper run's age; only an arrival above every + // run opens a fresh gap. + let since = self.runs.range(first..).next().map_or(now, |(_, run)| run.since); + let mut end = groups.end.max(below.map_or(groups.end, |(_, end)| end)); + let from = match below { + Some(_) => std::ops::Bound::Excluded(first), + None => std::ops::Bound::Included(first), }; - self.runs.splice(first..last, [merged]); + while let Some((&start, run)) = self.runs.range((from, std::ops::Bound::Included(end))).next() { + end = end.max(run.end); + self.remove(start); + } + match below { + Some(_) => self.runs.get_mut(&first).expect("the lower run stayed indexed").end = end, + None => { + self.ages.insert((since, first)); + self.runs.insert(first, Run { end, since }); + } + } } /// Fold every gap older than the grace into the runs around it. pub fn expire(&mut self, now: Instant) { - let grace = self.grace; - self.runs.dedup_by(|run, below| { - let expired = now.duration_since(run.since) >= grace; - if expired { - below.groups.end = run.groups.end; + let Some((&first, _)) = self.runs.first_key_value() else { + return; + }; + // There is no lower run to fold the leading gap into. Keep its age for a + // later lowered floor, but do not let it hide due gaps elsewhere in the index. + while let Some(&(since, start)) = self.ages.iter().find(|(_, start)| *start != first) { + if now.duration_since(since) < self.grace { + break; } - expired - }); + let run = self.remove(start); + self.runs + .range_mut(..start) + .next_back() + .expect("not the first run") + .1 + .end = run.end; + } } /// Whether a gap separates two runs, so [`Self::expire`] has anything to fold. @@ -108,30 +142,26 @@ impl Ledger { /// The lowest sequence accounted for. pub fn first(&self) -> Option { - self.runs.first().map(|run| run.groups.start) + self.runs.first_key_value().map(|(start, _)| *start) } /// One past the highest sequence accounted for. pub fn end(&self) -> Option { - self.runs.last().map(|run| run.groups.end) + self.runs.last_key_value().map(|(_, run)| run.end) } /// The exclusive end of the accounted run holding `sequence`, `None` while it is not. pub fn run_end(&self, sequence: u64) -> Option { - let run = self - .runs - .get(self.runs.partition_point(|run| run.groups.end <= sequence))?; - (run.groups.start <= sequence).then_some(run.groups.end) + self.runs + .range(..=sequence) + .next_back() + .filter(|(_, run)| sequence < run.end) + .map(|(_, run)| run.end) } /// Whether every group in `groups` is accounted for. pub fn covers(&self, groups: Range) -> bool { - // Runs merge on insert, so one run covers the span or none does. - groups.is_empty() - || self - .runs - .get(self.runs.partition_point(|run| run.groups.end <= groups.start)) - .is_some_and(|run| run.groups.start <= groups.start && groups.end <= run.groups.end) + groups.is_empty() || self.run_end(groups.start).is_some_and(|end| groups.end <= end) } } @@ -149,11 +179,13 @@ impl Ledger { return; } let mut below = 0; - for run in &mut self.runs { - if below < groups.end && groups.start < run.groups.start { + for (&start, run) in &mut self.runs { + if below < groups.end && groups.start < start { + self.ages.remove(&(run.since, start)); run.since = now; + self.ages.insert((now, start)); } - below = run.groups.end; + below = run.end; } } @@ -161,23 +193,25 @@ impl Ledger { /// group opened the gap below it. `None` while it is accounted for, or while no later /// group has arrived, since nothing says it is late yet. pub fn folds_at(&self, group: u64, grace: Duration) -> Option { - let above = self - .runs - .get(self.runs.partition_point(|run| run.groups.end <= group))?; - match above.groups.start <= group { - true => None, - false => above.since.checked_add(grace), + if self.run_end(group).is_some() { + return None; } + self.runs.range(group..).next()?.1.since.checked_add(grace) } /// The gaps within `groups`, each with when a later arrival opened it: `None` for the /// one past the last run, which nothing has opened yet. pub fn gaps(&self, groups: Range) -> impl Iterator, Option)> + '_ { - let first = self.runs.partition_point(|run| run.groups.end <= groups.start); + let first = self + .runs + .range(..=groups.start) + .next_back() + .filter(|(_, run)| run.end > groups.start) + .map_or(groups.start, |(start, _)| *start); let mut below = groups.start; - self.runs[first..] - .iter() - .map(|run| (run.groups.clone(), Some(run.since))) + self.runs + .range(first..) + .map(|(&start, run)| (start..run.end, Some(run.since))) .chain(std::iter::once((u64::MAX..u64::MAX, None))) .map_while(move |(run, since)| { let gap = below..run.start.min(groups.end); @@ -190,9 +224,9 @@ impl Ledger { /// Gaps whose age bounds a held fetch, including the gap before the first arrival. pub fn folds(&self, grace: Duration) -> impl Iterator, Instant)> + '_ { let mut below = 0; - self.runs.iter().filter_map(move |run| { - let gap = below..run.groups.start; - below = run.groups.end; + self.runs.iter().filter_map(move |(&start, run)| { + let gap = below..start; + below = run.end; if gap.is_empty() { return None; } @@ -212,7 +246,11 @@ mod tests { } fn groups(ledger: &Ledger) -> Vec> { - ledger.runs.iter().map(|run| run.groups.clone()).collect() + assert_eq!( + ledger.ages, + ledger.runs.iter().map(|(&start, run)| (run.since, start)).collect() + ); + ledger.runs.iter().map(|(&start, run)| start..run.end).collect() } #[test] @@ -329,4 +367,66 @@ mod tests { ledger.expire(lowered + GRACE); assert!(ledger.covers(1..10)); } + #[test] + fn demand_can_move_an_older_gap_after_a_newer_one() { + let start = Instant::now(); + let mut ledger = Ledger::new(GRACE); + arrive(&mut ledger, 0..1, start); + arrive(&mut ledger, 4..5, start); + arrive(&mut ledger, 8..9, start + GRACE / 4); + ledger.demand(1..4, start + GRACE / 2); + + ledger.expire(start + GRACE + GRACE / 4); + assert_eq!(groups(&ledger), vec![0..1, 4..9]); + assert_eq!(ledger.folds_at(1, GRACE), Some(start + GRACE + GRACE / 2)); + ledger.expire(start + GRACE + GRACE / 2); + assert_eq!(groups(&ledger), vec![0..9]); + } + + #[test] + fn changing_grace_uses_current_gap_ages_without_reopening_folds() { + let start = Instant::now(); + let mut ledger = Ledger::new(GRACE); + arrive(&mut ledger, 0..1, start); + arrive(&mut ledger, 2..3, start); + ledger.set_grace(GRACE * 2); + ledger.expire(start + GRACE); + assert_eq!(groups(&ledger), vec![0..1, 2..3]); + + ledger.set_grace(GRACE / 2); + ledger.expire(start + GRACE); + assert_eq!(groups(&ledger), vec![0..3]); + ledger.set_grace(GRACE * 2); + arrive(&mut ledger, 4..5, start + GRACE); + ledger.expire(start + GRACE * 2); + assert_eq!(groups(&ledger), vec![0..3, 4..5]); + } + + #[test] + fn a_former_first_run_keeps_its_age_when_a_lower_run_arrives() { + let start = Instant::now(); + let mut ledger = Ledger::new(GRACE); + arrive(&mut ledger, 10..11, start); + ledger.expire(start + GRACE * 2); + assert_eq!(groups(&ledger), vec![10..11]); + arrive(&mut ledger, 0..1, start + GRACE * 2); + assert_eq!(groups(&ledger), vec![0..11]); + } + #[test] + fn one_gap_expires_per_arrival_without_retaining_older_gaps() { + let start = Instant::now(); + for retained in [0u64, 1, 4, 16] { + let mut ledger = Ledger::new(Duration::from_millis(retained)); + for arrival in 0..retained * 4 + 8 { + arrive( + &mut ledger, + arrival * 2..arrival * 2 + 1, + start + Duration::from_millis(arrival), + ); + let runs = groups(&ledger); + assert_eq!(runs.len() as u64, (arrival + 1).min(retained + 1)); + assert_eq!(runs[0], 0..arrival.saturating_sub(retained) * 2 + 1); + } + } + } } diff --git a/rs/moq-net/src/model/delivered.rs b/rs/moq-net/src/model/delivered.rs index b21c00ddd34..0a696bccd26 100644 --- a/rs/moq-net/src/model/delivered.rs +++ b/rs/moq-net/src/model/delivered.rs @@ -80,6 +80,11 @@ impl Delivered { } } + /// The last run starting below `sequence`. + pub fn last_before(&self, sequence: u64) -> Option { + self.runs.range(..sequence).next_back().map(|(start, _)| *start) + } + /// How many runs it holds: one per gap, plus the floor. #[cfg(test)] pub fn runs(&self) -> usize { @@ -133,6 +138,11 @@ mod test { delivered.insert(sequence); } assert_eq!(delivered.above_gap(), Some(3)); + assert_eq!(delivered.last_before(0), None); + assert_eq!(delivered.last_before(3), None); + assert_eq!(delivered.last_before(5), Some(3)); + assert_eq!(delivered.last_before(9), Some(5)); + assert_eq!(delivered.last_before(13), Some(12)); delivered.forget_below(0); assert_eq!(delivered.runs(), 4); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index aa2ebf373a3..74b430dd2c5 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6004,31 +6004,28 @@ impl Cursor { let state = self.state.read(); let budget = clamp_max_delay(max_delay, state.max_age_bound()); let edge = state.drift_edge(&self.servable); - // Every group below the last successor found shares it, so a run of cached groups - // that cannot serve is walked once per pass, not once per gap. - let mut arrival: Option<&Slot> = None; - let mut successor: Option<&Slot> = None; + let now = state.cache.pool().now_wall(); while let Some(oldest) = delivered.above_gap() { - if arrival.is_none_or(|slot| slot.group.sequence <= oldest) { - arrival = state.arrival(oldest, &edge.servable); - } // Either clock, as `drifted` judges a group: without a successor, neither. - let Some(first) = arrival else { + let Some(arrival) = state.arrival(oldest, &edge.servable) else { break; }; - if successor.is_none_or(|slot| slot.group.sequence <= oldest) { - successor = state.successor(oldest, &edge.servable); - } - let past = state.aged(first, budget) - || successor.is_some_and(|slot| { - state - .live_above(oldest, &edge) - .is_some_and(|live| live.behind(slot.group.timestamp(), budget)) - }); - if !past { + let shared = if now.saturating_duration_since(arrival.arrived) >= budget { + arrival.group.sequence + } else if let Some(successor) = state.successor(oldest, &edge.servable) + && let Some(live) = state.live_above(oldest, &edge) + && live.behind(successor.group.timestamp(), budget) + { + successor.group.sequence.min(live.sequence) + } else { break; - } - delivered.forget_below(oldest); + }; + // Every delivered run below this successor shares its verdict. Retire + // them in one split, including the highest run, rather than once per gap. + let last = delivered + .last_before(shared) + .expect("the oldest run is below its successor"); + delivered.forget_below(last); } } @@ -16281,6 +16278,39 @@ mod test { ); } + /// A common wall-clock successor retires the gaps below an aborted prefix, but a + /// later arrival keeps the next gap within its budget. + #[test] + fn wall_time_batches_aborted_gaps_without_forgetting_a_fresh_successor() { + let budget = Duration::from_secs(1); + let producer = track_producer("test", None); + let mut subscriber = producer.subscribe(Subscription::default().with_max_delay(budget)); + let mut open = Vec::new(); + for sequence in (0..=10).step_by(2) { + let mut group = producer.create_group(group::Info { sequence }).unwrap(); + group.write_frame(None, &b"x"[..]).unwrap(); + open.push(group); + } + assert_eq!(drain_groups(&mut subscriber), [0, 2, 4, 6, 8, 10]); + for group in open.drain(..5) { + group.abort(Error::Cancel).unwrap(); + } + elapse(&producer, budget / 2); + write_untimed_group(&producer, 12); + assert_eq!(drain_groups(&mut subscriber), [12]); + elapse(&producer, budget / 2); + write_untimed_group(&producer, 14); + assert_eq!(drain_groups(&mut subscriber), [14]); + assert_eq!(subscriber.delivered_runs(), 4, "floor through 8, then 10, 12 and 14"); + assert!(!subscriber.delivered.contains(9), "the next gap is still remembered"); + write_untimed_group(&producer, 11); + assert_eq!( + drain_groups(&mut subscriber), + [11], + "the gap below fresh group 12 remains" + ); + } + /// An untimed sparse feed whose groups all arrive within the budget keeps every gap /// and still hands out every group. The hand-out cost stays flat over the retained /// gaps; `track_sparse_read` measures it.