From 81ac67f8c0102b23cc0c5281184279690fb75d8d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 16:59:34 -0800 Subject: [PATCH 1/6] chore(quest): claim quest/m0/nightly/publisher-drop-cost.md Quest-Claim: 1354a79a9afe9f3d From f419f4d34a9055e54a46ed5999f8b75d1d8460ea Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 21:57:56 -0800 Subject: [PATCH 2/6] perf(moq-net): a dropped group costs a subscription what it changes A drop that starts one time range of a subscription with many ranges rebuilt the window, re-derived what was owed, and rescanned every drop, so each drop cost every range and every drop before it. The publisher bench's wake/8192 fixture ran past Nightly's 600 s budget. - The window keeps waiting time starts by time and tries only those `Holdings::unplaced` leaves out, grows its groups by layering each started span's run (folding the layer at the square root of the fixed runs), and counts named starts instead of walking spans. - A drop cursor whose window and coverage only grew folds in what the window gained and the stale runs a wider coverage settles, instead of rescanning the span. - `publisher_drop` sweeps drop cost over ranges x drops. Co-Authored-By: Claude Opus 5.5 --- quest/m0/nightly/publisher-drop-cost.md | 13 - quest/m2/js-drop-cost.md | 32 ++ quest/m3/layered-runs.md | 27 -- rs/moq-net/benches/publisher.rs | 40 +- rs/moq-net/src/lite/publisher.rs | 55 ++- rs/moq-net/src/model/subscription.rs | 2 +- rs/moq-net/src/model/subscription/runs.rs | 117 ++++- rs/moq-net/src/model/subscription/window.rs | 476 +++++++++++++++----- rs/moq-net/src/model/track.rs | 248 +++++++++- 9 files changed, 819 insertions(+), 191 deletions(-) delete mode 100644 quest/m0/nightly/publisher-drop-cost.md create mode 100644 quest/m2/js-drop-cost.md delete mode 100644 quest/m3/layered-runs.md diff --git a/quest/m0/nightly/publisher-drop-cost.md b/quest/m0/nightly/publisher-drop-cost.md deleted file mode 100644 index 4fdb3da9dee..00000000000 --- a/quest/m0/nightly/publisher-drop-cost.md +++ /dev/null @@ -1,13 +0,0 @@ -# [L] A dropped group costs a subscription O(1), not O(ranges) - -## Goal - -Dropping a group from a subscription with many ranges costs about the same as with one, so moq-net's `publisher` bench `wake/8192` finishes in seconds instead of running past 600 s and timing out the Nightly `Rust bench targets` job (55-minute limit). A new bench sweeps drop cost over ranges × drops, so a regression shows in Nightly's measured coverage. - -## Plan - -Caused by [moq-dev/moq#8638](https://github.com/moq-dev/moq/pull/8638): each dropped group makes the subscription rebuild state over every range, about 2.4 ms per drop at 8193 ranges. Four places rebuild instead of updating incrementally, in `rs/moq-net/src/model/track.rs` and `subscription/{window,runs}.rs`: the window, what is owed, the misses check, and the servable set. Make each incremental, and keep the JS counterpart in step where it has the same shape. - -Decided 2026-11-18 (maintainer, /quest-plan): ✅ no stopgap (rejected: having the bench fixture do all its drops before one step, which takes 6 ms but hides the bug from the only bench that catches it). The Rust bench job stays red until this lands. - -Public API: none. Wire: none. diff --git a/quest/m2/js-drop-cost.md b/quest/m2/js-drop-cost.md new file mode 100644 index 00000000000..f0bdbf2ca2b --- /dev/null +++ b/quest/m2/js-drop-cost.md @@ -0,0 +1,32 @@ +# [M] A JS dropped group costs a subscription what it changes + +## Goal + +In `@moq/net`, a drop that starts one time range of a subscription with many +ranges costs about what it does with one, as it does in Rust since +[moq-dev/moq#9142](https://github.com/moq-dev/moq/pull/9142). A JS bench sweeps +drop cost over ranges and drops, so a regression shows in Nightly's JS +benchmarks. + +## Plan + +`js/net/src/window.ts` still has the shape Rust's `Window` had before the fix: +each settle tries every waiting time start (`#startWaiting`), and any move +rebuilds and re-sorts every span's runs (`#rebuild`), so each drop that +places one range costs every range. Port the Rust fix: + +- Waiting spans keyed by time, tried only outside the runs a new optional + `Holdings.unplaced` reports, with a mark that changes when a lite-07 copy's + reported time starts, `atKeys`, or anchoring change. The track's holdings + in `js/net/src/track.ts` need the dropped prefix in one lookup: `DropSet` + keeps runs with different errors apart, so add a coalesced key set as + Rust's `TrackState::dropped`. +- A span that starts or whose lookup answers inserts its runs into the sorted + runs instead of rebuilding them; only a span that starts looking up its + start rebuilds. +- `start()` from counted starts rather than a walk of the spans. + +Then measure what else in `track.ts` a window move costs per drop. JS has no +`DropCursor`, so check its drop reporting the same way. + +Public API: none. Wire: none. diff --git a/quest/m3/layered-runs.md b/quest/m3/layered-runs.md deleted file mode 100644 index deac17d5dd4..00000000000 --- a/quest/m3/layered-runs.md +++ /dev/null @@ -1,27 +0,0 @@ -# [S] Collapse a window's layered runs to one shared set - -## Goal - -`rs/moq-net`'s subscription window holds its groups as plain shared runs, -without the moving layer that time ends once walked, and every path that -diffed or searched that layer is gone, with no change in what readers get. - -## Plan - -Since [the model](https://github.com/moq-dev/moq/blob/7d1bee79cb8fcb4d816fc53f0547ef8b2b19756a/quest/m2/time-addressing/model.md), a time end settles at -once on keys, so `Window::rebuild` always builds `Layered` with an empty -layer. What still assumes one: - -- `model/subscription/runs.rs`: `Layered`, its `gained` and `minus`, and the - property tests over a moving layer. -- `model/track.rs`: `find_servable` leapfrogging the layer after the fixed - runs, `Admitted::gained`, and `Servable::layered` in its property test. -- `model/track/miss.rs`: the misses view that judges only what a moved layer - covered, and `shared_layer` in its property test. - -Replace `Layered` with `Arc`, comparing windows by pointer where they -compared fixed runs. A window that moves now rebuilds, so the incremental -paths become a full recompute of what is owed; keep the property tests that -check owed groups and servable finds against a scan. - -Public API: none. Wire: none. diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index 920486d547d..63c6d103e2b 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -7,9 +7,16 @@ //! 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. //! +//! `publisher_drop` gives up `drops` ranges of a subscription to `ranges` ranges, plus one +//! left open, timing each drop and every wake it causes. Each drop starts the range it gives +//! up, so a drop costing every range shows as a slope over `ranges`, and one costing every +//! drop before it as a slope over `drops`. +//! //! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. -use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use std::time::Duration; + +use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use moq_net::fuzz::CoverageBench; /// Ranges the source gave up before the wake. @@ -38,5 +45,34 @@ fn bench_covered_before_start(c: &mut Criterion) { group.finish(); } -criterion_group!(benches, bench_covered_before_start); +/// Ranges given up per measurement, and ranges subscribed besides the open one. +const SWEEP: [u64; 4] = [1, 32, 1024, 8192]; + +fn bench_drop(c: &mut Criterion) { + let mut group = c.benchmark_group("publisher_drop"); + // The largest cell gives up 8192 ranges per sample. + group.sample_size(10); + group.measurement_time(Duration::from_secs(2)); + for ranges in SWEEP { + for drops in SWEEP.into_iter().filter(|drops| *drops <= ranges) { + group.throughput(Throughput::Elements(drops)); + group.bench_function( + BenchmarkId::new(format!("ranges={ranges}"), format!("drops={drops}")), + |b| { + b.iter_batched( + || CoverageBench::spread(ranges + 1, drops), + |mut run| { + run.give_up(drops); + run + }, + BatchSize::PerIteration, + ) + }, + ); + } + } + group.finish(); +} + +criterion_group!(benches, bench_covered_before_start, bench_drop); criterion_main!(benches); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index e4973db61ee..4281692fcf7 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -5801,12 +5801,15 @@ 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. +/// `publisher` bench: a wake that finds nothing new ends in the coverage check, and a drop +/// gives up one more of its ranges. #[cfg(any(test, feature = "fuzz"))] pub struct CoverageBench { run: TrackRun, writer: Writer, track: track::Producer, + /// The drops not given up yet, the next one last. + drops: Vec<(Range, StreamError)>, _peer: crate::transport::memory::Session, } @@ -5814,15 +5817,24 @@ pub struct CoverageBench { 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. - /// - /// Each range starts at a time no group held places, so it may reach back to the group - /// before it: giving a range up gives that group up too. pub fn new(ranges: u64, skipped: u64) -> Self { assert!(skipped < ranges, "a range must stay open"); + let mut this = Self::spread(ranges, skipped); + this.give_up(skipped); + this.check(skipped); + this + } + + /// Subscribe to `ranges` one-group ranges, ready to give up the first `drops` of them + /// with [`Self::give_up`]. + /// + /// Each range starts at a time no group held places, so it may reach back to the group + /// before it: giving a range up gives that group up too, which starts the range. + pub fn spread(ranges: u64, drops: u64) -> Self { // Every other group, so no two ranges merge into one run. let wire = (0..ranges).map(|range| 2 * range..2 * range + 1); // Alternating errors keep adjacent drops separate entries in the skipped record. - let drops = (0..skipped) + let drops = (0..drops.min(ranges)) .map(|range| { let err = match range % 2 { 0 => StreamError::NotFound, @@ -5834,6 +5846,15 @@ impl CoverageBench { Self::build(wire, drops) } + /// Give up the next `count` drops, each followed by every wake it causes. + pub fn give_up(&mut self, count: u64) { + for _ in 0..count { + let (groups, err) = self.drops.pop().expect("a drop left to give up"); + self.track.drop_groups(groups, err); + while self.step().is_ready() {} + } + } + /// 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 { @@ -5846,7 +5867,10 @@ impl CoverageBench { (group..group + 1, err) }) .collect(); - Self::build(std::iter::once(0..skipped + 1), drops) + let mut this = Self::build(std::iter::once(0..skipped + 1), drops); + this.give_up(skipped); + this.check(skipped); + this } fn build(wire: impl Iterator>, drops: Vec<(Range, StreamError)>) -> Self { @@ -5887,24 +5911,27 @@ impl CoverageBench { outcomes: Default::default(), object_records: false, }; + let mut drops = drops; + drops.reverse(); let mut this = Self { run: TrackRun::new(ctx, subscriber, bounds, 0, steer), writer: Writer::new(stream, version), track, + drops, _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.resolved().is_none(), "no group resolves a start"); + while this.step().is_ready() {} + this + } + + /// Each of the `skipped` drops is held as its own entry, and no start resolved. + fn check(&self, skipped: u64) { + assert!(self.run.start.resolved().is_none(), "no group resolves a start"); assert_eq!( - this.run.start.skipped().expect("unresolved").iter().count(), + self.run.start.skipped().expect("unresolved").iter().count() as u64, skipped, "each drop is its own entry" ); - this } /// One wake with nothing new to serve. diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 126a4178e30..3e47eb7cd46 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -8,7 +8,7 @@ mod window; pub(super) use runs::{Layered, Runs}; pub(crate) use window::Accounted; pub(crate) use window::Resolved; -pub(super) use window::{Containing, Holdings, Unheld, Window}; +pub(super) use window::{Containing, Holdings, Unheld, Unplaced, Window}; /// Subscriber-side preferences for receiving a track. /// diff --git a/rs/moq-net/src/model/subscription/runs.rs b/rs/moq-net/src/model/subscription/runs.rs index 4d5b0906481..4d132e3078f 100644 --- a/rs/moq-net/src/model/subscription/runs.rs +++ b/rs/moq-net/src/model/subscription/runs.rs @@ -1,28 +1,61 @@ //! Sets of group keys held as sorted runs, and the layered set a [`Window`](super::Window) //! reads: fixed runs shared across its moves, with the few runs its time ends walk on top. //! -//! A subscription may name thousands of ranges, yet an unsettled time end moves its cap on -//! each group. Rebuilding or copying every run on each move would let a peer multiply what -//! one group costs by its range count, so the runs that never move are kept apart, shared -//! by reference, and only the layer above them is rebuilt. +//! A subscription may name thousands of ranges, yet each time start that resolves, or each +//! group a drop gives up under one, widens a single range. Rebuilding or copying every run +//! on each such change would let a peer multiply what one group costs by its range count, +//! so the runs that don't move are kept apart, shared by reference, and only the few runs +//! layered above them are copied. use std::{ops::RangeInclusive, sync::Arc}; -use rangemap::RangeInclusiveSet; - /// Disjoint runs of groups, lowest first and never adjacent, each found from either end by /// one search. /// -/// Not a [`RangeInclusiveSet`]: its overlapping iterator walks back from the last run, so -/// finding the highest run below a point costs every run above it. +/// Not a [`RangeInclusiveSet`](rangemap::RangeInclusiveSet): its overlapping iterator walks +/// back from the last run, so finding the highest run below a point costs every run above it. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(in crate::model) struct Runs(Vec>); impl Runs { /// The runs of `runs`, in any order, overlapping or not. pub fn new(runs: impl IntoIterator>) -> Self { - let set: RangeInclusiveSet = runs.into_iter().filter(|run| !run.is_empty()).collect(); - Self(set.into_iter().collect()) + let mut runs: Vec<_> = runs.into_iter().filter(|run| !run.is_empty()).collect(); + runs.sort_unstable_by_key(|run| *run.start()); + Self(coalesce(runs)) + } + + pub fn len(&self) -> usize { + self.0.len() + } + + /// Add `run`, merging it with every run it overlaps or touches: a search and a copy of + /// the runs above it. + pub fn insert(&mut self, run: RangeInclusive) { + if run.is_empty() { + return; + } + let (lo, hi) = (*run.start(), *run.end()); + let from = self.0.partition_point(|run| run.end().saturating_add(1) < lo); + let to = self.0.partition_point(|run| *run.start() <= hi.saturating_add(1)); + let merged = match self.0.get(from..to) { + Some([first, .., last]) => lo.min(*first.start())..=hi.max(*last.end()), + Some([only]) => lo.min(*only.start())..=hi.max(*only.end()), + _ => run, + }; + self.0.splice(from..to, std::iter::once(merged)); + } + + /// The runs of both, merged in one pass over each. + fn union(&self, other: &Self) -> Self { + let (mut a, mut b) = (self.0.iter().peekable(), other.0.iter().peekable()); + let merged = std::iter::from_fn(|| match (a.peek(), b.peek()) { + (Some(x), Some(y)) if x.start() <= y.start() => a.next(), + (Some(_), Some(_)) => b.next(), + (Some(_), None) => a.next(), + (None, _) => b.next(), + }); + Self(coalesce(merged.cloned().collect())) } pub fn contains(&self, key: u64) -> bool { @@ -181,6 +214,27 @@ impl Layered { self.last(0, u64::MAX).map(|run| *run.end()) } + /// Add `run`. The layer takes it, so what reads both diffs only the layers, until the + /// layer outgrows the square root of the fixed runs and folds into them: each add costs + /// that root, copying the layer and folding included, however many ranges lie under it. + /// A run already held changes nothing, so the result stays equal by reference. + pub fn insert(&mut self, run: RangeInclusive) { + let (lo, hi) = (*run.start(), *run.end()); + if lo > hi + || self + .first(lo, hi) + .is_some_and(|held| *held.start() == lo && *held.end() >= hi) + { + return; + } + let layer = Arc::make_mut(&mut self.layer); + layer.insert(run); + if layer.len().saturating_mul(layer.len()) > self.fixed.len() { + self.fixed = Arc::new(self.fixed.union(layer)); + self.layer = Arc::default(); + } + } + /// What `self` holds and `old` doesn't within `lo..=hi`, lowest first, when both share /// their fixed runs: `None` when they don't. /// @@ -204,6 +258,23 @@ impl Layered { } } +/// `runs`, sorted by start, with each run that overlaps or touches the one before merged +/// into it. +fn coalesce(runs: Vec>) -> Vec> { + let mut merged: Vec> = Vec::with_capacity(runs.len()); + for run in runs { + match merged.last_mut() { + Some(last) if *run.start() <= last.end().saturating_add(1) => { + if run.end() > last.end() { + *last = *last.start()..=*run.end(); + } + } + _ => merged.push(run), + } + } + merged +} + /// The parts of `run` that `runs` doesn't hold, lowest first. fn minus(run: RangeInclusive, runs: &Runs) -> impl Iterator> + '_ { let (lo, hi) = (*run.start(), *run.end()); @@ -234,6 +305,8 @@ fn minus(run: RangeInclusive, runs: &Runs) -> impl Iterator = fixed.iter().cloned().collect(); + for run in random_runs(&mut next, 30, 200) { + let before = layered.clone(); + let held = union.gaps(&run).next().is_none(); + layered.insert(run.clone()); + union.insert(run.clone()); + let context = || format!("round {round}: {fixed:?} after {run:?}"); + assert!(layered.runs(0).eq(union.iter().cloned()), "{}", context()); + assert!(layered.layer().len().pow(2) <= layered.fixed().len(), "{}", context()); + if held { + assert!(Arc::ptr_eq(&before.layer, &layered.layer), "{}", context()); + } + } + } + } + /// A layered set answers every query as the set of its runs merged would. #[test] fn layered_runs_match_their_union() { diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index d168c9815ea..6734c0fe3c9 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -4,7 +4,7 @@ //! what arrives, not what one subscriber asked for. A [`Window`] is the per-subscriber //! side: resolved at the hop serving the subscriber, it filters that subscriber's reads. -use std::{ops, sync::Arc, time::Duration}; +use std::{collections::BTreeMap, ops, sync::Arc, time::Duration}; use super::{Anchor, Layered, Range, Runs}; use crate::{Error, Result}; @@ -64,6 +64,13 @@ pub(in crate::model) trait Holdings { /// Where `time` falls among the groups held. fn containing(&self, time: u64) -> Containing; + /// Runs of times that [`Self::containing`] can't place, so a window skips its time starts + /// in them rather than try each again whenever the groups held change. `None` when it + /// can't tell, so each is tried. + fn unplaced(&self) -> Option { + None + } + /// The lowest group held starting at or after `from`. fn next_start(&self, from: u64) -> Option; @@ -103,6 +110,21 @@ pub(in crate::model) enum Containing { Unknown, } +/// The times a hop's [`Holdings::containing`] can't place, as up to two runs: every time in +/// them is [`Containing::Unknown`], except the few that `mark` places another way. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(in crate::model) struct Unplaced { + pub runs: [ops::RangeInclusive; 2], + /// Changes whenever a time inside the runs may have come to be placed by something other + /// than a group arriving or a drop, so a window that saw another mark tries every start. + pub mark: u64, +} + +impl Unplaced { + /// An empty run, for a hop that places every time on one side. + pub const NONE: ops::RangeInclusive = ops::RangeInclusive::new(1, 0); +} + /// A hop that holds no groups of its own, such as a reader following route copies that /// resolve its ranges for it. pub(in crate::model) struct Unheld; @@ -138,11 +160,15 @@ pub(in crate::model) struct Window { min: Option, spans: Vec, /// The spans still looking up their time start (see [`Self::found`]), which read nothing - /// meanwhile: kept apart so a probe finds them without visiting every span. - looking: Vec, + /// meanwhile, by that time: kept apart so a probe or an answer finds them without + /// visiting every span. + looking: BTreeMap>, /// The spans whose time start waits on a group arriving, which read only the groups from - /// that time meanwhile. - waiting: Vec, + /// that time meanwhile, by that time: a change to the groups held places only those its + /// [`Holdings::unplaced`] leaves out, found without visiting the rest. + waiting: BTreeMap>, + /// The [`Unplaced::mark`] every waiting span was last tried under. + waited: Option, /// The ranges, by index, of a live join whose reach back waits on the groups held to /// measure the live edge, which read nothing meanwhile: a group held is no reason to /// start at it. @@ -159,10 +185,11 @@ pub(in crate::model) struct Window { /// The floor the last [`Self::covered_by`] checked from, and where it stopped finding the /// groups accounted for. covered: Option<(u64, u64)>, - /// The lowest start among the spans that name theirs, and whether every span does: see - /// [`Self::named`] and [`Self::start`]. Kept so a probe reads them without a walk. - named: Option, - all_named: bool, + /// How many spans name each start, and how many name none: see [`Self::named`] and + /// [`Self::start`]. Counted so a probe reads them, and a span starting updates them, + /// without a walk. + named: BTreeMap, + unnamed: usize, } /// One non-empty range, resolved. @@ -205,16 +232,17 @@ impl Window { ranges: ranges.to_vec(), min, spans: Vec::new(), - looking: Vec::new(), - waiting: Vec::new(), + looking: BTreeMap::new(), + waiting: BTreeMap::new(), + waited: None, reaching: Vec::new(), groups: Layered::default(), missable: Layered::default(), pending: false, settled: false, covered: None, - named: None, - all_named: true, + named: BTreeMap::new(), + unnamed: 0, }; window.place(latest, holdings)?; Ok(window) @@ -258,62 +286,83 @@ impl Window { let _ = self.place(latest, holdings); return !self.pending; } - let mut moved = false; + // The spans placed or started here, each reading at least what it did before, unless + // one now looks up its start and reads nothing meanwhile. + let mut moved = Vec::new(); if !self.reaching.is_empty() { let placed = self.spans.len(); for index in std::mem::take(&mut self.reaching) { self.place_range(index, latest, holdings); } - if self.spans.len() > placed { - self.name(); - moved = true; - } + moved.extend(placed..self.spans.len()); } - moved |= !self.waiting.is_empty() && self.start_waiting(holdings); - if moved { - self.rebuild(holdings); + let shrank = !self.waiting.is_empty() && self.start_waiting(holdings, &mut moved); + match (moved.is_empty(), shrank) { + (true, _) => return false, + (false, true) => self.rebuild(holdings), + (false, false) => self.grow(moved, holdings), } - moved + true } /// Start the spans whose time start waits on a group, once one arrived, or once nothing - /// can arrive any more. Returns whether any did. - fn start_waiting(&mut self, holdings: &impl Holdings) -> bool { - let mut started = false; - for position in std::mem::take(&mut self.waiting) { - let Anchor::Time(time) = self.spans[position].anchor else { - unreachable!("only a time start waits on a group"); - }; + /// can arrive any more, adding each one started to `started`. Only the times the + /// holdings may now place are tried, so a change placing one of them costs that one, not + /// every span still waiting. Returns whether one started reads less than before: it looks + /// up its start, reading nothing meanwhile. + fn start_waiting(&mut self, holdings: &impl Holdings, started: &mut Vec) -> bool { + let complete = holdings.complete(); + let unplaced = holdings.unplaced().filter(|_| !complete); + let tried: Vec<(u64, Vec)> = match unplaced { + Some(unplaced) if self.waited == Some(unplaced.mark) => { + let times: Vec = outside(&unplaced.runs) + .flat_map(|gap| self.waiting.range(gap).map(|(time, _)| *time)) + .collect(); + times + .into_iter() + .filter_map(|time| self.waiting.remove_entry(&time)) + .collect() + } + _ => { + self.waited = unplaced.map(|unplaced| unplaced.mark); + std::mem::take(&mut self.waiting).into_iter().collect() + } + }; + let mut shrank = false; + for (time, positions) in tried { let (start, floored) = match holdings.containing(time) { Containing::Group(group) => (group, true), // The group holding it is older than any held, or nothing can arrive. Containing::Before => (0, false), - Containing::Unknown if holdings.complete() => (0, false), + Containing::Unknown if complete => (0, false), Containing::Unknown => { - self.waiting.push(position); + self.waiting.insert(time, positions); continue; } }; - started = true; - self.start_span(position, time, start, floored, holdings); - } - if started { - self.name(); + for position in positions { + shrank |= self.start_span(position, time, start, floored, holdings); + started.push(position); + } } - started + shrank } /// Start the span at `position`, whose time start `time` waited, at `start`: looked up /// where the groups held can't place it, as when first resolved, and otherwise reading - /// its groups. - fn start_span(&mut self, position: usize, time: u64, start: u64, floored: bool, holdings: &impl Holdings) { + /// its groups. Returns whether it looks its start up. + fn start_span(&mut self, position: usize, time: u64, start: u64, floored: bool, holdings: &impl Holdings) -> bool { let asked = matches!(self.ranges[self.spans[position].index].start, Anchor::Time(_)); + self.unname(position); let span = &mut self.spans[position]; (span.start, span.floored, span.waiting) = (start, floored, false); - if let Some(below) = unresolved(time, start, floored, holdings).filter(|_| asked) { + let looking = unresolved(time, start, floored, holdings).filter(|_| asked); + if let Some(below) = looking { span.looking = Some(Looking { time, below }); - self.looking.push(position); + self.looking.entry(time).or_default().push(position); } + self.name(position); + looking.is_some() } /// Whether group `key` is in range. Everything is while a relative anchor waits. @@ -333,9 +382,9 @@ impl Window { /// The lowest group the ranges name, `None` while one reaches back to the oldest group /// or an anchor waits. pub fn start(&self) -> Option { - match self.pending || !self.reaching.is_empty() || !self.all_named { + match self.pending || !self.reaching.is_empty() || self.unnamed > 0 { true => None, - false => self.named, + false => self.lowest_named(), } } @@ -344,7 +393,7 @@ impl Window { pub fn named(&self) -> Option { match self.pending || self.unplaced() { true => None, - false => self.named, + false => self.lowest_named(), } } @@ -478,7 +527,8 @@ impl Window { /// group held, which holds the group containing it if the track's history does. pub fn lookups(&self) -> impl Iterator + '_ { self.looking - .iter() + .values() + .flatten() .filter_map(|span| self.spans[*span].looking.map(|looking| (looking.time, looking.below))) } @@ -487,30 +537,22 @@ impl Window { /// that group is held. `None`, or an answer not held, means the history couldn't tell, so /// they start where the groups held say. Returns whether the groups in range moved. pub fn found(&mut self, time: u64, group: Option, holdings: &impl Holdings) -> bool { - let mut found = Vec::new(); - self.looking.retain(|span| match self.spans[*span].looking { - Some(looking) if looking.time == time => { - found.push(*span); - false - } - _ => true, - }); - if found.is_empty() { + let Some(found) = self.looking.remove(&time) else { return false; - } + }; let held = group.filter(|group| holdings.next_start(*group) == Some(*group)); - for index in found { + let (start, floored) = match (held, holdings.containing(time)) { + (Some(group), _) | (None, Containing::Group(group)) => (group, true), + (None, _) => (0, false), + }; + // A span looking up its start read nothing, so each one found only reads more. + for &index in &found { + self.unname(index); let span = &mut self.spans[index]; - if span.looking.take().is_none() { - continue; - } - (span.start, span.floored) = match (held, holdings.containing(time)) { - (Some(group), _) | (None, Containing::Group(group)) => (group, true), - (None, _) => (0, false), - }; + (span.looking, span.start, span.floored) = (None, start, floored); + self.name(index); } - self.name(); - self.rebuild(holdings); + self.grow(found, holdings); true } @@ -525,13 +567,16 @@ impl Window { self.looking.clear(); self.waiting.clear(); self.reaching.clear(); + self.named.clear(); + self.unnamed = 0; self.pending = false; for index in 0..self.ranges.len() { if !self.ranges[index].is_empty() { self.place_range(index, latest, holdings); } } - self.name(); + // Each span waiting was just tried against these holdings. + self.waited = holdings.unplaced().map(|unplaced| unplaced.mark); self.rebuild(holdings); Ok(()) } @@ -568,8 +613,11 @@ impl Window { // Reads only the groups from its time until the groups held place it; see // [`Self::start_waiting`]. Start::Waiting => { - self.waiting.push(self.spans.len()); - self.spans.push(Span { + let Anchor::Time(time) = anchor else { + unreachable!("only a time start waits on a group"); + }; + self.waiting.entry(time).or_default().push(self.spans.len()); + self.push(Span { index, anchor, start: 0, @@ -589,10 +637,10 @@ impl Window { } _ => None, }; - if looking.is_some() { - self.looking.push(self.spans.len()); + if let Some(looking) = looking { + self.looking.entry(looking.time).or_default().push(self.spans.len()); } - self.spans.push(Span { + self.push(Span { index, anchor, start, @@ -608,63 +656,138 @@ impl Window { !self.waiting.is_empty() || !self.reaching.is_empty() } - /// Keep [`Self::named`] and [`Self::start`] current with where the spans start. - fn name(&mut self) { - self.named = self - .spans - .iter() - .filter(|span| span.floored) - .map(|span| span.start) - .min(); - self.all_named = self.spans.iter().all(|span| span.floored); + /// Add `span`, counted in [`Self::named`] and [`Self::start`]. + fn push(&mut self, span: Span) { + self.spans.push(span); + self.name(self.spans.len() - 1); + } + + /// Count where the span at `position` starts in [`Self::named`] and [`Self::start`]. + fn name(&mut self, position: usize) { + let span = &self.spans[position]; + match span.floored { + true => *self.named.entry(span.start).or_default() += 1, + false => self.unnamed += 1, + } + } + + /// Stop counting where the span at `position` starts, before it moves. + fn unname(&mut self, position: usize) { + let span = &self.spans[position]; + if !span.floored { + self.unnamed -= 1; + return; + } + if let Some(count) = self.named.get_mut(&span.start) { + *count -= 1; + if *count == 0 { + self.named.remove(&span.start); + } + } + } + + /// The lowest start a span names. + fn lowest_named(&self) -> Option { + self.named.first_key_value().map(|(start, _)| *start) + } + + fn update_settled(&mut self) { + // A span still looking up its start reads nothing until it knows which group that is. + self.settled = !self.pending && self.looking.is_empty() && !self.unplaced(); } /// The groups in range: every span that knows where it starts, through its end, and /// every span waiting on a time start from that time. fn rebuild(&mut self, holdings: &impl Holdings) { self.covered = None; - // A span still looking up its start reads nothing until it knows which group that is. - self.settled = !self.pending && self.looking.is_empty() && !self.unplaced(); + self.update_settled(); let (mut groups, mut missable) = (Vec::new(), Vec::new()); - for span in self.spans.iter().filter(|span| span.looking.is_none()) { - let Some(last) = span.end.map_or(Some(u64::MAX), |end| end.checked_sub(1)) else { - continue; - }; - // A span waiting on its time start reads from that time meanwhile, and may ask a miss - // there, as one reaching back past every group held does: whichever group contains - // it starts at or before it, so every group from there on is in range. - if span.waiting { - if let Anchor::Time(time) = span.anchor - && time <= last - { - groups.push(time..=last); - missable.push(time..=last); + for span in &self.spans { + let (reads, asks) = span.reads(holdings); + groups.extend(reads); + missable.extend(asks); + } + self.groups = Layered::new(Arc::new(Runs::new(groups)), Runs::default()); + self.missable = Layered::new(Arc::new(Runs::new(missable)), Runs::default()); + } + + /// Take in the spans at `positions`, each now reading at least what it did before: their + /// runs layer over the groups, so a change to a few spans costs them, not every range, + /// and what reads the groups diffs only the layer. + fn grow(&mut self, positions: impl IntoIterator, holdings: &impl Holdings) { + self.update_settled(); + for position in positions { + let (reads, asks) = self.spans[position].reads(holdings); + if let Some(run) = reads { + // A coverage check resumes where it stopped, so groups added below that are + // checked again. + if let Some((_, through)) = &mut self.covered { + *through = (*through).min(*run.start()); } - continue; + self.groups.insert(run); } - if last < span.start { - continue; + if let Some(run) = asks { + self.missable.insert(run); } - groups.push(span.start..=last); - // A time start before every group held reaches back to the group containing it, - // which is unknown here, so it asks no miss below its time: every key from there - // on is in range, the key at the time itself being the containing group if it - // exists. A relative one counting back past every group held asks none below the - // oldest, since which of them it reaches is unknown. - let reach = match (span.anchor, span.floored) { - (Anchor::Time(time), false) => time, - (Anchor::Relative(_), false) => holdings.next_start(0).unwrap_or(span.start), - _ => span.start, + } + } +} + +impl Span { + /// The groups this span reads, and those of them a miss may ask for. + fn reads(&self, holdings: &impl Holdings) -> (Option>, Option>) { + // A span still looking up its start reads nothing until it knows which group that is. + if self.looking.is_some() { + return (None, None); + } + let Some(last) = self.end.map_or(Some(u64::MAX), |end| end.checked_sub(1)) else { + return (None, None); + }; + // A span waiting on its time start reads from that time meanwhile, and may ask a miss + // there, as one reaching back past every group held does: whichever group contains + // it starts at or before it, so every group from there on is in range. + if self.waiting { + return match self.anchor { + Anchor::Time(time) if time <= last => (Some(time..=last), Some(time..=last)), + _ => (None, None), }; - if reach <= last { - missable.push(reach..=last); - } } - self.groups = Layered::new(Arc::new(Runs::new(groups)), Runs::default()); - self.missable = Layered::new(Arc::new(Runs::new(missable)), Runs::default()); + if last < self.start { + return (None, None); + } + // A time start before every group held reaches back to the group containing it, + // which is unknown here, so it asks no miss below its time: every key from there + // on is in range, the key at the time itself being the containing group if it + // exists. A relative one counting back past every group held asks none below the + // oldest, since which of them it reaches is unknown. + let reach = match (self.anchor, self.floored) { + (Anchor::Time(time), false) => time, + (Anchor::Relative(_), false) => holdings.next_start(0).unwrap_or(self.start), + _ => self.start, + }; + (Some(self.start..=last), (reach <= last).then_some(reach..=last)) } } +/// The times outside `runs`, which are disjoint and lowest first, as runs lowest first. +fn outside(runs: &[ops::RangeInclusive]) -> impl Iterator> + '_ { + let mut from = Some(0); + let mut runs = runs.iter().filter(|run| !run.is_empty()); + std::iter::from_fn(move || { + loop { + let at = from?; + let Some(run) = runs.next() else { + from = None; + return Some(at..=u64::MAX); + }; + from = run.end().checked_add(1).map(|next| next.max(at)); + if at < *run.start() { + return Some(at..=*run.start() - 1); + } + } + }) +} + /// The Min Age a live join reaches back by: `min`, for [`Range::live`], where this hop resolves /// its own ranges. Any other start names where it starts, the oldest one included, which asks for /// everything the budget keeps. A zero minimum starts at the latest group anyway. @@ -756,6 +879,8 @@ mod tests { use std::collections::BTreeSet; + use rangemap::RangeInclusiveSet; + /// The group starts a hop holds, the key runs its producer skipped, and whether a /// handler serves its history. #[derive(Default)] @@ -764,6 +889,9 @@ mod tests { skipped: Vec>, complete: bool, looks_up: bool, + /// Every key below this was dropped, so a time below it is placed before every + /// group even while none is held. + dropped: u64, } impl Keys { @@ -783,11 +911,22 @@ mod tests { fn containing(&self, time: u64) -> Containing { match self.held.range(..=time).next_back() { Some(key) => Containing::Group(*key), - None if self.held.is_empty() => Containing::Unknown, + None if self.held.is_empty() && time >= self.dropped => Containing::Unknown, None => Containing::Before, } } + fn unplaced(&self) -> Option { + let unknown = match self.held.is_empty() { + true => self.dropped..=u64::MAX, + false => Unplaced::NONE, + }; + Some(Unplaced { + runs: [unknown, Unplaced::NONE], + mark: 0, + }) + } + fn next_start(&self, from: u64) -> Option { self.held.range(from..).next().copied() } @@ -812,6 +951,115 @@ mod tests { } } + /// [`Keys`] keeping its [`Holdings::unplaced`] to itself, so a window tries every start + /// still waiting on each change. + struct Blind<'a>(&'a Keys); + + impl Holdings for Blind<'_> { + fn looks_up(&self) -> bool { + self.0.looks_up() + } + + fn containing(&self, time: u64) -> Containing { + self.0.containing(time) + } + + fn next_start(&self, from: u64) -> Option { + self.0.next_start(from) + } + + fn back(&self, latest: u64, back: u64) -> Option { + self.0.back(latest, back) + } + + fn complete(&self) -> bool { + self.0.complete() + } + + fn never_produced(&self, keys: ops::Range) -> bool { + self.0.never_produced(keys) + } + } + + /// What `window` reads and may ask a miss for, and the lowest start its spans name, + /// worked out from its spans afresh. + fn rebuilt( + window: &Window, + holdings: &impl Holdings, + ) -> (RangeInclusiveSet, RangeInclusiveSet, Option) { + let (mut groups, mut missable) = (RangeInclusiveSet::new(), RangeInclusiveSet::new()); + for span in &window.spans { + let (reads, asks) = span.reads(holdings); + groups.extend(reads); + missable.extend(asks); + } + let named = window + .spans + .iter() + .filter(|span| span.floored) + .map(|span| span.start) + .min(); + (groups, missable, named) + } + + /// Everything a reader of `window` sees. + fn seen(window: &Window) -> impl PartialEq + std::fmt::Debug { + let lookups: BTreeSet<_> = window.lookups().collect(); + ( + window.runs(0).collect::>(), + window.missable(0).collect::>(), + (window.floor(), window.start(), window.named(), window.end()), + (window.resolved(), lookups, window.is_settled(), window.is_waiting()), + ) + } + + proptest::proptest! { + /// A window that skips the starts its holdings can't place and grows its groups as + /// spans start sees what one trying every start sees, and reads what rebuilding from + /// its spans would, through groups arriving, drops, lookups answering, and the track + /// completing. + #[test] + fn settling_by_steps_matches_a_full_settle( + ranges in proptest::collection::vec((0u64..40, proptest::option::of(0u64..48)), 1..12), + ops in proptest::collection::vec((0u8..4, 0u64..48), 0..24), + looks_up: bool, + ) { + let ranges: Vec = ranges + .into_iter() + .map(|(start, end)| range(time(start), end.map(time))) + .collect(); + let mut keys = Keys { + looks_up, + ..Keys::default() + }; + let mut fast = Window::new(&ranges, None, None, &keys).unwrap(); + let mut full = Window::new(&ranges, None, None, &Blind(&keys)).unwrap(); + for (op, value) in ops { + match op { + 0 => drop(keys.held.insert(value)), + 1 => keys.dropped = keys.dropped.max(value), + 2 => { + let Some((time, _)) = fast.lookups().next() else { + continue; + }; + let answer = (value % 2 == 0).then_some(value); + fast.found(time, answer, &keys); + full.found(time, answer, &Blind(&keys)); + } + _ => keys.complete = true, + } + let latest = keys.held.last().copied(); + fast.settle(latest, &keys); + full.settle(latest, &Blind(&keys)); + proptest::prop_assert_eq!(seen(&fast), seen(&full)); + let (groups, missable, named) = rebuilt(&fast, &keys); + proptest::prop_assert!(fast.runs(0).eq(groups.iter().cloned())); + proptest::prop_assert!(fast.missable(0).eq(missable.iter().cloned())); + proptest::prop_assert_eq!(fast.lowest_named(), named); + } + } + } + fn range(start: Anchor, end: impl Into>) -> Range { Range { start, end: end.into() } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index d30b32c485c..a33bcd1db2b 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -23,7 +23,7 @@ use super::record::{Pending, Record, Report}; use crate::ledger::{self, Ledger}; pub(crate) use super::subscription::Resolved; -use super::subscription::{Accounted, Containing, Holdings, Layered, Runs, Window}; +use super::subscription::{Accounted, Containing, Holdings, Layered, Runs, Unplaced, Window}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; @@ -745,6 +745,9 @@ pub(crate) struct TrackState { // a time it holds no group at, but its publisher reports where it resolved one. Replaced // whole as the ranges change, so it holds at most one entry per live range. time_starts: BTreeMap, + // Counts changes to how a time start is placed other than by groups arriving or drops: + // `time_starts`, `anchors`, and `starts_at_keys`. See [`Unplaced::mark`]. + placing: u64, // Keys the upstream publisher gave up, with why: an incoming SUBSCRIBE_SKIP or // SUBSCRIBE_DROP, or a producer cut. Kept apart from the cache so reclaiming a slot // cannot erase a disposition a relay must forward, and as ranges so a wide one costs @@ -1113,6 +1116,45 @@ impl Holdings for TrackState { } } + /// Exactly the times [`Self::containing`] can't place, but those `time_starts` places. + fn unplaced(&self) -> Option { + let none = Unplaced::NONE; + // The times at or after `from` whose keys from `from` through them drops do not all cover. + let uncovered = |from: u64| match self.dropped.get(&from) { + Some(run) if run.end == u64::MAX => none.clone(), + Some(run) => run.end..=u64::MAX, + None => from..=u64::MAX, + }; + let runs = if self.starts_at_keys { + [none.clone(), none] + } else { + match (self.lookup.first_key_value(), self.lookup.last_key_value()) { + // Before any group, only a time whose keys down to 0 were all dropped is placed. + (None, _) | (_, None) => [uncovered(0), none], + (Some(_), Some(_)) if self.anchors != Anchors::Wire => [none.clone(), none], + // A copy places a time below its groups only once the keys down to 0 were + // dropped, and one above them only once the keys down to the newest were. + (Some((&first, _)), Some((&last, _))) => { + let below = uncovered(0); + let below = *below.start()..=(*below.end()).min(first.saturating_sub(1)); + let below = match first { + 0 => none.clone(), + _ => below, + }; + let above = match last.checked_add(1) { + Some(next) => uncovered(next), + None => none, + }; + [below, above] + } + } + }; + Some(Unplaced { + runs, + mark: self.placing, + }) + } + fn next_start(&self, from: u64) -> Option { self.lookup.range(from..).next().map(|(&key, _)| key) } @@ -4734,6 +4776,7 @@ impl Producer { && state.anchors == Anchors::Local { state.anchors = Anchors::Refused; + state.placing += 1; } } @@ -4743,6 +4786,7 @@ impl Producer { pub(crate) fn anchored(&self) { if let Ok(mut state) = self.modify() { state.anchors = Anchors::Wire; + state.placing += 1; } } @@ -4765,6 +4809,7 @@ impl Producer { && state.time_starts != starts { state.time_starts = starts; + state.placing += 1; } } @@ -4774,6 +4819,7 @@ impl Producer { pub(crate) fn starts_at_keys(&self) { if let Ok(mut state) = self.modify() { state.starts_at_keys = true; + state.placing += 1; } } @@ -7614,6 +7660,33 @@ fn settles(budget: Duration, coverage: &Coverage, start: u64, hole: &Hole) -> bo budget <= stale && reads(above..u64::MAX) } +/// The starts at which a received stale run settles a reader whose coverage grew from `old` +/// to `new` under the same budget and did not before, or `None` inside for none: `None` when +/// the coverage changed other than by growing. See [`settles`]: only an uncapped reader is +/// settled, by a run whose next key the open run of its groups holds, and that run's start +/// only falls as the groups grow. +fn settling(old: &Coverage, new: &Coverage) -> Option>> { + let ((old, old_cap), (new, cap)) = match (old, new) { + (None, None) => return Some(None), + (Some(old), Some(new)) => (old, new), + _ => return None, + }; + if old_cap != cap || !new.shares_fixed(old) { + return None; + } + if cap.is_some() { + return Some(None); + } + // The lowest start settled: the one just below the open run, or only the last key. + let settled = |groups: &Layered| { + groups + .last(u64::MAX, u64::MAX) + .map_or(u64::MAX, |run| run.start().saturating_sub(1)) + }; + let run = settled(new)..settled(old); + Some((!run.is_empty()).then_some(run)) +} + impl Drift { /// Whether `hole`, over a run from `start`, settles this reader; see [`settles`]. fn settles(&self, start: u64, hole: &Hole) -> bool { @@ -8390,7 +8463,7 @@ struct Within<'a> { /// The span, window revision, and the budget and coverage that settle received stale runs /// (see [`Drift::settles`]) a [`DropCursor`] last scanned the whole span under; between them -/// it folds in only the runs that changed. +/// it folds in only the runs that changed, and what the window gained. type Scan = (ops::Range, u64, Option<(Duration, Coverage)>); /// What a wire publisher's cursor gave up, found as it changes rather than by rescanning @@ -8405,6 +8478,10 @@ struct DropCursor { /// The [`TrackState::drops_changes`] revision [`Record::pending`] folded in. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] seen: u64, + /// The window's groups as of [`Self::scanned`]'s revision, so a window that only grew + /// since folds in what it gained rather than rescanning the span. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + groups: Option, /// The budget and coverage of the last drift judged, which settle received stale runs /// until the next: a queue-only poll judges none, and must not unsettle them. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -8419,6 +8496,7 @@ impl DropCursor { cuts: state.cache.watch_cuts(), scanned: None, seen: 0, + groups: None, drift: None, } } @@ -8447,6 +8525,44 @@ impl DropCursor { } } + /// What a scan of `span` would newly find since the last, when the span is the same and + /// the window and the reader's coverage only grew: the runs the window gained, and each + /// received stale run a wider coverage now settles. `None` when anything else moved. + fn grown( + &self, + state: &TrackState, + window: &Window, + old_drift: Option<&(Duration, Coverage)>, + span: &ops::Range, + ) -> Option>> { + let last = span.end.checked_sub(1).filter(|last| *last >= span.start); + let (groups, old) = (window.groups()?, self.groups.as_ref()?); + let mut grown: Vec<_> = match last { + Some(last) => groups.gained(old, span.start, last)?, + None if groups.shares_fixed(old) => Vec::new(), + None => return None, + } + .into_iter() + .map(|run| *run.start()..run.end().saturating_add(1)) + .collect(); + let settling = match (old_drift, &self.drift) { + (None, None) => None, + (Some((old_budget, old)), Some((budget, new))) if old_budget == budget => settling(old, new)?, + _ => return None, + }; + // A scan judges a drop at its start within the span, so each one starting where a + // stale run newly settles is folded in whole. + if let Some(settling) = settling { + for (run, _) in state.drops.overlapping(&settling) { + let start = run.start.max(span.start); + if settling.contains(&start) { + grown.push(start..run.end); + } + } + } + Some(grown) + } + /// The lowest unreported run of `within` given up, now marked reported, with why. fn next( &mut self, @@ -8462,18 +8578,35 @@ impl DropCursor { Self::cut(record, key, slot); } } - // Rescan the span only after the window moves or what settles a stale run does, since - // out-of-window and unsettling runs were skipped and may be owed now; otherwise fold in - // only the runs that changed, so a drop costs each cursor the runs it touches. + // Rescan the span only after the window moves other than by growing, or what settles a + // stale run does, since out-of-window and unsettling runs were skipped and may be owed + // now; otherwise fold in only the runs that changed and what the window gained, so a + // drop costs each cursor the runs it touches, even one that starts a range. if let Some(drift) = within.drift { self.drift = Some((drift.budget, drift.coverage.clone())); } let scan = (span.clone(), within.revision, self.drift.clone()); - let full = std::mem::take(&mut record.rescan) || self.scanned.as_ref() != Some(&scan); - let changed: Vec> = match full { - false => state.drops_changes.since(self.seen).collect(), - true => { - self.scanned = Some(scan); + let rescan = std::mem::take(&mut record.rescan); + let grown = match &self.scanned { + _ if rescan => None, + Some(scanned) if *scanned == scan => Some(Vec::new()), + Some((old_span, _, old_drift)) if old_span == span => self.grown(state, window, old_drift.as_ref(), span), + _ => None, + }; + let full = grown.is_none(); + self.scanned = Some(scan); + self.groups = window.groups().cloned(); + let changed: Vec> = match grown { + Some(grown) => { + // What the window newly reads may hold cuts the scan skipped. + for run in &grown { + for (&key, slot) in state.lookup.range(run.clone()) { + Self::cut(record, key, slot); + } + } + state.drops_changes.since(self.seen).chain(grown).collect() + } + None => { let received: Vec<_> = record .pending .iter() @@ -12079,6 +12212,42 @@ mod test { } proptest::proptest! { + /// The times a track reports unplaced are exactly those its `containing` can't place, + /// but for the ones a copy's reported time starts place, on its own groups or on a + /// lite-07 copy's, through groups arriving and every way keys are given up. + #[test] + fn unplaced_matches_containing( + ops in proptest::collection::vec((0u8..4, 0u64..40, 1u64..8), 0..24), + wire: bool, + starts in proptest::collection::vec((0u64..48, 0u64..48), 0..3), + ) { + let producer = track_producer("test", Info::default()); + if wire { + producer.anchored(); + producer.time_starts(starts.clone()); + } + for (op, start, len) in ops { + let groups = start..start + len; + match op { + 0 => producer.drop_groups(groups, StreamError::Cancel), + 1 => producer.skip_groups(groups), + 2 => drop(producer.create_group(group::Info { start })), + _ => producer.modify().unwrap().forget_drops(groups), + } + } + let state = producer.state.read(); + let unplaced = state.unplaced().expect("a track always tells"); + let reported: BTreeSet = starts.iter().map(|(time, _)| *time).collect(); + for time in (0..64).chain([u64::MAX - 1, u64::MAX]) { + let inside = unplaced.runs.iter().any(|run| run.contains(&time)); + let unknown = state.containing(time) == Containing::Unknown; + match wire && reported.contains(&time) { + true => proptest::prop_assert!(!unknown, "a reported start places {}", time), + false => proptest::prop_assert_eq!(inside, unknown, "at {}", time), + } + } + } + /// The coalesced drop set answers coverage as a walk of the drop runs would, through /// every way a run is given up, split, replaced, or forgotten. #[test] @@ -12731,6 +12900,29 @@ mod test { assert_eq!(read_groups(|waiter| groups_only(ordered.poll_next_group(waiter))), want); } + /// A drop outside a reader's ranges is reported once a group arriving places a time start + /// below it, widening its range over the drop, as a scan of the whole window would. + #[test] + fn a_range_widened_by_a_group_reports_an_earlier_drop() { + let producer = track_producer("test", Info::default()); + // The range at 0 keeps the floor where it is, so only the window moves. + let at = |time| Range { + start: Anchor::Time(time), + end: Some(Anchor::Time(time)), + }; + let mut reader = producer.subscribe(replay().with_ranges([at(0), at(8)])); + let waiter = kio::Waiter::noop(); + assert!(reader.poll_drop(&waiter).is_pending()); + producer.drop_groups(5..6, StreamError::Cancel); + assert!(reader.poll_drop(&waiter).is_pending(), "outside the range"); + let _group = producer.create_group(group::Info { start: 3 }).unwrap(); + assert_eq!( + reader.poll_drop(&waiter), + Poll::Ready((5..6, Hole::drop(StreamError::Cancel))), + "the group containing time 8 starts at 3" + ); + } + /// A group missing its head is never served short: reading it is [`Error::Lagged`], /// which a route copy recovers. #[test] @@ -27219,6 +27411,42 @@ mod test { ); } + /// A received stale run in a reader's range settles it once a group arriving places its + /// open range's time start low enough to read every group above the run, so it is + /// reported then, as a scan of the whole window would, though the window grew only above + /// the run. + #[test] + #[cfg(feature = "async")] + fn a_stale_run_settles_once_the_open_range_reaches_it() { + let producer = track_producer("test", Info::timed()); + // An unsettled run is asked of a handler rather than given up as unfillable. + let _dynamic = producer.requests(); + let budget = Duration::from_millis(500); + // Time 0 names group 0, so the first range reads through 5 at once; the open one waits + // on a group to place its time start. + let first = Range { + start: Anchor::Time(0), + end: Some(Anchor::Time(5)), + }; + let open = Range { + start: Anchor::Time(8), + end: None, + }; + let mut reader = producer.subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(budget)) + .with_ranges([first, open]), + ); + producer.give_up(5..6, Hole::stale(budget)); + let waiter = kio::Waiter::noop(); + assert!( + reader.poll_drop(&waiter).is_pending(), + "the groups above it are not all read" + ); + let _group = producer.create_group(group::Info { start: 6 }).unwrap(); + assert_eq!(reader.poll_drop(&waiter), Poll::Ready((5..6, Hole::stale(budget)))); + } + /// A queue-only poll, which judges no drift, still reports the received stale runs the /// last probing poll found settled, rather than forgetting them until the next probe. #[test] From 78df8370b2e5ece59bbb35cdea7c79ffd2169168 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 23:48:34 -0800 Subject: [PATCH 3/6] perf(net): a dropped group costs a JS reader what it changes Port the window fix to `@moq/net`: waiting and looking spans are kept by time and tried only outside the runs a new `Holdings.unplaced` reports, started spans insert their runs instead of rebuilding them all, and starts are counted. `DropSet` keeps its keys coalesced too, so the track's holdings answer whether drops cover a span in one lookup. A new `window-drop` bench sweeps ranges by drops. In Rust and JS alike, a span counting back past every group held now reads the oldest group held again when it moves, as the full rebuild did. Co-Authored-By: Claude Opus 5.5 --- js/justfile | 2 +- js/net/bench/window-drop.ts | 54 +++ js/net/src/drops.test.ts | 28 ++ js/net/src/drops.ts | 16 + js/net/src/internal.ts | 7 +- js/net/src/track.test.ts | 33 ++ js/net/src/track.ts | 69 +++- js/net/src/window.test.ts | 78 +++- js/net/src/window.ts | 417 ++++++++++++++++---- quest/m2/js-drop-cost.md | 32 -- rs/moq-net/src/model/subscription/window.rs | 62 ++- sh/gh/nightly.sh | 2 +- 12 files changed, 661 insertions(+), 139 deletions(-) create mode 100644 js/net/bench/window-drop.ts delete mode 100644 quest/m2/js-drop-cost.md diff --git a/js/justfile b/js/justfile index 5950721c873..8226f330646 100644 --- a/js/justfile +++ b/js/justfile @@ -71,7 +71,7 @@ test-changed list: coverage package="": python3 ../sh/js/coverage.py '{{ package }}' -# Run one net benchmark: broadcasts, forward, requests, fills, fill-probe, reader, frames, track, resume, observed, drift, ledger, bandwidth, replay, varint, lite-varint, priority, subscribe-drop, refresh, compression, or datagrams. +# Run one net benchmark: broadcasts, forward, requests, fills, fill-probe, reader, frames, track, resume, observed, drift, ledger, bandwidth, replay, varint, lite-varint, priority, subscribe-drop, window-drop, refresh, compression, or datagrams. bench name: uv run --no-project python ../sh/js/install.py frozen bun net/bench/{{ name }}.ts diff --git a/js/net/bench/window-drop.ts b/js/net/bench/window-drop.ts new file mode 100644 index 00000000000..138e672a035 --- /dev/null +++ b/js/net/bench/window-drop.ts @@ -0,0 +1,54 @@ +/** + * Sweep what a drop costs a reader with many ranges: one-group time ranges a group apart, each + * waiting for the groups held to place it, given up lowest first so each drop places one more. + * Mirrors the Rust `publisher_drop` bench. A drop must cost about the same however many ranges + * the reader has, or the window has gone back to trying every range or rebuilding them all. + */ + +import { StreamCode, StreamError } from "../src/error.ts"; +import { hooks } from "../src/internal.ts"; +import { Timescale } from "../src/time.ts"; +import { Producer, type Range } from "../src/track.ts"; + +// `WINDOW_DROP_MAX` caps both axes, for a quick run. +const sweep = [1, 32, 1024, 8192].filter( + (count) => count <= Number(process.env.WINDOW_DROP_MAX ?? Number.POSITIVE_INFINITY), +); +// Each cell times at least this many drops, so a cell of one drop isn't one noisy sample. +const timed = 256; + +function measure(ranges: number, drops: number): number { + const producer = new Producer("bench").accept({ timescale: Timescale.MILLI, maxAge: null }); + // Every other group, so no two ranges merge into one run, and one open range past them all so + // the reader never settles. + const spans: Range[] = Array.from({ length: ranges }, (_, range) => { + const at = BigInt(2 * range); + return { start: { time: at }, end: { time: at } }; + }); + spans.push({ start: { time: BigInt(2 * ranges) } }); + const subscriber = producer.subscribe({ ranges: spans }); + subscriber.tryRecvGroup(); + // Alternating errors keep adjacent drops separate runs. + const errors = [new StreamError(StreamCode.NotFound), new StreamError(StreamCode.Cancel)]; + const start = performance.now(); + for (let range = 0; range < drops; range++) { + const from = range === 0 ? 0n : BigInt(2 * range - 1); + hooks.dropTrackGroups(producer, from, BigInt(2 * range + 1), { error: errors[range % 2] }); + subscriber.tryRecvGroup(); + } + const elapsed = performance.now() - start; + producer.close(); + return (elapsed * 1000) / drops; +} + +// Untimed, so the first cell doesn't measure the JIT warming up. +measure(32, 32); +console.log("ranges,drops,drop_us"); +for (const ranges of sweep) { + for (const drops of sweep) { + if (drops > ranges) continue; + const reps = Math.max(3, Math.ceil(timed / drops)); + const times = Array.from({ length: reps }, () => measure(ranges, drops)).sort((a, b) => a - b); + console.log(`${ranges},${drops},${times[Math.floor(reps / 2)].toFixed(2)}`); + } +} diff --git a/js/net/src/drops.test.ts b/js/net/src/drops.test.ts index ba67b88977d..a9610e60828 100644 --- a/js/net/src/drops.test.ts +++ b/js/net/src/drops.test.ts @@ -54,3 +54,31 @@ test("drops: what a change replaced or forgot is reported since its revision, un expect(drops.removedSince(recent)).toBeUndefined(); expect(drops.removedSince(drops.revision)).toEqual([]); }); + +test("drops: the keys given up from a key on stop where a walk of the runs stops", () => { + const drops = new DropSet(); + const errors = [new NotFound("a"), new NotFound("b")]; + let seed = 7; + const random = (below: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + // The high bits: an LCG's low bits cycle. + return (seed >>> 16) % below; + }; + for (let step = 0; step < 400; step++) { + const start = BigInt(random(64)); + const end = start + BigInt(1 + random(6)); + if (random(5) === 0) drops.clear([{ start, end }]); + else if (random(4) === 0) drops.set(start, end); + else drops.set(start, end, { error: errors[random(2)], skip: random(3) === 0 || undefined }); + for (let from = 0n; from < 72n; from++) { + let at = from; + for (const run of drops.overlapping(from, 1000n)) { + if (run.start > at) break; + at = run.end; + } + expect(drops.coveredFrom(from)).toBe(at); + expect(drops.covers(from, at)).toBe(true); + expect(drops.covers(from, at + 1n)).toBe(false); + } + } +}); diff --git a/js/net/src/drops.ts b/js/net/src/drops.ts index bcc6262dcc0..54be8c93950 100644 --- a/js/net/src/drops.ts +++ b/js/net/src/drops.ts @@ -131,6 +131,9 @@ const LOG_SLACK = 64; */ export class DropSet { readonly #runs = new Runs(sameHole); + // The keys in `#runs`, whatever the reason, as coalesced runs, so whether drops cover a span is + // one lookup rather than a walk of every run in it. Mirrors the Rust `TrackState::dropped`. + readonly #keys = new Runs(() => true); // Each run's last change, oldest first. An entry whose run was since replaced is dead, and // the log is rebuilt once dead entries clearly outnumber live ones. #log: { revision: number; run: DropRun }[] = []; @@ -169,6 +172,16 @@ export class DropSet { return this.#runs.overlapping(start, end); } + /** Where the keys given up from `from` on, whatever the reason, stop: `from` when it isn't given up. */ + coveredFrom(from: bigint): bigint { + return this.#keys.at(from)?.end ?? from; + } + + /** Whether every key in `[start, end)` is given up, whatever the reason. An empty run is. */ + covers(start: bigint, end: bigint): boolean { + return start >= end || this.coveredFrom(start) >= end; + } + /** Give up `[start, end)` as `run` says, or forget it when `run` is undefined. */ set(start: bigint, end: bigint, run?: Omit): void { this.#apply(this.#runs.set(start, end, run && { ...run, start, end })); @@ -181,6 +194,9 @@ export class DropSet { #apply({ removed, added }: { removed: DropRun[]; added: DropRun[] }): void { if (removed.length === 0 && added.length === 0) return; + // What was replaced leaves the keys, and its replacement puts back what it still holds. + for (const old of removed) this.#keys.set(old.start, old.end); + for (const fresh of added) this.#keys.set(fresh.start, fresh.end, { start: fresh.start, end: fresh.end }); for (const old of removed) this.#live.delete(old); for (const fresh of added) { this.#live.add(fresh); diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 193870c6ce6..31d47f6e666 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -33,7 +33,7 @@ import type { } from "./track.ts"; import { max, min } from "./util/key.ts"; import * as Varint from "./varint.ts"; -import type { Resolved, Window } from "./window.ts"; +import type { Holdings, Resolved, Window } from "./window.ts"; import type { Advertised, Advertisements } from "./wire.ts"; /** A null {@link Delay.max} in milliseconds, which the model holds as the largest value every peer can read. */ @@ -701,6 +701,8 @@ export const hooks: { subscriberEnd: (subscriber: Subscriber) => Position | undefined; /** The error that end carries, if the track does not end cleanly. Assigned by `track.ts`. */ subscriberEndError: (subscriber: Subscriber) => Error | undefined; + /** What this subscriber resolves its ranges against, for a test. Assigned by `track.ts`. */ + subscriberHoldings: (subscriber: Subscriber) => Holdings; /** * How many keys just below `key` the publisher declared it never produced: the skip run * ending at `key`, which a moq-transport Prior Group ID Gap names. Mirrors the Rust @@ -1127,6 +1129,9 @@ export const hooks: { subscriberEndError: () => { throw new Error("track.ts not loaded"); }, + subscriberHoldings: () => { + throw new Error("track.ts not loaded"); + }, skippedBelow: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 4f6773e0aa1..bc70a80ed3a 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5624,3 +5624,36 @@ test("finishing returns handles that write nothing new but fill a late group", a expect(hooks.tryRecvGroup(subscriber, false)).toEqual({ kind: "done" }); finished.close(); }); + +test("track: a reader's unplaced times are the times its holdings can't place", () => { + let seed = 5; + const random = (below: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + // The high bits: an LCG's low bits cycle. + return (seed >>> 16) % below; + }; + const errors = [new StreamError(StreamCode.NotFound), new StreamError(StreamCode.Cancel)]; + for (let round = 0; round < 400; round++) { + const received = random(2) === 0; + const producer = received ? receivedTrack() : new TrackProducer("test").accept({ maxAge: null }); + const reader = producer.subscribe(); + for (let step = random(16); step >= 0; step--) { + const holdings = hooks.subscriberHoldings(reader); + const unplaced = holdings.unplaced?.(); + expect(unplaced).toBeDefined(); + for (let at = 0n; at < 64n; at++) { + const open = unplaced?.runs.some((run) => run.start <= at && at < run.end) ?? false; + expect([received, at, holdings.containing(at) === "unknown"]).toEqual([received, at, open]); + } + const key = BigInt(random(48)); + if (random(2) === 0) { + try { + putGroup(producer, key); + } catch { + // A key this copy refuses, such as one a skip covers. + } + } else hooks.dropTrackGroups(producer, key, key + BigInt(1 + random(4)), { error: errors[random(2)] }); + } + producer.close(); + } +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 14b7d6c202c..c26cf7921ff 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1054,6 +1054,8 @@ let timeAnchors: (producer: Producer) => { atKeys: boolean; starts: ReadonlyMap; relative: ReadonlyMap; + /** Bumped whenever the others, or whether the producer is a received copy, change. */ + placing: number; }; /** The groups a reader reads, and the exclusive end it stops at; undefined before its ranges resolve. */ @@ -1318,6 +1320,9 @@ export class Producer { #timeStarts = new Map(); // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); + // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, + // `#timeStarts`, and `#startsAtKeys`. Mirrors the Rust `TrackState::placing`. + #placing = 0; // The highest-key checked start evicted. Eviction is not a key prefix, so a // group above it is checked against it as well as its cached neighbors; one at or below // it may have lost its lower bound. One entry, so nothing grows with the cache. @@ -1510,6 +1515,7 @@ export class Producer { resubscribe = (producer) => producer.#updateSubscription(); hooks.cacheTrack = (producer) => { producer.#wire = undefined; + producer.#placing++; // Nothing feeds the tail anymore, so it owes nothing and readers end at the boundary. for (const state of [producer.#state, ...producer.#sinks]) state.owed.set(false); producer.#endLargest(); @@ -1529,6 +1535,7 @@ export class Producer { : undefined; hooks.receiveTrack = (producer, starts = {}) => { producer.#wire = { starts, served: producer.#wire?.served }; + producer.#placing++; }; hooks.receiveGroup = (producer, { start: sequence, offset: start, fill }) => producer.#createGroup(sequence, start, fill); @@ -1548,12 +1555,15 @@ export class Producer { atKeys: producer.#startsAtKeys, starts: producer.#timeStarts, relative: producer.#relativeStarts, + placing: producer.#placing, }); hooks.startsAtKeys = (producer) => { producer.#startsAtKeys = true; + producer.#placing++; }; hooks.timeStarts = (producer, starts) => { producer.#timeStarts = new Map(starts); + producer.#placing++; }; hooks.relativeStarts = (producer, starts) => { producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); @@ -3590,17 +3600,16 @@ export class Subscriber { const producer = state.producer; if (this.#resume || !producer) return UNHELD; const keys = keysOf(producer); - // Whether drops of a kind `kind` accepts cover every key in `[start, end)`. An empty run is. - const covered = (start: bigint, end: bigint, kind: (run: DropRun) => boolean) => { - if (start >= end) return true; - let at = start; - for (const run of state.drops.overlapping(start, end)) { - if (!kind(run) || run.start > at) return false; - at = run.end; - } - return at >= end; - }; + const drops = state.drops; const anchors = timeAnchors(producer); + // A lite-07 copy holds only part of the track: a group held before a time contains it only + // once nothing between them is still to arrive. + const wire = receivedCopy(producer) && !state.dropsAnchors.peek(); + // The times from `from` on that the drops from there leave open. + const uncovered = (from: bigint): Run | undefined => { + const start = drops.coveredFrom(from); + return start < UNBOUNDED ? { start, end: UNBOUNDED } : undefined; + }; return { resolves: () => true, containing: (time) => { @@ -3612,22 +3621,43 @@ export class Subscriber { // A lite-07 copy holds only part of the track: a group held before `time` contains it // only once nothing between them is still to arrive. Mirrors the Rust // `TrackState::containing_received`. - if (receivedCopy(producer) && !state.dropsAnchors.peek()) { + if (wire) { if (below === time) return { group: time }; const start = anchors.starts.get(time); // The publisher serves nothing at or before it, as for a time older than every group. if (start !== undefined) return start <= time ? { group: start } : "before"; - if (below !== undefined && (covered(below + 1n, time + 1n, () => true) || index < keys.length)) { + if (below !== undefined && (drops.covers(below + 1n, time + 1n) || index < keys.length)) { return { group: below }; } - if (below === undefined && covered(0n, time + 1n, () => true)) return "before"; + if (below === undefined && drops.covers(0n, time + 1n)) return "before"; return "unknown"; } if (below !== undefined) return { group: below }; // Every key at or before it is gone, so whichever group held it will never arrive. - if (covered(0n, time + 1n, () => true)) return "before"; + if (drops.covers(0n, time + 1n)) return "before"; return keys.length === 0 ? "unknown" : "before"; }, + // The "unknown" times of `containing`, apart from those `anchors.starts` places, which the + // mark covers. Mirrors the Rust `Holdings::unplaced for TrackState`. + unplaced: () => { + const mark = anchors.placing * 4 + (state.dropsAnchors.peek() ? 2 : 0) + (state.fetch ? 1 : 0); + const runs: Run[] = []; + const first = keys[0]; + const last = keys.at(-1); + if (anchors.atKeys || state.fetch) { + // Every time starts a group. + } else if (first === undefined || last === undefined) { + const open = uncovered(0n); + if (open) runs.push(open); + } else if (wire) { + // Below every group, up to the first, and above the last, past the drops after it. + const below = uncovered(0n); + if (below && below.start < first) runs.push({ start: below.start, end: first }); + const above = uncovered(last + 1n); + if (above) runs.push(above); + } + return { runs, mark }; + }, nextStart: (from) => keys[keyIndex(keys, from)], // A lite-07 copy holds only part of the track, so it starts where its publisher did, // counting from the same latest group: a newer one moved the live edge. @@ -3648,7 +3678,15 @@ export class Subscriber { reachBack: (_latest, min) => this.#reachBack(min), // Skip runs coalesce, so a gap between two held groups is one run unless the cache // evicted a group inside it. - neverProduced: (start, end) => covered(start, end, (run) => run.skip === true), + neverProduced: (start, end) => { + if (start >= end) return true; + let at = start; + for (const run of drops.overlapping(start, end)) { + if (run.skip !== true || run.start > at) return false; + at = run.end; + } + return at >= end; + }, }; } @@ -4195,6 +4233,7 @@ export class Subscriber { makeSubscriber = (name, state) => new Subscriber(name, state); hooks.subscriberEnd = (subscriber) => subscriber.#resume ? subscriber.#resume.end() : subscriber.#state.end.peek()?.at; + hooks.subscriberHoldings = (subscriber) => subscriber.#holdings(); hooks.subscriberEndError = (subscriber) => subscriber.#resume ? subscriber.#resume.endError() : subscriber.#state.end.peek()?.error; hooks.askSubscriber = (subscriber, ask) => { diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 438b96d1e9f..2220cf550c7 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -17,6 +17,7 @@ import { rangesStart, span, UNHELD, + type Unplaced, union, Window, wireRanges, @@ -40,12 +41,16 @@ const groups = (start: bigint, end?: bigint): Range => const ascending = (a: bigint, b: bigint) => (a < b ? -1 : a > b ? 1 : 0); -/** The group starts a hop holds, the key runs its producer skipped, and whether a handler serves its history. */ +/** + * The group starts a hop holds, the key runs its producer skipped, whether a handler serves its + * history, and where the dropped prefix ends, which places a time below it before every group. + */ class Keys implements Holdings { held: Set; skipped: Run[] = []; done = false; looks = false; + dropped = 0n; constructor(held: Iterable = []) { this.held = new Set(held); @@ -63,7 +68,11 @@ class Keys implements Holdings { const sorted = this.#sorted(); const group = sorted.filter((key) => key <= at).at(-1); if (group !== undefined) return { group }; - return sorted.length === 0 ? "unknown" : "before"; + return sorted.length === 0 && at >= this.dropped ? "unknown" : "before"; + } + + unplaced(): Unplaced { + return { runs: this.held.size === 0 ? [{ start: this.dropped, end: UNBOUNDED }] : [], mark: 0 }; } nextStart(from: bigint): bigint | undefined { @@ -508,3 +517,68 @@ describe("ranges", () => { expect(joinsLive([range(relative(2n), relative(0n))])).toBe(false); }); }); + +/** {@link Keys} keeping its unplaced times to itself, so a window tries every start on each change. */ +function blind(keys: Keys): Holdings { + return { + resolves: () => true, + looksUp: () => keys.looksUp(), + neverProduced: (start, end) => keys.neverProduced(start, end), + complete: () => keys.complete(), + containing: (at) => keys.containing(at), + nextStart: (from) => keys.nextStart(from), + back: (latest, back) => keys.back(latest, back), + }; +} + +test("window: settling by steps matches trying every start and rebuilding from the spans", () => { + let seed = 11; + const random = (below: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + // The high bits: an LCG's low bits cycle. + return (seed >>> 16) % below; + }; + const seen = (window: Window) => ({ + runs: window.runs(), + missable: window.missable(), + bounds: [window.floor(), window.start(), window.end()], + resolved: window.resolved(), + lookups: window.lookups().map((lookup) => lookup.time), + state: [window.settled, window.pending], + }); + for (let round = 0; round < 3000; round++) { + const ranges = Array.from({ length: 1 + random(11) }, () => { + const start = random(5) === 0 ? relative(BigInt(random(6))) : time(BigInt(random(40))); + return range(start, random(3) === 0 ? undefined : time(BigInt(random(48)))); + }); + const keys = new Keys(); + keys.looks = random(2) === 0; + const fast = new Window(ranges, undefined, keys); + const full = new Window(ranges, undefined, blind(keys)); + for (let step = random(24); step > 0; step--) { + const value = BigInt(random(48)); + const op = random(5); + if (op === 0) keys.held.add(value); + else if (op === 1) { + if (value > keys.dropped) keys.dropped = value; + } else if (op === 2) { + const lookup = fast.lookups()[0]; + if (lookup === undefined) continue; + const answer = value % 2n === 0n ? value : undefined; + fast.found(lookup.time, answer, keys); + full.found(lookup.time, answer, blind(keys)); + } else if (op === 3) keys.done = true; + else if (op === 4) keys.held.delete(keys.nextStart(0n) ?? 0n); + const latest = [...keys.held].sort(ascending).at(-1); + fast.settle(latest, keys); + full.settle(latest, blind(keys)); + expect(seen(fast)).toEqual(seen(full)); + // A window waiting on a latest group reads everything, with no spans yet. + if (fast.pending) continue; + const fresh = fast.fresh(keys); + expect(fast.runs()).toEqual(fresh.runs); + expect(fast.missable()).toEqual(fresh.missable); + expect(fast.named()).toBe(fresh.named); + } + } +}); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 03596fa4498..3fd7834ba25 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -359,8 +359,21 @@ export interface Holdings { * even in the history a handler serves. Absent for none. */ neverProduced?(start: bigint, end: bigint): boolean; + /** + * Runs of times {@link containing} can't place, so a window skips its time starts in them rather + * than try each again whenever the groups held change. Absent, or undefined, when it can't tell, + * so each is tried. Mirrors the Rust `Holdings::unplaced`. + */ + unplaced?(): Unplaced | undefined; } +/** + * The times a {@link Holdings} can't place: sorted, disjoint runs, apart from the times a placement + * that the groups and drops held don't decide places. `mark` changes whenever such a placement may + * have, so a window that sees it unchanged tries only the times outside `runs` again. + */ +export type Unplaced = { runs: readonly Run[]; mark: number }; + /** A hop that holds no groups of its own, such as a reader following route copies that resolve its ranges for it. */ export const UNHELD: Holdings = Object.freeze({ resolves: () => false, @@ -428,8 +441,18 @@ export class Window { #missable: Run[] = []; /** A relative anchor waits on a first group, so nothing is filtered yet. */ #pending = false; - /** The spans, by position, whose time start waits on a group arriving. */ - #waiting: number[] = []; + /** + * The spans, by position, whose time start waits on a group arriving, by that time: a change to + * the groups held tries only those its {@link Holdings.unplaced} leaves out. + */ + #waiting = new ByTime(); + /** The {@link Unplaced.mark} every waiting span was last tried under. */ + #waited: number | undefined; + /** + * The spans, by position, still looking up their time start, which read nothing meanwhile, by + * that time: kept apart so an answer finds them without visiting every span. + */ + #looking = new ByTime(); /** * The ranges, by index, of a live join whose reach back waits on the groups held to measure the * live edge, so they read nothing yet: a group held is no reason to start at it. @@ -437,6 +460,19 @@ export class Window { #reaching: number[] = []; /** Nothing is left to resolve as groups arrive. */ #settled = false; + /** + * How many spans name each start, lowest first, and how many name none: see {@link start}. + * Counted so a read, and a span starting, costs no walk of the spans. + */ + #named: { start: bigint; count: number }[] = []; + #unnamed = 0; + /** + * The spans counting back past every group held, which may ask a miss from the oldest group + * held, and that group as of the last time they were read: a settle that finds it moved reads + * them again. + */ + #unfloored: number[] = []; + #oldest: bigint | undefined; /** * The subscriber's Min Age, which reaches a live join back from the live edge. Only a pending * window reads it, when it places its ranges, so a join already started stays where it started. @@ -473,49 +509,87 @@ export class Window { this.#place(latest, holdings); return !this.#pending; } - let moved = false; + // The spans placed or started here, each reading at least what it did before, unless one now + // looks up its start and reads nothing meanwhile. + const moved: number[] = []; if (this.#reaching.length > 0) { const placed = this.#spans.length; const reaching = this.#reaching; this.#reaching = []; for (const index of reaching) this.#placeRange(index, latest, holdings); - if (this.#spans.length > placed) moved = true; + for (let position = placed; position < this.#spans.length; position++) moved.push(position); + } + let shrank = this.#waiting.size > 0 && this.#startWaiting(holdings, moved); + // A span counting back past every group held asks misses from the oldest one, so it reads more + // once an older group arrives and less once that group leaves. + if (this.#unfloored.length > 0) { + const oldest = holdings.nextStart(0n); + if (oldest !== this.#oldest) { + if (oldest !== undefined && this.#oldest !== undefined && oldest < this.#oldest) { + moved.push(...this.#unfloored); + } else shrank = true; + this.#oldest = oldest; + } } - if (this.#waiting.length > 0 && this.#startWaiting(holdings)) moved = true; - if (moved) this.#rebuild(holdings); - return moved; + if (shrank) this.#rebuild(holdings); + else if (moved.length === 0) return false; + else this.#grow(moved, holdings); + return true; } // Start the spans whose time start waits on a group, once one arrived, or once nothing can - // arrive any more. Returns whether any did. - #startWaiting(holdings: Holdings): boolean { - let started = false; - const waiting = this.#waiting; - this.#waiting = []; - for (const position of waiting) { - const span = this.#spans[position]; - // Only a time start waits on a group. - const at = time(span.anchor) ?? 0n; - const containing = holdings.containing(at); + // arrive any more, adding each one started to `started`. Only the times the holdings may now + // place are tried, so a change placing one of them costs that one, not every span still + // waiting. Returns whether one started reads less than before: it looks up its start, reading + // nothing meanwhile. + #startWaiting(holdings: Holdings, started: number[]): boolean { + const complete = holdings.complete(); + const unplaced = complete ? undefined : holdings.unplaced?.(); + let tried: Timed[]; + if (unplaced !== undefined && this.#waited === unplaced.mark) { + tried = []; + for (const gap of outside(unplaced.runs)) tried.push(...this.#waiting.takeIn(gap.start, gap.end)); + } else { + tried = this.#waiting.takeAll(); + this.#waited = unplaced?.mark; + } + let shrank = false; + for (const entry of tried) { + const containing = holdings.containing(entry.time); let start: bigint; let floored: boolean; if (typeof containing === "object") [start, floored] = [containing.group, true]; // The group holding it is older than any held, or nothing can arrive. - else if (containing === "before" || holdings.complete()) [start, floored] = [0n, false]; + else if (containing === "before" || complete) [start, floored] = [0n, false]; else { - this.#waiting.push(position); + this.#waiting.put(entry); continue; } - started = true; - span.start = start; - span.floored = floored; - span.waiting = false; - // Looked up where the groups held can't place it, as when first resolved. - const asked = time(this.ranges[span.index].start) !== undefined; - const below = asked ? unresolved(at, start, floored, holdings) : undefined; - if (below !== undefined) span.looking = { time: at, below }; + for (const position of entry.positions) { + if (this.#startSpan(position, entry.time, start, floored, holdings)) shrank = true; + started.push(position); + } + } + return shrank; + } + + // Start the span at `position`, whose time start `at` waited, at `start`: looked up where the + // groups held can't place it, as when first resolved, and otherwise reading its groups. Returns + // whether it looks its start up. + #startSpan(position: number, at: bigint, start: bigint, floored: boolean, holdings: Holdings): boolean { + const span = this.#spans[position]; + const asked = time(this.ranges[span.index].start) !== undefined; + this.#unname(position); + span.start = start; + span.floored = floored; + span.waiting = false; + const below = asked ? unresolved(at, start, floored, holdings) : undefined; + if (below !== undefined) { + span.looking = { time: at, below }; + this.#looking.add(at, position); } - return started; + this.#name(position); + return below !== undefined; } /** Whether group `key` is in range. Everything is while a relative anchor waits. */ @@ -543,13 +617,8 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.#pending || this.#reaching.length > 0) return undefined; - let start: bigint | undefined; - for (const span of this.#spans) { - if (!span.floored) return undefined; - start = min(start ?? span.start, span.start); - } - return start; + if (this.#pending || this.#reaching.length > 0 || this.#unnamed > 0) return undefined; + return this.#named[0]?.start; } /** The exclusive end past every group in range, undefined when one stays open or an anchor waits. Nothing in range ends at 0. */ @@ -609,9 +678,16 @@ export class Window { }); } - /** The time starts still looked up. Mirrors the Rust `Window::lookups`. */ + /** The time starts still looked up, by time. Mirrors the Rust `Window::lookups`. */ lookups(): Lookup[] { - return this.#spans.flatMap((span) => (span.looking ? [span.looking] : [])); + const lookups: Lookup[] = []; + for (const { positions } of this.#looking.all) { + for (const position of positions) { + const looking = this.#spans[position].looking; + if (looking) lookups.push(looking); + } + } + return lookups; } /** @@ -622,45 +698,70 @@ export class Window { * `Window::found`. */ found(at: bigint, group: bigint | undefined, holdings: Holdings = UNHELD): boolean { - const found = this.#spans.filter((span) => span.looking?.time === at); - if (found.length === 0) return false; + const found = this.#looking.take(at); + if (found === undefined) return false; const held = group !== undefined && holdings.nextStart(group) === group ? group : undefined; const containing = holdings.containing(at); const start = held ?? (typeof containing === "object" ? containing.group : undefined); - for (const span of found) { + // A span looking up its start read nothing, so each one found only reads more. + for (const position of found) { + this.#unname(position); + const span = this.#spans[position]; span.looking = undefined; span.start = start ?? 0n; span.floored = start !== undefined; + this.#name(position); } - this.#rebuild(holdings); + this.#grow(found, holdings); return true; } + /** + * What rebuilding from the spans would read, may ask a miss for, and name as its start, for a + * test to check the runs grown span by span against. + */ + fresh(holdings: Holdings): { runs: Run[]; missable: Run[]; named: bigint | undefined } { + const groups: Run[] = []; + const missable: Run[] = []; + let named: bigint | undefined; + for (const span of this.#spans) { + const [reads, asks] = spanReads(span, holdings); + if (reads) groups.push(reads); + if (asks) missable.push(asks); + if (span.floored) named = min(named ?? span.start, span.start); + } + return { runs: merge(groups), missable: merge(missable), named }; + } + + /** The lowest start the spans name, counted. */ + named(): bigint | undefined { + return this.#named[0]?.start; + } + // Whether a time start or a join's reach back waits on the groups held to place it. #unplaced(): boolean { - return this.#waiting.length > 0 || this.#reaching.length > 0; + return this.#waiting.size > 0 || this.#reaching.length > 0; } // The first run ending past `key`. #runAt(key: bigint): Run | undefined { - let lo = 0; - let hi = this.#runs.length; - while (lo < hi) { - const mid = (lo + hi) >>> 1; - if (this.#runs[mid].end <= key) lo = mid + 1; - else hi = mid; - } - return this.#runs[lo]; + return this.#runs[above(this.#runs, key)]; } #place(latest: bigint | undefined, holdings: Holdings): void { this.#spans = []; this.#pending = false; - this.#waiting = []; + this.#waiting = new ByTime(); + this.#looking = new ByTime(); this.#reaching = []; + this.#named = []; + this.#unnamed = 0; + this.#unfloored = []; for (let index = 0; index < this.ranges.length; index++) { if (!isEmpty(this.ranges[index])) this.#placeRange(index, latest, holdings); } + // Each span waiting was just tried against these holdings. + this.#waited = holdings.unplaced?.()?.mark; this.#rebuild(holdings); } @@ -692,8 +793,8 @@ export class Window { } if (start === "waiting") { // Reads only the groups from its time until the groups held place it; see #startWaiting. - this.#waiting.push(this.#spans.length); - this.#spans.push({ index, anchor, start: 0n, end: until, floored: false, waiting: true }); + this.#waiting.add(time(anchor) ?? 0n, this.#spans.length); + this.#push({ index, anchor, start: 0n, end: until, floored: false, waiting: true }); return; } // A time start the groups held can't place reads nothing until its lookup answers. A join's @@ -701,49 +802,211 @@ export class Window { const at = time(anchor); const asked = time(range.start) !== undefined && at === time(range.start); const below = at === undefined || !asked ? undefined : unresolved(at, start.group, start.floored, holdings); - this.#spans.push({ + const looking = at !== undefined && below !== undefined ? { time: at, below } : undefined; + if (looking) this.#looking.add(looking.time, this.#spans.length); + this.#push({ index, anchor, start: start.group, end: until, floored: start.floored, - looking: at !== undefined && below !== undefined ? { time: at, below } : undefined, + looking, waiting: false, }); } + #push(span: Span): void { + if (reachesOldest(span)) this.#unfloored.push(this.#spans.length); + this.#spans.push(span); + this.#name(this.#spans.length - 1); + } + + // Count where the span at `position` starts in {@link start}. + #name(position: number): void { + const span = this.#spans[position]; + if (!span.floored) { + this.#unnamed++; + return; + } + const named = this.#named; + const at = lowerBound(named.length, (i) => named[i].start >= span.start); + if (named[at]?.start === span.start) named[at].count++; + else named.splice(at, 0, { start: span.start, count: 1 }); + } + + // Stop counting where the span at `position` starts, before it moves. + #unname(position: number): void { + const span = this.#spans[position]; + if (!span.floored) { + this.#unnamed--; + return; + } + const named = this.#named; + const at = lowerBound(named.length, (i) => named[i].start >= span.start); + if (named[at]?.start !== span.start) return; + if (--named[at].count === 0) named.splice(at, 1); + } + + #updateSettled(): void { + this.#settled = !this.#pending && !this.#unplaced() && this.#looking.size === 0; + } + // The groups in range: every span that knows where it starts, through its end, and every span // waiting on a time start from that time. #rebuild(holdings: Holdings): void { - this.#settled = !this.#pending && !this.#unplaced() && this.#spans.every((span) => !span.looking); + this.#updateSettled(); + this.#oldest = holdings.nextStart(0n); const groups: Run[] = []; const missable: Run[] = []; for (const span of this.#spans) { - if (span.looking) continue; - // A span waiting on its time start reads from that time meanwhile, and may ask a miss - // there, as one reaching back past every group held does: whichever group contains it - // starts at or before it, so every group from there on is in range. - if (span.waiting) { - const at = time(span.anchor) ?? 0n; - groups.push({ start: at, end: span.end }); - missable.push({ start: at, end: span.end }); - continue; - } - groups.push({ start: span.start, end: span.end }); - // A time start before every group held reaches back to the group containing it, which is - // unknown here, so it asks no miss below its time: every key from there on is in range, - // the key at the time itself being the containing group if it exists. A relative one - // counting back past every group held asks none below the oldest, since which of them it - // reaches is unknown. - let reach = span.start; - if (!span.floored && span.anchor !== undefined) { - reach = time(span.anchor) ?? holdings.nextStart(0n) ?? span.start; - } - missable.push({ start: reach, end: span.end }); + const [reads, asks] = spanReads(span, holdings); + if (reads) groups.push(reads); + if (asks) missable.push(asks); } this.#runs = merge(groups); this.#missable = merge(missable); } + + // Take in the spans at `positions`, each now reading at least what it did before: their runs + // are inserted into the sorted runs, so a change to a few spans costs them, not every range. + #grow(positions: readonly number[], holdings: Holdings): void { + this.#updateSettled(); + for (const position of positions) { + const [reads, asks] = spanReads(this.#spans[position], holdings); + if (reads) insert(this.#runs, reads); + if (asks) insert(this.#missable, asks); + } + } +} + +/** Spans by position, keyed by the time each waits on or looks up, lowest first. */ +type Timed = { time: bigint; positions: number[] }; + +/** {@link Timed} entries kept sorted by time, so the times in a run are found without a walk. */ +class ByTime { + #entries: Timed[] = []; + + get size(): number { + return this.#entries.length; + } + + get all(): readonly Timed[] { + return this.#entries; + } + + // The index of the first entry at or after `time`. + #at(time: bigint): number { + const entries = this.#entries; + return lowerBound(entries.length, (i) => entries[i].time >= time); + } + + /** Add the span at `position` under `time`. */ + add(time: bigint, position: number): void { + const at = this.#at(time); + const entry = this.#entries[at]; + if (entry?.time === time) entry.positions.push(position); + else this.#entries.splice(at, 0, { time, positions: [position] }); + } + + /** Put back an entry taken out, whose time no entry holds. */ + put(entry: Timed): void { + this.#entries.splice(this.#at(entry.time), 0, entry); + } + + /** Take out the spans under `time`, if any. */ + take(time: bigint): number[] | undefined { + const at = this.#at(time); + if (this.#entries[at]?.time !== time) return undefined; + return this.#entries.splice(at, 1)[0].positions; + } + + /** Take out every entry whose time is in `[start, end)`, lowest first. */ + takeIn(start: bigint, end: bigint): Timed[] { + const from = this.#at(start); + const to = end >= UNBOUNDED ? this.#entries.length : this.#at(end); + return to > from ? this.#entries.splice(from, to - from) : []; + } + + /** Take out every entry, lowest first. */ + takeAll(): Timed[] { + const entries = this.#entries; + this.#entries = []; + return entries; + } +} + +// What `span` reads, and the part of it a miss may ask for, each undefined for none. +function spanReads(span: Span, holdings: Holdings): [Run | undefined, Run | undefined] { + // A span still looking up its start reads nothing until it knows which group that is. + if (span.looking) return [undefined, undefined]; + // A span waiting on its time start reads from that time meanwhile, and may ask a miss there, as + // one reaching back past every group held does: whichever group contains it starts at or before + // it, so every group from there on is in range. + if (span.waiting) { + const at = time(span.anchor) ?? 0n; + if (at >= span.end) return [undefined, undefined]; + return [ + { start: at, end: span.end }, + { start: at, end: span.end }, + ]; + } + if (span.start >= span.end) return [undefined, undefined]; + // A time start before every group held reaches back to the group containing it, which is unknown + // here, so it asks no miss below its time: every key from there on is in range, the key at the + // time itself being the containing group if it exists. A relative one counting back past every + // group held asks none below the oldest, since which of them it reaches is unknown. + let reach = span.start; + if (!span.floored && span.anchor !== undefined) { + reach = time(span.anchor) ?? holdings.nextStart(0n) ?? span.start; + } + return [{ start: span.start, end: span.end }, reach < span.end ? { start: reach, end: span.end } : undefined]; +} + +// Whether `span` counts back past every group held, so what it asks follows the oldest group held. +function reachesOldest(span: Span): boolean { + return !span.floored && relative(span.anchor) !== undefined; +} + +// The first index in `[0, length)` for which `past` holds, `past` being false then true. +function lowerBound(length: number, past: (index: number) => boolean): number { + let lo = 0; + let hi = length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (past(mid)) hi = mid; + else lo = mid + 1; + } + return lo; +} + +// The index of the first of sorted, disjoint `runs` ending past `key`. +function above(runs: readonly Run[], key: bigint): number { + return lowerBound(runs.length, (i) => runs[i].end > key); +} + +// Insert `run` into sorted, disjoint `runs`, merging what it overlaps or touches. An empty one +// changes nothing. +function insert(runs: Run[], run: Run): void { + if (run.end <= run.start) return; + const from = lowerBound(runs.length, (i) => runs[i].end >= run.start); + const to = lowerBound(runs.length, (i) => runs[i].start > run.end); + if (from === to) { + runs.splice(from, 0, { ...run }); + return; + } + const merged = { start: min(runs[from].start, run.start), end: max(runs[to - 1].end, run.end) }; + runs.splice(from, to - from, merged); +} + +// The times outside sorted, disjoint `runs`, as runs lowest first. +function* outside(runs: readonly Run[]): Generator { + let from = 0n; + for (const run of runs) { + if (run.end <= run.start) continue; + if (run.start > from) yield { start: from, end: run.start }; + from = max(from, run.end); + } + if (from < UNBOUNDED) yield { start: from, end: UNBOUNDED }; } // `runs` merged into sorted, disjoint runs, the empty ones dropped. diff --git a/quest/m2/js-drop-cost.md b/quest/m2/js-drop-cost.md deleted file mode 100644 index f0bdbf2ca2b..00000000000 --- a/quest/m2/js-drop-cost.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] A JS dropped group costs a subscription what it changes - -## Goal - -In `@moq/net`, a drop that starts one time range of a subscription with many -ranges costs about what it does with one, as it does in Rust since -[moq-dev/moq#9142](https://github.com/moq-dev/moq/pull/9142). A JS bench sweeps -drop cost over ranges and drops, so a regression shows in Nightly's JS -benchmarks. - -## Plan - -`js/net/src/window.ts` still has the shape Rust's `Window` had before the fix: -each settle tries every waiting time start (`#startWaiting`), and any move -rebuilds and re-sorts every span's runs (`#rebuild`), so each drop that -places one range costs every range. Port the Rust fix: - -- Waiting spans keyed by time, tried only outside the runs a new optional - `Holdings.unplaced` reports, with a mark that changes when a lite-07 copy's - reported time starts, `atKeys`, or anchoring change. The track's holdings - in `js/net/src/track.ts` need the dropped prefix in one lookup: `DropSet` - keeps runs with different errors apart, so add a coalesced key set as - Rust's `TrackState::dropped`. -- A span that starts or whose lookup answers inserts its runs into the sorted - runs instead of rebuilding them; only a span that starts looking up its - start rebuilds. -- `start()` from counted starts rather than a walk of the spans. - -Then measure what else in `track.ts` a window move costs per drop. JS has no -`DropCursor`, so check its drop reporting the same way. - -Public API: none. Wire: none. diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 4eb85a79bd6..d6356c0b3f5 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -198,6 +198,11 @@ pub(in crate::model) struct Window { /// without a walk. named: BTreeMap, unnamed: usize, + /// The spans counting back past every group held, which may ask a miss from the oldest + /// group held (see [`Span::reads`]), and that group as of the last time they were read: + /// a settle that finds it moved reads them again. + unfloored: Vec, + oldest: Option, } /// One non-empty range, resolved. @@ -251,6 +256,8 @@ impl Window { covered: None, named: BTreeMap::new(), unnamed: 0, + unfloored: Vec::new(), + oldest: None, }; window.place(latest, holdings)?; Ok(window) @@ -304,10 +311,22 @@ impl Window { } moved.extend(placed..self.spans.len()); } - let shrank = !self.waiting.is_empty() && self.start_waiting(holdings, &mut moved); - match (moved.is_empty(), shrank) { - (true, _) => return false, - (false, true) => self.rebuild(holdings), + let mut shrank = !self.waiting.is_empty() && self.start_waiting(holdings, &mut moved); + // A span counting back past every group held asks misses from the oldest one, so it + // reads more once an older group arrives and less once that group leaves. + if !self.unfloored.is_empty() { + let oldest = holdings.next_start(0); + if oldest != self.oldest { + match (oldest, self.oldest) { + (Some(oldest), Some(was)) if oldest < was => moved.extend_from_slice(&self.unfloored), + _ => shrank = true, + } + self.oldest = oldest; + } + } + match (shrank, moved.is_empty()) { + (true, _) => self.rebuild(holdings), + (false, true) => return false, (false, false) => self.grow(moved, holdings), } true @@ -577,6 +596,7 @@ impl Window { self.reaching.clear(); self.named.clear(); self.unnamed = 0; + self.unfloored.clear(); self.pending = false; for index in 0..self.ranges.len() { if !self.ranges[index].is_empty() { @@ -666,6 +686,9 @@ impl Window { /// Add `span`, counted in [`Self::named`] and [`Self::start`]. fn push(&mut self, span: Span) { + if span.reaches_oldest() { + self.unfloored.push(self.spans.len()); + } self.spans.push(span); self.name(self.spans.len() - 1); } @@ -708,6 +731,7 @@ impl Window { /// every span waiting on a time start from that time. fn rebuild(&mut self, holdings: &impl Holdings) { self.covered = None; + self.oldest = holdings.next_start(0); self.update_settled(); let (mut groups, mut missable) = (Vec::new(), Vec::new()); for span in &self.spans { @@ -775,6 +799,12 @@ impl Span { }; (Some(self.start..=last), (reach <= last).then_some(reach..=last)) } + + /// Whether it counts back past every group held, so what it asks follows the oldest + /// group held (see [`Self::reads`]). + fn reaches_oldest(&self) -> bool { + matches!((self.anchor, self.floored), (Anchor::Relative(_), false)) + } } /// The times outside `runs`, which are disjoint and lowest first, as runs lowest first. @@ -1027,17 +1057,23 @@ mod tests { proptest::proptest! { /// A window that skips the starts its holdings can't place and grows its groups as /// spans start sees what one trying every start sees, and reads what rebuilding from - /// its spans would, through groups arriving, drops, lookups answering, and the track - /// completing. + /// its spans would, through groups arriving and leaving, drops, lookups answering, and + /// the track completing. #[test] fn settling_by_steps_matches_a_full_settle( - ranges in proptest::collection::vec((0u64..40, proptest::option::of(0u64..48)), 1..12), - ops in proptest::collection::vec((0u8..4, 0u64..48), 0..24), + ranges in proptest::collection::vec((proptest::bool::weighted(0.2), 0u64..40, proptest::option::of(0u64..48)), 1..12), + ops in proptest::collection::vec((0u8..5, 0u64..48), 0..24), looks_up: bool, ) { let ranges: Vec = ranges .into_iter() - .map(|(start, end)| range(time(start), end.map(time))) + .map(|(relative, start, end)| { + let start = match relative { + true => Anchor::Relative(start % 6), + false => time(start), + }; + range(start, end.map(time)) + }) .collect(); let mut keys = Keys { looks_up, @@ -1057,12 +1093,18 @@ mod tests { fast.found(time, answer, &keys); full.found(time, answer, &Blind(&keys)); } - _ => keys.complete = true, + 3 => keys.complete = true, + // The oldest group held is evicted. + _ => drop(keys.held.pop_first()), } let latest = keys.held.last().copied(); fast.settle(latest, &keys); full.settle(latest, &Blind(&keys)); proptest::prop_assert_eq!(seen(&fast), seen(&full)); + // A window waiting on a latest group reads everything, with no spans yet. + if fast.is_pending() { + continue; + } let (groups, missable, named) = rebuilt(&fast, &keys); proptest::prop_assert!(fast.runs(0).eq(groups.iter().cloned())); proptest::prop_assert!(fast.missable(0).eq(missable.iter().cloned())); diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index 2249892b097..c96c3da3bda 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -177,7 +177,7 @@ case "$1" in done ;; js) - for benchmark in broadcasts requests fills fill-probe forward reader frames drift ledger bandwidth replay varint lite-varint track resume priority subscribe-drop refresh observed compression datagrams; do + for benchmark in broadcasts requests fills fill-probe forward reader frames drift ledger bandwidth replay varint lite-varint track resume priority subscribe-drop window-drop refresh observed compression datagrams; do javascript "$benchmark" just js bench "$benchmark" done javascript catalog just js catalog-bench From 7540f355ef4454a0583e5152a0271f4446753889 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 00:04:14 -0800 Subject: [PATCH 4/6] perf(net): a window's unplaced gaps build an array, not a generator Co-Authored-By: Claude Opus 5.5 --- js/net/src/window.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 3fd7834ba25..7edf3112282 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -548,7 +548,9 @@ export class Window { let tried: Timed[]; if (unplaced !== undefined && this.#waited === unplaced.mark) { tried = []; - for (const gap of outside(unplaced.runs)) tried.push(...this.#waiting.takeIn(gap.start, gap.end)); + for (const gap of outside(unplaced.runs)) { + for (const entry of this.#waiting.takeIn(gap.start, gap.end)) tried.push(entry); + } } else { tried = this.#waiting.takeAll(); this.#waited = unplaced?.mark; @@ -999,14 +1001,16 @@ function insert(runs: Run[], run: Run): void { } // The times outside sorted, disjoint `runs`, as runs lowest first. -function* outside(runs: readonly Run[]): Generator { +function outside(runs: readonly Run[]): Run[] { + const gaps: Run[] = []; let from = 0n; for (const run of runs) { if (run.end <= run.start) continue; - if (run.start > from) yield { start: from, end: run.start }; + if (run.start > from) gaps.push({ start: from, end: run.start }); from = max(from, run.end); } - if (from < UNBOUNDED) yield { start: from, end: UNBOUNDED }; + if (from < UNBOUNDED) gaps.push({ start: from, end: UNBOUNDED }); + return gaps; } // `runs` merged into sorted, disjoint runs, the empty ones dropped. From 360646d175c26c4a7539a2950775ec2754014425 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 00:35:45 -0800 Subject: [PATCH 5/6] perf(net): a drop changes the coalesced keys once, not once per run it replaces Co-Authored-By: Claude Opus 5.5 --- js/net/src/drops.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/js/net/src/drops.ts b/js/net/src/drops.ts index 54be8c93950..fc33263ab51 100644 --- a/js/net/src/drops.ts +++ b/js/net/src/drops.ts @@ -184,19 +184,18 @@ export class DropSet { /** Give up `[start, end)` as `run` says, or forget it when `run` is undefined. */ set(start: bigint, end: bigint, run?: Omit): void { + this.#keys.set(start, end, run && { start, end }); this.#apply(this.#runs.set(start, end, run && { ...run, start, end })); } /** Forget each of `ranges`, sorted and disjoint, in one pass. */ clear(ranges: readonly Run[]): void { + this.#keys.clear(ranges); this.#apply(this.#runs.clear(ranges)); } #apply({ removed, added }: { removed: DropRun[]; added: DropRun[] }): void { if (removed.length === 0 && added.length === 0) return; - // What was replaced leaves the keys, and its replacement puts back what it still holds. - for (const old of removed) this.#keys.set(old.start, old.end); - for (const fresh of added) this.#keys.set(fresh.start, fresh.end, { start: fresh.start, end: fresh.end }); for (const old of removed) this.#live.delete(old); for (const fresh of added) { this.#live.add(fresh); From 99fa96d987f332103ead3d6249d647d9e4b299d3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 03:21:36 -0800 Subject: [PATCH 6/6] perf(net): eviction leaves a relative span's misses alone instead of rebuilding A span counting back past every group held asks misses from the lowest group held since it was placed, since it reached each one. An older group arriving grows it; evicting the oldest changes nothing, so a live copy no longer pays a full O(ranges) rebuild per evicted group while the window is unsettled. Co-Authored-By: Claude Opus 5.5 --- js/net/src/window.test.ts | 22 ++++- js/net/src/window.ts | 67 ++++++------- rs/moq-net/src/model/subscription/window.rs | 102 ++++++++++++-------- 3 files changed, 119 insertions(+), 72 deletions(-) diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 2220cf550c7..362d1a96a38 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -188,6 +188,20 @@ describe("Window", () => { expect(covered(ended, held.held)).toEqual(keys(0n, 8n)); }); + test("a relative start past every group asks from the lowest group held since", () => { + const held = new Keys([33n, 66n, 99n]); + const window = new Window([range(relative(10n))], 99n, held); + expect(window.missable()).toEqual([{ start: 33n, end: UNBOUNDED }]); + held.held.add(20n); + expect(window.settle(99n, held)).toBe(true); + expect(window.missable()).toEqual([{ start: 20n, end: UNBOUNDED }]); + // Evicting the oldest narrows nothing: the start reached it. + held.held.delete(20n); + held.held.delete(33n); + expect(window.settle(99n, held)).toBe(false); + expect(window.missable()).toEqual([{ start: 20n, end: UNBOUNDED }]); + }); + test("relative anchors count groups back from the latest", () => { let held = new Keys([0n, 33n, 66n, 99n]); let window = new Window([range(relative(2n), relative(1n))], 99n, held); @@ -555,6 +569,8 @@ test("window: settling by steps matches trying every start and rebuilding from t keys.looks = random(2) === 0; const fast = new Window(ranges, undefined, keys); const full = new Window(ranges, undefined, blind(keys)); + // The lowest group held since the spans were placed. + let reached = keys.nextStart(0n); for (let step = random(24); step > 0; step--) { const value = BigInt(random(48)); const op = random(5); @@ -570,12 +586,16 @@ test("window: settling by steps matches trying every start and rebuilding from t } else if (op === 3) keys.done = true; else if (op === 4) keys.held.delete(keys.nextStart(0n) ?? 0n); const latest = [...keys.held].sort(ascending).at(-1); + const placing = fast.pending; fast.settle(latest, keys); full.settle(latest, blind(keys)); expect(seen(fast)).toEqual(seen(full)); // A window waiting on a latest group reads everything, with no spans yet. if (fast.pending) continue; - const fresh = fast.fresh(keys); + const oldest = keys.nextStart(0n); + if (placing || reached === undefined) reached = oldest; + else if (oldest !== undefined && oldest < reached) reached = oldest; + const fresh = fast.fresh(reached); expect(fast.runs()).toEqual(fresh.runs); expect(fast.missable()).toEqual(fresh.missable); expect(fast.named()).toBe(fresh.named); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 7edf3112282..fe4ad557f01 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -467,12 +467,12 @@ export class Window { #named: { start: bigint; count: number }[] = []; #unnamed = 0; /** - * The spans counting back past every group held, which may ask a miss from the oldest group - * held, and that group as of the last time they were read: a settle that finds it moved reads - * them again. + * The spans counting back past every group held, which may ask a miss from the lowest group held + * since they were placed, and that group. They reached every group held since, so an older group + * arriving widens what they ask, and eviction narrows nothing. */ #unfloored: number[] = []; - #oldest: bigint | undefined; + #reached: bigint | undefined; /** * The subscriber's Min Age, which reaches a live join back from the live edge. Only a pending * window reads it, when it places its ranges, so a join already started stays where it started. @@ -520,20 +520,18 @@ export class Window { for (let position = placed; position < this.#spans.length; position++) moved.push(position); } let shrank = this.#waiting.size > 0 && this.#startWaiting(holdings, moved); - // A span counting back past every group held asks misses from the oldest one, so it reads more - // once an older group arrives and less once that group leaves. - if (this.#unfloored.length > 0) { - const oldest = holdings.nextStart(0n); - if (oldest !== this.#oldest) { - if (oldest !== undefined && this.#oldest !== undefined && oldest < this.#oldest) { - moved.push(...this.#unfloored); - } else shrank = true; - this.#oldest = oldest; - } + // A span counting back past every group held asks misses from the lowest group held since it + // was placed, so it asks more once an older group arrives, and evicting the oldest costs nothing. + const oldest = this.#unfloored.length > 0 ? holdings.nextStart(0n) : undefined; + if (oldest !== undefined) { + // With no group held, they asked from where they start. + if (this.#reached === undefined) shrank = true; + else if (oldest < this.#reached) moved.push(...this.#unfloored); + this.#reached = min(this.#reached ?? oldest, oldest); } - if (shrank) this.#rebuild(holdings); + if (shrank) this.#rebuild(); else if (moved.length === 0) return false; - else this.#grow(moved, holdings); + else this.#grow(moved); return true; } @@ -639,7 +637,7 @@ export class Window { /** * The part of {@link runs} a miss may ask a fill for: all of it but what starts before a time - * start that fell before every group held, or below the oldest group held when a relative start + * start that fell before every group held, or below the lowest group held since a relative start * counted back past them all. Mirrors the Rust `Window::missable`. */ missable(from = 0n, end = UNBOUNDED): Run[] { @@ -714,20 +712,21 @@ export class Window { span.floored = start !== undefined; this.#name(position); } - this.#grow(found, holdings); + this.#grow(found); return true; } /** - * What rebuilding from the spans would read, may ask a miss for, and name as its start, for a - * test to check the runs grown span by span against. + * What rebuilding from the spans would read, may ask a miss for, and name as its start, given the + * lowest group held since they were placed, for a test to check the runs grown span by span + * against. */ - fresh(holdings: Holdings): { runs: Run[]; missable: Run[]; named: bigint | undefined } { + fresh(reached: bigint | undefined): { runs: Run[]; missable: Run[]; named: bigint | undefined } { const groups: Run[] = []; const missable: Run[] = []; let named: bigint | undefined; for (const span of this.#spans) { - const [reads, asks] = spanReads(span, holdings); + const [reads, asks] = spanReads(span, reached); if (reads) groups.push(reads); if (asks) missable.push(asks); if (span.floored) named = min(named ?? span.start, span.start); @@ -759,12 +758,13 @@ export class Window { this.#named = []; this.#unnamed = 0; this.#unfloored = []; + this.#reached = holdings.nextStart(0n); for (let index = 0; index < this.ranges.length; index++) { if (!isEmpty(this.ranges[index])) this.#placeRange(index, latest, holdings); } // Each span waiting was just tried against these holdings. this.#waited = holdings.unplaced?.()?.mark; - this.#rebuild(holdings); + this.#rebuild(); } // Resolve the non-empty range at `index` into a span, or note what it waits on. @@ -855,13 +855,12 @@ export class Window { // The groups in range: every span that knows where it starts, through its end, and every span // waiting on a time start from that time. - #rebuild(holdings: Holdings): void { + #rebuild(): void { this.#updateSettled(); - this.#oldest = holdings.nextStart(0n); const groups: Run[] = []; const missable: Run[] = []; for (const span of this.#spans) { - const [reads, asks] = spanReads(span, holdings); + const [reads, asks] = spanReads(span, this.#reached); if (reads) groups.push(reads); if (asks) missable.push(asks); } @@ -871,10 +870,10 @@ export class Window { // Take in the spans at `positions`, each now reading at least what it did before: their runs // are inserted into the sorted runs, so a change to a few spans costs them, not every range. - #grow(positions: readonly number[], holdings: Holdings): void { + #grow(positions: readonly number[]): void { this.#updateSettled(); for (const position of positions) { - const [reads, asks] = spanReads(this.#spans[position], holdings); + const [reads, asks] = spanReads(this.#spans[position], this.#reached); if (reads) insert(this.#runs, reads); if (asks) insert(this.#missable, asks); } @@ -937,8 +936,9 @@ class ByTime { } } -// What `span` reads, and the part of it a miss may ask for, each undefined for none. -function spanReads(span: Span, holdings: Holdings): [Run | undefined, Run | undefined] { +// What `span` reads, and the part of it a miss may ask for, each undefined for none, given the +// lowest group held since it was placed. +function spanReads(span: Span, reached: bigint | undefined): [Run | undefined, Run | undefined] { // A span still looking up its start reads nothing until it knows which group that is. if (span.looking) return [undefined, undefined]; // A span waiting on its time start reads from that time meanwhile, and may ask a miss there, as @@ -956,15 +956,16 @@ function spanReads(span: Span, holdings: Holdings): [Run | undefined, Run | unde // A time start before every group held reaches back to the group containing it, which is unknown // here, so it asks no miss below its time: every key from there on is in range, the key at the // time itself being the containing group if it exists. A relative one counting back past every - // group held asks none below the oldest, since which of them it reaches is unknown. + // group held asks none below the lowest group held since, since which of them it reaches is unknown. let reach = span.start; if (!span.floored && span.anchor !== undefined) { - reach = time(span.anchor) ?? holdings.nextStart(0n) ?? span.start; + reach = time(span.anchor) ?? reached ?? span.start; } return [{ start: span.start, end: span.end }, reach < span.end ? { start: reach, end: span.end } : undefined]; } -// Whether `span` counts back past every group held, so what it asks follows the oldest group held. +// Whether `span` counts back past every group held, so what it asks follows the lowest group held +// since it was placed. function reachesOldest(span: Span): boolean { return !span.floored && relative(span.anchor) !== undefined; } diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index d6356c0b3f5..ad15522f916 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -198,11 +198,12 @@ pub(in crate::model) struct Window { /// without a walk. named: BTreeMap, unnamed: usize, - /// The spans counting back past every group held, which may ask a miss from the oldest - /// group held (see [`Span::reads`]), and that group as of the last time they were read: - /// a settle that finds it moved reads them again. + /// The spans counting back past every group held, which may ask a miss from the lowest + /// group held since they were placed (see [`Span::reads`]), and that group. They reached + /// every group held since, so an older group arriving widens what they ask, and eviction + /// narrows nothing. unfloored: Vec, - oldest: Option, + reached: Option, } /// One non-empty range, resolved. @@ -257,7 +258,7 @@ impl Window { named: BTreeMap::new(), unnamed: 0, unfloored: Vec::new(), - oldest: None, + reached: None, }; window.place(latest, holdings)?; Ok(window) @@ -312,22 +313,24 @@ impl Window { moved.extend(placed..self.spans.len()); } let mut shrank = !self.waiting.is_empty() && self.start_waiting(holdings, &mut moved); - // A span counting back past every group held asks misses from the oldest one, so it - // reads more once an older group arrives and less once that group leaves. - if !self.unfloored.is_empty() { - let oldest = holdings.next_start(0); - if oldest != self.oldest { - match (oldest, self.oldest) { - (Some(oldest), Some(was)) if oldest < was => moved.extend_from_slice(&self.unfloored), - _ => shrank = true, - } - self.oldest = oldest; + // A span counting back past every group held asks misses from the lowest group held + // since it was placed, so it asks more once an older group arrives, and evicting the + // oldest one costs nothing. + if !self.unfloored.is_empty() + && let Some(oldest) = holdings.next_start(0) + { + match self.reached { + Some(reached) if oldest >= reached => {} + Some(_) => moved.extend_from_slice(&self.unfloored), + // With no group held, they asked from where they start. + None => shrank = true, } + self.reached = Some(self.reached.map_or(oldest, |reached| reached.min(oldest))); } match (shrank, moved.is_empty()) { - (true, _) => self.rebuild(holdings), + (true, _) => self.rebuild(), (false, true) => return false, - (false, false) => self.grow(moved, holdings), + (false, false) => self.grow(moved), } true } @@ -515,7 +518,7 @@ impl Window { /// The groups in range from `from` on that a miss may ask for, lowest first: all of /// them but those starting before a time start that fell before every group held, or - /// below the oldest group held when a relative start counted back past them all. + /// below the lowest group held since a relative start counted back past them all. pub fn missable(&self, from: u64) -> impl Iterator> + '_ { self.from(&self.missable, from) } @@ -579,7 +582,7 @@ impl Window { (span.looking, span.start, span.floored) = (None, start, floored); self.name(index); } - self.grow(found, holdings); + self.grow(found); true } @@ -597,6 +600,7 @@ impl Window { self.named.clear(); self.unnamed = 0; self.unfloored.clear(); + self.reached = holdings.next_start(0); self.pending = false; for index in 0..self.ranges.len() { if !self.ranges[index].is_empty() { @@ -605,7 +609,7 @@ impl Window { } // Each span waiting was just tried against these holdings. self.waited = holdings.unplaced().map(|unplaced| unplaced.mark); - self.rebuild(holdings); + self.rebuild(); Ok(()) } @@ -729,13 +733,12 @@ impl Window { /// The groups in range: every span that knows where it starts, through its end, and /// every span waiting on a time start from that time. - fn rebuild(&mut self, holdings: &impl Holdings) { + fn rebuild(&mut self) { self.covered = None; - self.oldest = holdings.next_start(0); self.update_settled(); let (mut groups, mut missable) = (Vec::new(), Vec::new()); for span in &self.spans { - let (reads, asks) = span.reads(holdings); + let (reads, asks) = span.reads(self.reached); groups.extend(reads); missable.extend(asks); } @@ -746,10 +749,10 @@ impl Window { /// Take in the spans at `positions`, each now reading at least what it did before: their /// runs layer over the groups, so a change to a few spans costs them, not every range, /// and what reads the groups diffs only the layer. - fn grow(&mut self, positions: impl IntoIterator, holdings: &impl Holdings) { + fn grow(&mut self, positions: impl IntoIterator) { self.update_settled(); for position in positions { - let (reads, asks) = self.spans[position].reads(holdings); + let (reads, asks) = self.spans[position].reads(self.reached); if let Some(run) = reads { // A coverage check resumes where it stopped, so groups added below that are // checked again. @@ -766,8 +769,9 @@ impl Window { } impl Span { - /// The groups this span reads, and those of them a miss may ask for. - fn reads(&self, holdings: &impl Holdings) -> (Option>, Option>) { + /// The groups this span reads, and those of them a miss may ask for, given the lowest + /// group held since it was placed. + fn reads(&self, reached: Option) -> (Option>, Option>) { // A span still looking up its start reads nothing until it knows which group that is. if self.looking.is_some() { return (None, None); @@ -791,17 +795,17 @@ impl Span { // which is unknown here, so it asks no miss below its time: every key from there // on is in range, the key at the time itself being the containing group if it // exists. A relative one counting back past every group held asks none below the - // oldest, since which of them it reaches is unknown. + // lowest group held since, since which of them it reaches is unknown. let reach = match (self.anchor, self.floored) { (Anchor::Time(time), false) => time, - (Anchor::Relative(_), false) => holdings.next_start(0).unwrap_or(self.start), + (Anchor::Relative(_), false) => reached.unwrap_or(self.start), _ => self.start, }; (Some(self.start..=last), (reach <= last).then_some(reach..=last)) } - /// Whether it counts back past every group held, so what it asks follows the oldest - /// group held (see [`Self::reads`]). + /// Whether it counts back past every group held, so what it asks follows the lowest + /// group held since it was placed (see [`Self::reads`]). fn reaches_oldest(&self) -> bool { matches!((self.anchor, self.floored), (Anchor::Relative(_), false)) } @@ -1023,14 +1027,11 @@ mod tests { } /// What `window` reads and may ask a miss for, and the lowest start its spans name, - /// worked out from its spans afresh. - fn rebuilt( - window: &Window, - holdings: &impl Holdings, - ) -> (RangeInclusiveSet, RangeInclusiveSet, Option) { + /// worked out from its spans afresh, given the lowest group held since they were placed. + fn rebuilt(window: &Window, reached: Option) -> (RangeInclusiveSet, RangeInclusiveSet, Option) { let (mut groups, mut missable) = (RangeInclusiveSet::new(), RangeInclusiveSet::new()); for span in &window.spans { - let (reads, asks) = span.reads(holdings); + let (reads, asks) = span.reads(reached); groups.extend(reads); missable.extend(asks); } @@ -1081,6 +1082,8 @@ mod tests { }; let mut fast = Window::new(&ranges, None, None, &keys).unwrap(); let mut full = Window::new(&ranges, None, None, &Blind(&keys)).unwrap(); + // The lowest group held since the spans were placed. + let mut reached = None; for (op, value) in ops { match op { 0 => drop(keys.held.insert(value)), @@ -1098,6 +1101,7 @@ mod tests { _ => drop(keys.held.pop_first()), } let latest = keys.held.last().copied(); + let placing = fast.is_pending(); fast.settle(latest, &keys); full.settle(latest, &Blind(&keys)); proptest::prop_assert_eq!(seen(&fast), seen(&full)); @@ -1105,7 +1109,13 @@ mod tests { if fast.is_pending() { continue; } - let (groups, missable, named) = rebuilt(&fast, &keys); + let oldest = keys.held.first().copied(); + reached = match (placing, reached, oldest) { + (false, Some(reached), Some(oldest)) => Some(oldest.min(reached)), + (false, Some(reached), None) => Some(reached), + _ => oldest, + }; + let (groups, missable, named) = rebuilt(&fast, reached); proptest::prop_assert!(fast.runs(0).eq(groups.iter().cloned())); proptest::prop_assert!(fast.missable(0).eq(missable.iter().cloned())); proptest::prop_assert_eq!(fast.lowest_named(), named); @@ -1240,6 +1250,22 @@ mod tests { } } + /// A relative start counting back past every group held asks misses from the lowest + /// group held since it was placed: an older group arriving widens that, and evicting the + /// oldest narrows nothing, since the start reached it. + #[test] + fn a_relative_start_past_every_group_asks_from_the_lowest_reached() { + let mut keys = Keys::new([33, 66, 99]); + let mut window = Window::new(&[range(Anchor::Relative(10), None)], None, Some(99), &keys).unwrap(); + assert_eq!(window.missable(0).next(), Some(33..=u64::MAX)); + keys.held.insert(20); + assert!(window.settle(Some(99), &keys)); + assert_eq!(window.missable(0).next(), Some(20..=u64::MAX)); + keys.held.retain(|key| *key > 33); + assert!(!window.settle(Some(99), &keys)); + assert_eq!(window.missable(0).next(), Some(20..=u64::MAX)); + } + /// No hop can place the oldest group as an end, so it is refused. #[test] fn an_oldest_end_is_refused() {