diff --git a/quest/m2/lite07-update-cost.md b/quest/m2/lite07-update-cost.md index 9f675dff365..a977d16864a 100644 --- a/quest/m2/lite07-update-cost.md +++ b/quest/m2/lite07-update-cost.md @@ -1,4 +1,4 @@ -# [M] A lite-07 SUBSCRIBE_UPDATE costs its edit on the publisher's session too +# [M] A lite-07 SUBSCRIBE_UPDATE costs its edit on a relay's front and in JS too ## Goal @@ -8,41 +8,36 @@ lock every subscriber of the track shares, in Rust and JS alike. ## Plan -Left by the [range union fix](https://github.com/moq-dev/moq/pull/9045), which -made an update that leaves every range live cost nothing for the ranges, and -held the shared lock only for the ranges an edit moves. What remains is linear -in n on the session's own task for any update that edits a range or changes a -preference: n is bounded by one message's ranges, so this is CPU a peer spends -one small message to buy, not unbounded work. - -- Rust: `TrackRun::update` (`rs/moq-net/src/lite/publisher.rs`) re-pins every - range, rebuilds its Range ID positions and `Bounds`, and hands the model a - whole `Subscription`. The model resolves ranges by position - (`track.resolved()`), so editing by Range ID means the model's ranges and - their resolutions are keyed rather than positional - (`rs/moq-net/src/model/track.rs`, `model/track/demand.rs`). A preference-only - update still clones the model's ranges. +Left by [#9134](https://github.com/moq-dev/moq/pull/9134), which made the +Rust publisher hand the model the edit by Range ID +(`track::Subscriber::edit`), keyed each subscriber's ranges with a change +log (`model/track/demand.rs` `Prefs`), and made the plain cursor, its +window, the misses, and the shared aggregate move only what an edit touched +(`model/subscription/cover.rs`). `publisher_update` measures it. What +remains: + +- Rust, a relay's front (`rs/moq-net/src/model/resume.rs`): `Reader::sync` + materializes the whole `Subscription` on each change, rebuilds the reader's + window, and hands every copy a whole `update`, whose own cursor then + rebuilds. The demand fold (`poll_combined`) materializes each reader's + preferences too. Replay `Prefs::changes` into the reader's window and pass + each copy the edit, clamped per copy, through `track::Subscriber::edit`; the + copy's positional `resolved_ranges` and `Copy::from` become keyed. Add a + routed case to `publisher_update`. +- Rust, the servable index (`model/track.rs` `Live`): rebuilt from the cache + arrivals whenever the groups' cover moves, so an edit costs the cache held, + not the ranges. - JS: `combineSubscriptions` in `js/net/src/track.ts` rebuilds the union over - every subscriber on each `Subscriber.update`, so a preference-only update - costs the whole audience there; give it the incremental union - `rs/moq-net/src/model/track/demand/ranges.rs` keeps. - -Recommended: key the model's ranges and their resolutions by an id the -caller assigns, so a publisher passes the edit straight through, and measure -an update stream against a 16k-range subscription before and after. + every subscriber on each `Subscriber.update`; give it the incremental union + `rs/moq-net/src/model/track/demand/ranges.rs` keeps, and mirror the Rust + edit path in `js/net/src/lite/publisher.ts` and `lite/subscribe.ts`. The rework is internal-only: no public API changes in `moq-net`, `moq-ffi`, or -`@moq/net` (checked when the range union fix landed, the condition for leaving -this to m2). Keep it that way: +`@moq/net`. Keep it that way: -- Rust: add a crate-private edit entry point on `track::Subscriber` (retired - ids, added `(id, Range)` pairs, and the preferences) beside the public - `update(Subscription)`. The per-subscriber ranges and the window's - resolutions become keyed; `Window` is `pub(in crate::model)`, and - `Resolved`, `resolved()`, and `Preferences` are crate-private already. The - public `update` stays a whole-set replacement, costing what its caller - passes, and `subscription()` keeps returning ranges in the order last given. - `Subscription`, `Range`, and the aggregate getters keep their shapes. +- Rust: the edit entry point stays crate-private; the public `update` stays a + whole-set replacement and `subscription()` keeps returning ranges in the + order last given. - JS: reach the edit through an internal `hooks.*` accessor, as the lite publisher already does for the track, and keep the per-producer union incremental behind the existing `Subscription` signal. `Subscriber.update` diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index 920486d547d..785a9d40a27 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -7,10 +7,15 @@ //! 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_update` feeds a lite-07 subscription of `ranges` one-group ranges a stream of +//! SUBSCRIBE_UPDATEs, each retiring the `edited` oldest ranges and adding as many, then serving +//! until parked; `steer` moves only the priority. A cost that grows with the live ranges +//! rather than the edit shows up as a slope over `ranges`. +//! //! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; -use moq_net::fuzz::CoverageBench; +use moq_net::fuzz::{CoverageBench, UpdateBench}; /// Ranges the source gave up before the wake. const SKIPPED: [u64; 4] = [1, 32, 1024, 8192]; @@ -38,5 +43,31 @@ fn bench_covered_before_start(c: &mut Criterion) { group.finish(); } -criterion_group!(benches, bench_covered_before_start); +/// Live ranges in the subscription, up to about the most one-group ranges a SUBSCRIBE fits. +const LIVE: [u64; 3] = [16, 512, 8192]; + +/// Ranges each update retires, and adds. +const EDITED: [u64; 2] = [1, 16]; + +fn bench_update(c: &mut Criterion) { + let mut group = c.benchmark_group("publisher_update"); + group.throughput(Throughput::Elements(1)); + for ranges in LIVE { + for edited in EDITED { + let mut run = None; + group.bench_function(BenchmarkId::new(format!("edit{edited}"), ranges), |b| { + let run = run.get_or_insert_with(|| UpdateBench::new(ranges)); + b.iter(|| run.edit(edited)); + }); + } + let mut run = None; + group.bench_function(BenchmarkId::new("steer", ranges), |b| { + let run = run.get_or_insert_with(|| UpdateBench::new(ranges)); + b.iter(|| run.steer()); + }); + } + group.finish(); +} + +criterion_group!(benches, bench_covered_before_start, bench_update); criterion_main!(benches); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 8add0465517..dbb0c24132c 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -22,7 +22,7 @@ pub use request::request; pub use session::{session_ietf, session_lite}; /// Lite-07 publisher run loops for the publisher bench. -pub use crate::lite::CoverageBench; +pub use crate::lite::{CoverageBench, UpdateBench}; use bytes::Bytes; diff --git a/rs/moq-net/src/lite/mod.rs b/rs/moq-net/src/lite/mod.rs index ab1c5e0515c..722cf362511 100644 --- a/rs/moq-net/src/lite/mod.rs +++ b/rs/moq-net/src/lite/mod.rs @@ -33,7 +33,7 @@ mod probe; #[cfg(feature = "async")] mod publisher; #[cfg(all(feature = "async", any(test, feature = "fuzz")))] -pub use publisher::CoverageBench; +pub use publisher::{CoverageBench, UpdateBench}; #[cfg(feature = "async")] mod replay; #[cfg(feature = "async")] diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 36c80eb5f03..610c0ee775c 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1678,6 +1678,13 @@ trait Resolve: for<'a> Decode<'a, Version> + std::fmt::Debug { fn state(msg: &M, version: Version) -> Self::State; fn resolve(self, state: &mut Self::State) -> Result; + + /// `earlier` followed by `later`, for updates that wait on the reply to start: by default + /// the later replaces the earlier. + fn then(earlier: Self::Resolved, later: Self::Resolved) -> Self::Resolved { + let _ = earlier; + later + } } /// The update a request applies. @@ -1719,10 +1726,20 @@ impl Resolve> for lite::SubscribeUpdate { let ranges = live.apply(edit)?; Ok(self.with_ranges(ranges)) } + + fn then(earlier: LiveUpdate, later: LiveUpdate) -> LiveUpdate { + then(earlier, later) + } } -/// A SUBSCRIBE_UPDATE resolved to every live range, by Range ID. -type LiveUpdate = lite::SubscribeUpdate; +/// A SUBSCRIBE_UPDATE resolved by Range ID. +type LiveUpdate = lite::SubscribeUpdate; + +/// `earlier` followed by `later`: the later's preferences, and both edits as one. +fn then(earlier: LiveUpdate, mut later: LiveUpdate) -> LiveUpdate { + let ranges = std::mem::replace(&mut later.ranges, lite::Applied::Replace(Vec::new())); + later.with_ranges(earlier.ranges.then(ranges)) +} /// Serves one request stream: decode the request, resolve its broadcast, drive the /// kind's reply, then FIN. @@ -1744,8 +1761,7 @@ struct RequestServe { _working: Option, // What resolves each update, seeded once the request decodes. history: Option<>::State>, - // The latest update to arrive before the reply started. Each one, resolved, replaces the - // last. + // The updates that arrived before the reply started, folded into one. update: Option>, /// Ends the request, even mid-reply, once the session stops allowing its broadcast. gate: Option, @@ -2085,7 +2101,12 @@ impl RequestServe { fn apply(&mut self, update: Resolved) { match &mut self.state { RequestState::Serve(request) => request.update(update), - _ => self.update = Some(update), + _ => { + self.update = Some(match self.update.take() { + Some(earlier) => >::then(earlier, update), + None => update, + }) + } } } } @@ -2389,7 +2410,12 @@ impl Request for SubscribeServe { fn update(&mut self, update: LiveUpdate) { match self { - Self::Confirm { update: pending, .. } => *pending = Some(update), + Self::Confirm { update: pending, .. } => { + *pending = Some(match pending.take() { + Some(earlier) => then(earlier, update), + None => update, + }) + } Self::Run(run) => run.update(update), Self::Datagrams { consumer, @@ -4669,6 +4695,101 @@ impl Bounds { } } +/// A subscription's requested ranges in the model's whole groups, by Range ID, with the +/// bounds the read cursor takes from them counted, so an edit moves them in what it costs. +#[derive(Debug, Default)] +struct Requested { + ranges: std::collections::BTreeMap, + /// The time starts of the non-empty ranges, and how many of them start otherwise. + starts: std::collections::BTreeMap, + unnamed: usize, + /// The time ends of the non-empty ranges, and how many of them end otherwise. + ends: std::collections::BTreeMap, + unbounded: usize, + /// The ranges with no end, empty ones included. + open: usize, +} + +impl Requested { + fn new(ranges: impl IntoIterator) -> Self { + let mut requested = Self::default(); + for (id, range) in ranges { + requested.insert(id, range); + } + requested + } + + fn insert(&mut self, id: u64, range: crate::track::Range) { + self.remove(id); + self.count(&range, 1); + self.ranges.insert(id, range); + } + + fn remove(&mut self, id: u64) { + if let Some(range) = self.ranges.remove(&id) { + self.count(&range, -1); + } + } + + fn count(&mut self, range: &crate::track::Range, delta: isize) { + use crate::track::Anchor; + let tally = |counts: &mut std::collections::BTreeMap, key: u64| { + let count = counts.entry(key).or_default(); + *count = count.checked_add_signed(delta).expect("a counted range"); + if *count == 0 { + counts.remove(&key); + } + }; + let shift = |count: &mut usize| *count = count.checked_add_signed(delta).expect("a counted range"); + if range.end.is_none() { + shift(&mut self.open); + } + if range.is_empty() { + return; + } + match range.start { + Anchor::Time(start) => tally(&mut self.starts, start), + _ => shift(&mut self.unnamed), + } + match range.end { + Some(Anchor::Time(last)) => tally(&mut self.ends, last), + _ => shift(&mut self.unbounded), + } + } + + /// The lowest group the ranges name, `None` unless each names its start; see + /// [`crate::track::Subscription::start`]. + fn start(&self) -> Option { + match self.unnamed { + 0 => self.starts.first_key_value().map(|(start, _)| *start), + _ => None, + } + } + + /// The exclusive end past every range, `None` when one is open or counts back; see + /// [`crate::track::Subscription::end`]. + fn end(&self) -> Option { + match self.unbounded { + 0 => self + .ends + .last_key_value() + .map_or(Some(0), |(last, _)| last.checked_add(1)), + _ => None, + } + } + + /// Whether every range has an end; see [`Bounds::ends`]. + fn bounded(&self) -> bool { + self.open == 0 + } + + /// One past the highest Range ID held. + #[cfg(test)] + fn next(&self) -> u64 { + self.ranges.last_key_value().map_or(0, |(id, _)| id + 1) + } +} + /// The part of each group's rank a SUBSCRIBE_UPDATE can move. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] struct Steer { @@ -4844,15 +4965,12 @@ struct TrackRun { steer_tx: kio::Producer, // The frame bounds of the requested ranges, which the model leaves to `position_group`. frames: Frames, - // The lowest group the requested ranges name, `None` unless each names its start: kept - // with the ranges, so a wake reads it without copying every range. - named: Option, + /// The requested ranges in the model's whole groups, by Range ID, and what the read + /// cursor needs of them, kept current per edit. + requested: Requested, // A SUBSCRIBE_UPDATE this track cannot serve, such as a time anchor on a track the // peer was told is untimed, which ends the subscription. refused: Option, - /// The live ranges the last update applied and the model subscription made of them, so - /// an update leaving every range live costs nothing for them. - applied: Option<(lite::Snapshot, crate::track::Subscription)>, // Where delivery starts, which SUBSCRIBE_START reports. start: Start, end_sent: bool, @@ -4875,13 +4993,14 @@ struct TrackRun { idle: Option>, /// Lite-05/06: the name's datagram track, which the subscription carries too. datagrams: Option, - /// The Range ID of each requested range, in the order the model holds them. - ids: Vec, /// How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. initial: u64, /// Lite-07: the ranges an update added whose start no SUBSCRIBE_STARTED reported yet, by /// Range ID. - unstarted: Vec, + unstarted: std::collections::BTreeSet, + /// Lite-07: the ranges whose relative or time anchors may still resolve; see + /// [`Self::pin`]. + unpinned: Unpinned, /// Lite-07: the skipped runs not sent as SUBSCRIBE_SKIP, each by the start of the cached /// group just above it, whose GROUP's Previous Start passes over the run instead. Each /// waits for that group to be served, when its stream carries the run, or given up, when @@ -4892,6 +5011,56 @@ struct TrackRun { recheck_elided: bool, } +/// Lite-07 ranges whose relative or time anchors may still resolve, by Range ID, each due +/// once the latest group reaches the lowest start that could resolve it; see [`TrackRun::pin`]. +#[derive(Debug, Default)] +struct Unpinned { + due: std::collections::BTreeMap, + by_due: std::collections::BTreeSet<(u64, u64)>, +} + +impl Unpinned { + /// Hold range `id` until `due`, or let it go for `None`. + fn insert(&mut self, id: u64, due: Option) { + self.remove(id); + if let Some(due) = due { + self.due.insert(id, due); + self.by_due.insert((due, id)); + } + } + + fn remove(&mut self, id: u64) { + if let Some(due) = self.due.remove(&id) { + self.by_due.remove(&(due, id)); + } + } + + /// The ranges due once the latest group starts at `latest`. + fn due(&self, latest: u64) -> Vec { + self.by_due.range(..=(latest, u64::MAX)).map(|(_, id)| *id).collect() + } +} + +/// The lowest latest group start that could resolve `range`'s next relative or time anchor: +/// its start unless `started`, then its end. `None` once neither can move. +/// +/// A start may resolve once any group is there, even a time start above every group, which +/// the latest group may contain. A time end settles once a group starts past it. +fn due(range: &crate::track::Range, started: bool) -> Option { + use crate::track::Anchor; + if range.is_empty() { + return None; + } + if !started && matches!(range.start, Anchor::Relative(_) | Anchor::Time(_)) { + return Some(0); + } + match range.end { + Some(Anchor::Relative(_)) => Some(0), + Some(Anchor::Time(last)) => Some(last.saturating_add(1)), + _ => None, + } +} + impl TrackRun { fn new( ctx: Subscription, @@ -4918,17 +5087,22 @@ impl TrackRun { let bounded = bounds.ends(ctx.version); let initial = bounds.ranges.len() as u64; + let mut unpinned = Unpinned::default(); + if ctx.version.has_ranges() { + for (id, range) in (0..).zip(&bounds.ranges) { + unpinned.insert(id, due(range, false)); + } + } Self { - ids: (0..initial).collect(), initial, - unstarted: Vec::new(), + unstarted: Default::default(), + unpinned, elided: Default::default(), recheck_elided: false, bounded, + requested: Requested::new((0..).zip(bounds.ranges.iter().copied())), frames: bounds.frames, - named: groups.start(), refused: None, - applied: None, ctx, track, floor, @@ -4991,50 +5165,17 @@ impl TrackRun { min: upd.min_delay, max: upd.max_delay, }; - // Every range still live as last applied: the model's ranges stand, and only the - // preferences beside them may move. - if let Some((ranges, model)) = &self.applied - && std::sync::Arc::ptr_eq(ranges, &upd.ranges) - { - if (model.priority, model.delay, model.order) == (upd.priority, delay, upd.order) { - return; - } - let model = crate::track::Subscription { - priority: upd.priority, - delay, - order: upd.order, - ..model.clone() - }; - match self.track.update(model.clone()) { - Ok(()) => self.applied = Some((upd.ranges, model)), - Err(err) => self.refused = Some(err), - } - return; - } - // Feed the full update into the model subscriber so the producer's - // aggregate reflects it (and a relay re-forwards it upstream). - // Read first: `update` replaces these preferences. - let requested = self.named; - let floored = requested.is_some_and(|group| group > 0); - let (ids, ranges): (Vec, Vec) = upd.ranges.iter().copied().unzip(); - let ranges = self.pin(&ids, ranges); - let bounds = Bounds::new(&ranges, !self.ctx.version.has_ranges()); - - let groups = bounds.groups(); - let (start, end) = (groups.start(), groups.end()); - // A refusal leaves the model's ranges as they were, so this side keeps them too. - let model = crate::track::Subscription { - priority: upd.priority, - delay, - order: upd.order, - ..groups + // Read first: the edit moves it. + let requested = self.requested.start(); + let edited = match upd.ranges { + lite::Applied::Edit { retired, added } => self.edit(retired, added, upd.priority, delay, upd.order), + lite::Applied::Replace(ranges) => self.replace_ranges(ranges, upd.priority, delay, upd.order), }; - if let Err(err) = self.track.update(model.clone()) { - self.refused = Some(err); + if !edited { return; } - self.applied = Some((upd.ranges.clone(), model)); - self.named = start; + let floored = requested.is_some_and(|group| group > 0); + let (start, end) = (self.requested.start(), self.requested.end()); // An explicit start moves the read cursor. Lite-06+ encodes an absent // start as no floor, so a subscription that had one above group 0 has to // drop back to 0: the cursor only rises on its own, and a finished group @@ -5073,53 +5214,144 @@ impl TrackRun { if (self.floor, self.cap) != range { self.move_cursor(); } - self.bounded = bounds.ends(self.ctx.version); - self.frames = bounds.frames; - if self.ctx.version.has_ranges() { - // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. - // Both list Range IDs in ascending order, since each one assigned is the next. - self.unstarted.retain(|id| ids.binary_search(id).is_ok()); - let added = ids - .iter() - .filter(|id| **id >= self.initial && self.ids.binary_search(id).is_err()); - self.unstarted.extend(added); - } - self.ids = ids; + self.bounded = self.ctx.version.has_ranges() && self.requested.bounded(); } - /// Where each live Range ID sits among the model's ranges. - fn positions(&self) -> HashMap { - self.ids.iter().enumerate().map(|(index, id)| (*id, index)).collect() + /// Lite-07: retire the ranges with Range IDs `retired` and add `added`, passing the edit + /// straight to the model, so it costs the ranges it names whatever else is live. Returns + /// whether the ranges moved. + /// + /// A range left live keeps where it resolved: the model keeps each by its Range ID, so it + /// never counts relative anchors back from a newer latest group, nor looks a time start up + /// again in newer groups. + fn edit( + &mut self, + retired: Vec, + added: Vec<(u64, lite::Range)>, + priority: i8, + delay: crate::track::Delay, + order: Order, + ) -> bool { + let added: Vec<(u64, crate::track::Range)> = added + .into_iter() + .map(|(id, range)| (id, Bounds::new(std::slice::from_ref(&range), false).ranges[0])) + .collect(); + for id in &retired { + self.unpinned.remove(*id); + } + let pins = self.pin(); + let moved = !retired.is_empty() || !added.is_empty() || !pins.is_empty(); + // A pinned range is replaced in place, keeping its Range ID. + let edit = crate::track::Edit { + retire: retired.iter().copied().chain(pins.iter().map(|(id, _)| *id)).collect(), + add: pins.iter().chain(&added).copied().collect(), + priority, + delay, + order, + }; + // A refusal leaves the model's ranges as they were, so this side keeps them too. + if let Err(err) = self.track.edit(edit) { + self.refused = Some(err); + return false; + } + for id in &retired { + self.requested.remove(*id); + // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. + self.unstarted.remove(id); + } + for (id, range) in pins { + self.requested.insert(id, range); + } + for (id, range) in added { + self.requested.insert(id, range); + self.unpinned.insert(id, due(&range, false)); + if id >= self.initial { + self.unstarted.insert(id); + } + } + moved } - /// `ranges`, numbered by `ids`, with each range this subscription already held resolved - /// as it was: a range resolves once, so an update that leaves it live never counts its - /// relative anchors back from a newer latest group, nor looks a time start up again in - /// newer groups. A start reaching back to the oldest group held stays as it was, since an - /// absolute start would turn it into a floor the subscriber never named. Lite-07 only: an - /// older update replaces every range. - fn pin(&mut self, ids: &[u64], mut ranges: Vec) -> Vec { - if !self.ctx.version.has_ranges() { - return ranges; - } - let resolved = self.track.resolved(); - let positions = self.positions(); - for (id, range) in ids.iter().zip(&mut ranges) { - let Some(resolved) = positions - .get(id) - .and_then(|index| resolved.get(*index).copied().flatten()) - else { + /// Each live range whose relative or time anchors resolved since the last update, with + /// them replaced by the groups they resolved to: a range resolves once, so an update that + /// leaves it live never counts its relative anchors back from a newer latest group, nor + /// looks a time start up again in newer groups, here or on a relay's copies, and its + /// start names the group it resolved to for the read cursor's floor. A start reaching + /// back to the oldest group held stays as it was, since an absolute start would turn it + /// into a floor the subscriber never named. + /// + /// Only the ranges still waiting on a start, or on an end a group passed, are asked (see + /// [`due`]), once for the batch, and each is let go once its anchors settle: this costs + /// the ranges still waiting rather than every range. + fn pin(&mut self) -> Vec<(u64, crate::track::Range)> { + use crate::track::{Anchor, Range}; + let Some(latest) = self.track.latest_key() else { + return Vec::new(); + }; + let mut pins = Vec::new(); + let asked = self.unpinned.due(latest); + let resolved = self.track.resolved_keys(asked.iter().copied()); + for (id, resolved) in asked.into_iter().zip(resolved) { + let Some(range) = self.requested.ranges.get(&id).copied() else { + self.unpinned.remove(id); continue; }; - if resolved.floored && matches!(range.start, Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_))) { - range.start = Some(lite::Anchor::Absolute(track::Position::group(resolved.start))); - } - // An end resolves once it settles; until then it is reported open. - if let (Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_)), Some(end)) = (range.end, resolved.end) { - range.end = Some(lite::Anchor::Absolute(track::Position::group(end))); + let Some(resolved) = resolved else { + continue; + }; + let start = match range.start { + Anchor::Relative(_) | Anchor::Time(_) if resolved.floored => Anchor::Time(resolved.start), + start => start, + }; + // An end resolves once it settles; until then it stays as given. One settled at + // group 0 holds nothing. + let pinned = match (range.end, resolved.end) { + (Some(Anchor::Relative(_) | Anchor::Time(_)), Some(end)) => match end.checked_sub(1) { + Some(last) => Range { + start, + end: Some(Anchor::Time(last)), + }, + None => Range::EMPTY, + }, + (end, _) => Range { start, end }, + }; + // The start settled; the end is due again once it may settle. + let next = match resolved.end { + Some(_) => None, + None => due(&pinned, true), + }; + self.unpinned.insert(id, next); + if pinned != range { + pins.push((id, pinned)); } } - ranges + pins + } + + /// Before lite-07: replace every range, as an update there does. Returns whether it applied. + fn replace_ranges( + &mut self, + ranges: Vec<(u64, lite::Range)>, + priority: i8, + delay: crate::track::Delay, + order: Order, + ) -> bool { + let (ids, ranges): (Vec, Vec) = ranges.into_iter().unzip(); + let bounds = Bounds::new(&ranges, !self.ctx.version.has_ranges()); + let model = crate::track::Subscription { + priority, + delay, + order, + ..bounds.groups() + }; + // A refusal leaves the model's ranges as they were, so this side keeps them too. + if let Err(err) = self.track.update(model) { + self.refused = Some(err); + return false; + } + self.requested = Requested::new(ids.into_iter().zip(bounds.ranges.iter().copied())); + self.frames = bounds.frames; + true } /// Report where each range an update added starts, once it resolves, or with no start @@ -5136,20 +5368,19 @@ impl TrackRun { if self.unstarted.is_empty() { return Ok(false); } - let resolved = self.track.resolved(); - let positions = self.positions(); - let mut waiting = Vec::new(); + let mut waiting = std::collections::BTreeSet::new(); let mut reported = false; - for id in std::mem::take(&mut self.unstarted) { - let index = *positions.get(&id).expect("unstarted ranges are live"); - let start = match resolved.get(index).copied().flatten() { + let unstarted = std::mem::take(&mut self.unstarted); + let resolved = self.track.resolved_keys(unstarted.iter().copied()); + for (id, resolved) in unstarted.into_iter().zip(resolved) { + let start = match resolved { Some(resolved) => { let start = resolved.start.max(from); resolved.end.is_none_or(|end| end > start).then_some(start) } None if finished => None, None => { - waiting.push(id); + waiting.insert(id); continue; } }; @@ -5164,8 +5395,15 @@ impl TrackRun { /// lite-07 does. #[cfg(test)] fn replace(&mut self, update: lite::SubscribeUpdate>) { - let next = self.ids.iter().max().map_or(0, |last| last + 1).max(self.initial); - let ranges = (next..).zip(update.ranges.iter().copied()).collect(); + let next = self.requested.next().max(self.initial); + let added = (next..).zip(update.ranges.iter().copied()).collect(); + let ranges = match self.ctx.version.has_ranges() { + true => lite::Applied::Edit { + retired: self.requested.ranges.keys().copied().collect(), + added, + }, + false => lite::Applied::Replace(added), + }; self.update(update.with_ranges(ranges)); } @@ -5292,7 +5530,7 @@ impl TrackRun { // what it holds against the answer. if self.start.is_unresolved() && self.ctx.version.has_largest() - && let Some(start) = self.named + && let Some(start) = self.requested.start() && let Poll::Ready(Some(largest)) = self.track.poll_live(waiter) && (track::Position { group: start, @@ -5656,10 +5894,14 @@ impl TrackRun { } // SUBSCRIBE_START answers the SUBSCRIBE's ranges, by Range ID: none for one an update // retired already, and SUBSCRIBE_STARTED answers each one an update added. - let starts = self.track.starts(start); - let positions = self.positions(); - (0..self.initial) - .map(|id| starts.get(*positions.get(&id)?).copied().flatten()) + self.track + .resolved_keys(0..self.initial) + .into_iter() + .map(|resolved| { + let resolved = resolved?; + let from = resolved.start.max(start); + resolved.end.is_none_or(|end| end > from).then_some(from) + }) .collect() } @@ -5698,7 +5940,7 @@ impl TrackRun { // floor. Lite-06 encodes a floor of group 0 as 0, which decodes as no named floor; // that 0 is group 0, so it is not pinned. Raised, not assigned: an update that // landed while the group was held may already have raised it past. - if self.named.is_none() && !self.ctx.version.resolves_start() { + if self.requested.start().is_none() && !self.ctx.version.resolves_start() { self.raise_floor(start); } self.serve(group); @@ -5991,6 +6233,121 @@ impl CoverageBench { } } +/// A lite-07 subscription's run loop taking a stream of SUBSCRIBE_UPDATEs, for the +/// `publisher` bench: each edit retires the oldest ranges and adds as many new ones, so the +/// live set keeps its size and an update's cost shows up as a slope over it. +#[cfg(any(test, feature = "fuzz"))] +pub struct UpdateBench { + run: TrackRun, + live: lite::LiveRanges, + writer: Writer, + /// The oldest live Range ID. + oldest: u64, + priority: u8, + _track: track::Producer, + _peer: crate::transport::memory::Session, +} + +#[cfg(any(test, feature = "fuzz"))] +impl UpdateBench { + /// Subscribe to `ranges` one-group time ranges on a track whose groups start at their keys, + /// so every range resolves without a group held and nothing is served. + pub fn new(ranges: u64) -> Self { + let version = Version::Lite07; + let (local, peer) = crate::transport::memory::pair(Default::default()); + let session = Transport::new(local); + let Poll::Ready(Ok(stream)) = + session.poll_open_uni(&mut Default::default(), &kio::Waiter::noop(), SendOptions::default()) + else { + panic!("a memory stream opens at once"); + }; + let wire: Vec<_> = (0..ranges).map(Self::range).collect(); + let bounds = Bounds::new(&wire, false); + let track = track::Producer::new( + Arc::new(crate::broadcast::Info::default()), + "bench", + track::Info::default() + .with_timescale(crate::Timescale::MILLI) + .with_max_age(None), + ); + track.starts_at_keys(); + let subscriber = track.subscribe(bounds.groups()); + let steer = kio::Producer::new(Steer::default()); + let ctx = Subscription { + session, + id: 0, + track_name: "bench".into(), + priority: PriorityQueue::default(), + publisher: 0, + group: None, + steer: steer.consume(), + steer_seen: Steer::default(), + version, + stamp: None, + outcomes: Default::default(), + object_records: false, + }; + let mut this = Self { + run: TrackRun::new(ctx, subscriber, bounds, 0, steer), + live: lite::LiveRanges::new(version, &wire), + writer: Writer::new(stream, version), + oldest: 0, + priority: 0, + _track: track, + _peer: peer, + }; + this.settle(); + this + } + + /// The one-group range Range ID `id` asks for, each a group of its own while the live + /// set stays under 8K ranges, small enough that 8K of them fit one SUBSCRIBE. + fn range(id: u64) -> lite::Range { + let time = Some(lite::Anchor::Time(2 * id % (1 << 14))); + lite::Range { start: time, end: time } + } + + /// Retire the `edited` oldest ranges and add as many new ones, then serve until parked. + pub fn edit(&mut self, edited: u64) { + let remove = (self.oldest..self.oldest + edited).collect(); + self.oldest += edited; + let next = self.live.next(); + let add = (next..next + edited).map(Self::range).collect(); + self.apply(lite::RangeEdit::Edit { remove, add }, 0); + } + + /// Move only the priority, leaving every range live. + pub fn steer(&mut self) { + self.priority ^= 1; + self.apply(self.live.unchanged(), self.priority); + } + + fn apply(&mut self, ranges: lite::RangeEdit, priority: u8) { + let update = lite::SubscribeUpdate { + wanted_group: None, + priority: priority as i8, + max_delay: Some(Duration::from_secs(3600)), + min_delay: None, + ranges, + order: Order::Ascending, + }; + let update = >>::resolve(update, &mut self.live) + .expect("a valid edit"); + self.run.update(update); + self.settle(); + } + + fn settle(&mut self) { + loop { + match self.run.poll_step(&mut self.writer, &kio::Waiter::noop()) { + Poll::Ready(Ok(ControlFlow::Continue(()))) => continue, + Poll::Ready(res) => panic!("the subscription ended: {res:?}"), + Poll::Pending => return, + } + } + } +} + /// Where a group stream is ranked. enum Rank { /// In the subscription's send group, by its key in the subscription's order. @@ -8001,7 +8358,6 @@ mod serve_group_test { let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); // The SUBSCRIBE carried the subscriber's four ranges. run.initial = 4; - run.ids = (0..4).collect(); let mut run = std::pin::pin!(kio::wait(move |waiter| run.poll(&mut writer, waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); @@ -8032,21 +8388,40 @@ mod serve_group_test { responses } - /// A lite-07 update that keeps every live range and adds `added`, each taking the next - /// Range ID after `next`. - fn lite07_add(live: &[(u64, lite::Range)], next: u64, added: &[lite::Range]) -> LiveUpdate { - let mut ranges = live.to_vec(); - ranges.extend((next..).zip(added.iter().copied())); + /// A lite-07 update that keeps the ranges of `live` and adds `added`, each taking the next + /// Range ID from `next`: every other range `run` holds is retired. + fn lite07_add(run: &TrackRun, live: &[(u64, lite::Range)], next: u64, added: &[lite::Range]) -> LiveUpdate { + let kept: Vec = live.iter().map(|(id, _)| *id).collect(); lite::SubscribeUpdate { wanted_group: None, priority: 0, max_delay: Some(Duration::from_secs(3600)), min_delay: None, - ranges: ranges.into(), + ranges: lite::Applied::Edit { + retired: run + .requested + .ranges + .keys() + .filter(|id| !kept.contains(id)) + .copied() + .collect(), + added: (next..).zip(added.iter().copied()).collect(), + }, order: Order::Descending, } } + /// An update to `version` that keeps the one range the subscription holds, `range`. + fn kept_range(version: Version, range: lite::Range) -> lite::Applied { + match version.has_ranges() { + true => lite::Applied::Edit { + retired: Vec::new(), + added: Vec::new(), + }, + false => lite::Applied::Replace(vec![(0, range)]), + } + } + /// A range an update adds learns where it starts from SUBSCRIBE_STARTED, after /// SUBSCRIBE_START, and keeps that start however many later updates leave it live. #[kio::test] @@ -8077,7 +8452,7 @@ mod serve_group_test { end: None, }; let live = [(0, first)]; - run.update(lite07_add(&live, 1, &[back])); + run.update(lite07_add(&run, &live, 1, &[back])); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} let started: Vec<_> = responses(&mut writer, &log) .await @@ -8094,9 +8469,62 @@ mod serve_group_test { write_group(&mut track, key, key * 1000); } let live = [(0, first), (1, back)]; - run.update(lite07_add(&live, 2, &[lite::Range::span(Some(9), 0, None, None)])); - let resolved = run.track.resolved()[1].map(|r| (r.start, r.end)); + run.update(lite07_add(&run, &live, 2, &[lite::Range::span(Some(9), 0, None, None)])); + let resolved = run.track.resolved_range(1).map(|r| (r.start, r.end)); assert_eq!(resolved, Some((2, None)), "the update kept where it started"); + // The model holds it pinned to that group, so a relay's copies, which take the whole + // preferences, resolve it there too. + assert_eq!( + run.track.subscription().ranges[1], + track::Range { + start: track::Anchor::Time(2), + end: None, + } + ); + } + + /// A time start names the group it resolved to as the read cursor's floor, not its time, + /// once an update leaves it live: the group containing the time starts below it. + #[kio::test] + async fn lite07_a_time_start_floors_at_the_group_it_resolved_to() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + write_group(&mut track, 4000, 4000); + write_group(&mut track, 6000, 6000); + let served = lite::Range::span(Some(6000), 0, Some(6000), None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(6000..6001), + ); + let session = Log::default(); + let (mut run, mut writer, _log) = lite07_run(SinkSession::new(session.clone()), subscriber); + while run.start.resolved().is_none() { + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + } + // 4.5 s falls in the group at 4 s. + let time = lite::Range { + start: Some(lite::Anchor::Time(4500)), + end: None, + }; + run.update(lite07_add(&run, &[(0, served)], 1, &[time])); + let live = [(0, served), (1, time)]; + run.update(lite07_add( + &run, + &live, + 2, + &[lite::Range::span(Some(9000), 0, None, None)], + )); + assert_eq!(run.floor, 4000); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let served: Vec = group_headers(&session, Version::Lite07) + .into_iter() + .map(|(start, _)| start) + .collect(); + assert!(served.contains(&4000), "served {served:?}"); } /// An update the model refuses, here a relative start that a group-only copy's cache @@ -8129,7 +8557,7 @@ mod serve_group_test { start: Some(lite::Anchor::Relative(2)), end: Some(lite::Anchor::Time(100_000)), }; - run.update(lite07_add(&[], 1, &[later])); + run.update(lite07_add(&run, &[], 1, &[later])); let result = (0..100) .find_map(|_| match run.poll_step(&mut writer, &kio::Waiter::noop()) { Poll::Ready(Err(err)) => Some(err), @@ -8165,14 +8593,19 @@ mod serve_group_test { start: Some(lite::Anchor::Time(time)), end: None, }; - run.update(lite07_add(&[(0, served)], 1, &[time])); + run.update(lite07_add(&run, &[(0, served)], 1, &[time])); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} - let before = run.track.resolved()[1].expect("resolved"); + let before = run.track.resolved_range(1).expect("resolved"); write_group(&mut track, newer, newer); let live = [(0, served), (1, time)]; - run.update(lite07_add(&live, 2, &[lite::Range::span(Some(99), 0, None, None)])); - let after = run.track.resolved()[1].expect("resolved"); + run.update(lite07_add( + &run, + &live, + 2, + &[lite::Range::span(Some(99), 0, None, None)], + )); + let after = run.track.resolved_range(1).expect("resolved"); [(before.start, before.floored), (after.start, after.floored)] } @@ -8200,6 +8633,44 @@ mod serve_group_test { ); } + /// A time start above every group held resolves to the latest group, which contains it, + /// and floors there once an update leaves it live, before any group is served. + #[kio::test] + async fn lite07_a_time_start_in_the_latest_group_floors_there() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + write_group(&mut track, 4000, 4000); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(0..), + ); + let session = Log::default(); + let (mut run, mut writer, _log) = lite07_run(SinkSession::new(session.clone()), subscriber); + let time = lite::Range { + start: Some(lite::Anchor::Time(4500)), + end: None, + }; + run.update(lite07_add(&run, &[], 1, &[time])); + assert_eq!(run.track.resolved_range(1).map(|r| r.start), Some(4000)); + run.update(lite07_add( + &run, + &[(1, time)], + 2, + &[lite::Range::span(Some(9000), 0, None, None)], + )); + assert_eq!(run.floor, 4000); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let served: Vec = group_headers(&session, Version::Lite07) + .into_iter() + .map(|(start, _)| start) + .collect(); + assert!(served.contains(&4000), "served {served:?}"); + } + /// A range retired before SUBSCRIBE_STARTED reported it gets none, and one still waiting /// once the track ends is reported with no start. #[kio::test] @@ -8219,8 +8690,8 @@ mod serve_group_test { let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); let added = lite::Range::span(Some(4), 0, None, None); // Added and retired before the run reported it. - run.update(lite07_add(&[(0, first)], 1, &[added])); - run.update(lite07_add(&[(0, first)], 2, &[])); + run.update(lite07_add(&run, &[(0, first)], 1, &[added])); + run.update(lite07_add(&run, &[(0, first)], 2, &[])); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} assert!(run.start.resolved().is_some()); assert!( @@ -8657,7 +9128,7 @@ mod serve_group_test { priority: 0, max_delay: Some(if widen { Duration::from_secs(10) } else { budget }), min_delay: Some(Duration::ZERO), - ranges: [(0, lite::Range::span(None, 0, None, None))].into(), + ranges: kept_range(version, lite::Range::span(None, 0, None, None)), order: Order::Descending, }); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} @@ -8697,7 +9168,7 @@ mod serve_group_test { priority: 0, max_delay: Some(max_delay), min_delay: None, - ranges: [(0, lite::Range::span(None, 0, None, None))].into(), + ranges: kept_range(version, lite::Range::span(None, 0, None, None)), order: Order::Descending, }; diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 96781ecb8ed..c66f6083fe5 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -839,13 +839,46 @@ pub(crate) struct LiveRanges { size: usize, /// How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_OK reports. initial: u64, - /// The live ranges [`Self::apply`] last returned, until one changes, so an edit that - /// changes nothing costs nothing. - snapshot: Option, } -/// Every live range with its Range ID, shared while none changes. -pub(crate) type Snapshot = std::sync::Arc<[(u64, Range)]>; +/// A received update's ranges, resolved against the live ones by Range ID. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Applied { + /// Lite-07: the Range IDs retired, then each range added with its Range ID, so applying + /// it costs the ranges it names. + Edit { + retired: Vec, + added: Vec<(u64, Range)>, + }, + /// Before lite-07: every live range, numbered afresh. + Replace(Vec<(u64, Range)>), +} + +impl Applied { + /// This followed by `later`, as one: a range `later` retires that this added never was. + /// Holds no more than the ranges live before this and after `later`. + pub fn then(self, later: Self) -> Self { + match (self, later) { + ( + Self::Edit { mut retired, mut added }, + Self::Edit { + retired: gone, + added: came, + }, + ) => { + let gone: std::collections::BTreeSet = gone.into_iter().collect(); + // Each Range ID is added once, so one this added and `later` retired was never live. + let fresh: std::collections::BTreeSet = + added.iter().map(|(id, _)| *id).filter(|id| gone.contains(id)).collect(); + added.retain(|(id, _)| !fresh.contains(id)); + retired.extend(gone.difference(&fresh)); + added.extend(came); + Self::Edit { retired, added } + } + (_, later) => later, + } + } +} impl LiveRanges { /// The ranges a SUBSCRIBE carried. @@ -856,7 +889,6 @@ impl LiveRanges { live: Default::default(), size: 0, initial: ranges.len() as u64, - snapshot: None, }; live.add(ranges.iter().copied()); live @@ -864,10 +896,6 @@ impl LiveRanges { fn add(&mut self, ranges: impl IntoIterator) { let form: Form = self.version.into(); - let mut ranges = ranges.into_iter().peekable(); - if ranges.peek().is_some() { - self.snapshot = None; - } for range in ranges { // One past the varint's reach can't come off the wire, and is refused below. self.size = self.size.saturating_add(range_size(&range, form).unwrap_or(usize::MAX)); @@ -878,7 +906,6 @@ impl LiveRanges { fn remove(&mut self, id: u64) -> Option { let range = self.live.remove(&id)?; - self.snapshot = None; self.size = self .size .saturating_sub(range_size(&range, self.version.into()).unwrap_or(usize::MAX)); @@ -928,22 +955,24 @@ impl LiveRanges { self.initial } - /// Resolve a received edit, returning every live range with its Range ID: the same - /// snapshot as last time while nothing changed. - pub fn apply(&mut self, edit: RangeEdit) -> Result { - match edit { + /// Resolve a received edit by Range ID, costing the ranges it names. + pub fn apply(&mut self, edit: RangeEdit) -> Result { + let applied = match edit { RangeEdit::Replace(ranges) if !self.version.has_ranges() => { *self = Self::new(self.version, &ranges); + Applied::Replace(self.iter().map(|(id, range)| (id, *range)).collect()) } RangeEdit::Edit { remove, add } if self.version.has_ranges() => { - for id in remove { + for id in &remove { // An id never assigned, or already retired (including earlier in this list). - self.remove(id).ok_or(DecodeError::InvalidValue)?; + self.remove(*id).ok_or(DecodeError::InvalidValue)?; } + let added = (self.next..).zip(add.iter().copied()).collect(); self.add(add); + Applied::Edit { retired: remove, added } } _ => return Err(DecodeError::Version), - } + }; // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE // could carry like one too large to send. if self.live.is_empty() { @@ -952,11 +981,7 @@ impl LiveRanges { if self.size > super::message::MAX_MESSAGE_SIZE { return Err(DecodeError::TooMany); } - let live = &self.live; - let snapshot = self - .snapshot - .get_or_insert_with(|| live.iter().map(|(id, range)| (*id, *range)).collect()); - Ok(snapshot.clone()) + Ok(applied) } /// The edit that keeps the live ranges as they are. @@ -1766,12 +1791,8 @@ mod test { let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| SubscribeUpdate::decode(r, v)) .unwrap(); let mut live = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); - let resolved: Vec<_> = live - .apply(got.ranges) - .unwrap() - .iter() - .map(|(_, range)| *range) - .collect(); + live.apply(got.ranges).unwrap(); + let resolved: Vec<_> = live.iter().map(|(_, range)| *range).collect(); assert_eq!(resolved, msg.ranges); } @@ -1860,7 +1881,8 @@ mod test { let buf = update.encode_bytes(version).unwrap(); let got = crate::coding::decode_buf(&mut buf.as_ref(), version, |r, v| SubscribeUpdate::decode(r, v)).unwrap(); - assert_eq!(*live.apply(got.ranges).unwrap(), *sent.numbered()); + live.apply(got.ranges).unwrap(); + assert_eq!(live.numbered(), sent.numbered()); }; let edit = |remove: &[u64], add: &[Range]| RangeEdit::Edit { @@ -1915,29 +1937,41 @@ mod test { assert!(refused, "the live ranges are held to one SUBSCRIBE"); } - /// An edit that changes nothing hands back the snapshot it last did, so a stream of empty - /// updates copies nothing; one that changes a range takes a fresh one. + /// An edit resolves to the Range IDs it names, and edits waiting to apply fold into one + /// that names what stayed retired or added. #[test] - fn lite07_an_empty_edit_reuses_the_snapshot() { + fn lite07_an_edit_names_its_range_ids() { let mut live = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); - let empty = || RangeEdit::Edit { - remove: Vec::new(), - add: Vec::new(), - }; - let first = live.apply(empty()).unwrap(); - assert!(std::sync::Arc::ptr_eq(&first, &live.apply(empty()).unwrap())); - let range = Range { - start: absolute(4, 0), + let range = |group| Range { + start: absolute(group, 0), end: None, }; - let edited = live + let first = live .apply(RangeEdit::Edit { remove: vec![0], - add: vec![range], + add: vec![range(4), range(5)], + }) + .unwrap(); + assert_eq!( + first, + Applied::Edit { + retired: vec![0], + added: vec![(1, range(4)), (2, range(5))], + } + ); + let second = live + .apply(RangeEdit::Edit { + remove: vec![1], + add: vec![range(6)], }) .unwrap(); - assert_eq!(*edited, [(1, range)]); - assert!(std::sync::Arc::ptr_eq(&edited, &live.apply(empty()).unwrap())); + assert_eq!( + first.then(second), + Applied::Edit { + retired: vec![0], + added: vec![(2, range(5)), (3, range(6))], + } + ); } /// An edit to ranges too large for a SUBSCRIBE is refused before anything changes, so the diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 1c43339c028..dd06a439315 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -181,7 +181,7 @@ impl Producer { let mut ranges = Vec::new(); for reader in readers { let mut reader = reader.lock().unwrap_or_else(PoisonError::into_inner); - let preferences = reader.subscription.read().clone(); + let preferences = reader.subscription.read().subscription(); // A failed copy's resolution is no placement, as in `Reader::window`: the reader's // own stands, or the anchors do for ranges it has yet to take up. let failed = reader.route.end().is_none() @@ -462,14 +462,23 @@ impl Consumer { /// Subscribe to the logical track with the reader's own preferences. pub(super) fn subscribe(&self, subscription: track::Preferences) -> Subscriber { - let mirrored = subscription.read().clone(); - let window = Window::new(&mirrored.ranges, None, None, &Unheld).expect("an unheld hop refuses nothing"); + let (mirrored, keys, revision) = { + let prefs = subscription.read(); + ( + prefs.subscription(), + prefs.ranges().map(|(key, _)| key).collect::>(), + prefs.revision, + ) + }; + let window = keyed(&keys, &mirrored.ranges, &Unheld); let reader = Arc::new_cyclic(|this| { Mutex::new(Reader { this: this.clone(), route: self.clone(), subscription: subscription.clone(), mirrored, + keys, + revision, generation: None, copies: Vec::new(), newest: None, @@ -1009,14 +1018,11 @@ impl Subscriber { self.reader().window().start() } - /// Where each of the reader's ranges starts; see [`track::Subscriber::starts`]. - pub(super) fn starts(&self, from: u64) -> Vec> { - self.reader().window().starts(from) - } - - /// Each of the reader's ranges resolved; see [`track::Subscriber::resolved`]. - pub(super) fn resolved(&self) -> Vec> { - self.reader().window().resolved() + /// The reader's ranges keyed in `keys` resolved; see [`track::Subscriber::resolved_keys`]. + pub(super) fn resolved_keys(&self, keys: impl IntoIterator) -> Vec> { + let mut reader = self.reader(); + let window = reader.window(); + keys.into_iter().map(|key| window.resolved_key(key)).collect() } /// The lowest group at or above `from` the reader's ranges cover; see @@ -1224,6 +1230,11 @@ struct Reader { /// The reader's own preferences, mirrored into every copy's subscription. subscription: track::Preferences, mirrored: Subscription, + /// The key of each of [`Self::mirrored`]'s ranges, in order, so the reader's window answers + /// a range by the key an edit names it with. + keys: Vec, + /// The preferences' revision [`Self::mirrored`] holds. + revision: u64, /// The route generation last read. generation: Option, /// Oldest first, except that a backfill goes before the copy of the route it backfills: the @@ -1335,7 +1346,7 @@ impl Reader { if unplaced { return; } - let window = Window::new(&ranges, None, None, &Placed).expect("a placed hop refuses nothing"); + let window = keyed(&self.keys, &ranges, &Placed); if !window.is_pending() { self.window = window; } @@ -1349,16 +1360,26 @@ impl Reader { // Snapshot and re-arm together, including when a change was observed: another // update must wake a reader that parks after applying this one. let mut changed = None; + let mirrored = &self.mirrored; + let revision = self.revision; let _ = self.subscription.poll_ref(waiter, |sub| { - if **sub != self.mirrored { - changed = Some((**sub).clone()); + let moved = (sub.priority, sub.delay, sub.order, sub.feed) + != (mirrored.priority, mirrored.delay, mirrored.order, mirrored.feed); + if moved || sub.revision != revision { + let keys = (sub.revision != revision).then(|| sub.ranges().map(|(key, _)| key).collect()); + changed = Some((sub.subscription(), keys, sub.revision)); } Poll::<()>::Pending }); - if let Some(sub) = changed { - let replaced = sub.ranges != self.mirrored.ranges; + if let Some((sub, keys, revision)) = changed { + self.revision = revision; + let replaced = keys.is_some_and(|keys: Vec| { + let replaced = sub.ranges != self.mirrored.ranges || keys != self.keys; + self.keys = keys; + replaced + }); if replaced { - self.window = Window::new(&sub.ranges, None, None, &Unheld).expect("an unheld hop refuses nothing"); + self.window = keyed(&self.keys, &sub.ranges, &Unheld); // The serving copy's resume point may cut off groups the new ranges cover, judged // by the reader's window. self.resumed = Some((None, Vec::new())); @@ -2927,6 +2948,12 @@ impl kio::Task for Fetching { } } +/// A window over `ranges`, each keyed as `keys` names it, on a hop that refuses nothing. +fn keyed(keys: &[u64], ranges: &[track::Range], holdings: &impl Holdings) -> Window { + Window::keyed(keys.iter().copied().zip(ranges.iter().copied()), None, None, holdings) + .expect("a hop that holds nothing or holds it placed refuses nothing") +} + #[cfg(test)] mod test { use std::time::Duration; @@ -3552,7 +3579,7 @@ mod test { let mut read = drain(&mut joined, false); read.sort_unstable(); assert_eq!(read, [66, 67]); - assert_eq!(joined.resolved()[0].map(|resolved| resolved.start), Some(66)); + assert_eq!(joined.resolved_range(0).map(|resolved| resolved.start), Some(66)); drop(held); } @@ -3920,12 +3947,12 @@ mod test { // the oldest group rather than naming one. assert_eq!(sub.floor(), None, "routed={routed}"); assert_eq!( - sub.resolved(), - [Some(Resolved { + sub.resolved_range(0), + Some(Resolved { start: 0, end: Some(6), floored: false, - })], + }), "routed={routed}" ); assert_eq!(sub.runs_in_range(0, 10), [0..6], "routed={routed}"); @@ -4690,9 +4717,7 @@ mod test { write(&a, 2); a.skip_groups(3..5); routes.serve(a.consume()); - let sub = routes - .consume() - .subscribe(kio::Producer::new(Subscription::default()).into()); + let sub = routes.consume().subscribe(Subscription::default().into()); let b = copy(); write(&b, 4); write(&b, 5); @@ -7412,9 +7437,7 @@ mod test { let routes = Producer::new(); let a = copy(); routes.serve(a.consume()); - let sub = routes - .consume() - .subscribe(kio::Producer::new(Subscription::default()).into()); + let sub = routes.consume().subscribe(Subscription::default().into()); let mut reader = sub.reader.lock().unwrap(); let copy = &mut reader.copies[0]; copy.floor = Some(0); @@ -7440,9 +7463,7 @@ mod test { write(&a, sequence); } routes.serve(a.consume()); - let sub = routes - .consume() - .subscribe(kio::Producer::new(Subscription::default()).into()); + let sub = routes.consume().subscribe(Subscription::default().into()); let mut reader = sub.reader.lock().unwrap(); let copy = &mut reader.copies[0]; copy.floor = Some(0); @@ -7793,7 +7814,7 @@ mod test { let subscription = Subscription::default() .with_delay(Delay::default().with_max(Duration::from_secs(30))) .with_ranges([range]); - let sub = routes.consume().subscribe(kio::Producer::new(subscription).into()); + let sub = routes.consume().subscribe(subscription.into()); let mut reader = sub.reader.lock().unwrap(); let (groups, mirrored) = (reader.groups, reader.mirrored.clone()); let old = &mut reader.copies[0]; @@ -7848,7 +7869,7 @@ mod test { let subscription = Subscription::default() .with_delay(Delay::default().with_max(HOUR)) .with_ranges([range]); - let sub = routes.consume().subscribe(kio::Producer::new(subscription).into()); + let sub = routes.consume().subscribe(subscription.into()); let mut reader = sub.reader.lock().unwrap(); let (groups, mirrored) = (reader.groups, reader.mirrored.clone()); let old = &mut reader.copies[0]; @@ -7878,7 +7899,7 @@ mod test { let subscription = Subscription::default() .with_delay(Delay::default().with_max(HOUR)) .with_ranges([range]); - let sub = routes.consume().subscribe(kio::Producer::new(subscription).into()); + let sub = routes.consume().subscribe(subscription.into()); let mut reader = sub.reader.lock().unwrap(); let (groups, mirrored) = (reader.groups, reader.mirrored.clone()); let old = &mut reader.copies[0]; @@ -7903,8 +7924,7 @@ mod test { let routes = Producer::new(); let a = copy(); routes.serve(a.consume()); - let subscription = kio::Producer::new(Subscription::default()); - let sub = routes.consume().subscribe(subscription.into()); + let sub = routes.consume().subscribe(Subscription::default().into()); let mut reader = sub.reader.lock().unwrap(); let old = &mut reader.copies[0]; old.until = Some(last); @@ -8058,8 +8078,8 @@ mod test { end: None, }]) }; - let preferences = kio::Producer::new(ranges(5)); - let mut sub = routes.consume().subscribe(preferences.clone().into()); + let preferences = track::Preferences::from(ranges(5)); + let mut sub = routes.consume().subscribe(preferences.clone()); let delivered = Delivered::default(); let Poll::Ready(Ok(Some(candidate))) = sub.poll_peek_group(false, false, &delivered, &kio::Waiter::noop()) else { @@ -8073,18 +8093,14 @@ mod test { assert_eq!(start(), track::Anchor::Time(5)); // Changed, but not yet mirrored into the copies. - if let Ok(mut changed) = preferences.write() { - *changed = ranges(0); - } + preferences.update(ranges(0)).unwrap(); assert_eq!(start(), track::Anchor::Time(20)); sub.mirror().sync(); let (b, _requests) = pending_copy(); routes.serve(b.consume()); assert_eq!(start(), track::Anchor::Time(20)); - if let Ok(mut changed) = preferences.write() { - *changed = ranges(3); - } + preferences.update(ranges(3)).unwrap(); assert_eq!(start(), track::Anchor::Relative(3)); } @@ -9161,12 +9177,22 @@ mod test { .unwrap() .unwrap(); let _ = sub.poll_start(&kio::Waiter::noop()); - assert_eq!(sub.starts(0), [Some(0)]); + // Where the range starts once delivery starts at `from`, none past its end. + let start = |sub: &mut track::Subscriber, from: u64| { + let resolved = sub.resolved_range(0)?; + let from = resolved.start.max(from); + resolved.end.is_none_or(|end| end > from).then_some(from) + }; + assert_eq!(start(&mut sub, 0), Some(0)); for key in 1..3 { write(&a, key); } - assert_eq!(sub.starts(10), [None]); - assert_eq!(sub.starts(0), [Some(0)], "the start stays pinned while the end settles"); + assert_eq!(start(&mut sub, 10), None); + assert_eq!( + start(&mut sub, 0), + Some(0), + "the start stays pinned while the end settles" + ); } } diff --git a/rs/moq-net/src/model/resume_model.rs b/rs/moq-net/src/model/resume_model.rs index 52d2f716966..3fb5a1327b9 100644 --- a/rs/moq-net/src/model/resume_model.rs +++ b/rs/moq-net/src/model/resume_model.rs @@ -694,9 +694,7 @@ impl Model { } // Only an arrival cursor says where its range resolved. let resolved = match reader.cursor.as_mut().unwrap() { - Cursor::Arrival(sub) => { - sub.resolved().first().copied().flatten().map(|resolved| resolved.start) - } + Cursor::Arrival(sub) => sub.resolved_range(0).map(|resolved| resolved.start), Cursor::Ordered(_) => None, }; if !reader.admits(key, resolved) { diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 126a4178e30..e8acd087dc7 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -3,9 +3,9 @@ use std::{ time::Duration, }; -mod runs; +mod cover; mod window; -pub(super) use runs::{Layered, Runs}; +pub(super) use cover::{Cover, minus}; pub(crate) use window::Accounted; pub(crate) use window::Resolved; pub(super) use window::{Containing, Holdings, Unheld, Window}; @@ -267,7 +267,7 @@ impl Range { /// /// An [`Anchor::Oldest`] end delivers nothing either: every reader holding one is /// refused, so it never reaches the demand asked upstream. - pub(super) fn is_empty(&self) -> bool { + pub(crate) fn is_empty(&self) -> bool { match (self.start, self.end) { (_, Some(Anchor::Oldest)) => true, (Anchor::Time(start), Some(Anchor::Time(end))) => end < start, @@ -502,15 +502,6 @@ impl Subscription { .collect(); Self { ranges, ..self.clone() } } - - /// Whether a subscriber joins at the live edge: a range counting back from the latest - /// group and following it. A fixed start resumes a known group instead, such as a route - /// switch picking up where the reader stopped. - pub(super) fn joins_live(&self) -> bool { - self.ranges - .iter() - .any(|range| matches!(range.start, Anchor::Oldest | Anchor::Relative(_)) && range.end.is_none()) - } } /// Group spans for unit tests whose groups start at consecutive keys, where a span of diff --git a/rs/moq-net/src/model/subscription/cover.rs b/rs/moq-net/src/model/subscription/cover.rs new file mode 100644 index 00000000000..c0e5e48a59a --- /dev/null +++ b/rs/moq-net/src/model/subscription/cover.rs @@ -0,0 +1,639 @@ +//! Counted runs of group keys, persistent so a copy costs nothing and an edit costs its own +//! runs, never every other one. +//! +//! A subscription may name thousands of ranges, and an update may edit one of them. Each run +//! counts +1 at its start and -1 just past its end, so a key is covered while the running sum +//! above it stays positive. A treap keeps each subtree's sum and its lowest and highest +//! running sums, so adding or removing a run touches two keys and every query is one descent. +//! Nodes are shared between copies and copied only along the path an edit walks, and a key's +//! priority is the same in every copy, so two copies of one lineage share every subtree an +//! edit between them didn't touch: [`Cover::moved`] finds what changed by skipping those. + +use std::{ + hash::{BuildHasher, RandomState}, + ops::RangeInclusive, + sync::{Arc, LazyLock}, +}; + +/// One seed per process, so a key ranks the same in every cover and copies stay comparable, +/// and so a peer can't pick keys that degrade the treap. +static PRIORITIES: LazyLock = LazyLock::new(RandomState::new); + +/// A multiset of runs of keys, read as the set of keys any run covers. +#[derive(Clone, Default)] +pub(in crate::model) struct Cover { + root: Link, +} + +type Link = Option>; + +#[derive(Clone)] +struct Node { + key: u64, + /// Runs starting here, minus runs ending just before here. Never zero. + delta: i64, + priority: u64, + /// The sum of every delta in this subtree. + sum: i64, + /// The lowest and highest running sums at any key of this subtree, counted from its first. + low: i64, + high: i64, + left: Link, + right: Link, +} + +impl PartialEq for Cover { + /// The same copy, unedited since: two covers holding the same keys by different edits + /// compare unequal, which costs a reader one spurious refresh at most. + fn eq(&self, other: &Self) -> bool { + match (&self.root, &other.root) { + (None, None) => true, + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + _ => false, + } + } +} + +impl std::fmt::Debug for Cover { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_list().entries(self.runs(0)).finish() + } +} + +/// What a running sum is searched for. +#[derive(Clone, Copy)] +enum Want { + /// Covered: above zero. + Covered, + /// Uncovered: zero, since no key counts below it. + Gap, +} + +impl Want { + fn at(self, sum: i64) -> bool { + match self { + Self::Covered => sum > 0, + Self::Gap => sum <= 0, + } + } + + /// Whether a subtree entered with `offset` may hold a match. + fn within(self, offset: i64, node: &Node) -> bool { + match self { + Self::Covered => offset.saturating_add(node.high) > 0, + Self::Gap => offset.saturating_add(node.low) <= 0, + } + } +} + +impl Cover { + /// The runs given, each counted once. + #[cfg(test)] + pub fn new(runs: impl IntoIterator>) -> Self { + let mut cover = Self::default(); + for run in runs { + cover.add(run, 1); + } + cover + } + + /// Count `delta` more of `run`, or fewer when negative: a run is removed only as often as + /// it was added. An empty run counts nothing. + pub fn add(&mut self, run: RangeInclusive, delta: i64) { + let (start, end) = (*run.start(), *run.end()); + if start > end || delta == 0 { + return; + } + self.shift(start, delta); + if let Some(past) = end.checked_add(1) { + self.shift(past, -delta); + } + } + + fn shift(&mut self, key: u64, delta: i64) { + let (below, rest) = split(self.root.take(), key, false); + let (at, above) = split(rest, key, true); + let delta = at.map_or(0, |node| node.delta) + delta; + let at = (delta != 0).then(|| Node::new(key, delta)); + self.root = merge(merge(below, at), above); + } + + /// Whether no run is counted. + #[cfg(test)] + pub fn is_empty(&self) -> bool { + self.root.is_none() + } + + /// The running sum at `key`: how many runs cover it. + fn count(&self, key: u64) -> i64 { + let mut link = &self.root; + let mut total = 0; + while let Some(node) = link { + if node.key <= key { + total += sum(&node.left) + node.delta; + link = &node.right; + } else { + link = &node.left; + } + } + total + } + + pub fn contains(&self, key: u64) -> bool { + self.count(key) > 0 + } + + /// The first key at or after `from` whose running sum is `want`. + fn first_key(&self, from: u64, want: Want) -> Option { + first_from(&self.root, 0, from, want) + } + + /// Where the run covering `key` ends. + fn run_end(&self, key: u64) -> u64 { + match self.first_key(key, Want::Gap) { + Some(gap) => gap - 1, + None => u64::MAX, + } + } + + /// The lowest run reaching into `lo..=hi`, its start clipped to `lo`. + pub fn first(&self, lo: u64, hi: u64) -> Option> { + if lo > hi { + return None; + } + let start = match self.contains(lo) { + true => lo, + false => self.first_key(lo, Want::Covered).filter(|start| *start <= hi)?, + }; + Some(start..=self.run_end(start)) + } + + /// The runs from `from` on, lowest first, each whole but clipped to `from`. + /// + /// One walk in key order that skips every subtree holding no run's edge, so each run + /// costs about one descent, however many runs overlap inside it. + pub fn runs(&self, from: u64) -> impl Iterator> + '_ { + Runs::new(self, from) + } + + /// The runs reaching into `lo..=hi`, lowest first, clipped to it. + pub fn within(&self, lo: u64, hi: u64) -> impl Iterator> + '_ { + self.runs(lo) + .take_while(move |run| *run.start() <= hi) + .map(move |run| *run.start()..=(*run.end()).min(hi)) + } + + /// The lowest key covered: the first edge, where the first run starts. + pub fn lowest(&self) -> Option { + first(&self.root) + } + + /// The highest key covered: just below the last edge, where the last run ends, or every + /// key on while a run is open. + pub fn highest(&self) -> Option { + match sum(&self.root) > 0 { + true => Some(u64::MAX), + false => last(&self.root).map(|key| key - 1), + } + } + + /// The spans where `self` and `old` may cover differently, lowest first: where their counts + /// differ, a superset of where coverage does. + /// + /// Costs the keys edited between them, times the depth, when one is an edited copy of the + /// other: every subtree they still share is skipped. Unrelated covers cost both. + pub fn moved(&self, old: &Self) -> Vec> { + let mut changed = Vec::new(); + diff(self.root.clone(), old.root.clone(), &mut changed); + changed.sort_unstable_by_key(|(key, _)| *key); + // A key met on both sides of a split is listed once per side. + changed.dedup_by(|next, kept| { + let same = next.0 == kept.0; + if same { + kept.1 += next.1; + } + same + }); + let mut spans = Vec::new(); + let mut open: Option = None; + let mut difference = 0; + for (key, delta) in changed { + difference += delta; + match (open, difference != 0) { + (None, true) => open = Some(key), + (Some(start), false) => { + spans.push(start..=key - 1); + open = None; + } + _ => {} + } + } + spans.extend(open.map(|start| start..=u64::MAX)); + spans + } + + /// What `self` covers and `old` doesn't within `lo..=hi`, lowest first. Costs what + /// [`Self::moved`] does, and the runs inside where they differ. + pub fn gained(&self, old: &Self, lo: u64, hi: u64) -> Vec> { + let mut gained = Vec::new(); + if lo > hi { + return gained; + } + for span in self.moved(old) { + let (lo, hi) = ((*span.start()).max(lo), (*span.end()).min(hi)); + for run in self.within(lo, hi) { + gained.extend(minus(run, old)); + } + } + gained + } +} + +/// The parts of `run` that `cover` doesn't hold, lowest first. +pub(in crate::model) fn minus(run: RangeInclusive, cover: &Cover) -> Vec> { + let (lo, hi) = (*run.start(), *run.end()); + let mut gaps = Vec::new(); + let mut from = (lo <= hi).then_some(lo); + for held in cover.within(lo, hi) { + let Some(at) = from else { break }; + if *held.start() > at { + gaps.push(at..=*held.start() - 1); + } + from = held.end().checked_add(1).filter(|next| *next <= hi); + } + gaps.extend(from.map(|at| at..=hi)); + gaps +} + +impl Node { + fn new(key: u64, delta: i64) -> Arc { + Arc::new(Self { + key, + delta, + priority: PRIORITIES.hash_one(key), + sum: delta, + low: delta, + high: delta, + left: None, + right: None, + }) + } + + fn update(&mut self) { + let at = sum(&self.left) + self.delta; + self.sum = at + sum(&self.right); + let (left_low, left_high) = bounds(&self.left); + let (right_low, right_high) = bounds(&self.right); + self.low = left_low.min(at).min(at.saturating_add(right_low)); + self.high = left_high.max(at).max(at.saturating_add(right_high)); + } +} + +fn sum(link: &Link) -> i64 { + link.as_ref().map_or(0, |node| node.sum) +} + +/// A subtree's lowest and highest running sums, neutral for an empty one. +fn bounds(link: &Link) -> (i64, i64) { + link.as_ref().map_or((i64::MAX, i64::MIN), |node| (node.low, node.high)) +} + +/// Split into the keys below `key` (through it when `inclusive`) and the rest, copying the +/// nodes on the path only where another cover still shares them. +fn split(link: Link, key: u64, inclusive: bool) -> (Link, Link) { + let Some(mut node) = link else { + return (None, None); + }; + let inner = Arc::make_mut(&mut node); + if inner.key < key || (inclusive && inner.key == key) { + let (below, rest) = split(inner.right.take(), key, inclusive); + inner.right = below; + inner.update(); + (Some(node), rest) + } else { + let (rest, above) = split(inner.left.take(), key, inclusive); + inner.left = above; + inner.update(); + (rest, Some(node)) + } +} + +/// Join two treaps, every key of `left` below every key of `right`. +fn merge(left: Link, right: Link) -> Link { + match (left, right) { + (None, link) | (link, None) => link, + (Some(mut left), Some(mut right)) => { + if left.priority > right.priority { + let inner = Arc::make_mut(&mut left); + inner.right = merge(inner.right.take(), Some(right)); + inner.update(); + Some(left) + } else { + let inner = Arc::make_mut(&mut right); + inner.left = merge(Some(left), inner.left.take()); + inner.update(); + Some(right) + } + } + } +} + +fn first(link: &Link) -> Option { + let mut node = link.as_ref()?; + while let Some(left) = &node.left { + node = left; + } + Some(node.key) +} + +fn last(link: &Link) -> Option { + let mut node = link.as_ref()?; + while let Some(right) = &node.right { + node = right; + } + Some(node.key) +} + +/// [`Cover::runs`]: the edges above where it started, in key order, each node waiting with +/// how many runs were open entering it. +struct Runs<'a> { + stack: Vec<(&'a Node, i64)>, + /// Where the run being walked started, `None` between runs. + open: Option, + done: bool, +} + +impl<'a> Runs<'a> { + fn new(cover: &'a Cover, from: u64) -> Self { + let mut runs = Self { + stack: Vec::new(), + open: (cover.count(from) > 0).then_some(from), + done: false, + }; + // Every edge above `from`, the ones at or below it counted into where it starts. + let mut link = &cover.root; + let mut offset = 0; + while let Some(node) = link { + if node.key <= from { + offset += sum(&node.left) + node.delta; + link = &node.right; + } else { + runs.stack.push((node, offset)); + link = &node.left; + } + } + runs + } + + /// Queue the subtree under `link`, entered with `offset` runs open, down its left edge, + /// leaving out each subtree whose running sum stays on the side it entered on. + fn descend(&mut self, mut link: &'a Link, offset: i64) { + while let Some(node) = link { + let crosses = match offset > 0 { + true => offset.saturating_add(node.low) <= 0, + false => offset.saturating_add(node.high) > 0, + }; + if !crosses { + return; + } + self.stack.push((node, offset)); + link = &node.left; + } + } +} + +impl Iterator for Runs<'_> { + type Item = RangeInclusive; + + fn next(&mut self) -> Option { + if self.done { + return None; + } + while let Some((node, offset)) = self.stack.pop() { + let at = offset + sum(&node.left) + node.delta; + self.descend(&node.right, at); + match self.open { + Some(start) if at <= 0 => { + self.open = None; + return Some(start..=node.key - 1); + } + None if at > 0 => self.open = Some(node.key), + _ => {} + } + } + self.done = true; + self.open.take().map(|start| start..=u64::MAX) + } +} + +/// The first key of the subtree, entered with `offset` runs open, whose running sum is `want`. +fn first_in(link: &Link, offset: i64, want: Want) -> Option { + let node = link.as_ref()?; + if !want.within(offset, node) { + return None; + } + if let Some(key) = first_in(&node.left, offset, want) { + return Some(key); + } + let at = offset + sum(&node.left) + node.delta; + if want.at(at) { + return Some(node.key); + } + first_in(&node.right, at, want) +} + +/// [`first_in`] over the keys at or after `from`. +fn first_from(link: &Link, offset: i64, from: u64, want: Want) -> Option { + let node = link.as_ref()?; + let at = offset + sum(&node.left) + node.delta; + if node.key < from { + return first_from(&node.right, at, from, want); + } + if let Some(key) = first_from(&node.left, offset, from, want) { + return Some(key); + } + if want.at(at) { + return Some(node.key); + } + first_in(&node.right, at, want) +} + +/// Collect each key whose delta differs between `new` and `old`, with how much more `new` +/// counts there. A key's priority is fixed, so the higher-ranked root of the two holds a key +/// the other lacks, unless both roots are the same key. +fn diff(new: Link, old: Link, changed: &mut Vec<(u64, i64)>) { + match (new, old) { + (None, None) => {} + (Some(a), Some(b)) if Arc::ptr_eq(&a, &b) => {} + (Some(a), None) => collect(&Some(a), 1, changed), + (None, Some(b)) => collect(&Some(b), -1, changed), + (Some(a), Some(b)) if a.key == b.key => { + if a.delta != b.delta { + changed.push((a.key, a.delta - b.delta)); + } + diff(a.left.clone(), b.left.clone(), changed); + diff(a.right.clone(), b.right.clone(), changed); + } + (Some(a), Some(b)) if a.priority > b.priority => { + changed.push((a.key, a.delta)); + let (below, above) = split(Some(b), a.key, false); + diff(a.left.clone(), below, changed); + diff(a.right.clone(), above, changed); + } + (Some(a), Some(b)) => { + changed.push((b.key, -b.delta)); + let (below, above) = split(Some(a), b.key, false); + diff(below, b.left.clone(), changed); + diff(above, b.right.clone(), changed); + } + } +} + +/// Every key of the subtree, its delta scaled by `sign`. +fn collect(link: &Link, sign: i64, changed: &mut Vec<(u64, i64)>) { + if let Some(node) = link { + collect(&node.left, sign, changed); + changed.push((node.key, sign * node.delta)); + collect(&node.right, sign, changed); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use rangemap::RangeInclusiveSet; + + /// A small deterministic generator, so a failure replays. + fn generator() -> impl FnMut(u64) -> u64 { + let mut x = 0x9e37_79b9_7f4a_7c15_u64; + move |bound| { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + x % bound + } + } + + fn random_run(next: &mut impl FnMut(u64) -> u64, span: u64) -> RangeInclusive { + let start = match next(16) { + 0 => 0, + _ => next(span), + }; + // Now and then a run reaching the last key. + let end = match next(8) { + 0 => u64::MAX, + _ => start + next(4), + }; + start..=end + } + + /// The points a cover is probed at: each bound of every run, either side of it. + fn points(runs: &[RangeInclusive]) -> Vec { + let mut points: Vec = runs + .iter() + .flat_map(|run| [*run.start(), *run.end()]) + .flat_map(|point| [point.saturating_sub(1), point, point.saturating_add(1)]) + .chain([0, u64::MAX]) + .collect(); + points.sort_unstable(); + points.dedup(); + points + } + + /// A cover answers every query as the set of the runs it holds would, through any + /// sequence of adds and removes, and a copy taken before an edit is left as it was. + #[test] + fn a_cover_matches_the_union_of_its_runs() { + let mut next = generator(); + for round in 0..400 { + let mut cover = Cover::default(); + let mut held: Vec> = Vec::new(); + for step in 0..24 { + let before = (cover.clone(), held.clone()); + if held.is_empty() || next(3) != 0 { + let run = random_run(&mut next, 40); + cover.add(run.clone(), 1); + held.push(run); + } else { + let run = held.swap_remove(next(held.len() as u64) as usize); + cover.add(run, -1); + } + let union: RangeInclusiveSet = held.iter().cloned().collect(); + let context = || format!("round {round} step {step}: {held:?}"); + assert!(cover.runs(0).eq(union.iter().cloned()), "{}", context()); + assert_eq!(cover.is_empty(), held.is_empty(), "{}", context()); + let all = points(&union.iter().cloned().collect::>()); + assert_eq!( + cover.lowest(), + union.iter().next().map(|run| *run.start()), + "{}", + context() + ); + assert_eq!( + cover.highest(), + union.iter().next_back().map(|run| *run.end()), + "{}", + context() + ); + for &lo in &all { + assert_eq!(cover.contains(lo), union.contains(&lo), "{} at {lo}", context()); + let from = union + .overlapping(lo..=u64::MAX) + .map(|run| (*run.start()).max(lo)..=*run.end()); + assert!(cover.runs(lo).eq(from), "{} runs from {lo}", context()); + for &hi in all.iter().filter(|hi| **hi >= lo) { + let first = union + .overlapping(lo..=hi) + .next() + .map(|run| (*run.start()).max(lo)..=*run.end()); + assert_eq!(cover.first(lo, hi), first, "{} first in {lo}..={hi}", context()); + } + } + // The copy before the edit still answers as it did. + let (old, old_held) = before; + let old_union: RangeInclusiveSet = old_held.iter().cloned().collect(); + assert!(old.runs(0).eq(old_union.iter().cloned()), "{}", context()); + // What moved covers every key whose coverage changed, and what was gained is + // exactly the keys newly covered. + let moved: RangeInclusiveSet = cover.moved(&old).into_iter().collect(); + let gained: RangeInclusiveSet = cover.gained(&old, 0, u64::MAX).into_iter().collect(); + for &key in &all { + let (now, then) = (union.contains(&key), old_union.contains(&key)); + assert!(now == then || moved.contains(&key), "{} moved at {key}", context()); + assert_eq!(gained.contains(&key), now && !then, "{} gained at {key}", context()); + } + } + } + } + + /// An edit to a large cover moves exactly the span it touched, and undoing it moves nothing. + #[test] + fn an_edit_moves_only_what_it_touched() { + let mut cover = Cover::new((0..10_000u64).map(|key| 4 * key..=4 * key + 1)); + let old = cover.clone(); + cover.add(20_001..=20_002, 1); + assert_eq!(cover.moved(&old), vec![20_001..=20_002]); + assert_eq!(cover.gained(&old, 0, u64::MAX), vec![20_002..=20_002]); + cover.add(20_001..=20_002, -1); + assert!(cover.moved(&old).is_empty()); + } + + /// A run minus a cover is the parts of it the cover doesn't hold. + #[test] + fn a_run_minus_a_cover_is_its_gaps() { + let mut next = generator(); + for _ in 0..2000 { + let runs: Vec<_> = (0..next(7)).map(|_| random_run(&mut next, 40)).collect(); + let cover = Cover::new(runs.clone()); + let start = next(40); + let run = start..=start + next(10); + let expected: Vec = run.clone().filter(|key| !cover.contains(*key)).collect(); + let found: Vec = minus(run.clone(), &cover).into_iter().flatten().collect(); + assert_eq!(found, expected, "{run:?} minus {runs:?}"); + } + let whole = Cover::new(std::iter::once(0..=u64::MAX)); + assert!(minus(0..=u64::MAX, &whole).is_empty()); + let low = Cover::new(std::iter::once(0..=9)); + assert_eq!(minus(5..=u64::MAX, &low), vec![10..=u64::MAX]); + } +} diff --git a/rs/moq-net/src/model/subscription/runs.rs b/rs/moq-net/src/model/subscription/runs.rs deleted file mode 100644 index 4d5b0906481..00000000000 --- a/rs/moq-net/src/model/subscription/runs.rs +++ /dev/null @@ -1,349 +0,0 @@ -//! 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. - -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. -#[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()) - } - - pub fn contains(&self, key: u64) -> bool { - let index = self.0.partition_point(|run| *run.end() < key); - self.0.get(index).is_some_and(|run| *run.start() <= key) - } - - /// The lowest run reaching into `lo..=hi`. - pub fn first(&self, lo: u64, hi: u64) -> Option<&RangeInclusive> { - let index = self.0.partition_point(|run| *run.end() < lo); - self.0.get(index).filter(|run| *run.start() <= hi) - } - - /// The highest run reaching into `lo..=hi`. - pub fn last(&self, lo: u64, hi: u64) -> Option<&RangeInclusive> { - let index = self.0.partition_point(|run| *run.start() <= hi); - self.0.get(index.checked_sub(1)?).filter(|run| *run.end() >= lo) - } - - /// The runs reaching into `lo..=hi`, lowest first, clipped to it. - pub fn within(&self, lo: u64, hi: u64) -> impl DoubleEndedIterator> + '_ { - let from = self.0.partition_point(|run| *run.end() < lo); - let to = self.0.partition_point(|run| *run.start() <= hi).max(from); - self.0[from..to] - .iter() - .map(move |run| (*run.start()).max(lo)..=(*run.end()).min(hi)) - } -} - -/// A set of groups held as shared fixed runs with a layer of runs over them, which may -/// overlap them or each other. -/// -/// Each query answers from both by searches, never by visiting the fixed runs a layered run -/// covers, so a layer that moves costs its own runs, however many fixed ones lie under it. -/// Both are shared, so a copy costs nothing until the layer moves. -#[derive(Clone, Debug, Default)] -pub(in crate::model) struct Layered { - fixed: Arc, - layer: Arc, -} - -impl PartialEq for Layered { - /// Equal as held: the same fixed runs and layer, each compared by reference first. The - /// same groups split differently between the two compare unequal. - fn eq(&self, other: &Self) -> bool { - let same = |a: &Arc, b: &Arc| Arc::ptr_eq(a, b) || a == b; - same(&self.fixed, &other.fixed) && same(&self.layer, &other.layer) - } -} - -impl Layered { - pub fn new(fixed: Arc, layer: Runs) -> Self { - Self { - fixed, - layer: Arc::new(layer), - } - } - - pub fn fixed(&self) -> &Arc { - &self.fixed - } - - pub fn layer(&self) -> &Runs { - &self.layer - } - - /// Whether both hold the same fixed runs, so they differ only in their layers. - pub fn shares_fixed(&self, other: &Self) -> bool { - Arc::ptr_eq(&self.fixed, &other.fixed) - } - - pub fn contains(&self, key: u64) -> bool { - self.fixed.contains(key) || self.layer.contains(key) - } - - /// The one set holding runs, when the other holds none: its runs are the union's, each - /// already whole, so a read needs no growing across the two. - fn only(&self) -> Option<&Runs> { - match (self.fixed.0.is_empty(), self.layer.0.is_empty()) { - (_, true) => Some(&self.fixed), - (true, false) => Some(&self.layer), - (false, false) => None, - } - } - - /// The lowest whole run of the union reaching into `lo..=hi`, its start clipped to `lo`. - pub fn first(&self, lo: u64, hi: u64) -> Option> { - // Most windows have no layer: their fixed runs are already the union's. - if let Some(only) = self.only() { - return only.first(lo, hi).map(|run| (*run.start()).max(lo)..=*run.end()); - } - let starts = [self.fixed.first(lo, hi), self.layer.first(lo, hi)]; - let start = starts.into_iter().flatten().map(|run| (*run.start()).max(lo)).min()?; - // Grow the end through whatever either set holds next to it: each step crosses into - // the other set, so a layered run over many fixed ones costs a search, not a walk. - let mut end = start; - loop { - let grown = [&*self.fixed, &*self.layer] - .into_iter() - .filter_map(|runs| runs.first(end, end.saturating_add(1))) - .map(|run| *run.end()) - .max() - .filter(|grown| *grown > end); - match grown { - Some(grown) => end = grown, - None => return Some(start..=end), - } - if end == u64::MAX { - return Some(start..=end); - } - } - } - - /// The highest whole run of the union reaching into `lo..=hi`, its end clipped to `hi`. - pub fn last(&self, lo: u64, hi: u64) -> Option> { - if let Some(only) = self.only() { - return only.last(lo, hi).map(|run| *run.start()..=(*run.end()).min(hi)); - } - let ends = [self.fixed.last(lo, hi), self.layer.last(lo, hi)]; - let end = ends.into_iter().flatten().map(|run| (*run.end()).min(hi)).max()?; - let mut start = end; - loop { - let grown = [&*self.fixed, &*self.layer] - .into_iter() - .filter_map(|runs| runs.last(start.saturating_sub(1), start)) - .map(|run| *run.start()) - .min() - .filter(|grown| *grown < start); - match grown { - Some(grown) => start = grown, - None => return Some(start..=end), - } - if start == 0 { - return Some(start..=end); - } - } - } - - /// The union's runs from `from` on, lowest first, each whole but clipped to `from`. - pub fn runs(&self, from: u64) -> impl Iterator> + '_ { - let mut next = Some(from); - std::iter::from_fn(move || { - let run = self.first(next?, u64::MAX)?; - next = run.end().checked_add(1); - Some(run) - }) - } - - /// The lowest group held. - pub fn lowest(&self) -> Option { - self.first(0, u64::MAX).map(|run| *run.start()) - } - - /// The highest group held. - pub fn highest(&self) -> Option { - self.last(0, u64::MAX).map(|run| *run.end()) - } - - /// What `self` holds and `old` doesn't within `lo..=hi`, lowest first, when both share - /// their fixed runs: `None` when they don't. - /// - /// Costs the two layers, and the fixed runs inside where they differ: a layered run that - /// grows over many fixed ones, from the live edge to past every group, costs them only - /// within `lo..=hi`. - pub fn gained(&self, old: &Self, lo: u64, hi: u64) -> Option>> { - if !self.shares_fixed(old) { - return None; - } - let mut gained = Vec::new(); - if lo > hi { - return Some(gained); - } - for run in self.layer.within(lo, hi) { - for piece in minus(run, &old.layer) { - gained.extend(minus(piece, &self.fixed)); - } - } - Some(gained) - } -} - -/// 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()); - let mut from = (lo <= hi).then_some(lo); - let mut inside = runs.within(lo, hi).peekable(); - std::iter::from_fn(move || { - loop { - let at = from?; - match inside.peek() { - Some(held) if *held.start() <= at => { - from = held.end().checked_add(1).filter(|next| *next <= hi); - inside.next(); - } - Some(held) => { - let gap = at..=*held.start() - 1; - from = held.end().checked_add(1).filter(|next| *next <= hi); - inside.next(); - return Some(gap); - } - None => { - from = None; - return Some(at..=hi); - } - } - } - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - /// A small deterministic generator, so a failure replays. - fn generator() -> impl FnMut(u64) -> u64 { - let mut x = 0x9e37_79b9_7f4a_7c15_u64; - move |bound| { - x ^= x << 13; - x ^= x >> 7; - x ^= x << 17; - x % bound - } - } - - fn random_runs(next: &mut impl FnMut(u64) -> u64, count: u64, span: u64) -> Vec> { - (0..next(count + 1)) - .map(|_| { - let start = next(span); - // Now and then a run reaching the last key. - let end = match next(8) { - 0 => u64::MAX, - _ => start + next(4), - }; - start..=end - }) - .collect() - } - - /// The points a layered set is probed at: each bound of every run, either side of it. - fn points(runs: &[RangeInclusive]) -> Vec { - let mut points: Vec = runs - .iter() - .flat_map(|run| [*run.start(), *run.end()]) - .flat_map(|point| [point.saturating_sub(1), point, point.saturating_add(1)]) - .chain([0, u64::MAX]) - .collect(); - points.sort_unstable(); - points.dedup(); - points - } - - /// A layered set answers every query as the set of its runs merged would. - #[test] - fn layered_runs_match_their_union() { - let mut next = generator(); - for round in 0..2000 { - let fixed = random_runs(&mut next, 6, 40); - let layer = random_runs(&mut next, 3, 40); - let layered = Layered::new(Arc::new(Runs::new(fixed.clone())), Runs::new(layer.clone())); - let union: RangeInclusiveSet = fixed.iter().chain(&layer).cloned().collect(); - let context = || format!("round {round}: {fixed:?} under {layer:?}"); - assert!(layered.runs(0).eq(union.iter().cloned()), "{}", context()); - let points = points(&union.iter().cloned().collect::>()); - for &lo in &points { - assert_eq!(layered.contains(lo), union.contains(&lo), "{} at {lo}", context()); - let from: Vec<_> = union - .overlapping(lo..=u64::MAX) - .map(|run| (*run.start()).max(lo)..=*run.end()) - .collect(); - assert!(layered.runs(lo).eq(from), "{} from {lo}", context()); - for &hi in points.iter().filter(|hi| **hi >= lo) { - let mut reaching = union.overlapping(lo..=hi); - let first = reaching.next().map(|run| (*run.start()).max(lo)..=*run.end()); - assert_eq!(layered.first(lo, hi), first, "{} first in {lo}..={hi}", context()); - let last = union - .overlapping(lo..=hi) - .next_back() - .map(|run| *run.start()..=(*run.end()).min(hi)); - assert_eq!(layered.last(lo, hi), last, "{} last in {lo}..={hi}", context()); - } - } - } - } - - /// What a moved layer gains is exactly the groups the new set holds and the old one - /// doesn't, within the span asked about. - #[test] - fn a_moved_layer_gains_exactly_the_new_groups() { - let mut next = generator(); - for round in 0..2000 { - let fixed = Arc::new(Runs::new(random_runs(&mut next, 6, 40))); - let old = Layered::new(fixed.clone(), Runs::new(random_runs(&mut next, 3, 40))); - let new = Layered::new(fixed, Runs::new(random_runs(&mut next, 3, 40))); - let (lo, hi) = (next(30), 20 + next(30)); - let gained: RangeInclusiveSet = new.gained(&old, lo, hi).unwrap().into_iter().collect(); - let expected: Vec = (lo..=hi) - .filter(|key| new.contains(*key) && !old.contains(*key)) - .collect(); - let found: Vec = (lo..=hi).filter(|key| gained.contains(key)).collect(); - assert_eq!(found, expected, "round {round}: {old:?} to {new:?} in {lo}..={hi}"); - } - let unshared = Layered::new(Arc::new(Runs::default()), Runs::default()); - assert!(unshared.gained(&Layered::default(), 0, 9).is_none()); - } - - /// A run minus a set is the parts of it the set doesn't hold. - #[test] - fn a_run_minus_runs_is_its_gaps() { - let mut next = generator(); - for _ in 0..2000 { - let runs = Runs::new(random_runs(&mut next, 6, 40)); - let start = next(40); - let run = start..=start + next(10); - let expected: Vec = run.clone().filter(|key| !runs.contains(*key)).collect(); - let found: Vec = minus(run.clone(), &runs).flatten().collect(); - assert_eq!(found, expected, "{run:?} minus {runs:?}"); - } - let whole = Runs::new(std::iter::once(0..=u64::MAX)); - assert!(minus(0..=u64::MAX, &whole).next().is_none()); - let low = Runs::new(std::iter::once(0..=9)); - let above: Vec<_> = minus(5..=u64::MAX, &low).collect(); - assert_eq!((above.len(), above.first()), (1, Some(&(10..=u64::MAX)))); - } -} diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 6231a37afc9..8a481914123 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -4,9 +4,13 @@ //! 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, BTreeSet}, + ops, + time::Duration, +}; -use super::{Anchor, Layered, Range, Runs}; +use super::{Anchor, Cover, Range}; use crate::{Error, Result}; /// One range resolved to groups: from `start` up to the exclusive `end`, `None` while it @@ -137,47 +141,47 @@ impl Holdings for Unheld { } } -/// A subscriber's [`Range`]s resolved to the group starts it reads. +/// A subscriber's [`Range`]s resolved to the group starts it reads, each by the key its +/// caller gave it, so an edit resolves only the ranges it adds and leaves every other where +/// it resolved. #[derive(Debug)] pub(in crate::model) struct Window { - /// What these were resolved from, so only replacing the ranges resolves again. - ranges: Vec, + /// What these were resolved from, by key, in the order given. + ranges: BTreeMap, /// The subscriber's Min Age, which reaches a live join back from the live edge. min: Option, - spans: Vec, + /// Each non-empty range placed, by key. + spans: BTreeMap, + /// The ranges waiting on a first group, by key: a relative anchor, or a live join with + /// nothing to reach back from. Everything is in range meanwhile. + pending: BTreeSet, /// 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 the time looked up, then key, so a probe finds them without visiting + /// every span. + looking: BTreeSet<(u64, u64)>, /// The spans whose time start waits on a group arriving, which read only the groups from /// that time meanwhile. - waiting: Vec, - /// 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. - reaching: Vec, + waiting: BTreeSet, + /// The ranges 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. + reaching: BTreeSet, /// The groups the spans cover, shared with what reads them. - groups: Layered, + groups: Cover, /// The part of `groups` a miss may ask for: see [`Self::missable`]. - missable: Layered, - /// A relative anchor waits on a first group, so nothing is filtered yet. - pending: bool, - /// Nothing is left to resolve: see [`Self::is_settled`]. Kept so a read checks it once - /// per group, not once per range. - settled: bool, + missable: Cover, /// 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 start at each group they name, so a probe reads the lowest without a + /// walk: see [`Self::named`] and [`Self::start`]. + named: BTreeMap, + /// How many spans reach back to the oldest group instead of naming one. + unnamed: usize, } /// One non-empty range, resolved. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug)] struct Span { - /// Which of the ranges it came from. - index: usize, /// Where it starts: its range's start, or where a live join's Min Age reached it back to. anchor: Anchor, start: u64, @@ -191,6 +195,10 @@ struct Span { /// A time start the groups held can't place yet: the span reads only the groups from /// that time until it can. waiting: bool, + /// What it counts in [`Window::groups`] and [`Window::missable`], so taking it out + /// uncounts exactly that. + groups: Option>, + missable: Option>, } /// A time start being looked up: its time, and the exclusive end of the run below every @@ -202,40 +210,89 @@ struct Looking { } impl Window { - /// Resolve `ranges`, counting relative anchors back from `latest`, and reaching a live - /// join back by `min` (see [`joins`]). + /// Resolve `ranges`, keyed by position, counting relative anchors back from `latest`, and + /// reaching a live join back by `min` (see [`joins`]). /// /// Refuses an [`Anchor::Oldest`] end with [`Error::InvalidRange`] where the hop /// [resolves](Holdings::resolves) ranges itself, and an anchor a received copy's wire /// drops with [`Error::Unsupported`]. pub fn new(ranges: &[Range], min: Option, latest: Option, holdings: &impl Holdings) -> Result { + Self::keyed((0..).zip(ranges.iter().copied()), min, latest, holdings) + } + + /// [`Self::new`] for ranges the caller keys itself, in key order. + pub fn keyed( + ranges: impl IntoIterator, + min: Option, + latest: Option, + holdings: &impl Holdings, + ) -> Result { let mut window = Self { - ranges: ranges.to_vec(), + ranges: ranges.into_iter().collect(), min, - spans: Vec::new(), - looking: Vec::new(), - waiting: Vec::new(), - reaching: Vec::new(), - groups: Layered::default(), - missable: Layered::default(), - pending: false, - settled: false, + spans: BTreeMap::new(), + pending: BTreeSet::new(), + looking: BTreeSet::new(), + waiting: BTreeSet::new(), + reaching: BTreeSet::new(), + groups: Cover::default(), + missable: Cover::default(), covered: None, - named: None, - all_named: true, + named: BTreeMap::new(), + unnamed: 0, }; window.place(latest, holdings)?; Ok(window) } + /// Retire the ranges keyed `retired`, then resolve `added`, leaving every other range + /// where it resolved. A key added again replaces its range. Costs the ranges edited, never + /// the rest. + /// + /// Refuses what [`Self::new`] does, changing nothing. + pub fn edit( + &mut self, + retired: impl IntoIterator, + added: &[(u64, Range)], + latest: Option, + holdings: &impl Holdings, + ) -> Result<()> { + check(added.iter().map(|(_, range)| range), holdings)?; + for key in retired { + self.retire(key); + } + for &(key, range) in added { + self.retire(key); + self.ranges.insert(key, range); + if !range.is_empty() { + self.place_range(key, latest, holdings); + } + } + self.covered = None; + Ok(()) + } + + /// Take out the range keyed `key`, if any. + fn retire(&mut self, key: u64) { + if self.ranges.remove(&key).is_none() { + return; + } + self.pending.remove(&key); + self.reaching.remove(&key); + self.waiting.remove(&key); + if let Some(span) = self.spans.remove(&key) { + self.uncount(key, &span); + } + } + /// Whether nothing is left to resolve as groups arrive. pub fn is_settled(&self) -> bool { - self.settled + self.pending.is_empty() && self.looking.is_empty() && !self.unplaced() } /// Whether a relative anchor still waits on a first group. pub fn is_pending(&self) -> bool { - self.pending + !self.pending.is_empty() } /// Take the subscriber's current Min Age. Only a pending window reads it, when it places @@ -258,28 +315,27 @@ impl Window { /// Resolve what the groups now held settle: relative anchors once `latest` exists, and /// time starts once a group arrives. Returns whether the groups in range moved. pub fn settle(&mut self, latest: Option, holdings: &impl Holdings) -> bool { - if self.pending { + if self.is_pending() { if latest.is_none() { return false; } - // Checked when the ranges were first resolved, against the same track. + // Checked when the ranges were first resolved, against the same track. Every range + // places again, as when first resolved: the first group arrives once per track. let _ = self.place(latest, holdings); - return !self.pending; + self.covered = None; + return !self.is_pending(); } let mut moved = false; 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; + for key in std::mem::take(&mut self.reaching) { + self.place_range(key, latest, holdings); } + moved = self.spans.len() > placed; } moved |= !self.waiting.is_empty() && self.start_waiting(holdings); if moved { - self.rebuild(holdings); + self.covered = None; } moved } @@ -288,8 +344,8 @@ impl Window { /// 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 { + for key in std::mem::take(&mut self.waiting) { + let Anchor::Time(time) = self.spans[&key].anchor else { unreachable!("only a time start waits on a group"); }; let (start, floored) = match holdings.containing(time) { @@ -298,41 +354,39 @@ impl Window { Containing::Before => (0, false), Containing::Unknown if holdings.complete() => (0, false), Containing::Unknown => { - self.waiting.push(position); + self.waiting.insert(key); continue; } }; started = true; - self.start_span(position, time, start, floored, holdings); - } - if started { - self.name(); + self.start_span(key, time, start, floored, holdings); } started } - /// Start the span at `position`, whose time start `time` waited, at `start`: looked up + /// Start the span keyed `key`, 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) { - let asked = matches!(self.ranges[self.spans[position].index].start, Anchor::Time(_)); - let span = &mut self.spans[position]; + fn start_span(&mut self, key: u64, time: u64, start: u64, floored: bool, holdings: &impl Holdings) { + let asked = matches!(self.ranges[&key].start, Anchor::Time(_)); + let mut span = self.spans.remove(&key).expect("a waiting span"); + self.uncount(key, &span); (span.start, span.floored, span.waiting) = (start, floored, false); if let Some(below) = unresolved(time, start, floored, holdings).filter(|_| asked) { span.looking = Some(Looking { time, below }); - self.looking.push(position); } + self.count(key, span, holdings); } /// Whether group `key` is in range. Everything is while a relative anchor waits. pub fn contains(&self, key: u64) -> bool { - self.pending || self.groups.contains(key) + self.is_pending() || self.groups.contains(key) } /// The lowest group in range, `None` while an anchor waits. Nothing in range floors /// nowhere: group 0. pub fn floor(&self) -> Option { - match self.pending || self.unplaced() { + match self.is_pending() || self.unplaced() { true => None, false => Some(self.groups.lowest().unwrap_or(0)), } @@ -341,63 +395,58 @@ 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.is_pending() || !self.reaching.is_empty() || self.unnamed > 0 { true => None, - false => self.named, + false => self.lowest_named(), } } /// The lowest group a range names, `None` when none does or an anchor waits. /// Unlike [`Self::start`], a range reaching back to the oldest group names nothing. pub fn named(&self) -> Option { - match self.pending || self.unplaced() { + match self.is_pending() || self.unplaced() { true => None, - false => self.named, + false => self.lowest_named(), } } - /// Each range resolved, in the order given: `None` for one whose start still waits, or - /// still looking up its start. One empty as given ends at or below its start. - pub fn resolved(&self) -> Vec> { - let mut resolved: Vec<_> = self - .ranges - .iter() - .map(|range| match (range.start, range.end) { - (Anchor::Time(start), Some(Anchor::Time(_))) if range.is_empty() => Some(Resolved { - start, - end: Some(start), - floored: true, - }), - _ => None, - }) - .collect(); - for span in self.spans.iter().filter(|span| span.looking.is_none() && !span.waiting) { - resolved[span.index] = Some(Resolved { - start: span.start, - end: span.end, - floored: span.floored, + fn lowest_named(&self) -> Option { + self.named.first_key_value().map(|(start, _)| *start) + } + + /// The range keyed `key` resolved: `None` for one whose start still waits, or still + /// looking up its start, or that isn't held. One empty as given ends at or below its start. + pub fn resolved_key(&self, key: u64) -> Option { + let range = self.ranges.get(&key)?; + if let (Anchor::Time(start), Some(Anchor::Time(_))) = (range.start, range.end) + && range.is_empty() + { + return Some(Resolved { + start, + end: Some(start), + floored: true, }); } - resolved - } - - /// Where each range starts once delivery starts at `from`, in the order given: `None` - /// for one with no group there, or whose start still waits. - pub fn starts(&self, from: u64) -> Vec> { - self.resolved() - .into_iter() - .map(|r| { - let r = r?; - let start = r.start.max(from); - r.end.is_none_or(|end| end > start).then_some(start) - }) - .collect() + let span = self + .spans + .get(&key) + .filter(|span| span.looking.is_none() && !span.waiting)?; + Some(Resolved { + start: span.start, + end: span.end, + floored: span.floored, + }) + } + + /// Each range resolved, in key order, as [`Self::resolved_key`] reads it. + pub fn resolved(&self) -> Vec> { + self.ranges.keys().map(|key| self.resolved_key(*key)).collect() } /// The exclusive end past every group in range, `None` when one stays open or an anchor /// waits. pub fn end(&self) -> Option { - if !self.settled { + if !self.is_settled() { return None; } match self.groups.highest() { @@ -452,26 +501,26 @@ impl Window { self.from(&self.missable, from) } - fn from<'a>(&'a self, groups: &'a Layered, from: u64) -> impl Iterator> + 'a { - let all = self.pending.then_some(from..=u64::MAX); - let runs = (!self.pending).then(|| groups.runs(from)); + fn from<'a>(&'a self, groups: &'a Cover, from: u64) -> impl Iterator> + 'a { + let all = self.is_pending().then_some(from..=u64::MAX); + let runs = (!self.is_pending()).then(|| groups.runs(from)); all.into_iter().chain(runs.into_iter().flatten()) } /// The groups in range, `None` while a relative anchor waits and every group is. - pub fn groups(&self) -> Option<&Layered> { - (!self.pending).then_some(&self.groups) + pub fn groups(&self) -> Option<&Cover> { + (!self.is_pending()).then_some(&self.groups) } /// The groups a miss may ask for, as [`Self::groups`]. - pub fn missable_groups(&self) -> Option<&Layered> { - (!self.pending).then_some(&self.missable) + pub fn missable_groups(&self) -> Option<&Cover> { + (!self.is_pending()).then_some(&self.missable) } /// The first run of `range` that is in range. pub fn clip(&self, range: ops::Range) -> Option> { let last = range.end.checked_sub(1).filter(|last| *last >= range.start)?; - if self.pending { + if self.is_pending() { return Some(range); } let run = self.groups.first(range.start, last)?; @@ -487,7 +536,7 @@ impl Window { pub fn lookups(&self) -> impl Iterator + '_ { self.looking .iter() - .filter_map(|span| self.spans[*span].looking.map(|looking| (looking.time, looking.below))) + .filter_map(|(_, key)| self.spans[key].looking.map(|looking| (looking.time, looking.below))) } /// The lookup of time start `time` answered with the group containing it, or the oldest @@ -495,60 +544,58 @@ 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, - }); + let found: Vec = self + .looking + .range((time, 0)..=(time, u64::MAX)) + .map(|(_, key)| *key) + .collect(); if found.is_empty() { return false; } let held = group.filter(|group| holdings.next_start(*group) == Some(*group)); - for index in found { - let span = &mut self.spans[index]; - if span.looking.take().is_none() { - continue; - } + for key in found { + let mut span = self.spans.remove(&key).expect("a span looking up its start"); + self.uncount(key, &span); + span.looking = None; (span.start, span.floored) = match (held, holdings.containing(time)) { (Some(group), _) | (None, Containing::Group(group)) => (group, true), (None, _) => (0, false), }; + self.count(key, span, holdings); } - self.name(); - self.rebuild(holdings); + self.covered = None; true } + /// Resolve every range again from scratch. fn place(&mut self, latest: Option, holdings: &impl Holdings) -> Result<()> { - if holdings.resolves() && self.ranges.iter().any(|range| range.end == Some(Anchor::Oldest)) { - return Err(Error::InvalidRange); - } - if holdings.drops_anchors() && !self.ranges.iter().all(Range::is_wire) { - return Err(Error::Unsupported); - } + check(self.ranges.values(), holdings)?; self.spans.clear(); + self.pending.clear(); self.looking.clear(); self.waiting.clear(); self.reaching.clear(); - self.pending = false; - for index in 0..self.ranges.len() { - if !self.ranges[index].is_empty() { - self.place_range(index, latest, holdings); - } + self.groups = Cover::default(); + self.missable = Cover::default(); + self.named.clear(); + self.unnamed = 0; + let keys: Vec = self + .ranges + .iter() + .filter(|(_, range)| !range.is_empty()) + .map(|(key, _)| *key) + .collect(); + for key in keys { + self.place_range(key, latest, holdings); } - self.name(); - self.rebuild(holdings); Ok(()) } - /// Resolve the non-empty range at `index` into a span, or note what it waits on. - fn place_range(&mut self, index: usize, latest: Option, holdings: &impl Holdings) { - let range = self.ranges[index]; + /// Resolve the non-empty range keyed `key` into a span, or note what it waits on. + fn place_range(&mut self, key: u64, latest: Option, holdings: &impl Holdings) { + let range = self.ranges[&key]; let Some(end) = end(range.end, latest, holdings) else { - self.pending = true; + self.pending.insert(key); return; }; let anchor = match joins(&range, self.min, holdings) { @@ -558,11 +605,11 @@ impl Window { // Nothing to measure the live edge by yet: with groups held, read none of them // until one measures it; with none, wait as a relative start does. Some(None) => { - self.reaching.push(index); + self.reaching.insert(key); return; } None => { - self.pending = true; + self.pending.insert(key); return; } }, @@ -570,22 +617,15 @@ impl Window { let (start, floored) = match start(anchor, latest, holdings) { Start::At(start, floored) => (start, floored), Start::Pending => { - self.pending = true; + self.pending.insert(key); return; } // 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 { - index, - anchor, - start: 0, - end, - floored: false, - looking: None, - waiting: true, - }); + let span = Span::new(anchor, 0, end, false, None); + self.waiting.insert(key); + self.count(key, Span { waiting: true, ..span }, holdings); return; } }; @@ -597,18 +637,7 @@ impl Window { } _ => None, }; - if looking.is_some() { - self.looking.push(self.spans.len()); - } - self.spans.push(Span { - index, - anchor, - start, - end, - floored, - looking, - waiting: false, - }); + self.count(key, Span::new(anchor, start, end, floored, looking), holdings); } /// Whether a time start or a join's reach back waits on the groups held to place it. @@ -616,61 +645,110 @@ 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); + /// Hold `span` under `key`, counting the groups it reads and where it starts. + fn count(&mut self, key: u64, mut span: Span, holdings: &impl Holdings) { + (span.groups, span.missable) = span.reads(holdings); + if let Some(run) = &span.groups { + self.groups.add(run.clone(), 1); + } + if let Some(run) = &span.missable { + self.missable.add(run.clone(), 1); + } + match span.floored { + true => *self.named.entry(span.start).or_default() += 1, + false => self.unnamed += 1, + } + if let Some(looking) = span.looking { + self.looking.insert((looking.time, key)); + } + self.spans.insert(key, span); } - /// 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(); - 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); + /// Take back what [`Self::count`] counted for `span`, already out of [`Self::spans`]. + fn uncount(&mut self, key: u64, span: &Span) { + if let Some(run) = &span.groups { + self.groups.add(run.clone(), -1); + } + if let Some(run) = &span.missable { + self.missable.add(run.clone(), -1); + } + match span.floored { + true => { + let count = self.named.get_mut(&span.start).expect("a counted start"); + *count -= 1; + if *count == 0 { + self.named.remove(&span.start); } - continue; - } - if last < span.start { - continue; } - 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, + false => self.unnamed -= 1, + } + if let Some(looking) = span.looking { + self.looking.remove(&(looking.time, key)); + } + } +} + +impl Span { + fn new(anchor: Anchor, start: u64, end: Option, floored: bool, looking: Option) -> Self { + Self { + anchor, + start, + end, + floored, + looking, + waiting: false, + groups: None, + missable: None, + } + } + + /// The groups it reads, and those of them a miss may ask for: from its start through its + /// end once it knows where it starts, from its time while it waits on its time start, and + /// none while it looks its start up. + fn reads(&self, holdings: &impl Holdings) -> (Option>, Option>) { + 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 held when it was placed, 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)) + } +} + +/// Refuse an [`Anchor::Oldest`] end with [`Error::InvalidRange`] where the hop +/// [resolves](Holdings::resolves) ranges itself, and an anchor a received copy's wire drops +/// with [`Error::Unsupported`]. +fn check<'a>(mut ranges: impl Iterator + Clone, holdings: &impl Holdings) -> Result<()> { + if holdings.resolves() && ranges.clone().any(|range| range.end == Some(Anchor::Oldest)) { + return Err(Error::InvalidRange); + } + if holdings.drops_anchors() && !ranges.all(Range::is_wire) { + return Err(Error::Unsupported); } + Ok(()) } /// The Min Age a live join reaches back by: `min`, for [`Range::live`], where this hop resolves @@ -863,9 +941,6 @@ mod tests { }), ] ); - // Delivery starting inside a range raises its start; past its end leaves it none. - assert_eq!(window.starts(3), [Some(3), Some(7)]); - assert_eq!(window.starts(5), [None, Some(7)]); // Nothing asked for is nothing in range, floored nowhere. let window = Window::new(&[], None, None, &keys).unwrap(); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 550c1b2c8de..416e8fb28a3 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -23,11 +23,12 @@ 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, Cover, Holdings, Window}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; mod demand; +pub(crate) use demand::Edit; pub(super) use demand::Preferences; use demand::Subscriptions; @@ -2070,9 +2071,8 @@ impl TrackState { } /// The first live-feed group at or above `from` that `servable` covers for which `f` - /// answers, lowest first or, with `rev`, highest first: one lookup for a single run. - /// Several runs leapfrog the live groups, so a run holding none (a reader's misses, or - /// fetched backfill) costs one lookup rather than a walk over every run past it. + /// answers, lowest first or, with `rev`, highest first: one lookup for a single run, and + /// the reader's index of the live groups among several. fn find_servable<'a, R>( &'a self, servable: &Servable, @@ -2102,60 +2102,15 @@ impl TrackState { if from > hi { return None; } - let groups = &several.groups; - // The fixed runs through the reader's index of the live groups among them, then - // the walks' few runs layered over them, short of what the index found. - let fixed = several.live.find(self, groups.fixed(), from, hi, rev, &mut f); - // Nothing lies past a find at the far end, so only the find answers. - let short = match (&fixed, rev) { - (None, _) => Some((from, hi)), - (Some((found, _)), true) => found.checked_add(1).map(|lo| (lo, hi)), - (Some((found, _)), false) => found.checked_sub(1).map(|hi| (from, hi)), - }; - let layered = short - .filter(|(lo, hi)| lo <= hi) - .and_then(|(lo, hi)| self.leapfrog(groups.layer(), lo, hi, rev, &mut f)); - layered.or(fixed.map(|(_, found)| found)) + // Through the reader's index of the live groups among its runs. + several + .live + .find(self, &several.groups, from, hi, rev, &mut f) + .map(|(_, found)| found) } } } - /// [`Self::find_servable`] over `runs` within `lo..=hi`: several runs leapfrog the live - /// groups, so a run holding none costs one lookup rather than a walk over every run past - /// it. - fn leapfrog<'a, R>( - &'a self, - runs: &Runs, - mut lo: u64, - mut hi: u64, - rev: bool, - f: &mut impl FnMut(&'a Slot) -> Option, - ) -> Option { - // What is left to search, shrinking from the end the search starts at. - while lo <= hi { - let run = match rev { - true => runs.last(lo, hi), - false => runs.first(lo, hi), - }?; - let (start, end) = ((*run.start()).max(lo), (*run.end()).min(hi)); - // The cache from the run on, toward the far end: one lookup either finds the run's - // groups or the next cached group past a run holding none. - let leap = match rev { - true => leap(self.live(lo..=end).rev(), start..=end, f), - false => leap(self.live(start..=hi), start..=end, f), - }; - match leap { - Leap::Found(found) => return Some(found), - Leap::Empty => return None, - Leap::Past(key) if rev => hi = key, - Leap::Past(key) => lo = key, - Leap::Exhausted if rev => hi = start.checked_sub(1)?, - Leap::Exhausted => lo = end.checked_add(1)?, - } - } - None - } - /// Whether `key` still holds the servable incarnation `stamp`. fn holds(&self, key: u64, stamp: u32) -> bool { self.lookup @@ -7627,7 +7582,7 @@ struct Drift { /// The groups a reader reads, and the exclusive end it stops at, or `None` before its ranges /// resolve. -type Coverage = Option<(Layered, Option)>; +type Coverage = Option<(Cover, Option)>; /// Whether a reader judging under `budget` with `coverage` is settled by `hole` over a run /// from `start`. Any hole but a received stale one settles every reader. A stale verdict @@ -7675,7 +7630,7 @@ struct GroupExpiry { /// locking `state`. cache: Arc, reading: Reading, - subscription: kio::Consumer, + subscription: kio::Consumer, /// What the cursor can be served; see [`Cursor::published`]. servable: kio::Consumer, key: u64, @@ -7859,13 +7814,13 @@ struct Edge { servable: Servable, } -/// What a cursor's arrival walk reads: its window from the floor, below the cap. Holds the -/// window's groups as they are layered, so taking it again after a time end moves costs -/// the walks' runs, not every run of the window. +/// What a cursor's arrival walk reads: its window from the floor, below the cap. Holds a +/// snapshot of the window's cover, so taking it again after the window moves diffs the two +/// snapshots and costs what moved, not every run of the window. #[derive(Clone, Default)] struct Admitted { /// The window's groups, `None` while a relative anchor waits and every group is in it. - groups: Option, + groups: Option, floor: u64, cap: Option, } @@ -7911,17 +7866,52 @@ impl Admitted { self.overlapping(0..=u64::MAX) } - /// What this admits and `old` doesn't within `lo..=hi`, when the two differ only in the - /// window's walks: `None` otherwise. + /// What this admits and `old` doesn't within `lo..=hi`, when neither waits on a relative + /// anchor: `None` otherwise. Costs where the window, the floor, or the cap moved between + /// them, never the runs that stayed. fn gained(&self, old: &Self, lo: u64, hi: u64) -> Option>> { let (groups, old_groups) = (self.groups.as_ref()?, old.groups.as_ref()?); - if (self.floor, self.cap) != (old.floor, old.cap) || !groups.shares_fixed(old_groups) { - return None; - } let Some(last) = self.last() else { return Some(Vec::new()); }; - groups.gained(old_groups, lo.max(self.floor), hi.min(last)) + let (lo, hi) = (lo.max(self.floor), hi.min(last)); + if lo > hi { + return Some(Vec::new()); + } + let mut moved: RangeInclusiveSet = groups.moved(old_groups).into_iter().collect(); + if self.floor != old.floor { + moved.insert(self.floor.min(old.floor)..=self.floor.max(old.floor) - 1); + } + let old_last = old.last(); + match old_last { + Some(old_last) if old_last != last => { + moved.insert(old_last.min(last) + 1..=old_last.max(last)); + } + Some(_) => {} + // The old admitted nothing, so all of this is new. + None => moved.insert(lo..=hi), + } + let mut gained = Vec::new(); + for span in moved.overlapping(lo..=hi) { + for run in groups.within((*span.start()).max(lo), (*span.end()).min(hi)) { + let (start, end) = (*run.start(), *run.end()); + // What lies outside the old bounds is new whatever its runs held. + let inside = old_last.map(|old_last| (start.max(old.floor), end.min(old_last))); + match inside.filter(|(from, to)| from <= to) { + None => gained.push(run), + Some((from, to)) => { + if start < from { + gained.push(start..=from - 1); + } + gained.extend(super::subscription::minus(from..=to, old_groups)); + if to < end { + gained.push(to + 1..=end); + } + } + } + } + } + Some(gained) } } @@ -8011,39 +8001,6 @@ impl, I: DoubleEndedIterator> Doub } } -/// What one run's lookup found, for [`TrackState::find_servable`] leapfrogging the cache. -enum Leap { - /// A group in the run that the search answered. - Found(R), - /// The run's groups, none answering. - Exhausted, - /// Nothing in the run: the nearest cached group past it. - Past(u64), - /// Nothing cached from the run on. - Empty, -} - -/// Search `cached`, ordered from the run toward the far end, for a group in `run` that `f` -/// answers. -fn leap<'a, R>( - mut cached: impl Iterator, - run: ops::RangeInclusive, - f: &mut impl FnMut(&'a Slot) -> Option, -) -> Leap { - let Some((&first, slot)) = cached.next() else { - return Leap::Empty; - }; - if !run.contains(&first) { - return Leap::Past(first); - } - let mut within = - std::iter::once(slot).chain(cached.take_while(|(key, _)| run.contains(*key)).map(|(_, slot)| slot)); - match within.find_map(f) { - Some(found) => Leap::Found(found), - None => Leap::Exhausted, - } -} - /// The groups a reader can be served, the only ones its drift is measured against: one it /// never reads, past its cap or in a gap between its ranges, never stands in for one it does. #[derive(Clone, PartialEq)] @@ -8056,13 +8013,14 @@ enum Servable { Runs(Arc), } -/// A servable window of several runs: the window's groups as it layers them, below the -/// exclusive `cap`, with the reader's index of the live groups inside its fixed runs. +/// A servable window of several runs: a snapshot of the window's cover, below the exclusive +/// `cap`, with the reader's index of the live groups inside that cover. /// -/// Built again whenever the window or cap moves, which a time end does on each group, so it -/// shares the fixed runs and their index, and copies only the walks' runs. +/// Built again whenever the window or cap moves, which a time end does on each group. The +/// snapshot is a pointer clone, and the index is kept while the cover is unchanged +/// (pointer-equal), so a move of only the cap rebuilds nothing. struct Several { - groups: Layered, + groups: Cover, cap: Option, live: Arc, } @@ -8073,12 +8031,12 @@ impl PartialEq for Several { } } -/// A reader's index of the live feed's cached groups inside its window's fixed runs, so a +/// A reader's index of the live feed's cached groups inside its window's cover, so a /// search costs the live groups it passes, never the runs between them: thousands of runs /// in the gaps of a sparsely numbered feed would otherwise each cost a lookup. /// /// Taken in from the arrival order, which every live group enters once, on the reader's own -/// search: each arrival costs it one lookup in the fixed runs. A slot that leaves or is +/// search: each arrival costs it one lookup in the cover. A slot that leaves or is /// replaced leaves its entry stale, dropped when a search meets it or once stale entries /// could outnumber the live groups. /// @@ -8094,7 +8052,7 @@ struct Indexed { track: Weak, /// How far into the arrival order this has taken in, as an absolute index. taken: usize, - /// The stamp of each live group inside the fixed runs, by key, as it arrived. + /// The stamp of each live group inside the cover, by key, as it arrived. groups: BTreeMap, } @@ -8109,9 +8067,9 @@ impl Indexed { } /// Take in what arrived since the last call. - fn catch_up(&mut self, state: &TrackState, fixed: &Runs) { + fn catch_up(&mut self, state: &TrackState, groups: &Cover) { for entry in state.arrivals_from(self.taken) { - if fixed.contains(entry.key) { + if groups.contains(entry.key) { self.groups.insert(entry.key, entry.stamp); } } @@ -8151,12 +8109,12 @@ impl Live { &mut indexes[position] } - /// The first live group inside `fixed`, within `lo..=hi`, for which `f` answers, lowest + /// The first live group inside `groups`, within `lo..=hi`, for which `f` answers, lowest /// first or, with `rev`, highest first, with its key. fn find<'a, R>( &self, state: &'a TrackState, - fixed: &Runs, + groups: &Cover, mut lo: u64, mut hi: u64, rev: bool, @@ -8172,7 +8130,7 @@ impl Live { let mut indexes = self.0.lock().unwrap_or_else(PoisonError::into_inner); let indexed = Self::index(&mut indexes, state); if !caught_up { - indexed.catch_up(state, fixed); + indexed.catch_up(state, groups); caught_up = true; } let entries = indexed.groups.range(lo..=hi).map(|(&key, &stamp)| (key, stamp)); @@ -8236,17 +8194,17 @@ impl Servable { } /// The groups `window` reads. `previous`, what the same reader was served before, lends - /// its index of the live groups when the fixed runs are the same. + /// its index of the live groups while the cover is unchanged. fn of(window: &Window, previous: &Self) -> Self { match window.groups() { None => Self::ALL, - Some(groups) => Self::layered(groups.clone(), None, previous), + Some(groups) => Self::covering(groups.clone(), None, previous), } } /// The groups in `groups` below the exclusive `cap`. Only several runs allocate: every /// subscriber resolves one, so the common single run stays off the heap. - fn layered(groups: Layered, cap: Option, previous: &Self) -> Self { + fn covering(groups: Cover, cap: Option, previous: &Self) -> Self { let last = match cap { Some(cap) => match cap.checked_sub(1) { Some(last) => last, @@ -8268,7 +8226,7 @@ impl Servable { }; } let live = match previous { - Self::Runs(several) if several.groups.shares_fixed(&groups) => several.live.clone(), + Self::Runs(several) if several.groups == groups => several.live.clone(), _ => Arc::default(), }; Self::Runs(Arc::new(Several { groups, cap, live })) @@ -8292,7 +8250,7 @@ impl Servable { } Self::Runs(several) => { let cap = several.cap.map_or(cap, |own| own.min(cap)); - Self::layered(several.groups.clone(), Some(cap), self) + Self::covering(several.groups.clone(), Some(cap), self) } } } @@ -8613,6 +8571,14 @@ impl DropCursor { } } +/// What changed in a cursor's ranges since its window resolved them. +enum Change { + /// The ranges keyed so, each as it stands now: `None` for one retired. + Edit(Vec<(u64, Option)>), + /// Every range, in key order, when the changes are no longer known one by one. + All(Vec<(u64, Range)>), +} + /// A cursor's own ranges, resolved, and what keeps them current. struct Filter { /// Only the groups in it are read. @@ -8623,6 +8589,8 @@ struct Filter { revision: u64, /// Why the ranges can't be served here: a time anchor on an untimed track. refused: Option, + /// The window was left as it was by a refused change, so the next one resolves every range. + stale: bool, /// What the arrival walk reads: the window from the floor, below the cap. The walk /// passes a group outside it holding nothing, so widening it re-scans the cache. admitted: Admitted, @@ -8646,20 +8614,25 @@ impl Cursor { } let (filter, cache) = { let preferences = subscription.read(); - let ranges_revision = subscription.ranges_revision(); + let ranges_revision = preferences.revision; let state = state.read(); - let (window, refused) = - match Window::new(&preferences.ranges, preferences.delay.min, state.latest_live(), &*state) { - Ok(window) => (window, None), - Err(err) => ( - Window::new(&[], None, None, &*state).expect("nothing to refuse"), - Some(err), - ), - }; + let (window, refused) = match Window::keyed( + preferences.ranges(), + preferences.delay.min, + state.latest_live(), + &*state, + ) { + Ok(window) => (window, None), + Err(err) => ( + Window::new(&[], None, None, &*state).expect("nothing to refuse"), + Some(err), + ), + }; let filter = Filter { window, resolved: ranges_revision, revision: 0, + stale: refused.is_some(), refused, admitted: Admitted::default(), admitted_at: None, @@ -8794,37 +8767,63 @@ impl Cursor { } } - /// Bring [`Self::window`] up to date: resolved again once the subscriber replaces its - /// ranges, counting relative anchors back from the latest group then, and settled as - /// the groups it waits on arrive. Returns whether the window moved. + /// Bring [`Self::window`] up to date: the ranges the subscriber changed resolved, counting + /// relative anchors back from the latest group then, and settled as the groups it waits on + /// arrive. Returns whether the window moved. fn sync(&mut self) -> bool { - let (replaced, min) = { + let (changed, min) = { let preferences = self.subscription.read(); - let revision = self.subscription.ranges_revision(); - let min = preferences.delay.min; - ( - (revision != self.filter.resolved).then(|| (revision, preferences.ranges.clone())), - min, - ) + let changed = (preferences.revision != self.filter.resolved).then(|| { + let keys = match self.filter.stale { + true => None, + false => preferences.changes(self.filter.resolved), + }; + let change = match keys { + Some(keys) => Change::Edit(keys.into_iter().map(|key| (key, preferences.range(key))).collect()), + None => Change::All(preferences.ranges().collect()), + }; + (preferences.revision, change) + }); + (changed, preferences.delay.min) }; // A settled window has nothing to read from the track, whose lock every subscriber // of it takes. - if replaced.is_none() && self.filter.window.is_settled() { + if changed.is_none() && self.filter.window.is_settled() { return false; } let state = self.state.read(); let holdings = &*state; - let moved = match replaced { - Some((revision, ranges)) => { + let moved = match changed { + Some((revision, change)) => { self.filter.resolved = revision; - match Window::new(&ranges, min, state.latest_live(), holdings) { - Ok(window) => { + let applied = match change { + Change::Edit(changes) => { + let added: Vec<(u64, Range)> = changes + .iter() + .filter_map(|(key, range)| Some((*key, (*range)?))) + .collect(); + self.filter.window.set_min(min); + let applied = self.filter.window.edit( + changes.iter().map(|(key, _)| *key), + &added, + state.latest_live(), + holdings, + ); + // A time looked up and answered is asked again for a range that names it anew. + self.filter.lookups.retain(|lookup| lookup.join.is_some()); + applied + } + Change::All(ranges) => Window::keyed(ranges, min, state.latest_live(), holdings).map(|window| { self.filter.window = window; - self.filter.refused = None; self.filter.lookups.clear(); - } - Err(err) => self.filter.refused = Some(err), - } + }), + }; + // A refused change leaves the window as it was, which no longer matches the ranges, + // so the next change resolves them all. + (self.filter.refused, self.filter.stale) = match applied { + Ok(()) => (None, false), + Err(err) => (Some(err), true), + }; true } None if self.filter.window.is_settled() => false, @@ -9322,15 +9321,22 @@ impl Cursor { /// Look up each time start the window can't resolve from the cache, once, and take in /// the answers, parking `waiter` on the lookups still out. + /// + /// Indexed by time, so a pass costs O(L log L) in the lookups out rather than O(L²). fn look_up(&mut self, waiter: &kio::Waiter) { - let lookups: Vec<(u64, u64)> = self.filter.window.lookups().collect(); + // The first span looking a time up sets how far its lookup reaches. + let mut lookups = BTreeMap::new(); + for (time, reach) in self.filter.window.lookups() { + lookups.entry(time).or_insert(reach); + } if lookups.is_empty() && self.filter.lookups.iter().all(|lookup| lookup.join.is_none()) { return; } let asking = &asking(&self.subscription); let fills = self.state.read().fills.clone(); - for (time, reach) in lookups { - if self.filter.lookups.iter().any(|lookup| lookup.time == time) { + let asked: BTreeSet = self.filter.lookups.iter().map(|lookup| lookup.time).collect(); + for (&time, &reach) in &lookups { + if asked.contains(&time) { continue; } let ask = fill::Ask { @@ -9349,8 +9355,9 @@ impl Cursor { } let mut moved = false; for lookup in &mut self.filter.lookups { - // The window stops looking once replaced; with no join, nothing can answer. - if !self.filter.window.lookups().any(|(time, _)| time == lookup.time) { + // The window stops looking once replaced; with no join, nothing can answer. An + // answer taken in below stops only the spans looking up that time. + if !lookups.contains_key(&lookup.time) { lookup.join = None; continue; } @@ -9660,11 +9667,13 @@ impl Cursor { /// Ask a fill for the head of `key`, as a fetch from frame 0 would, taking in an /// answer already at hand. fn ask_head(&mut self, key: u64, waiter: &kio::Waiter) { - let asking = self.subscription.read().clone(); + let asking = self.subscription.read(); + let (priority, max_delay) = (asking.priority, asking.delay.max); + drop(asking); let request = FetchOptions { options: group::Fetch { - priority: asking.priority, - max_delay: asking.delay.max, + priority, + max_delay, frame_start: 0, }, end: None, @@ -10019,14 +10028,18 @@ impl Republish { }; let (ranges, min, revision) = { let current = preferences.read(); - (current.ranges.clone(), current.delay.min, preferences.ranges_revision()) + ( + current.ranges().collect::>(), + current.delay.min, + current.revision, + ) }; if published.read().revision >= revision { return; } let state = self.state.read(); // The update was refused before it got here if the track can't resolve it. - let Ok(window) = Window::new(&ranges, min, state.latest_live(), &*state) else { + let Ok(window) = Window::keyed(ranges, min, state.latest_live(), &*state) else { return; }; drop(state); @@ -10042,7 +10055,7 @@ impl Republish { impl Control { /// This subscriber's current preferences. pub fn subscription(&self) -> Subscription { - self.subscription.read().clone() + self.subscription.read().subscription() } /// Replace this subscriber's preferences, updating the producer's aggregate. @@ -10661,36 +10674,32 @@ impl Subscriber { Some(start?.max(self.bounds().start)) } - /// Where each of this subscriber's ranges starts, by index, as resolved here and raised - /// to `from`, where delivery starts: `None` for one with no group there or still waiting - /// on the first group from the live feed. + /// Each of this subscriber's ranges keyed in `keys` as resolved here, in that order: its + /// first group and exclusive end, `None` while open, or `None` for one whose start still + /// waits on the live feed or a lookup, or that isn't held. One empty as given, or that only + /// resolves empty, ends at or below its start. /// /// A publisher tells its peer where a relative or time start landed, which the peer - /// can't work out from the groups it receives. + /// can't work out from the groups it receives. Settles the ranges once for the batch, so + /// asking for k of them costs O(k log n) past that. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn starts(&mut self, from: u64) -> Vec> { + pub(crate) fn resolved_keys(&mut self, keys: impl IntoIterator) -> Vec> { match &mut self.inner { Inner::Plain(cursor) => { cursor.sync(); - cursor.filter.window.starts(from) + keys.into_iter() + .map(|key| cursor.filter.window.resolved_key(key)) + .collect() } - Inner::Resume(resume, _) => resume.starts(from), + Inner::Resume(resume, _) => resume.resolved_keys(keys), } } - /// Each of this subscriber's ranges as resolved here, by index: its first group and - /// exclusive end, `None` while open, or `None` for one whose start still waits on the - /// live feed or a lookup. One empty as given, or that only resolves empty, ends at or - /// below its start. + /// This subscriber's range keyed `key` as resolved here; see [`Self::resolved_keys`]. + #[cfg(test)] #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn resolved(&mut self) -> Vec> { - match &mut self.inner { - Inner::Plain(cursor) => { - cursor.sync(); - cursor.filter.window.resolved() - } - Inner::Resume(resume, _) => resume.resolved(), - } + pub(crate) fn resolved_range(&mut self, key: u64) -> Option { + self.resolved_keys([key]).pop().flatten() } /// The lowest group at or above `from` this subscriber's ranges and @@ -11015,6 +11024,28 @@ impl Subscriber { Ok(()) } + /// Apply `edit` to this subscriber's preferences, costing the ranges it names: only the + /// ranges it adds resolve, and every other keeps where it resolved; see [`Edit`]. + /// + /// The keys are the caller's. The ranges the subscription started with, or that + /// [`Self::update`] last gave, are keyed by position. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn edit(&mut self, edit: Edit) -> Result<()> { + let channel = match &self.inner { + Inner::Plain(cursor) => &cursor.subscription, + Inner::Resume(resume, _) => resume.subscription(), + }; + channel.edit(edit)?; + match &mut self.inner { + Inner::Plain(cursor) => { + cursor.sync(); + } + Inner::Resume(resume, _) => resume.mirror().sync(), + } + let _ = self.settle_record(None); + Ok(()) + } + /// Poll for the track's largest location within [`Self::with_range`]: the current one, then each newer one, and `None` once the track ends. /// /// The largest is the newest group at the newest frame (0 while empty), as @@ -13286,8 +13317,11 @@ mod test { #[test] #[cfg(feature = "async")] fn a_received_copy_reaches_back_only_to_groups_it_holds() { - let placed = - |subscriber: &mut Subscriber| subscriber.resolved()[0].map(|resolved| (resolved.start, resolved.floored)); + let placed = |subscriber: &mut Subscriber| { + subscriber + .resolved_range(0) + .map(|resolved| (resolved.start, resolved.floored)) + }; let producer = track_producer("test", Info::timed()); producer.received(); producer.anchored(); @@ -15370,8 +15404,11 @@ mod test { }]) .with_delay(Delay::default().with_max(None)) }; - let placed = - |subscriber: &mut Subscriber| subscriber.resolved()[0].map(|resolved| (resolved.start, resolved.floored)); + let placed = |subscriber: &mut Subscriber| { + subscriber + .resolved_range(0) + .map(|resolved| (resolved.start, resolved.floored)) + }; let producer = track_producer("test", Info::timed()); producer.received(); producer.anchored(); @@ -15424,7 +15461,7 @@ mod test { }]) .with_delay(Delay::default().with_max(None)) }; - let placed = |subscriber: &mut Subscriber| subscriber.resolved()[0].map(|resolved| resolved.start); + let placed = |subscriber: &mut Subscriber| subscriber.resolved_range(0).map(|resolved| resolved.start); let producer = track_producer("test", Info::timed()); producer.received(); producer.anchored(); @@ -15448,7 +15485,7 @@ mod test { }]) .with_delay(Delay::default().with_max(None)), ); - assert_eq!(ended.resolved()[0].and_then(|resolved| resolved.end), Some(2)); + assert_eq!(ended.resolved_range(0).and_then(|resolved| resolved.end), Some(2)); // A newer group moved the live edge, so a later reader counts back from it. write_group(&producer, 10); @@ -17140,13 +17177,13 @@ mod test { } } - /// A search over a layered servable window, through the reader's index of the live - /// groups and the layer's runs, finds what a scan of every live group would, in either - /// direction, as groups arrive, start, abort, leave the cache, or arrive again between - /// searches that keep the same index. Each window is searched on two tracks, as a + /// A search over a servable window of several runs, through the reader's index of the + /// live groups, finds what a scan of every live group would, in either direction, as + /// groups arrive, start, abort, leave the cache, or arrive again between searches that + /// keep the same index, and as the window is edited. Each window is searched on two tracks, as a /// group's expiry that moved to a replacement copy searches its reader's. #[test] - fn layered_searches_match_a_scan_of_every_live_group() { + fn live_index_searches_match_a_scan_of_every_live_group() { // A small deterministic generator, so a failure replays. let seed = std::cell::Cell::new(0x2545_f491_4f6c_dd1d_u64); let next = |bound: u64| { @@ -17164,13 +17201,16 @@ mod test { track_producer("test", Info::timed()), track_producer("copy", Info::timed()), ]; - let runs = |count: u64| -> Runs { - Runs::new((0..next(count + 1)).map(|_| { - let start = next(KEYS); - start..=start + next(5) - })) + let runs = |count: u64| -> Vec<_> { + (0..next(count + 1)) + .map(|_| { + let start = next(KEYS); + start..=start + next(5) + }) + .collect() }; - let fixed = Arc::new(runs(8)); + let mut cover = Cover::new(runs(8)); + let mut layer = Vec::new(); let mut groups = [Vec::new(), Vec::new()]; let mut servable = Servable::NONE; for step in 0..30 { @@ -17198,11 +17238,19 @@ mod test { } _ => {} } - // Now and then the window's walks move, keeping the fixed runs and their index. + // Now and then the cap moves, keeping the runs and their index, or the runs are + // edited. if step == 0 || next(4) == 0 { let cap = (next(4) == 0).then(|| next(KEYS + 2)); - let layered = Layered::new(fixed.clone(), runs(2)); - servable = Servable::layered(layered, cap, &servable); + if next(2) == 0 { + for run in std::mem::replace(&mut layer, runs(2)) { + cover.add(run, -1); + } + for run in &layer { + cover.add(run.clone(), 1); + } + } + servable = Servable::covering(cover.clone(), cap, &servable); } for (track, producer) in producers.iter().enumerate() { let state = producer.state.read(); @@ -17217,12 +17265,7 @@ mod test { let found = state.find_servable(&servable, from, rev, shown); let shape = match &servable { Servable::Run { start, end } => format!("run {start}..{end:?}"), - Servable::Runs(several) => format!( - "{:?} under {:?} below {:?}", - several.groups.fixed(), - several.groups.layer(), - several.cap - ), + Servable::Runs(several) => format!("{:?} below {:?}", several.groups, several.cap), }; let cached: Vec<_> = state.live(..).map(|(key, slot)| (*key, slot.is_shown())).collect(); assert_eq!( @@ -17245,8 +17288,8 @@ mod test { let groups: Vec<_> = (0..100) .map(|key| producer.create_group(group::Info { start: key }).unwrap()) .collect(); - let fixed = Arc::new(Runs::new([0..=999, 1001..=u64::MAX])); - let servable = Servable::layered(Layered::new(fixed, Runs::default()), None, &Servable::NONE); + let runs = Cover::new([0..=999, 1001..=u64::MAX]); + let servable = Servable::covering(runs, None, &Servable::NONE); let Servable::Runs(several) = &servable else { panic!("several runs"); }; @@ -17310,13 +17353,10 @@ mod test { let points = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, u64::MAX - 1, u64::MAX]; for bounds in windows { let runs = || bounds.iter().map(|&(first, last)| first..=last); - // The runs fixed, and again split between fixed runs and a layer over them. - let layered = [ - Layered::new(Arc::new(Runs::new(runs())), Runs::default()), - Layered::new(Arc::new(Runs::new(runs().skip(1))), Runs::new(runs().take(1))), - ]; - for groups in layered { - let servable = Servable::layered(groups, None, &Servable::NONE); + // The runs counted once, and again with the first counted twice. + let covers = [Cover::new(runs()), Cover::new(runs().chain(runs().take(1)))]; + for groups in covers { + let servable = Servable::covering(groups, None, &Servable::NONE); if let [(first, last)] = bounds { assert!( servable @@ -26387,6 +26427,66 @@ mod test { drop(dynamic); } + /// Ranges edited one key at a time resolve and cover what the same ranges given whole do, + /// so an edit leaves the subscriber as a fresh subscription to the ranges it keeps would be. + #[test] + #[cfg(feature = "async")] + fn edited_ranges_match_the_same_ranges_given_whole() { + use rand::{RngExt, SeedableRng}; + let mut rng = rand::rngs::StdRng::seed_from_u64(0x65646974); + let producer = track_producer("test", Info::timed()); + for key in (0..20_000).step_by(1000) { + write_group(&producer, key); + } + let range = |rng: &mut rand::rngs::StdRng| { + let start = match rng.random_range(0..4) { + 0 => Anchor::Relative(rng.random_range(0..5)), + _ => Anchor::Time(rng.random_range(0..24_000)), + }; + let end = rng.random_bool(0.6).then(|| Anchor::Time(rng.random_range(0..24_000))); + Range { start, end } + }; + // Key 0 is the default subscription's one range. + let mut live = BTreeMap::from([(0, Range::oldest())]); + let mut next = 0; + let mut edited = producer.subscribe(Subscription::default()); + for step in 0..400 { + let retire: Vec = live.keys().copied().filter(|_| rng.random_bool(0.2)).collect(); + let add: Vec<(u64, Range)> = (0..rng.random_range(0..3)) + .map(|_| { + next += 1; + (next, range(&mut rng)) + }) + .collect(); + for key in &retire { + live.remove(key); + } + live.extend(add.iter().copied()); + edited + .edit(Edit { + retire, + add, + priority: 0, + delay: Delay::default(), + order: Order::default(), + }) + .unwrap(); + let mut whole = producer.subscribe(Subscription::default().with_ranges(live.values().copied())); + for (index, key) in live.keys().enumerate() { + assert_eq!( + edited.resolved_range(*key), + whole.resolved_range(index as u64), + "step {step}, key {key}" + ); + } + assert_eq!( + edited.runs_in_range(0, 30_000), + whole.runs_in_range(0, 30_000), + "step {step}" + ); + } + } + /// Write a finished one-frame group, presented at its key in milliseconds. fn write_group(producer: &Producer, key: u64) { let mut group = producer.create_group(group::Info { start: key }).unwrap(); diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 5a365a1e429..9863430ed13 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -1,15 +1,12 @@ //! Incremental demand, separate from the track's cached content. use std::{ - collections::BTreeMap, - sync::{ - Arc, - atomic::{self, AtomicU64}, - }, + collections::{BTreeMap, BTreeSet, VecDeque}, + sync::Arc, task::Poll, time::Duration, }; -use super::{Delay, Order, Range, Subscription, TrackState, clamp_combined}; +use super::{Anchor, Delay, Order, Range, Subscription, TrackState, clamp_combined}; mod ranges; use ranges::Ranges; @@ -248,15 +245,15 @@ impl Subscriptions { /// doesn't, so the lock every subscriber shares is held for what changed alone. fn replace( &mut self, - old: Option<&Subscription>, - new: Option<&Subscription>, + old: Option, + new: Option, moved: (&[Range], &[Range]), ) -> kio::WaiterList { let prev = self.scalars(); // New ranges count before old ones leave, so a range both hold never reads as gone. let mut reshaped = false; let (gone, came) = moved; - if let Some(new) = new { + if let Some(new) = &new { add(&mut self.priority, new.priority); add(&mut self.max_delay, new.budget()); if let Some(min) = new.delay.min { @@ -269,7 +266,7 @@ impl Subscriptions { self.feed += usize::from(new.feed); self.count += 1; } - if let Some(old) = old { + if let Some(old) = &old { remove(&mut self.priority, old.priority); remove(&mut self.max_delay, old.budget()); if let Some(min) = old.delay.min { @@ -332,48 +329,267 @@ fn remove(counts: &mut BTreeMap, value: T) -> bool { last } +/// What one subscriber adds to the aggregate besides its ranges. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(super) struct Contribution { + priority: i8, + delay: Delay, + order: Order, + feed: bool, +} + +impl Contribution { + fn budget(&self) -> Duration { + self.delay.budget() + } +} + +/// A change to a subscriber's preferences that edits its ranges by key: the ranges keyed +/// `retire` go, then each of `add` joins under its key, and the rest stay as they resolved. +/// Costs the ranges it names, however many the subscriber holds. +#[derive(Clone, Debug, Default)] +pub(crate) struct Edit { + pub retire: Vec, + pub add: Vec<(u64, Range)>, + pub priority: i8, + pub delay: Delay, + pub order: Order, +} + +/// A subscriber's preferences as the model holds them: the ranges by the key each was given, +/// so an [`Edit`] costs only what it names, with a log of the keys recent changes touched so +/// a reader catches up on those alone. +pub(in crate::model) struct Prefs { + pub priority: i8, + pub delay: Delay, + pub order: Order, + pub feed: bool, + /// In key order, which is the order given: a whole replacement keys by position, and each + /// key an edit adds is past every key before it. + ranges: BTreeMap, + /// How many ranges start at the oldest or latest group with no end: see [`Self::joins_live`]. + joining: usize, + /// Bumped whenever the ranges change. + pub revision: u64, + /// The keys each change since `base` touched, by revision. Held to twice the ranges, so a + /// reader that fell further behind resolves every range again, at most once per as many + /// changes as there are ranges. + log: VecDeque<(u64, u64)>, + /// The oldest revision [`Self::changes`] can catch a reader up from. + base: u64, +} + +impl Prefs { + fn new(value: Subscription) -> Self { + let mut prefs = Self { + priority: value.priority, + delay: value.delay, + order: value.order, + feed: value.feed, + ranges: BTreeMap::new(), + joining: 0, + revision: 0, + log: VecDeque::new(), + base: 0, + }; + for (key, range) in (0..).zip(value.ranges) { + prefs.insert(key, range); + } + prefs + } + + /// These preferences as a [`Subscription`], its ranges in key order. Costs every range. + pub fn subscription(&self) -> Subscription { + Subscription { + priority: self.priority, + delay: self.delay, + ranges: self.ranges.values().copied().collect(), + order: self.order, + feed: self.feed, + } + } + + pub fn budget(&self) -> Duration { + self.delay.budget() + } + + /// Whether a subscriber joins at the live edge: a range counting back from the latest + /// group and following it. A fixed start resumes a known group instead, such as a route + /// switch picking up where the reader stopped. + pub fn joins_live(&self) -> bool { + self.joining > 0 + } + + /// Every range with its key, in key order. + pub fn ranges(&self) -> impl ExactSizeIterator + Clone + '_ { + self.ranges.iter().map(|(key, range)| (*key, *range)) + } + + /// The range keyed `key`, if held. + pub fn range(&self, key: u64) -> Option { + self.ranges.get(&key).copied() + } + + /// The keys the changes since `revision` touched, or `None` once the log no longer + /// reaches back that far, when the reader resolves every range again. + pub fn changes(&self, revision: u64) -> Option> { + if revision < self.base { + return None; + } + let from = self.log.partition_point(|(at, _)| *at <= revision); + Some(self.log.range(from..).map(|(_, key)| *key).collect()) + } + + fn contribution(&self) -> Contribution { + Contribution { + priority: self.priority, + delay: self.delay, + order: self.order, + feed: self.feed, + } + } + + fn insert(&mut self, key: u64, range: Range) { + self.joining += usize::from(joins(&range)); + if let Some(old) = self.ranges.insert(key, range) { + self.joining -= usize::from(joins(&old)); + } + } + + fn remove(&mut self, key: u64) -> Option { + let range = self.ranges.remove(&key)?; + self.joining -= usize::from(joins(&range)); + Some(range) + } + + /// Note that the ranges keyed `keys` changed. + fn touched(&mut self, keys: impl IntoIterator) { + self.revision += 1; + let revision = self.revision; + self.log.extend(keys.into_iter().map(|key| (revision, key))); + while self.log.len() > 2 * self.ranges.len() + 64 { + let (at, _) = self.log.pop_front().expect("a logged change"); + self.base = at; + } + } + + /// Replace every preference, rekeying the ranges by position when they change: every + /// reader resolves them all again. + fn replace(&mut self, next: Subscription) { + (self.priority, self.delay, self.order, self.feed) = (next.priority, next.delay, next.order, next.feed); + if self.ranges.values().eq(&next.ranges) { + return; + } + self.ranges.clear(); + self.joining = 0; + for (key, range) in (0..).zip(next.ranges) { + self.insert(key, range); + } + self.revision += 1; + self.log.clear(); + self.base = self.revision; + } +} + +/// Whether `range` starts at the oldest or latest group with no end. +fn joins(range: &Range) -> bool { + matches!(range.start, Anchor::Oldest | Anchor::Relative(_)) && range.end.is_none() +} + /// Clones share one contribution, including controls and pending subscriptions. #[derive(Clone)] pub(in crate::model) struct Preferences(Arc); struct Preference { - value: kio::Producer, + value: kio::Producer, subscriptions: Option>, - /// Bumped under the write whenever the ranges change. - ranges_revision: AtomicU64, } impl Preferences { pub(super) fn register(state: kio::Ref<'_, TrackState>, value: Subscription) -> Self { let subscriptions = (!state.is_closed()).then(|| state.subscriptions.clone()); drop(state); + let value = Prefs::new(value); if let Some(subs) = &subscriptions { - let mut wakes = subs.lock().replace(None, Some(&value), (&[], &value.ranges)); + let ranges: Vec = value.ranges.values().copied().collect(); + let mut wakes = subs.lock().replace(None, Some(value.contribution()), (&[], &ranges)); wakes.wake(); } Self(Arc::new(Preference { value: kio::Producer::new(value), subscriptions, - ranges_revision: AtomicU64::new(0), })) } - pub(super) fn update(&self, next: Subscription) -> crate::Result<()> { + /// Replace every preference, as [`super::Subscriber::update`] does: replaced ranges are + /// keyed afresh and resolve again. + pub(in crate::model) fn update(&self, next: Subscription) -> crate::Result<()> { let mut value = self.0.value.write().map_err(|_| crate::Error::Closed)?; - if *value == next { + let old: Vec = value.ranges.values().copied().collect(); + let contribution = value.contribution(); + let next_contribution = Contribution { + priority: next.priority, + delay: next.delay, + order: next.order, + feed: next.feed, + }; + if contribution == next_contribution && old == next.ranges { return Ok(()); } // Found before taking the lock every subscriber shares. - let (gone, came) = moved(&value.ranges, &next.ranges); + let (gone, came) = moved(&old, &next.ranges); + let mut wakes = self.0.subscriptions.as_ref().map(|subs| { + subs.lock() + .replace(Some(contribution), Some(next_contribution), (&gone, &came)) + }); + value.replace(next); + drop(value); + if let Some(wakes) = &mut wakes { + wakes.wake(); + } + Ok(()) + } + + /// Apply `edit`, costing the ranges it names: see [`Edit`]. A key retired that isn't held + /// is ignored, and a key added again replaces its range. + pub(in crate::model) fn edit(&self, edit: Edit) -> crate::Result<()> { + let mut value = self.0.value.write().map_err(|_| crate::Error::Closed)?; + let contribution = value.contribution(); + let next = Contribution { + priority: edit.priority, + delay: edit.delay, + order: edit.order, + ..contribution + }; + if contribution == next && edit.retire.is_empty() && edit.add.is_empty() { + return Ok(()); + } + let mut gone = Vec::new(); + let mut came = Vec::new(); + let mut touched = Vec::with_capacity(edit.retire.len() + edit.add.len()); + for key in edit.retire { + if let Some(range) = value.remove(key) { + gone.push(range); + touched.push(key); + } + } + for (key, range) in edit.add { + if let Some(old) = value.remove(key) { + gone.push(old); + } + value.insert(key, range); + came.push(range); + touched.push(key); + } + (value.priority, value.delay, value.order) = (edit.priority, edit.delay, edit.order); + if !touched.is_empty() { + value.touched(touched); + } let mut wakes = self .0 .subscriptions .as_ref() - .map(|subs| subs.lock().replace(Some(&value), Some(&next), (&gone, &came))); - if value.ranges != next.ranges { - self.0.ranges_revision.fetch_add(1, atomic::Ordering::Relaxed); - } - *value = next; + .map(|subs| subs.lock().replace(Some(contribution), Some(next), (&gone, &came))); drop(value); if let Some(wakes) = &mut wakes { wakes.wake(); @@ -386,24 +602,15 @@ impl Preferences { pub(in crate::model) fn same_channel(&self, other: &Self) -> bool { self.0.value.same_channel(&other.0.value) } - pub(in crate::model) fn read(&self) -> kio::Ref<'_, Subscription> { + pub(in crate::model) fn read(&self) -> kio::Ref<'_, Prefs> { self.0.value.read() } - /// Counts changes to the ranges, so a reader can tell them apart without comparing every - /// range. Read it while holding [`Self::read`] to get the count matching those ranges. - pub(in crate::model) fn ranges_revision(&self) -> u64 { - self.0.ranges_revision.load(atomic::Ordering::Relaxed) - } - pub(in crate::model) fn consume(&self) -> kio::Consumer { + pub(in crate::model) fn consume(&self) -> kio::Consumer { self.0.value.consume() } - pub(in crate::model) fn poll_ref( - &self, - waiter: &kio::Waiter, - f: F, - ) -> Poll>> + pub(in crate::model) fn poll_ref(&self, waiter: &kio::Waiter, f: F) -> Poll>> where - F: FnOnce(&kio::Ref<'_, Subscription>) -> Poll, + F: FnOnce(&kio::Ref<'_, Prefs>) -> Poll, { self.0.value.poll_ref(waiter, f) } @@ -413,19 +620,19 @@ impl Drop for Preference { fn drop(&mut self) { if let Some(subs) = &self.subscriptions { let value = self.value.read(); - let mut wakes = subs.lock().replace(Some(&value), None, (&value.ranges, &[])); + let ranges: Vec = value.ranges.values().copied().collect(); + let mut wakes = subs.lock().replace(Some(value.contribution()), None, (&ranges, &[])); wakes.wake(); } } } #[cfg(test)] -impl From> for Preferences { - fn from(value: kio::Producer) -> Self { +impl From for Preferences { + fn from(value: Subscription) -> Self { Self(Arc::new(Preference { - value, + value: kio::Producer::new(Prefs::new(value)), subscriptions: None, - ranges_revision: AtomicU64::new(0), })) } } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index ce3bb57ffea..517a5d8f78a 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -28,7 +28,7 @@ use std::{ use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; use super::super::delivered::Delivered; -use super::super::subscription::Layered; +use super::super::subscription::Cover; pub(super) use super::fill::Asking; use super::fill::{Ask, FillState, Join}; use super::{Drift, Order, Position, TrackState}; @@ -81,10 +81,10 @@ pub(super) struct Misses { given_up: RangeSet, /// The window revision and cap what this holds was last clipped to, with the window's /// missable groups then, `None` while every group was. - clipped: Option<(u64, Option, Option)>, + clipped: Option<(u64, Option, Option)>, /// The window's missable groups the last probe judged against, `None` while every group - /// was, so a move of only its time ends' runs judges only what they moved over. - view: Option, + /// was, so a moved window diffs this snapshot against its own and judges only what moved. + view: Option, /// Where the track leaves what it evicts that this reader judged cached, once it judged. inbox: Option, /// The track's eviction count this reader last emptied its inbox at. @@ -457,7 +457,7 @@ impl Misses { pub fn retain( &mut self, (from, revision, cap): (u64, u64, Option), - view: Option<&Layered>, + view: Option<&Cover>, admitted: impl FnOnce() -> RangeInclusiveSet, ) { let clipped = self.clipped.replace((revision, cap, view.cloned())); @@ -469,7 +469,7 @@ impl Misses { Some((clipped, at, _)) if (*clipped, *at) == (revision, cap) => Some(Vec::new()), Some((_, at, Some(old))) if *at == cap => { let highest = self.highest().unwrap_or(lowest); - view.and_then(|view| old.gained(view, from, highest)) + view.map(|view| old.gained(view, from, highest)) } _ => None, }; @@ -694,7 +694,7 @@ impl Misses { &mut self, state: &TrackState, id: (u64, u64, Option), - view: Option<&Layered>, + view: Option<&Cover>, named: Option, bound: Option, mut target: impl FnMut(RangeInclusive) -> RangeInclusiveSet, @@ -762,14 +762,14 @@ impl Misses { state.cache.watch_wall(at, waiter); return asks; } - // A window that moved only its time ends' runs keeps its fixed runs, so only what those - // runs moved over is judged again below. - let layered = match (&self.view, view) { - (Some(old), Some(view)) if old.shares_fixed(view) => Some((old.clone(), view)), + // A window that moved keeps most of its runs, so only what it moved over is judged again + // below. + let diffed = match (&self.view, view) { + (Some(old), Some(view)) => Some((old.clone(), view)), _ => None, }; let terms = self.at.is_none_or(|previous| { - (previous.1 != at.1 && layered.is_none()) + (previous.1 != at.1 && diffed.is_none()) || previous.2 != at.2 || previous.4 != at.4 || previous.5 != at.5 @@ -834,16 +834,16 @@ impl Misses { } self.deferred.remove(0..low); } - // What the time ends' runs no longer cover is forgotten, and what they newly cover + // What the window no longer covers is forgotten, and what it newly covers // judged, within what the probe judges: from the floor up to the newest group, // below the cap. let last = id.2.map_or(Some(top), |cap| cap.checked_sub(1).map(|cap| cap.min(top))); - if let Some((old, view)) = &layered + if let Some((old, view)) = &diffed && let Some(last) = last && self.at.is_some_and(|previous| previous.1 != at.1) { let half = |run: RangeInclusive| *run.start()..run.end().saturating_add(1); - for run in old.gained(view, low, last).into_iter().flatten().map(half) { + for run in old.gained(view, low, last).into_iter().map(half) { for set in [ &mut self.judged, &mut self.refused, @@ -854,7 +854,7 @@ impl Misses { } self.deferred.remove(run); } - for run in view.gained(old, low, last).into_iter().flatten().map(half) { + for run in view.gained(old, low, last).into_iter().map(half) { for fresh in self.judged.gaps(&run) { candidates.insert(fresh); } @@ -1101,7 +1101,7 @@ impl Misses { state: &TrackState, id: (u64, u64, Option), at: JudgedAt, - view: Option<&Layered>, + view: Option<&Cover>, target: &mut impl FnMut(RangeInclusive) -> RangeInclusiveSet, taken: &Delivered, drift: &Drift, @@ -2144,7 +2144,7 @@ mod tests { /// cache in any order, on either clock, against a window of one run or several. #[test] fn expiring_by_successor_matches_each_stretch_judged_alone() { - use super::super::super::subscription::{Layered, Runs}; + use super::super::super::subscription::Cover; use crate::Timestamp; /// The old judgement: each stretch between the live groups cached in a wanted run, by @@ -2194,11 +2194,13 @@ mod tests { .unwrap(); let mut groups = Vec::new(); let budget = Duration::from_millis(50 + next(200)); - let runs = |count: u64| { - Runs::new((0..1 + next(count)).map(|_| { - let start = next(KEYS); - start..=start + next(6) - })) + let runs = |count: u64| -> Vec<_> { + (0..1 + next(count)) + .map(|_| { + let start = next(KEYS); + start..=start + next(6) + }) + .collect() }; let servable = match next(3) { 0 => Servable::ALL, @@ -2206,8 +2208,8 @@ mod tests { start: next(8), end: Some(KEYS - next(8)), }, - _ => Servable::layered( - Layered::new(Arc::new(runs(6)), runs(2)), + _ => Servable::covering( + Cover::new(runs(6).into_iter().chain(runs(2))), (next(3) == 0).then(|| next(KEYS + 2)), &Servable::NONE, ), @@ -2267,13 +2269,13 @@ mod tests { assert!(judged > 100, "only {judged} stale runs judged"); } - /// A window whose time ends move only their runs over the same fixed runs is followed by - /// what those runs moved over, and ends up judging and holding exactly what judging the - /// whole window again would: the second reader below is handed an unshared copy of the - /// same fixed runs on each move, so it always judges the window whole. + /// A window edited from the one before is followed by what the edit moved over, and ends + /// up judging and holding exactly what judging the whole window again would: the second + /// reader below forgets its last window before each move, so it always judges the window + /// whole. #[kio::test] - async fn a_moved_layer_is_judged_as_the_whole_window_would_be() { - use super::super::super::subscription::{Layered, Runs}; + async fn a_moved_window_is_judged_as_the_whole_window_would_be() { + use super::super::super::subscription::Cover; let seed = std::cell::Cell::new(0x2545_f491_4f6c_dd1d_u64); let next = |bound: u64| { @@ -2285,11 +2287,13 @@ mod tests { x % bound }; const KEYS: u64 = 40; - let runs = |count: u64| { - Runs::new((0..next(count + 1)).map(|_| { - let start = next(KEYS); - start..=start + next(5) - })) + let runs = |count: u64| -> Vec<_> { + (0..next(count + 1)) + .map(|_| { + let start = next(KEYS); + start..=start + next(5) + }) + .collect() }; let snapshot = |misses: &Misses| { let sets = [ @@ -2325,7 +2329,9 @@ mod tests { } let fills = track.state.read().fills.clone(); fills.lock().add_handler(); - let fixed = Arc::new(runs(6)); + let fixed = runs(6); + let mut shared = Cover::new(fixed.clone()); + let mut moving: Vec> = Vec::new(); let cap = (next(3) == 0).then(|| next(KEYS + 4)); let asking = Asking { priority: 0, @@ -2336,8 +2342,14 @@ mod tests { let (mut given_moved, mut given_whole) = (Vec::new(), Vec::new()); for revision in 0..8 { let layer = runs(3); - let shared = Layered::new(fixed.clone(), layer.clone()); - let unshared = Layered::new(Arc::new((*fixed).clone()), layer); + for run in std::mem::replace(&mut moving, layer.clone()) { + shared.add(run, -1); + } + for run in &layer { + shared.add(run.clone(), 1); + } + let unshared = Cover::new(fixed.iter().chain(&layer).cloned()); + (whole.view, whole.clipped) = (None, None); let id = (0, revision, cap); for (misses, view, given_up) in [ (&mut moved, &shared, &mut given_moved), @@ -2380,17 +2392,12 @@ mod tests { assert_eq!( snapshot(&moved), snapshot(&whole), - "round {round} revision {revision}: {fixed:?} under {:?}", - shared_layer(&moved) + "round {round} revision {revision}: {fixed:?} under {moving:?}", ); assert_eq!(given_moved, given_whole, "round {round} revision {revision}"); } } assert!(asked > 100, "only {asked} runs asked"); - - fn shared_layer(misses: &Misses) -> Option { - misses.view.as_ref().map(|view| view.layer().clone()) - } } /// A reader waiting on many fills looks only at the ones that answered, yet each still