From a3be0d33bbe18e72bcdeac9b951713e9103ae2de Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:24:25 -0800 Subject: [PATCH 01/10] chore(quest): claim quest/m1/precut-fixes/lite07-range-union.md Quest-Claim: 18bb7e0c17b6eb32 From 3f5b32f36b2eb588385c1551bb6d3bbd44b9fb88 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:51:08 -0800 Subject: [PATCH 02/10] fix(lite): a lite-07 subscriber widens ranges that don't fit one message Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscribe.rs | 22 +- rs/moq-net/src/lite/subscriber.rs | 80 +++++- rs/moq-net/src/lite/subscriber/fills.rs | 22 +- rs/moq-net/src/lite/subscriber/fit.rs | 271 ++++++++++++++++++ rs/moq-net/tests/integration/main.rs | 1 + .../tests/integration/range_union_fit.rs | 171 +++++++++++ 6 files changed, 542 insertions(+), 25 deletions(-) create mode 100644 rs/moq-net/src/lite/subscriber/fit.rs create mode 100644 rs/moq-net/tests/integration/range_union_fit.rs diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index a873c6b9b7d..d880690c025 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -338,7 +338,7 @@ fn encode_range_list(w: &mut Encoder<'_>, ranges: &[Range]) -> Result<(), Encode } /// The bytes a lite-07 range takes on the wire, or `None` when it can't go out. -fn range_size(range: &Range, form: Form) -> Option { +pub(super) fn range_size(range: &Range, form: Form) -> Option { let size = |value| varint::size(value, form).ok(); let anchor = |anchor, end| -> Option { let (kind, value) = wire_anchor(anchor, end).ok()?; @@ -878,6 +878,11 @@ impl LiveRanges { Some(range) } + /// The Range ID the next range added takes. + pub fn next(&self) -> u64 { + self.next + } + /// Every live range with its Range ID, in Range ID order. pub fn numbered(&self) -> Vec<(u64, Range)> { self.iter().map(|(id, range)| (id, *range)).collect() @@ -1006,15 +1011,20 @@ impl LiveRanges { }) .copied() .collect(); + let add: Vec = added.iter().map(|(_, range)| *range).collect(); + // Checked before anything changes, so a refused edit leaves the ranges as last sent. + let form = self.version.into(); + let size = |range: &Range| range_size(range, form).unwrap_or(usize::MAX); + let removed = remove.iter().map(|id| size(&self.live[id])).fold(0, usize::saturating_add); + let size = add.iter().map(size).fold(self.size.saturating_sub(removed), usize::saturating_add); + if size > super::message::MAX_MESSAGE_SIZE { + return Err(EncodeError::TooLarge); + } for &id in &remove { self.remove(id); } - let ids = (self.next..).zip(added.iter().map(|(key, _)| *key)).collect(); - let add: Vec = added.into_iter().map(|(_, range)| range).collect(); + let ids = (self.next..).zip(added.into_iter().map(|(key, _)| key)).collect(); self.add(add.iter().copied()); - if self.size > super::message::MAX_MESSAGE_SIZE { - return Err(EncodeError::TooLarge); - } Ok((RangeEdit::Edit { remove, add }, ids)) } } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 36f45b791e9..a34aee817ce 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10,7 +10,7 @@ use std::{ use crate::{ Error, Path, Timescale, Timestamp, bandwidth, - coding::{Decode as _, Reader, Recv, Stream}, + coding::{Decode as _, Encode as _, Reader, Recv, Stream}, lite, track::{Anchor, Position, Subscription}, util::SendCell, @@ -23,6 +23,7 @@ use kio::Lock; mod fills; use fills::{FillAsk, RangeFills}; +mod fit; pub(super) mod route_stream; mod routed; @@ -3466,7 +3467,7 @@ mod tests { } else { let begin = h .serve - .begin_subscription(&mut track, &mut Sub::None, lowered, &FillAsk::default(), true); + .begin_subscription(&mut track, &mut Sub::None, lowered, &mut RangeFills::default(), true); assert!(matches!(begin, Ok(Begin::Establish(_)))); } @@ -11121,7 +11122,7 @@ impl TrackServe { producer: &mut track::Producer, sub: &mut Sub, pref: Option, - fills: &FillAsk, + range_fills: &mut RangeFills, supports_update: bool, ) -> Result { let opening = matches!(sub, Sub::None); @@ -11129,8 +11130,9 @@ impl TrackServe { // while they are out keeps what it asked until they are answered, rather than retire // the live feed's ranges under a subscription that still runs. let none = FillAsk::default(); - let fills = match self.subscriber.version.has_ranges() { - true => fills, + let ranged = self.subscriber.version.has_ranges(); + let fills = match ranged { + true => &range_fills.ask, false => &none, }; let pref = match (pref, &*sub) { @@ -11138,12 +11140,37 @@ impl TrackServe { (pref, _) => pref, }; let request = self.wire_request(pref, opening, producer.idle_newest()); + // The demand's ranges and the fills' groups, widened together until they fit. + let (request, groups) = match ranged { + true => { + let next = match &*sub { + Sub::Active(active) => active.live.next(), + Sub::None => 0, + }; + let (demand, demand_ranges) = match request { + Some((demand, ranges)) => (Some(demand), ranges), + None => (None, Vec::new()), + }; + let count = demand_ranges.len() + fills.groups.iter().count(); + let fields = self.subscribe_fields(demand.as_ref().unwrap_or(&fills.subscription())); + let budget = fit::Budget::new(self.subscriber.version, fields, next, count); + let (demand_ranges, groups) = fit::fit(demand_ranges, &fills.groups, &range_fills.avoid(), budget)?; + // A fill's range starting past its run speaks only while that run is asked whole. + range_fills.retire_skipped(&groups); + (demand.map(|demand| (demand, demand_ranges)), groups) + } + false => (request, Default::default()), + }; + let fills = match ranged { + true => &range_fills.ask, + false => &none, + }; // What goes on the wire: the demand's ranges, then the fills', under the demand's // parameters. The demand alone sets the floor. let request = match request { Some((demand, demand_ranges)) => { let mut ranges = demand_ranges.clone(); - ranges.extend(fills.wire()); + ranges.extend(FillAsk::wire(&groups)); Some(Wire { subscription: fills.merge(demand.clone()), demand: Some(demand), @@ -11156,7 +11183,7 @@ impl TrackServe { subscription: fills.subscription(), demand: None, demand_ranges: Vec::new(), - ranges: fills.wire().collect(), + ranges: FillAsk::wire(&groups).collect(), }), }; @@ -11167,7 +11194,7 @@ impl TrackServe { // stale run an earlier subscription reported is no verdict on them. Sub::None => { producer.supersede_stale(); - Ok(Begin::Establish(self.prepare_establish(producer, wire, &fills.groups))) + Ok(Begin::Establish(self.prepare_establish(producer, wire, &groups))) } Sub::Active(active) => { let Wire { @@ -11203,14 +11230,14 @@ impl TrackServe { tail.demand(groups, now); } // So do the groups a fill newly asks for. - for groups in fills.groups.iter() { + for groups in groups.iter() { let asked = active.fill_groups.gaps(groups).collect::>(); for groups in asked { tail.demand(groups, now); } } } - active.fill_groups = fills.groups.clone(); + active.fill_groups = groups; active.demand = demand; active.start = subscription.start(); if supports_update { @@ -11256,6 +11283,27 @@ impl TrackServe { } } + /// The bytes a SUBSCRIBE for `subscription` takes besides its ranges, at the widest id, + /// measured with the smallest range, since it carries at least one. + fn subscribe_fields(&self, subscription: &Subscription) -> usize { + let msg = lite::Subscribe { + instance: self.instance.clone(), + node: self.node, + depth: self.depth, + id: u64::MAX, + broadcast: self.path.clone(), + track: self.name.as_str().into(), + priority: subscription.priority, + max_delay: subscription.delay.max, + min_delay: subscription.delay.min, + ranges: vec![lite::Range::OLDEST], + order: subscription.order, + }; + // One that can't go out leaves no room, and fails as it would have. + msg.encode_bytes(self.subscriber.version) + .map_or(usize::MAX, |bytes| bytes.len()) + } + /// What a SUBSCRIBE carries for the demand `pref`: the subscription and its ranges, or /// `None` when it asks for nothing. /// @@ -11304,6 +11352,14 @@ impl TrackServe { let Some((subscription, ranges)) = self.wire_request(Some(pref.clone()), true, None) else { return Ok(None); }; + let ranges = match self.subscriber.version.has_ranges() { + true => { + let fields = self.subscribe_fields(&subscription); + let budget = fit::Budget::new(self.subscriber.version, fields, 0, ranges.len()); + fit::fit(ranges, &Default::default(), &Default::default(), budget)?.0 + } + false => ranges, + }; let establish = self.register(subscription, ranges, None); Ok(Some(Pipelined { pref, @@ -11463,7 +11519,7 @@ impl TrackServe { pref: Option, supports_update: bool, ) -> Result<(), Error> { - match self.begin_subscription(producer, sub, pref, &FillAsk::default(), supports_update)? { + match self.begin_subscription(producer, sub, pref, &mut RangeFills::default(), supports_update)? { Begin::Establish(est) => { let mut est = Box::new(est); let id = est.id; @@ -12795,7 +12851,7 @@ impl ServeLoop { &mut self.serving, &mut self.sub, pref, - &self.range_fills.ask, + &mut self.range_fills, self.supports_update, )? { Begin::Establish(est) => { diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index a9294a5c816..8eda571c896 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -57,10 +57,10 @@ impl FillAsk { } } - /// The bounded ranges the groups take on the wire, each ending before a group's first + /// The bounded ranges `groups` take on the wire, each ending before a group's first /// frame, as `wire_ranges` ends an absolute one. - pub fn wire(&self) -> impl Iterator + '_ { - self.groups.iter().map(|groups| lite::Range { + pub fn wire(groups: &RangeSet) -> impl Iterator + '_ { + groups.iter().map(|groups| lite::Range { start: Some(lite::Anchor::Absolute(Position::group(groups.start))), end: Some(lite::Anchor::Absolute(Position::group(groups.end))), }) @@ -447,14 +447,22 @@ impl RangeFills { if ask == self.ask { return Ok(false); } - self.retire_skipped(&ask.groups); self.ask = ask; Ok(true) } - /// Asking for `asked` from now on retires the range of each run not asked whole any more, - /// and what its start skipped with it. - fn retire_skipped(&mut self, asked: &RangeSet) { + /// The groups the ask leaves out on purpose: refused, or recovered on their own. + pub fn avoid(&self) -> RangeSet { + let mut avoid = self.recovering.clone(); + for (groups, _) in self.refused.iter() { + avoid.insert(groups.clone()); + } + avoid + } + + /// Asking for `asked` from now on, the runs the subscription carries, retires the range + /// of each run not asked whole any more, and what its start skipped with it. + pub fn retire_skipped(&mut self, asked: &RangeSet) { let retired: Vec<_> = self .skipped .iter() diff --git a/rs/moq-net/src/lite/subscriber/fit.rs b/rs/moq-net/src/lite/subscriber/fit.rs new file mode 100644 index 00000000000..63e3e8096fc --- /dev/null +++ b/rs/moq-net/src/lite/subscriber/fit.rs @@ -0,0 +1,271 @@ +//! Widening a lite-07 subscription's ranges until they fit the message that carries them. +//! +//! A relay asks its upstream for the union of every viewer's ranges, and each viewer's +//! request fits one message while the union need not. Rather than give back the copy every +//! viewer reads through, the relay asks for a covering superset: the narrowest gaps close +//! first, so the fewest unwanted groups are asked for. Each viewer still reads only its own +//! ranges from the copy. + +use rangemap::RangeSet; + +use crate::{ + EncodeError, + coding::{Form, varint}, + lite::{self, Anchor, Range, message::MAX_MESSAGE_SIZE}, + track::Position, +}; + +/// The most a SUBSCRIBE_UPDATE's fields besides its ranges take, size prefix included: nine +/// varints of at most nine bytes, and the priority byte. +const UPDATE_FIELDS: usize = 9 * 9 + 1; + +/// What ranges may take on the wire. +#[derive(Clone, Copy, Debug)] +pub(super) struct Budget { + /// The bytes every range and its Range ID may take together. + bytes: usize, + form: Form, + /// The bytes the largest Range ID any of the ranges may take. + id: usize, +} + +impl Budget { + /// The budget for ranges a subscription takes Range IDs below `next` for, whose SUBSCRIBE + /// carries `fields` bytes besides its ranges, `count` the most ranges it asks for. + /// + /// Half a message, so that the ranges sent last, retired by Range ID, and the ranges + /// replacing them fit one SUBSCRIBE_UPDATE however little the two share: a relay never + /// narrows its ask in between, which would reset group streams still arriving. + pub fn new(version: lite::Version, fields: usize, next: u64, count: usize) -> Self { + let form = version.into(); + let half = (MAX_MESSAGE_SIZE - UPDATE_FIELDS) / 2; + Self { + bytes: half.min(MAX_MESSAGE_SIZE.saturating_sub(fields)), + form, + id: varint::size(next.saturating_add(count as u64), form).unwrap_or(9), + } + } + + /// What `range` costs, or `None` when it can't go out. + fn cost(&self, range: &Range) -> Option { + Some(lite::range_size(range, self.form)? + self.id) + } + + fn span(&self, start: Position, end: Option) -> usize { + self.cost(&span(start, end)).unwrap_or(usize::MAX) + } +} + +fn span(start: Position, end: Option) -> Range { + Range { + start: Some(Anchor::Absolute(start)), + end: end.map(Anchor::Absolute), + } +} + +/// `demand`'s ranges and the `fills` groups, each widened only as far as both must be to fit +/// `budget`. A gap between fills closes across `avoid`, groups the fills leave out on +/// purpose, only once every other gap has. +/// +/// Fails when even one range per kind of start would not fit, which only a broadcast path +/// or track name filling the message leaves. +pub(super) fn fit( + demand: Vec, + fills: &RangeSet, + avoid: &RangeSet, + budget: Budget, +) -> Result<(Vec, RangeSet), EncodeError> { + let fill = |groups: &std::ops::Range| (Position::group(groups.start), Some(Position::group(groups.end))); + let costs: Option> = demand + .iter() + .copied() + .chain(fills.iter().map(|groups| { + let (start, end) = fill(groups); + span(start, end) + })) + .map(|range| budget.cost(&range)) + .collect(); + // One that can't go out fails when it is encoded, as it would unfitted. + let Some(costs) = costs else { + return Ok((demand, fills.clone())); + }; + if costs.into_iter().fold(0, usize::saturating_add) <= budget.bytes { + return Ok((demand, fills.clone())); + } + + let mut spans = Vec::new(); + let mut others = Vec::new(); + for range in demand { + match range.start { + Some(Anchor::Absolute(start)) => spans.push(( + start, + match range.end { + Some(Anchor::Absolute(end)) => Some(end), + _ => None, + }, + )), + _ => others.push(range), + } + } + let spans = Spans::new(spans, budget, |_| false); + let fills = Spans::new(fills.iter().map(fill).collect(), budget, |gap| avoid.overlaps(&gap)); + + // The other ranges count back from the latest group or start at the oldest, so no gap + // between them is measured: when even one span per list leaves them no room, each kind + // collapses to the one range covering it. + let cost = |ranges: &[Range]| -> usize { + ranges + .iter() + .map(|range| budget.cost(range).unwrap_or(usize::MAX)) + .fold(0, usize::saturating_add) + }; + if cost(&others) + .saturating_add(spans.least()) + .saturating_add(fills.least()) + > budget.bytes + { + others = collapse(others); + } + let mut total = cost(&others).saturating_add(spans.total).saturating_add(fills.total); + + // Close the narrowest gaps first, of either list, until everything fits. + let mut gaps: Vec<_> = spans.gaps(false).chain(fills.gaps(true)).collect(); + gaps.sort_unstable(); + let mut lists = [spans, fills]; + for (_, fill, index) in gaps { + if total <= budget.bytes { + break; + } + let list = &mut lists[usize::from(fill)]; + total -= list.total; + list.join(index); + total += list.total; + } + if total > budget.bytes { + return Err(EncodeError::TooLarge); + } + + let [spans, fills] = lists; + let mut ranges: Vec = spans.ranges().map(|(start, end)| span(start, end)).collect(); + ranges.extend(others); + let groups = fills.ranges().map(|(start, end)| start.group..end.expect("fills are bounded").group); + Ok((ranges, groups.collect())) +} + +/// Disjoint spans in order, each the run of the input spans from one start to its chain's end. +struct Spans { + spans: Vec<(Position, Option)>, + /// The last span of the chain starting at each chain's first, and the first of the chain + /// ending at each chain's last. + end: Vec, + start: Vec, + /// Whether the gap after each span closes only once every other gap has. + last: Vec, + total: usize, + budget: Budget, +} + +impl Spans { + fn new( + mut input: Vec<(Position, Option)>, + budget: Budget, + last: impl Fn(std::ops::Range) -> bool, + ) -> Self { + input.sort_unstable(); + // Spans that overlap or touch cost less as one, and ask for nothing more. + let mut spans: Vec<(Position, Option)> = Vec::with_capacity(input.len()); + for (start, end) in input { + match spans.last_mut() { + Some((_, last)) if last.is_none_or(|last| last >= start) => { + *last = match (*last, end) { + (Some(last), Some(end)) => Some(last.max(end)), + _ => None, + }; + } + _ => spans.push((start, end)), + } + } + let last = spans + .windows(2) + .map(|pair| last(pair[0].1.expect("only the last is open").group..pair[1].0.group)) + .collect(); + let total = spans.iter().map(|(start, end)| budget.span(*start, *end)).sum(); + Self { + end: (0..spans.len()).collect(), + start: (0..spans.len()).collect(), + spans, + last, + total, + budget, + } + } + + /// What the one span covering every span costs, or nothing when there are none. + fn least(&self) -> usize { + match (self.spans.first(), self.spans.last()) { + (Some((start, _)), Some((_, end))) => self.budget.span(*start, *end), + _ => 0, + } + } + + /// Every gap: how late it closes, `tag`, and the span before it. + fn gaps(&self, tag: bool) -> impl Iterator + '_ { + self.spans.windows(2).enumerate().map(move |(index, pair)| { + let end = pair[0].1.expect("only the last is open"); + ((self.last[index], pair[1].0.group.saturating_sub(end.group)), tag, index) + }) + } + + /// Close the gap after span `index`, joining the chains on either side. + fn join(&mut self, index: usize) { + let first = self.start[index]; + let last = self.end[index + 1]; + self.total -= self.budget.span(self.spans[first].0, self.spans[index].1); + self.total -= self.budget.span(self.spans[index + 1].0, self.spans[last].1); + self.total += self.budget.span(self.spans[first].0, self.spans[last].1); + self.end[first] = last; + self.start[last] = first; + } + + /// Each chain as the one span covering it. + fn ranges(&self) -> impl Iterator)> + '_ { + let mut index = 0; + std::iter::from_fn(move || { + let first = index; + let last = *self.end.get(first)?; + index = last + 1; + Some((self.spans[first].0, self.spans[last].1)) + }) + } +} + +/// One range per kind of start covering every range of that kind: from the earliest start +/// to the latest end. +fn collapse(ranges: Vec) -> Vec { + let kind = |range: &Range| match range.start { + None => 0, + Some(Anchor::Relative(_)) => 1, + Some(Anchor::Time(_)) => 2, + Some(Anchor::Absolute(_)) => 3, + }; + let mut kinds: Vec = Vec::new(); + for range in ranges { + let Some(cover) = kinds.iter_mut().find(|cover| kind(cover) == kind(&range)) else { + kinds.push(range); + continue; + }; + cover.start = match (cover.start, range.start) { + (Some(Anchor::Relative(a)), Some(Anchor::Relative(b))) => Some(Anchor::Relative(a.max(b))), + (Some(Anchor::Time(a)), Some(Anchor::Time(b))) => Some(Anchor::Time(a.min(b))), + (Some(Anchor::Absolute(a)), Some(Anchor::Absolute(b))) => Some(Anchor::Absolute(a.min(b))), + (start, _) => start, + }; + cover.end = match (cover.end, range.end) { + (Some(Anchor::Absolute(a)), Some(Anchor::Absolute(b))) => Some(Anchor::Absolute(a.max(b))), + (Some(Anchor::Time(a)), Some(Anchor::Time(b))) => Some(Anchor::Time(a.max(b))), + // Ends of different kinds can't be ordered without the latest group: follow the edge. + _ => None, + }; + } + kinds +} diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 94c0937d9bd..742cdd074ea 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -63,6 +63,7 @@ mod quiet_catalog; mod range_anchors; mod range_fill; mod range_read; +mod range_union_fit; mod received_rewind; mod rejoin; mod relay_upstream_close; diff --git a/rs/moq-net/tests/integration/range_union_fit.rs b/rs/moq-net/tests/integration/range_union_fit.rs new file mode 100644 index 00000000000..826ca3730cd --- /dev/null +++ b/rs/moq-net/tests/integration/range_union_fit.rs @@ -0,0 +1,171 @@ +#![cfg(feature = "async")] + +//! A lite-07 relay whose viewers' ranges, merged, outgrow one message asks its upstream for a +//! covering superset rather than give back the copy every viewer reads through. + +use crate::support; + +use std::time::Duration; + +use moq_net::track::{Delay, Info, Range, Subscription}; +use moq_net::{Timestamp, Version}; +use support::harness::{MockConnectOptions, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(10); +const VERSION: &str = "moq-lite-07-wip"; + +/// Group keys past 2^32, so each range's times take five bytes on the wire. +const BASE: u64 = 1 << 32; +/// Sparse viewers, each asking for every `VIEWERS * 2`th group from its own offset. +const VIEWERS: u64 = 3; +/// Single-group ranges per sparse viewer: each viewer's fits what a subscriber asks in one +/// message, and all together outgrow even a whole message. +const RANGES: u64 = 2_000; + +fn produce_origin(hop: u64) -> moq_net::origin::Producer { + let (producer, driver) = + moq_net::origin::Producer::new(moq_net::origin::Config::new(std::num::NonZeroU64::new(hop).unwrap())); + support::harness::spawn(driver); + producer.with_proxy(true) +} + +fn write_group(track: &moq_net::track::Producer, key: u64) { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group + .write_frame(Timestamp::from_millis(key).unwrap(), &b"x"[..]) + .unwrap(); + group.finish().unwrap(); +} + +/// A viewer of the relay, on a session of its own. +async fn viewer(relay: &moq_net::origin::Producer, hop: u64) -> (moq_net::broadcast::Consumer, impl Sized) { + let origin = produce_origin(hop); + let mut options = MockConnectOptions::new(VERSION.parse::().unwrap()); + options.server_publish = Some(relay.consume()); + options.client_subscribe = Some(origin.clone()); + let session = connect_mock(options).await; + let broadcast = kio::clock::timeout(TIMEOUT, origin.consume().follow("bcast", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + (broadcast, (session, origin)) +} + +/// Every `VIEWERS * 2`th group from `offset`, one range each, so no two viewers' ranges touch. +fn sparse(offset: u64) -> Vec { + (0..RANGES) + .map(|index| BASE + 2 * VIEWERS * index + offset) + .map(|key| support::keys(key..=key)) + .collect() +} + +/// Whether `ranges` asks for every group `want` does. +fn covers(ranges: &[Range], want: &[Range]) -> bool { + want.iter().all(|want| { + let moq_net::track::Anchor::Time(key) = want.start else { + unreachable!() + }; + ranges.iter().any(|range| { + let start = match range.start { + moq_net::track::Anchor::Time(start) => start <= key, + _ => false, + }; + let end = match range.end { + Some(moq_net::track::Anchor::Time(end)) => key <= end, + None => true, + _ => false, + }; + start && end + }) + }) +} + +/// Ranges no group resolves yet reach the publisher as asked, so its view is what the relay +/// sent. +#[kio::test] +async fn a_union_too_large_for_one_message_asks_a_covering_superset() { + let publisher = produce_origin(1); + let broadcast = publisher + .route("bcast", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let info = Info::default() + .with_timescale(moq_net::Timescale::MILLI) + .with_max_age(None); + let track = broadcast.broadcast().create_track("quiet", info).unwrap(); + broadcast.announce().unwrap(); + + let relay = produce_origin(2); + let mut options = MockConnectOptions::new(VERSION.parse::().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(relay.clone()); + let _upstream = connect_mock(options).await; + + let mut viewers = Vec::new(); + let mut readers = Vec::new(); + let mut asked = Vec::new(); + for index in 0..VIEWERS { + let (broadcast, session) = viewer(&relay, 4 + index).await; + let ranges = sparse(2 * index); + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_ranges(ranges.clone()); + readers.push( + broadcast + .track("quiet") + .unwrap() + .subscribe(subscription) + .await + .expect("subscribe"), + ); + asked.extend(ranges); + viewers.push((broadcast, session)); + } + + // A plain viewer joins the copy the sparse ones share, asking for everything past them. + let key = BASE + 2 * VIEWERS * RANGES; + let (plain, _plain) = viewer(&relay, 3).await; + let mut live = plain + .track("quiet") + .unwrap() + .subscribe(Subscription::default().with_ranges([support::keys(key..)])) + .await + .expect("subscribe"); + + // The publisher is asked for every group a viewer wants, in fewer ranges, none reaching + // below them. + let mut demand = track.clone(); + kio::clock::timeout(TIMEOUT, async { + loop { + if demand + .subscription() + .is_some_and(|subscription| covers(&subscription.ranges, &asked)) + { + break; + } + demand.subscription_changed().await.unwrap(); + } + }) + .await + .expect("the union never reached the publisher"); + let ranges = track.subscription().unwrap().ranges; + assert!(ranges.len() < asked.len(), "coarsened to {} ranges", ranges.len()); + assert!( + ranges + .iter() + .all(|range| matches!(range.start, moq_net::track::Anchor::Time(start) if start >= BASE)), + "nothing below the viewers' ranges" + ); + + // Each viewer reads only its own groups, none of the coarsened extra. + write_group(&track, BASE + 2); + write_group(&track, BASE + 3); + write_group(&track, BASE + 2 * VIEWERS + 2); + write_group(&track, key); + for want in [BASE + 2, BASE + 2 * VIEWERS + 2] { + let got = kio::clock::timeout(TIMEOUT, readers[1].recv_group()).await.unwrap().unwrap(); + assert_eq!(got.map(|group| group.start), Some(want)); + } + let got = kio::clock::timeout(TIMEOUT, live.recv_group()).await.unwrap().unwrap(); + assert_eq!(got.map(|group| group.start), Some(key), "the plain viewer keeps receiving"); +} From b4f3ebeabf438b8f80c372016461f28dc44d0e55 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:55:11 -0800 Subject: [PATCH 03/10] test(lite): fit's budget holds any edit between fitted sets Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscribe.rs | 23 ++++ rs/moq-net/src/lite/subscriber/fit.rs | 176 ++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index d880690c025..5e7e9dda2d7 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -1886,6 +1886,29 @@ mod test { assert!(refused, "the live ranges are held to one SUBSCRIBE"); } + /// An edit to ranges too large for a SUBSCRIBE is refused before anything changes, so the + /// live ranges stay those last sent and the next edit is made against them. + #[test] + fn lite07_a_refused_edit_leaves_the_ranges_as_sent() { + let mut live = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); + let wide: Vec = (0..super::super::message::MAX_MESSAGE_SIZE as u64 / 4) + .map(|group| Range { + start: absolute(group << 20, 0), + end: absolute((group << 20) + 1, 0), + }) + .collect(); + assert!(matches!(live.edit(&wide), Err(EncodeError::TooLarge))); + assert_eq!(live.numbered(), [(0, Range::OLDEST)]); + assert_eq!(live.next(), 1); + assert_eq!( + live.edit(&wide[..2]).unwrap(), + RangeEdit::Edit { + remove: vec![0], + add: wide[..2].to_vec(), + } + ); + } + /// SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it. #[test] fn subscribe_started_is_type_4_on_lite07() { diff --git a/rs/moq-net/src/lite/subscriber/fit.rs b/rs/moq-net/src/lite/subscriber/fit.rs index 63e3e8096fc..7d4e0831834 100644 --- a/rs/moq-net/src/lite/subscriber/fit.rs +++ b/rs/moq-net/src/lite/subscriber/fit.rs @@ -8,6 +8,9 @@ use rangemap::RangeSet; +#[cfg(test)] +use super::FillAsk; + use crate::{ EncodeError, coding::{Form, varint}, @@ -269,3 +272,176 @@ fn collapse(ranges: Vec) -> Vec { } kinds } + +#[cfg(test)] +mod tests { + use super::*; + use rand::{RngExt, SeedableRng}; + + fn budget(bytes: usize) -> Budget { + Budget { + bytes, + form: lite::Version::Lite07.into(), + id: 2, + } + } + + fn total(budget: Budget, ranges: &[Range], groups: &RangeSet) -> usize { + let fills = FillAsk::wire(groups); + ranges.iter().copied().chain(fills).map(|range| budget.cost(&range).unwrap()).sum() + } + + /// Whether `outer` asks for everything `inner` does, judged without the latest group. + fn covers(outer: &Range, inner: &Range) -> bool { + let start = match (outer.start, inner.start) { + (None, _) => true, + (Some(Anchor::Relative(outer)), Some(Anchor::Relative(inner))) => outer >= inner, + (Some(Anchor::Absolute(outer)), Some(Anchor::Absolute(inner))) => outer <= inner, + (Some(Anchor::Time(outer)), Some(Anchor::Time(inner))) => outer <= inner, + _ => false, + }; + let end = match (outer.end, inner.end) { + (None, _) => true, + (Some(Anchor::Absolute(outer)), Some(Anchor::Absolute(inner))) => outer >= inner, + (Some(Anchor::Time(outer)), Some(Anchor::Time(inner))) => outer >= inner, + (Some(Anchor::Relative(outer)), Some(Anchor::Relative(inner))) => outer <= inner, + _ => false, + }; + start && end + } + + fn random(rng: &mut rand::rngs::StdRng) -> (Vec, RangeSet, RangeSet) { + let group = |rng: &mut rand::rngs::StdRng| rng.random_range(0..2_000u64) << rng.random_range(0..30); + let mut demand = Vec::new(); + for _ in 0..rng.random_range(0..300) { + let start = group(rng); + let end = (rng.random_bool(0.9)).then(|| Position::group(start + rng.random_range(1..50))); + demand.push(match rng.random_range(0..10) { + 0 => Range { + start: Some(Anchor::Relative(rng.random_range(0..20))), + end: end.map(Anchor::Absolute), + }, + 1 => Range { + start: None, + end: end.map(Anchor::Absolute), + }, + _ => span(Position::group(start), end), + }); + } + let mut fills = RangeSet::new(); + let mut avoid = RangeSet::new(); + for _ in 0..rng.random_range(0..300) { + let start = group(rng); + let groups = start..start + rng.random_range(1..20); + match rng.random_bool(0.8) { + true => fills.insert(groups), + false => avoid.insert(groups), + } + } + for groups in avoid.iter() { + fills.remove(groups.clone()); + } + (demand, fills, avoid) + } + + /// What fits stays as asked; what doesn't is widened to a covering superset that fits, + /// never asking for a group the fills avoid while a narrower choice remains. + #[test] + fn widens_only_what_does_not_fit() { + let mut rng = rand::rngs::StdRng::seed_from_u64(0x666974); + for step in 0..2_000 { + let (demand, fills, avoid) = random(&mut rng); + let budget = budget(rng.random_range(200..4_000)); + let before = total(budget, &demand, &fills); + let (ranges, groups) = fit(demand.clone(), &fills, &avoid, budget).unwrap(); + if before <= budget.bytes { + assert_eq!((&ranges, &groups), (&demand, &fills), "step {step}"); + continue; + } + assert!(total(budget, &ranges, &groups) <= budget.bytes, "step {step}"); + for range in &demand { + assert!(ranges.iter().any(|outer| covers(outer, range)), "step {step}: {range:?}"); + } + for run in fills.iter() { + assert!(groups.gaps(run).next().is_none(), "step {step}: {run:?}"); + } + } + } + + /// Two fitted sets fit one SUBSCRIBE_UPDATE between them however little they share, at its + /// widest fields, and either fits a SUBSCRIBE. + #[test] + fn any_edit_between_fitted_ranges_fits_one_message() { + use crate::coding::Encode as _; + let version = lite::Version::Lite07; + let widest = |ranges| lite::SubscribeUpdate { + wanted_group: Some(u64::MAX - 1), + priority: i8::MIN, + max_delay: Some(std::time::Duration::from_millis(u64::MAX - 1)), + min_delay: Some(std::time::Duration::from_millis(u64::MAX - 1)), + ranges, + order: crate::track::Order::Descending, + }; + let empty = lite::RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }; + assert!(widest(empty).encode_bytes(version).unwrap().len() <= UPDATE_FIELDS); + + // Sets of many short ranges and of fewer long ones, alternately, so the IDs one retires + // outweigh what the other's ranges leave. + let mut rng = rand::rngs::StdRng::seed_from_u64(0x65646974); + let wide = |rng: &mut rand::rngs::StdRng, next: u64, bits: u32| { + let ranges: Vec = (0..10_000) + .map(|_| { + let start = rng.random_range(0..1u64 << bits); + span(Position::group(start), Some(Position::group(start + 1))) + }) + .collect(); + let budget = Budget::new(version, 0, next, ranges.len()); + fit(ranges, &RangeSet::new(), &RangeSet::new(), budget).unwrap().0 + }; + let mut live = lite::LiveRanges::new(version, &wide(&mut rng, 0, 14)); + for round in 0..4 { + let next = wide(&mut rng, live.next(), [50, 14][round % 2]); + let edit = live.edit(&next).unwrap(); + widest(edit).encode_bytes(version).unwrap(); + } + } + + /// Each kind of start collapses to the one range covering it once nothing else fits. + #[test] + fn collapses_ranges_with_no_gap_to_close() { + let relative: Vec = (0..200) + .map(|back| Range { + start: Some(Anchor::Relative(back)), + end: Some(Anchor::Absolute(Position::group(1_000 + back))), + }) + .collect(); + let (ranges, _) = fit(relative.clone(), &RangeSet::new(), &RangeSet::new(), budget(100)).unwrap(); + assert_eq!( + ranges, + [Range { + start: Some(Anchor::Relative(199)), + end: Some(Anchor::Absolute(Position::group(1_199))), + }] + ); + assert!(matches!( + fit(relative, &RangeSet::new(), &RangeSet::new(), budget(4)), + Err(EncodeError::TooLarge) + )); + } + + /// The narrowest gaps close first, so the fewest unwanted groups are asked for. + #[test] + fn closes_the_narrowest_gaps_first() { + let groups = |runs: &[std::ops::Range]| runs.iter().cloned().collect::>(); + let fills = groups(&[0..1, 2..3, 100..101, 103..104, 1_000..1_001]); + let fits = total(budget(0), &[], &groups(&[0..3, 100..104, 1_000..1_001])); + let (_, got) = fit(Vec::new(), &fills, &RangeSet::new(), budget(fits)).unwrap(); + assert_eq!(got, groups(&[0..3, 100..104, 1_000..1_001])); + // A gap the fills avoid closes last, however narrow. + let (_, got) = fit(Vec::new(), &fills, &groups(&[1..2]), budget(fits)).unwrap(); + assert_eq!(got, groups(&[0..1, 2..104, 1_000..1_001])); + } +} From 5be74a257f510c0fe59819d39fe673ab3cdeb868 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 18:00:39 -0800 Subject: [PATCH 04/10] perf(lite): an update leaving every range live costs nothing for them Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 47 +++++++++++++---- rs/moq-net/src/lite/subscribe.rs | 52 ++++++++++++++++--- rs/moq-net/src/model/track/demand.rs | 76 +++++++++++++++++++++++----- 3 files changed, 145 insertions(+), 30 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 5e2ce460ae5..487f661321f 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1771,7 +1771,7 @@ impl Resolve> for lite::SubscribeUpdate { } /// A SUBSCRIBE_UPDATE resolved to every live range, by Range ID. -type LiveUpdate = lite::SubscribeUpdate>; +type LiveUpdate = lite::SubscribeUpdate>; /// Serves one request stream: decode the request, resolve its broadcast, drive the /// kind's reply, then FIN. @@ -4921,6 +4921,9 @@ struct TrackRun { // 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<(std::sync::Arc<[(u64, lite::Range)]>, crate::track::Subscription)>, // Where delivery starts, which SUBSCRIBE_START reports. start: Start, end_sent: bool, @@ -4996,6 +4999,7 @@ impl TrackRun { frames: bounds.frames, named: groups.start(), refused: None, + applied: None, ctx, track, floor, @@ -5054,6 +5058,30 @@ impl TrackRun { group.set_priority(crate::priority::group(upd.priority, self.ctx.publisher)); } } + let delay = crate::track::Delay { + 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. @@ -5066,18 +5094,17 @@ impl TrackRun { 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. - if let Err(err) = self.track.update(crate::track::Subscription { + let model = crate::track::Subscription { priority: upd.priority, - delay: crate::track::Delay { - min: upd.min_delay, - max: upd.max_delay, - }, + delay, order: upd.order, ..groups - }) { + }; + if let Err(err) = self.track.update(model.clone()) { self.refused = Some(err); return; } + self.applied = Some((upd.ranges.clone(), model)); self.named = start; // 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 @@ -8130,7 +8157,7 @@ mod serve_group_test { priority: 0, max_delay: Some(Duration::from_secs(3600)), min_delay: None, - ranges, + ranges: ranges.into(), order: Order::Descending, } } @@ -8745,7 +8772,7 @@ mod serve_group_test { priority: 0, max_delay: Some(if widen { Duration::from_secs(10) } else { budget }), min_delay: Some(Duration::ZERO), - ranges: vec![(0, lite::Range::span(None, 0, None, None))], + ranges: [(0, lite::Range::span(None, 0, None, None))].into(), order: Order::Descending, }); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} @@ -8786,7 +8813,7 @@ mod serve_group_test { priority: 0, max_delay: Some(max_delay), min_delay: None, - ranges: vec![(0, lite::Range::span(None, 0, None, None))], + ranges: [(0, lite::Range::span(None, 0, None, None))].into(), order: Order::Descending, }; diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 5e7e9dda2d7..d39278c9336 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -844,6 +844,9 @@ 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>, } impl LiveRanges { @@ -855,6 +858,7 @@ impl LiveRanges { live: Default::default(), size: 0, initial: ranges.len() as u64, + snapshot: None, }; live.add(ranges.iter().copied()); live @@ -862,6 +866,10 @@ 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)); @@ -872,6 +880,7 @@ 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)); @@ -884,6 +893,7 @@ impl LiveRanges { } /// Every live range with its Range ID, in Range ID order. + #[cfg(test)] pub fn numbered(&self) -> Vec<(u64, Range)> { self.iter().map(|(id, range)| (id, *range)).collect() } @@ -920,8 +930,9 @@ impl LiveRanges { self.initial } - /// Resolve a received edit, returning every live range with its Range ID. - pub fn apply(&mut self, edit: RangeEdit) -> Result, DecodeError> { + /// 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, DecodeError> { match edit { RangeEdit::Replace(ranges) if !self.version.has_ranges() => { *self = Self::new(self.version, &ranges); @@ -943,7 +954,11 @@ impl LiveRanges { if self.size > super::message::MAX_MESSAGE_SIZE { return Err(DecodeError::TooMany); } - Ok(self.numbered()) + let live = &self.live; + let snapshot = self + .snapshot + .get_or_insert_with(|| live.iter().map(|(id, range)| (*id, *range)).collect()); + Ok(snapshot.clone()) } /// The edit that keeps the live ranges as they are. @@ -1740,8 +1755,8 @@ mod test { let resolved: Vec<_> = live .apply(got.ranges) .unwrap() - .into_iter() - .map(|(_, range)| range) + .iter() + .map(|(_, range)| *range) .collect(); assert_eq!(resolved, msg.ranges); } @@ -1831,7 +1846,7 @@ 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()); + assert_eq!(*live.apply(got.ranges).unwrap(), *sent.numbered()); }; let edit = |remove: &[u64], add: &[Range]| RangeEdit::Edit { @@ -1886,6 +1901,31 @@ 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. + #[test] + fn lite07_an_empty_edit_reuses_the_snapshot() { + 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), + end: None, + }; + let edited = live + .apply(RangeEdit::Edit { + remove: vec![0], + add: vec![range], + }) + .unwrap(); + assert_eq!(*edited, [(1, range)]); + assert!(std::sync::Arc::ptr_eq(&edited, &live.apply(empty()).unwrap())); + } + /// An edit to ranges too large for a SUBSCRIBE is refused before anything changes, so the /// live ranges stay those last sent and the next edit is made against them. #[test] diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 93c9160aed0..aa8b2301d60 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -9,7 +9,7 @@ use std::{ time::Duration, }; -use super::{Delay, Order, Subscription, TrackState, clamp_combined}; +use super::{Delay, Order, Range, Subscription, TrackState, clamp_combined}; mod ranges; use ranges::Ranges; @@ -136,21 +136,27 @@ impl Subscriptions { } /// Mutate counts under one lock, taking wakes only when the aggregate changes. - fn replace(&mut self, old: Option<&Subscription>, new: Option<&Subscription>) -> kio::WaiterList { + /// + /// `moved` is the ranges `old` holds and `new` doesn't, then those `new` holds and `old` + /// doesn't, so the lock every subscriber shares is held for what changed alone. + fn replace( + &mut self, + old: Option<&Subscription>, + new: Option<&Subscription>, + 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 moved = old.map(|old| &old.ranges) != new.map(|new| &new.ranges); + let (gone, came) = moved; 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 { add(&mut self.min_delay, min); } - if moved { - for range in &new.ranges { - reshaped |= self.ranges.add(*range); - } + for range in came { + reshaped |= self.ranges.add(*range); } self.descending += usize::from(new.order == Order::Descending); self.feed += usize::from(new.feed); @@ -162,10 +168,8 @@ impl Subscriptions { if let Some(min) = old.delay.min { remove(&mut self.min_delay, min); } - if moved { - for range in &old.ranges { - reshaped |= self.ranges.remove(*range); - } + for range in gone { + reshaped |= self.ranges.remove(*range); } self.descending -= usize::from(old.order == Order::Descending); self.feed -= usize::from(old.feed); @@ -183,6 +187,26 @@ impl Subscriptions { } } +/// The ranges `old` holds more of than `new`, then those `new` holds more of than `old`. +fn moved(old: &[Range], new: &[Range]) -> (Vec, Vec) { + if old == new { + return Default::default(); + } + let mut counts = std::collections::HashMap::::with_capacity(old.len()); + for range in old { + *counts.entry(*range).or_default() += 1; + } + for range in new { + *counts.entry(*range).or_default() -= 1; + } + let (mut gone, mut came) = (Vec::new(), Vec::new()); + for (range, count) in counts { + let into = if count > 0 { &mut gone } else { &mut came }; + into.extend(std::iter::repeat_n(range, count.unsigned_abs())); + } + (gone, came) +} + /// Count `value`, returning whether it is new. fn add(counts: &mut BTreeMap, value: T) -> bool { let count = counts.entry(value).or_default(); @@ -217,7 +241,7 @@ impl Preferences { let subscriptions = (!state.is_closed()).then(|| state.subscriptions.clone()); drop(state); if let Some(subs) = &subscriptions { - let mut wakes = subs.lock().replace(None, Some(&value)); + let mut wakes = subs.lock().replace(None, Some(&value), (&[], &value.ranges)); wakes.wake(); } Self(Arc::new(Preference { @@ -232,11 +256,13 @@ impl Preferences { if *value == next { return Ok(()); } + // Found before taking the lock every subscriber shares. + let (gone, came) = moved(&value.ranges, &next.ranges); let mut wakes = self .0 .subscriptions .as_ref() - .map(|subs| subs.lock().replace(Some(&value), Some(&next))); + .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); } @@ -279,7 +305,8 @@ impl Preferences { impl Drop for Preference { fn drop(&mut self) { if let Some(subs) = &self.subscriptions { - let mut wakes = subs.lock().replace(Some(&self.value.read()), None); + let value = self.value.read(); + let mut wakes = subs.lock().replace(Some(&value), None, (&value.ranges, &[])); wakes.wake(); } } @@ -300,6 +327,27 @@ impl From> for Preferences { mod tests { use super::super::super::subscription::Combined; use super::super::{Anchor, Range}; + + /// The ranges an update moves are its multiset difference, in either direction. + #[test] + fn an_update_moves_only_the_ranges_it_changes() { + let range = |start| Range { + start: Anchor::Time(start), + end: None, + }; + let sorted = |(mut gone, mut came): (Vec, Vec)| { + gone.sort(); + came.sort(); + (gone, came) + }; + let old = [range(1), range(2), range(2), range(3)]; + assert_eq!(sorted(super::moved(&old, &old)), (vec![], vec![])); + assert_eq!( + sorted(super::moved(&old, &[range(3), range(2), range(4), range(1)])), + (vec![range(2)], vec![range(4)]) + ); + assert_eq!(sorted(super::moved(&[], &old[..2])), (vec![], vec![range(1), range(2)])); + } use super::*; use rand::{RngExt, SeedableRng}; use std::sync::atomic::{AtomicUsize, Ordering}; From 3ea167d00f692c18cf2cdb10d3e370a9ef984d6c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 18:16:23 -0800 Subject: [PATCH 05/10] fix(js/net): a lite-07 subscriber widens ranges that don't fit one message Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 1 + js/net/src/lite/fills.ts | 16 +- js/net/src/lite/fit.test.ts | 180 +++++++++++++++++++ js/net/src/lite/fit.ts | 275 ++++++++++++++++++++++++++++++ js/net/src/lite/publisher.ts | 78 +++++++-- js/net/src/lite/subscribe.test.ts | 30 ++++ js/net/src/lite/subscribe.ts | 48 ++++-- js/net/src/lite/subscriber.ts | 70 ++++++-- 8 files changed, 655 insertions(+), 43 deletions(-) create mode 100644 js/net/src/lite/fit.test.ts create mode 100644 js/net/src/lite/fit.ts diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index d4fa2b0046a..5c0d46f9854 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1705,6 +1705,7 @@ A value of 0 is a PROTOCOL_VIOLATION, since a subscription that asks for nothing A span of the Track to deliver, from its `Start` up to its `End`. The subscription is the union of its Ranges: the publisher delivers each Group inside any of them once, in `Group Order` across all of them, and accounts with SUBSCRIBE_SKIP or SUBSCRIBE_DROP for every Group it will not deliver. Each Range implicitly takes the next Range ID on the stream, starting at 0, which a [SUBSCRIBE_UPDATE](#subscribe-update) uses to retire it. +A relay asking for the union of several subscriptions' Ranges, whose exact union does not fit one SUBSCRIBE, SHOULD ask for fewer Ranges covering it, joining the Ranges separated by the narrowest gaps, rather than refuse a subscription: each reader still reads only its own Ranges from the relay's copy. **Anchor Type**: Which fields follow, and what the Anchor names: diff --git a/js/net/src/lite/fills.ts b/js/net/src/lite/fills.ts index 7640010808c..11e254ea38a 100644 --- a/js/net/src/lite/fills.ts +++ b/js/net/src/lite/fills.ts @@ -339,7 +339,6 @@ export class RangeFills { ask.groups = remaining; if (ask.priority === this.ask.priority && sameRuns(ask.groups, this.ask.groups)) return this.isEmpty() !== empty; - this.#retireSkipped(ask.groups); this.ask = ask; return true; } @@ -400,9 +399,18 @@ export class RangeFills { this.#refused = refuseRun(this.#refused, start, end, refusal); } - // Asking for `asked` from now on retires the range of each run not asked whole any more, and - // what its start skipped with it. - #retireSkipped(asked: readonly Run[]): void { + /** The groups the ask leaves out on purpose: refused, or recovered on their own. */ + avoid(): Run[] { + const avoid = [...this.#recovering]; + for (const refused of this.#refused) insertRun(avoid, refused.start, refused.end); + return avoid; + } + + /** + * Asking for `asked` from now on, the runs the subscription carries, retires the range of each + * run not asked whole any more, and what its start skipped with it. + */ + retireSkipped(asked: readonly Run[]): void { this.#skipped = this.#skipped.filter((skipped) => asked.some((run) => run.start === skipped.asked.start && run.end === skipped.asked.end), ); diff --git a/js/net/src/lite/fit.test.ts b/js/net/src/lite/fit.test.ts new file mode 100644 index 00000000000..5bb157a58e6 --- /dev/null +++ b/js/net/src/lite/fit.test.ts @@ -0,0 +1,180 @@ +import { expect, test } from "bun:test"; +import { insertRun, type Run, removeRun, runGaps } from "../internal.ts"; +import { Writer } from "../stream.ts"; +import { type Budget, budget as budgetOf, fit, UPDATE_FIELDS } from "./fit.ts"; +import { LiveRanges, rangeSize, SubscribeUpdate, type WireAnchor, type WireRange } from "./subscribe.ts"; +import { Version } from "./version.ts"; + +const version = Version.DRAFT_07; + +function budget(bytes: number): Budget { + return { bytes, id: 2 }; +} + +/** A seeded generator, so a failing step replays: splitmix32. */ +function seeded(seed: number) { + let state = seed >>> 0; + const next = () => { + state = (state + 0x9e3779b9) >>> 0; + let z = state; + z = Math.imul(z ^ (z >>> 16), 0x85ebca6b) >>> 0; + z = Math.imul(z ^ (z >>> 13), 0xc2b2ae35) >>> 0; + return (z ^ (z >>> 16)) >>> 0; + }; + return { + /** In `[low, high)`. */ + int: (low: number, high: number) => low + (next() % (high - low)), + /** In `[0, 2^bits)`, for up to 64 bits. */ + bits: (bits: number) => ((BigInt(next()) << 32n) | BigInt(next())) & ((1n << BigInt(bits)) - 1n), + chance: (p: number) => next() / 2 ** 32 < p, + }; +} + +type Rng = ReturnType; + +function fillRange(run: Run): WireRange { + return { start: { absolute: run.start }, end: { absolute: run.end } }; +} + +function total(budget: Budget, ranges: readonly WireRange[], groups: readonly Run[]): number { + return [...ranges, ...groups.map(fillRange)].reduce((sum, range) => sum + rangeSize(range) + budget.id, 0); +} + +/** The group a start names, or the exclusive group an end stops before. */ +function group(anchor: WireAnchor | undefined, end: boolean): bigint | undefined { + if (anchor?.absolute !== undefined) return anchor.absolute; + if (anchor?.time !== undefined) return end ? anchor.time + 1n : anchor.time; + return undefined; +} + +/** Whether `outer` asks for everything `inner` does, judged without the latest group. */ +function covers(outer: WireRange, inner: WireRange): boolean { + const start = (() => { + if (outer.start === undefined) return true; + if (outer.start.relative !== undefined) + return inner.start?.relative !== undefined && outer.start.relative >= inner.start.relative; + const [a, b] = [group(outer.start, false), group(inner.start, false)]; + return a !== undefined && b !== undefined && a <= b; + })(); + const end = (() => { + if (outer.end === undefined) return true; + if (outer.end.relative !== undefined) + return inner.end?.relative !== undefined && outer.end.relative <= inner.end.relative; + const [a, b] = [group(outer.end, true), group(inner.end, true)]; + return a !== undefined && b !== undefined && a >= b; + })(); + return start && end; +} + +function random(rng: Rng): { demand: WireRange[]; fills: Run[]; avoid: Run[] } { + const start = () => BigInt(rng.int(0, 2_000)) << BigInt(rng.int(0, 30)); + const demand: WireRange[] = []; + for (let count = rng.int(0, 300); count > 0; count--) { + const from = start(); + const to = rng.chance(0.9) ? from + BigInt(rng.int(1, 50)) : undefined; + const end: WireAnchor | undefined = to === undefined ? undefined : { absolute: to }; + const kind = rng.int(0, 10); + if (kind === 0) demand.push({ start: { relative: BigInt(rng.int(0, 20)) }, end }); + else if (kind === 1) demand.push({ end }); + // The demand names groups by time, inclusive as an end; a fill's are absolute. + else if (kind < 6) + demand.push({ start: { time: from }, end: to === undefined ? undefined : { time: to - 1n } }); + else demand.push({ start: { absolute: from }, end }); + } + for (const range of demand) if (range.end === undefined) delete range.end; + let fills: Run[] = []; + const avoid: Run[] = []; + for (let count = rng.int(0, 300); count > 0; count--) { + const from = start(); + const to = from + BigInt(rng.int(1, 20)); + if (rng.chance(0.8)) insertRun(fills, from, to); + else insertRun(avoid, from, to); + } + for (const run of avoid) fills = removeRun(fills, run.start, run.end); + return { demand, fills, avoid }; +} + +// What fits stays as asked; what doesn't is widened to a covering superset that fits, never asking +// for a group the fills avoid while a narrower choice remains. Mirrors the Rust +// `widens_only_what_does_not_fit`. +test("fit widens only what does not fit", () => { + const rng = seeded(0x666974); + for (let step = 0; step < 2_000; step++) { + const { demand, fills, avoid } = random(rng); + const limit = budget(rng.int(200, 4_000)); + const before = total(limit, demand, fills); + const got = fit(demand, fills, avoid, limit); + if (before <= limit.bytes) { + expect(got.ranges).toBe(demand); + expect(got.fills).toBe(fills); + continue; + } + expect(total(limit, got.ranges, got.fills)).toBeLessThanOrEqual(limit.bytes); + for (const range of demand) { + if (!got.ranges.some((outer) => covers(outer, range))) + throw new Error( + `step ${step}: ${JSON.stringify(range, (_, v) => (typeof v === "bigint" ? `${v}` : v))}`, + ); + } + for (const run of fills) expect(runGaps(got.fills, run.start, run.end)).toEqual([]); + } +}); + +// Two fitted sets fit one SUBSCRIBE_UPDATE between them however little they share, at its widest +// fields, and either fits a SUBSCRIBE. Mirrors the Rust +// `any_edit_between_fitted_ranges_fits_one_message`. +test("fit leaves any edit between fitted ranges one message", async () => { + const widest = (edit: { remove: bigint[]; add: WireRange[] }) => + new SubscribeUpdate({ + wantedGroup: (1n << 64n) - 2n, + priority: -128, + maxDelay: Number.MAX_SAFE_INTEGER, + minDelay: Number.MAX_SAFE_INTEGER, + order: "ascending", + remove: edit.remove, + ranges: edit.add, + }); + const encode = (update: SubscribeUpdate) => Writer.collect(version, (w) => update.encode(w, version)); + expect((await encode(widest({ remove: [], add: [] }))).byteLength).toBeLessThanOrEqual(UPDATE_FIELDS); + + // Sets of many short ranges and of fewer long ones, alternately, so the IDs one retires outweigh + // what the other's ranges leave. + const rng = seeded(0x65646974); + const wide = (next: bigint, bits: number) => { + const ranges: WireRange[] = Array.from({ length: 10_000 }, () => { + const start = rng.bits(bits); + return { start: { time: start }, end: { time: start } }; + }); + return [...fit(ranges, [], [], budgetOf(0, next, ranges.length)).ranges]; + }; + const live = new LiveRanges(version, wide(0n, 14)); + for (let round = 0; round < 4; round++) { + const next = wide(live.next(), [50, 14][round % 2]); + await encode(widest(live.edit(next))); + } +}); + +// Each kind of start collapses to the one range covering it once nothing else fits. Mirrors the +// Rust `collapses_ranges_with_no_gap_to_close`. +test("fit collapses ranges with no gap to close", () => { + const relative: WireRange[] = Array.from({ length: 200 }, (_, back) => ({ + start: { relative: BigInt(back) }, + end: { absolute: 1_000n + BigInt(back) }, + })); + expect(fit(relative, [], [], budget(100)).ranges).toEqual([ + { start: { relative: 199n }, end: { absolute: 1_199n } }, + ]); + expect(() => fit(relative, [], [], budget(4))).toThrow("too many ranges"); +}); + +// The narrowest gaps close first, so the fewest unwanted groups are asked for. Mirrors the Rust +// `closes_the_narrowest_gaps_first`. +test("fit closes the narrowest gaps first", () => { + const runs = (...pairs: [number, number][]): Run[] => + pairs.map(([start, end]) => ({ start: BigInt(start), end: BigInt(end) })); + const fills = runs([0, 1], [2, 3], [100, 101], [103, 104], [1_000, 1_001]); + const fits = total(budget(0), [], runs([0, 3], [100, 104], [1_000, 1_001])); + expect(fit([], fills, [], budget(fits)).fills).toEqual(runs([0, 3], [100, 104], [1_000, 1_001])); + // A gap the fills avoid closes last, however narrow. + expect(fit([], fills, runs([1, 2]), budget(fits)).fills).toEqual(runs([0, 1], [2, 104], [1_000, 1_001])); +}); diff --git a/js/net/src/lite/fit.ts b/js/net/src/lite/fit.ts new file mode 100644 index 00000000000..902357d58ba --- /dev/null +++ b/js/net/src/lite/fit.ts @@ -0,0 +1,275 @@ +// Widening a lite-07 subscription's ranges until they fit the message that carries them. Mirrors +// the Rust `lite/subscriber/fit.rs`. +// +// A relay asks its upstream for the union of every viewer's ranges, and each viewer's request +// fits one message while the union need not. Rather than give back the copy every viewer reads +// through, the relay asks for a covering superset: the narrowest gaps close first, so the fewest +// unwanted groups are asked for. Each viewer still reads only its own ranges from the copy. + +import type { Run } from "../internal.ts"; +import { max } from "../util/key.ts"; +import { MAX_MESSAGE_SIZE } from "./message.ts"; +import { rangeSize, varintSize, type WireRange } from "./subscribe.ts"; + +/** + * The most a SUBSCRIBE_UPDATE's fields besides its ranges take, size prefix included: nine + * varints of at most nine bytes, and the priority byte. + */ +export const UPDATE_FIELDS = 9 * 9 + 1; + +/** What lite-07 ranges may take on the wire. */ +export interface Budget { + /** The bytes every range and its Range ID may take together. */ + bytes: number; + /** The bytes the largest Range ID any of the ranges may take. */ + id: number; +} + +/** + * The budget for ranges a subscription takes Range IDs from `next` for, whose SUBSCRIBE carries + * `fields` bytes besides its ranges, `count` the most ranges it asks for. + * + * Half a message, so that the ranges sent last, retired by Range ID, and the ranges replacing + * them fit one SUBSCRIBE_UPDATE however little the two share: a relay never narrows its ask in + * between, which would reset group streams still arriving. + */ +export function budget(fields: number, next: bigint, count: number): Budget { + const half = Math.floor((MAX_MESSAGE_SIZE - UPDATE_FIELDS) / 2); + const largest = next + BigInt(count); + return { + bytes: Math.min(half, Math.max(0, MAX_MESSAGE_SIZE - fields)), + // Past the varint's reach it takes the most a varint can. + id: largest >= 1n << 64n ? 9 : varintSize(largest), + }; +} + +/** What `range` costs, or undefined when it can't go out. */ +function cost(budget: Budget, range: WireRange): number | undefined { + // An end at group 0 asks for nothing, which encoding refuses. + if (range.end?.absolute === 0n) return undefined; + try { + return rangeSize(range) + budget.id; + } catch { + // Past the varint's reach, which encoding refuses too. + return undefined; + } +} + +/** Groups from `start`, up to the exclusive `end` or open. */ +interface Span { + start: bigint; + end?: bigint; +} + +function spanCost(budget: Budget, span: Span): number { + const range: WireRange = { start: { absolute: span.start } }; + if (span.end !== undefined) range.end = { absolute: span.end }; + return cost(budget, range) ?? Number.POSITIVE_INFINITY; +} + +/** + * The groups `range` asks for when it starts at one. A time is a group's start, inclusive as an + * end; any other end is taken as open. + */ +function groupSpan(range: WireRange): Span | undefined { + const start = range.start?.absolute ?? range.start?.time; + if (start === undefined) return undefined; + const end = range.end?.absolute ?? (range.end?.time === undefined ? undefined : range.end.time + 1n); + return end === undefined ? { start } : { start, end }; +} + +/** + * `demand`'s ranges and the `fills` groups, each widened only as far as both must be to fit + * `budget`, as the same arrays when nothing must. A gap between fills closes across `avoid`, + * groups the fills leave out on purpose, only once every other gap has. A widened demand range + * names its groups by time, as the demand's own do. + * + * Throws when even one range per kind of start would not fit, which only a broadcast path or + * track name filling the message leaves. + */ +export function fit( + demand: readonly WireRange[], + fills: readonly Run[], + avoid: readonly Run[], + budget: Budget, +): { ranges: readonly WireRange[]; fills: readonly Run[] } { + let sum = 0; + for (const range of [...demand, ...fills.map(fillRange)]) { + const bytes = cost(budget, range); + // One that can't go out fails when it is encoded, as it would unfitted. + if (bytes === undefined) return { ranges: demand, fills }; + sum += bytes; + } + if (sum <= budget.bytes) return { ranges: demand, fills }; + + const starts: Span[] = []; + let others: WireRange[] = []; + for (const range of demand) { + const span = groupSpan(range); + if (span) starts.push(span); + else others.push(range); + } + const spans = new Spans(starts, budget, () => false); + const runs = new Spans( + fills.map((run) => ({ ...run })), + budget, + (gap) => avoid.some((run) => run.start < gap.end && gap.start < run.end), + ); + + // The other ranges count back from the latest group or start at the oldest, so no gap between + // them is measured: when even one span per list leaves them no room, each kind collapses to + // the one range covering it. + const costOf = (ranges: readonly WireRange[]) => + ranges.reduce((sum, range) => sum + (cost(budget, range) ?? Number.POSITIVE_INFINITY), 0); + if (costOf(others) + spans.least() + runs.least() > budget.bytes) others = collapse(others); + const rest = costOf(others); + const total = () => rest + spans.total + runs.total; + + // Close the narrowest gaps first, of either list, until everything fits. + const gaps = [...spans.gaps(false), ...runs.gaps(true)].sort(byGap); + for (const gap of gaps) { + if (total() <= budget.bytes) break; + (gap.fill ? runs : spans).join(gap.index); + } + if (total() > budget.bytes) throw new Error("too many ranges for one SUBSCRIBE"); + + const ranges: WireRange[] = spans.ranges().map(({ start, end }) => { + const range: WireRange = { start: { time: start } }; + if (end !== undefined) range.end = { time: end - 1n }; + return range; + }); + ranges.push(...others); + return { ranges, fills: runs.ranges().map(({ start, end }) => ({ start, end: bounded(end) })) }; +} + +/** The range a fill's run takes on the wire, as `RangeFills` asks it. */ +function fillRange(run: Run): WireRange { + return { start: { absolute: run.start }, end: { absolute: run.end } }; +} + +/** A gap's end: only the last span is open, and no gap follows it. */ +function bounded(end: bigint | undefined): bigint { + if (end === undefined) throw new Error("only the last span is open"); + return end; +} + +/** A gap between two spans: how late it closes, which list it is in, and the span before it. */ +interface Gap { + late: boolean; + width: bigint; + fill: boolean; + index: number; +} + +function byGap(a: Gap, b: Gap): number { + if (a.late !== b.late) return a.late ? 1 : -1; + if (a.width !== b.width) return a.width < b.width ? -1 : 1; + if (a.fill !== b.fill) return a.fill ? 1 : -1; + return a.index - b.index; +} + +/** Disjoint spans in order, each the run of the input spans from one start to its chain's end. */ +class Spans { + readonly #spans: Span[] = []; + // The last span of the chain starting at each chain's first, and the first of the chain ending + // at each chain's last. + readonly #end: number[]; + readonly #start: number[]; + // Whether the gap after each span closes only once every other gap has. + readonly #late: boolean[] = []; + readonly #budget: Budget; + total = 0; + + constructor(input: Span[], budget: Budget, late: (gap: Run) => boolean) { + this.#budget = budget; + input.sort(bySpan); + // Spans that overlap or touch cost less as one, and ask for nothing more. + for (const span of input) { + const last = this.#spans.at(-1); + if (last && (last.end === undefined || last.end >= span.start)) { + last.end = last.end === undefined || span.end === undefined ? undefined : max(last.end, span.end); + } else { + this.#spans.push({ ...span }); + } + } + for (let index = 0; index + 1 < this.#spans.length; index++) + this.#late.push(late({ start: bounded(this.#spans[index].end), end: this.#spans[index + 1].start })); + for (const span of this.#spans) this.total += spanCost(budget, span); + this.#end = this.#spans.map((_, index) => index); + this.#start = this.#spans.map((_, index) => index); + } + + /** What the one span covering every span costs, or nothing when there are none. */ + least(): number { + const first = this.#spans.at(0); + const last = this.#spans.at(-1); + if (!first || !last) return 0; + return spanCost(this.#budget, { start: first.start, end: last.end }); + } + + /** Every gap, tagged with whether this list is the fills'. */ + gaps(fill: boolean): Gap[] { + return this.#late.map((late, index) => ({ + late, + width: this.#spans[index + 1].start - bounded(this.#spans[index].end), + fill, + index, + })); + } + + /** Close the gap after span `index`, joining the chains on either side. */ + join(index: number): void { + const first = this.#start[index]; + const last = this.#end[index + 1]; + const spans = this.#spans; + this.total -= spanCost(this.#budget, { start: spans[first].start, end: spans[index].end }); + this.total -= spanCost(this.#budget, { start: spans[index + 1].start, end: spans[last].end }); + this.total += spanCost(this.#budget, { start: spans[first].start, end: spans[last].end }); + this.#end[first] = last; + this.#start[last] = first; + } + + /** Each chain as the one span covering it. */ + ranges(): Span[] { + const ranges: Span[] = []; + for (let first = 0; first < this.#spans.length; first = this.#end[first] + 1) { + const last = this.#end[first]; + ranges.push({ start: this.#spans[first].start, end: this.#spans[last].end }); + } + return ranges; + } +} + +/** By start, an open end before any bound one, then by end. */ +function bySpan(a: Span, b: Span): number { + if (a.start !== b.start) return a.start < b.start ? -1 : 1; + if (a.end === b.end) return 0; + if (a.end === undefined) return -1; + if (b.end === undefined) return 1; + return a.end < b.end ? -1 : 1; +} + +/** + * One range per kind of start covering every range of that kind: from the earliest start to the + * latest end. A time or absolute start is a span, so only the oldest and counts back are left. + */ +function collapse(ranges: readonly WireRange[]): WireRange[] { + const kinds: WireRange[] = []; + for (const range of ranges) { + const relative = range.start?.relative; + const cover = kinds.find((cover) => (cover.start?.relative === undefined) === (relative === undefined)); + if (!cover) { + kinds.push({ ...range }); + continue; + } + if (cover.start?.relative !== undefined && relative !== undefined) + cover.start = { relative: max(cover.start.relative, relative) }; + const [a, b] = [cover.end, range.end]; + if (a?.absolute !== undefined && b?.absolute !== undefined) + cover.end = { absolute: max(a.absolute, b.absolute) }; + else if (a?.time !== undefined && b?.time !== undefined) cover.end = { time: max(a.time, b.time) }; + // Ends of different kinds can't be ordered without the latest group: follow the edge. + else delete cover.end; + } + return kinds; +} diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 71dc9b0bc6c..40542f5c40d 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -328,8 +328,11 @@ type SubscriptionControlOptions = { version: Version; /** The SUBSCRIBE's ranges, which each lite-07 update edits. */ live: LiveRanges; - /** Apply an update, with every live range by Range ID on lite-07. */ - apply: (update: SubscribeUpdate, live: { id: bigint; range: WireRange }[]) => void; + /** + * Apply an update, with every live range by Range ID on lite-07: the same array as the last + * update's while it left every range live. + */ + apply: (update: SubscribeUpdate, live: readonly { id: bigint; range: WireRange }[]) => void; }; /** @@ -349,6 +352,9 @@ class SubscriptionControls { #writer: Writer; #live: LiveRanges; #update?: { update: SubscribeUpdate; ids: bigint[] }; + // The live ranges last applied and their Range IDs, so an update leaving every range live hands + // the serving loop the same ids. + #applied?: { live: readonly { id: bigint; range: WireRange }[]; ids: bigint[] }; // Sticky, first one wins: null once the stream is over, an Error once it failed. #end?: Error | null; #ended: Promise; @@ -447,8 +453,9 @@ class SubscriptionControls { // Each lite-07 update edits the live ranges, which only the stream's history names. const ranges = live.apply(update); apply(update, ranges); + if (this.#applied?.live !== ranges) this.#applied = { live: ranges, ids: ranges.map(({ id }) => id) }; // Resolved in full, so the latest one replaces any the loop has yet to take. - this.#update = { update, ids: ranges.map(({ id }) => id) }; + this.#update = { update, ids: this.#applied.ids }; this.#changed.update((value) => value + 1); } } catch (err: unknown) { @@ -1083,6 +1090,16 @@ export class Publisher { const asked = rangesOf(msg); // Where each Range ID sits among the model's ranges, as of the last update applied. let positions = new Map(asked.map((_, index) => [BigInt(index), index])); + // The live ranges the last update applied, what they resolved to, and the terms beside + // them, so an update leaving every range live costs nothing for them. + let applied: + | { + live: readonly { id: bigint; range: WireRange }[]; + span: ReturnType; + model: track.Subscription; + terms: Pick; + } + | undefined; controls = new SubscriptionControls({ quic: this.#quic, reader: stream.reader, @@ -1092,29 +1109,57 @@ export class Publisher { apply: (update, live) => { legacyPriority.set(update.priority); if (wireOf(front).localDatagrams) legacy?.setPriority(update.priority); + // Every update restates the wanted group. Past our largest it is a join, which + // repeats the pending ask; at or below it the group already exists. + const largest = hooks.peekLargest(track).largest?.group; + if (update.wantedGroup !== undefined && (largest === undefined || update.wantedGroup > largest)) + hooks.joinSubscriber(track); + const terms = { + priority: update.priority, + order: update.order, + maxDelay: update.maxDelay, + minDelay: update.minDelay, + }; + const delay = { + max: servingMaxDelay(update.maxDelay), + min: update.minDelay === undefined ? null : Duration.millis(update.minDelay), + }; + // Every range still live as last applied: the model's ranges stand, and only the + // terms beside them may move. + if (applied?.live === live) { + Object.assign(update, applied.span); + const was = applied.terms; + if ( + was.priority === terms.priority && + was.order === terms.order && + was.maxDelay === terms.maxDelay && + was.minDelay === terms.minDelay + ) + return; + applied.model = { ...applied.model, priority: update.priority, order: update.order, delay }; + applied.terms = terms; + track.update(applied.model); + return; + } + let span: ReturnType | undefined; if (hasRanges(this.version)) { const resolved = hooks.servingResolved(track); const ranges = live.map(({ id, range }) => { const index = positions.get(id); return index === undefined ? range : pin(range, resolved[index]); }); - Object.assign(update, spanOf(this.version, ranges)); + span = spanOf(this.version, ranges); + Object.assign(update, span); positions = new Map(live.map(({ id }, index) => [id, index])); } - // Every update restates the wanted group. Past our largest it is a join, which - // repeats the pending ask; at or below it the group already exists. - const largest = hooks.peekLargest(track).largest?.group; - if (update.wantedGroup !== undefined && (largest === undefined || update.wantedGroup > largest)) - hooks.joinSubscriber(track); - track.update({ + const model: track.Subscription = { priority: update.priority, order: update.order, - delay: { - max: servingMaxDelay(update.maxDelay), - min: update.minDelay === undefined ? null : Duration.millis(update.minDelay), - }, + delay, ranges: requestedRanges(update), - }); + }; + track.update(model); + if (span) applied = { live, span, model, terms }; }, }); const finished = await this.#runTrack(track, stream.writer, controls, { @@ -1570,7 +1615,8 @@ export class Publisher { // An update may reach the finished track's history, so it is served again. finished = false; const update = control.update; - if (hasRanges(this.version)) { + // The same ids as last applied leave every range live, which owes nothing new. + if (hasRanges(this.version) && control.ids !== ids) { // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. const live = new Set(control.ids); unstarted = unstarted.filter((id) => live.has(id)); diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 6bf17146733..8af416c3dd4 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -2,6 +2,7 @@ import { expect, test } from "bun:test"; import { ProtocolViolation } from "../error.ts"; import * as Path from "../path.ts"; import { Reader, Writer } from "../stream.ts"; +import { MAX_MESSAGE_SIZE } from "./message.ts"; import { decodeSubscribeResponse, encodeSubscribeResponse, @@ -626,6 +627,35 @@ test("lite-07 updates edit only the changed ranges", async () => { expect(() => refused([0n], [])).toThrow(ProtocolViolation); }); +// 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. Mirrors the Rust +// `lite07_an_empty_edit_reuses_the_snapshot`. +test("lite-07 an empty edit reuses the snapshot", () => { + const live = new LiveRanges(Version.DRAFT_07, [{}]); + const empty = () => new SubscribeUpdate({ priority: 0, remove: [], ranges: [] }); + const first = live.apply(empty()); + expect(live.apply(empty())).toBe(first); + const range: WireRange = { start: { absolute: 4n } }; + const edited = live.apply(new SubscribeUpdate({ priority: 0, remove: [0n], ranges: [range] })); + expect(edited).toEqual([{ id: 1n, range }]); + expect(live.apply(empty())).toBe(edited); +}); + +// An edit to ranges too large for a SUBSCRIBE is refused before anything changes, so the live +// ranges stay those last sent and the next edit is made against them. Mirrors the Rust +// `lite07_a_refused_edit_leaves_the_ranges_as_sent`. +test("lite-07 a refused edit leaves the ranges as sent", () => { + const live = new LiveRanges(Version.DRAFT_07, [{}]); + const wide: WireRange[] = Array.from({ length: Math.floor(MAX_MESSAGE_SIZE / 4) }, (_, group) => ({ + start: { absolute: BigInt(group) << 20n }, + end: { absolute: (BigInt(group) << 20n) + 1n }, + })); + expect(() => live.edit(wide)).toThrow("too many ranges"); + expect(live.entries()).toEqual([[0n, {}]]); + expect(live.next()).toBe(1n); + expect(live.edit(wide.slice(0, 2))).toEqual({ remove: [0n], add: wide.slice(0, 2), fillIds: [] }); +}); + test("SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it", async () => { for (const start of [undefined, 0n, 7n]) { const started = new SubscribeStarted(4n, start); diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index 2696d8bb85a..8041b6ca808 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -328,12 +328,15 @@ export function rangesOf(msg: Span & { ranges?: WireRange[] }): WireRange[] { return [range]; } +/** The bytes `value` takes as a lite-07 varint, which counts leading ones. */ +export function varintSize(value: number | bigint): number { + const lo = split(value); + return lengthLeadingOnes(parts.hi, lo); +} + /** The bytes a lite-07 range takes on the wire, whose varints count leading ones. */ -function rangeSize(range: WireRange): number { - const size = (value: number | bigint) => { - const lo = split(value); - return lengthLeadingOnes(parts.hi, lo); - }; +export function rangeSize(range: WireRange): number { + const size = varintSize; const anchor = (anchor: WireAnchor | undefined, end: boolean): number => { // An end at group 0 can't go out, which encoding refuses; size it as the smallest. if (end && anchor?.absolute === 0n) return size(1) + size(0); @@ -369,6 +372,9 @@ export class LiveRanges { readonly #live = new Map(); // The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries. #size = 0; + // The live ranges `apply` last returned, until one changes, so an edit that changes nothing + // costs nothing. + #snapshot?: readonly { id: bigint; range: WireRange }[]; /** How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. */ readonly initial: bigint; @@ -380,6 +386,7 @@ export class LiveRanges { } #add(ranges: readonly WireRange[]) { + if (ranges.length > 0) this.#snapshot = undefined; for (const range of ranges) { // Only lite-07 numbers ranges on the wire, and only it is held to one SUBSCRIBE. if (hasRanges(this.#version)) this.#size += rangeSize(range); @@ -390,11 +397,17 @@ export class LiveRanges { #remove(id: bigint): boolean { const range = this.#live.get(id); if (!range) return false; + this.#snapshot = undefined; if (hasRanges(this.#version)) this.#size -= rangeSize(range); this.#live.delete(id); return true; } + /** The Range ID the next range added takes. */ + next(): bigint { + return this.#next; + } + /** The live range with Range ID `id`. */ get(id: bigint): WireRange | undefined { return this.#live.get(id); @@ -426,11 +439,11 @@ export class LiveRanges { } /** - * Apply a received lite-07 `update`'s edit, returning every live range by Range ID. Throws - * {@link ProtocolViolation} for an edit it cannot apply. Before lite-07 an update restates - * its ranges, so nothing is numbered. + * Apply a received lite-07 `update`'s edit, returning every live range by Range ID: the same + * array as last time while nothing changed. Throws {@link ProtocolViolation} for an edit it + * cannot apply. Before lite-07 an update restates its ranges, so nothing is numbered. */ - apply(update: SubscribeUpdate): { id: bigint; range: WireRange }[] { + apply(update: SubscribeUpdate): readonly { id: bigint; range: WireRange }[] { if (!hasRanges(this.#version)) return []; for (const id of update.remove ?? []) { // An id never assigned, or already retired (including earlier in this list). @@ -441,7 +454,8 @@ export class LiveRanges { // could carry like one too large to send. if (this.#live.size === 0) throw new ProtocolViolation("a subscription asks for no range"); if (this.#size > Message.MAX_MESSAGE_SIZE) throw new ProtocolViolation("too many ranges"); - return [...this.#live].map(([id, range]) => ({ id, range })); + this.#snapshot ??= [...this.#live].map(([id, range]) => ({ id, range })); + return this.#snapshot; } /** @@ -479,11 +493,17 @@ export class LiveRanges { wanted.set(k, (wanted.get(k) ?? 0) + 1); }); const remove: bigint[] = []; + // The bytes the live ranges take once the edit applies. + let size = this.#size; for (const [id, range] of this.#live) { const k = key(range, isFill(id)); const count = wanted.get(k) ?? 0; - if (count > 0) wanted.set(k, count - 1); - else remove.push(id); + if (count > 0) { + wanted.set(k, count - 1); + } else { + remove.push(id); + size -= rangeSize(range); + } } // In the order asked for, so the publisher numbers them the way this side does. const add: WireRange[] = []; @@ -497,9 +517,11 @@ export class LiveRanges { add.push(range); if (index >= demand) fillIds.push(first + BigInt(add.length - 1)); }); + // Checked before anything changes, so a refused edit leaves the ranges as last sent. + for (const range of add) size += rangeSize(range); + if (size > Message.MAX_MESSAGE_SIZE) throw new Error("too many ranges for one SUBSCRIBE"); for (const id of remove) this.#remove(id); this.#add(add); - if (this.#size > Message.MAX_MESSAGE_SIZE) throw new Error("too many ranges for one SUBSCRIBE"); return { remove, add, fillIds }; } } diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 30425e4b235..f95247a6d3b 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -73,6 +73,7 @@ import { Datagram as DatagramMessage, Datagrams, DatagramsInfo, DatagramsUpdate import { closeViolation } from "./error.ts"; import { Fetch as FetchMessage } from "./fetch.ts"; import { dropped, passing, RangeFills, type RecoveryHandle, type Refusal, type Refused, refuseRun } from "./fills.ts"; +import * as Fit from "./fit.ts"; import { Floors } from "./floors.ts"; import { type Group as GroupMessage, readFrames } from "./group.ts"; import { Probe } from "./probe.ts"; @@ -240,8 +241,9 @@ interface SubscribeEntry { // The Range IDs of the fills' live ranges; the rest are the demand's, which alone say where the // live feed starts. fillIds: Set; - // The groups the fills' ranges ask for, as last sent. - fillGroups: Run[]; + // The groups the fills' ranges ask for, as last sent: widened with the demand's ranges until + // they fit, so a run may cover more than the fills ask. + fillGroups: readonly Run[]; // Bumped as what the fills ask changes, for the update loop. fillsChanged: Signal; // Until the SUBSCRIBE's first response (lite-05+); see `Unanswered`. @@ -1644,7 +1646,9 @@ export class Subscriber { settle = resolve; }), ); - const sending = this.#sendSubscribe(state, this.#withRanges(msg, ranges), deadline); + const sending = this.#fitted(this.#withRanges(msg, ranges)).then((msg) => + this.#sendSubscribe(state, msg, deadline), + ); try { // Either failing fails the subscription: a reset TRACK rejects the request, so the // origin can try another route, and the caller resets the SUBSCRIBE. @@ -1687,6 +1691,39 @@ export class Subscriber { return msg; } + // `msg` with its lite-07 ranges widened until they fit, as the Rust `pipeline` does: the union of + // a relay's readers' ranges need not fit one message though each reader's does. + async #fitted(msg: Subscribe): Promise { + if (!msg.ranges || !hasRanges(this.version)) return msg; + const fields = await this.#subscribeFields(msg, msg); + msg.ranges = [...Fit.fit(msg.ranges, [], [], Fit.budget(fields, 0n, msg.ranges.length)).ranges]; + return msg; + } + + // The bytes a SUBSCRIBE like `msg` under `terms` takes besides its ranges, at the widest id, + // measured with the smallest range, since it carries at least one. Mirrors the Rust + // `subscribe_fields`. + async #subscribeFields( + msg: Subscribe, + terms: { priority: number; order?: track.Order; maxDelay: number | null; minDelay?: number }, + ): Promise { + const widest = new Subscribe({ + ...msg, + priority: terms.priority, + order: terms.order, + maxDelay: terms.maxDelay, + minDelay: terms.minDelay, + id: (1n << 64n) - 1n, + ranges: [{}], + }); + try { + return (await Writer.collect(this.version, (w) => widest.encode(w, this.version))).byteLength; + } catch { + // One that can't go out leaves no room, and fails as it would have. + return Number.POSITIVE_INFINITY; + } + } + // Register a subscription's accepted copy, asking for `ranges`, so incoming groups and // datagrams find it. #register( @@ -2816,13 +2853,26 @@ export class Subscriber { // Lite-07 edits the live ranges: a range already live keeps its Range ID, and where it // started. The retired ones owe nothing more. let edit: { remove: bigint[]; add: WireRange[] } | undefined; - const fillRanges: WireRange[] = fillGroups.map((run) => ({ - start: { absolute: run.start }, - end: { absolute: run.end }, - })); if (hasRanges(this.version)) { + // The demand's ranges and the fills' groups, widened together until they fit. The fills + // keep their exact ask, so what they ask next is compared with that, not the wire. + const demanded = toWireRanges(ranges); + const fields = await this.#subscribeFields(msg, { + priority, + order: current.order, + maxDelay: wireMaxDelay(current), + minDelay: minDelayOf(current), + }); + const budget = Fit.budget(fields, entry.live.next(), demanded.length + fillGroups.length); + const fitted = Fit.fit(demanded, fillGroups, entry.fills?.avoid() ?? [], budget); + // A fill's range starting past its run speaks only while that run is asked whole. + entry.fills?.retireSkipped(fitted.fills); + const fillRanges: WireRange[] = fitted.fills.map((run) => ({ + start: { absolute: run.start }, + end: { absolute: run.end }, + })); // The demand's ranges, then the fills'. - const edited = entry.live.edit([...toWireRanges(ranges), ...fillRanges], fillRanges.length, (id) => + const edited = entry.live.edit([...fitted.ranges, ...fillRanges], fillRanges.length, (id) => entry.fillIds.has(id), ); edit = edited; @@ -2834,10 +2884,10 @@ export class Subscriber { for (const id of entry.fillIds) if (!entry.live.get(id)) entry.fillIds.delete(id); for (const id of edited.fillIds) entry.fillIds.add(id); // The groups a fill newly asks for wait a fresh grace too. - for (const run of fillGroups) + for (const run of fitted.fills) for (const gap of runGaps(entry.fillGroups, run.start, run.end)) entry.tail.demand(gap.start, gap.end); - entry.fillGroups = fillGroups; + entry.fillGroups = fitted.fills; entry.floor = span.start; // Pending while a range the update adds awaits SUBSCRIBE_STARTED. this.#declare(entry); From bff3a16c9d5b48247ed439428596ef3e7d51068c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 18:52:41 -0800 Subject: [PATCH 06/10] chore: clippy and fmt Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 4 +-- rs/moq-net/src/lite/subscribe.rs | 17 +++++++++--- rs/moq-net/src/lite/subscriber.rs | 6 ++--- rs/moq-net/src/lite/subscriber/fit.rs | 26 +++++++++++++++---- .../tests/integration/range_union_fit.rs | 11 ++++++-- 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 487f661321f..dcc6eaf6546 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1771,7 +1771,7 @@ impl Resolve> for lite::SubscribeUpdate { } /// A SUBSCRIBE_UPDATE resolved to every live range, by Range ID. -type LiveUpdate = lite::SubscribeUpdate>; +type LiveUpdate = lite::SubscribeUpdate; /// Serves one request stream: decode the request, resolve its broadcast, drive the /// kind's reply, then FIN. @@ -4923,7 +4923,7 @@ struct TrackRun { 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<(std::sync::Arc<[(u64, lite::Range)]>, crate::track::Subscription)>, + applied: Option<(lite::Snapshot, crate::track::Subscription)>, // Where delivery starts, which SUBSCRIBE_START reports. start: Start, end_sent: bool, diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index d39278c9336..9c2300efd57 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -846,9 +846,12 @@ pub(crate) struct LiveRanges { initial: u64, /// The live ranges [`Self::apply`] last returned, until one changes, so an edit that /// changes nothing costs nothing. - snapshot: Option>, + snapshot: Option, } +/// Every live range with its Range ID, shared while none changes. +pub(crate) type Snapshot = std::sync::Arc<[(u64, Range)]>; + impl LiveRanges { /// The ranges a SUBSCRIBE carried. pub fn new(version: Version, ranges: &[Range]) -> Self { @@ -932,7 +935,7 @@ impl LiveRanges { /// 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, DecodeError> { + pub fn apply(&mut self, edit: RangeEdit) -> Result { match edit { RangeEdit::Replace(ranges) if !self.version.has_ranges() => { *self = Self::new(self.version, &ranges); @@ -1030,8 +1033,14 @@ impl LiveRanges { // Checked before anything changes, so a refused edit leaves the ranges as last sent. let form = self.version.into(); let size = |range: &Range| range_size(range, form).unwrap_or(usize::MAX); - let removed = remove.iter().map(|id| size(&self.live[id])).fold(0, usize::saturating_add); - let size = add.iter().map(size).fold(self.size.saturating_sub(removed), usize::saturating_add); + let removed = remove + .iter() + .map(|id| size(&self.live[id])) + .fold(0, usize::saturating_add); + let size = add + .iter() + .map(size) + .fold(self.size.saturating_sub(removed), usize::saturating_add); if size > super::message::MAX_MESSAGE_SIZE { return Err(EncodeError::TooLarge); } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index a34aee817ce..95f5c3754a4 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3465,9 +3465,9 @@ mod tests { .await .unwrap(); } else { - let begin = h - .serve - .begin_subscription(&mut track, &mut Sub::None, lowered, &mut RangeFills::default(), true); + let begin = + h.serve + .begin_subscription(&mut track, &mut Sub::None, lowered, &mut RangeFills::default(), true); assert!(matches!(begin, Ok(Begin::Establish(_)))); } diff --git a/rs/moq-net/src/lite/subscriber/fit.rs b/rs/moq-net/src/lite/subscriber/fit.rs index 7d4e0831834..9561d91302e 100644 --- a/rs/moq-net/src/lite/subscriber/fit.rs +++ b/rs/moq-net/src/lite/subscriber/fit.rs @@ -151,7 +151,9 @@ pub(super) fn fit( let [spans, fills] = lists; let mut ranges: Vec = spans.ranges().map(|(start, end)| span(start, end)).collect(); ranges.extend(others); - let groups = fills.ranges().map(|(start, end)| start.group..end.expect("fills are bounded").group); + let groups = fills + .ranges() + .map(|(start, end)| start.group..end.expect("fills are bounded").group); Ok((ranges, groups.collect())) } @@ -215,7 +217,11 @@ impl Spans { fn gaps(&self, tag: bool) -> impl Iterator + '_ { self.spans.windows(2).enumerate().map(move |(index, pair)| { let end = pair[0].1.expect("only the last is open"); - ((self.last[index], pair[1].0.group.saturating_sub(end.group)), tag, index) + ( + (self.last[index], pair[1].0.group.saturating_sub(end.group)), + tag, + index, + ) }) } @@ -288,7 +294,12 @@ mod tests { fn total(budget: Budget, ranges: &[Range], groups: &RangeSet) -> usize { let fills = FillAsk::wire(groups); - ranges.iter().copied().chain(fills).map(|range| budget.cost(&range).unwrap()).sum() + ranges + .iter() + .copied() + .chain(fills) + .map(|range| budget.cost(&range).unwrap()) + .sum() } /// Whether `outer` asks for everything `inner` does, judged without the latest group. @@ -360,7 +371,10 @@ mod tests { } assert!(total(budget, &ranges, &groups) <= budget.bytes, "step {step}"); for range in &demand { - assert!(ranges.iter().any(|outer| covers(outer, range)), "step {step}: {range:?}"); + assert!( + ranges.iter().any(|outer| covers(outer, range)), + "step {step}: {range:?}" + ); } for run in fills.iter() { assert!(groups.gaps(run).next().is_none(), "step {step}: {run:?}"); @@ -441,7 +455,9 @@ mod tests { let (_, got) = fit(Vec::new(), &fills, &RangeSet::new(), budget(fits)).unwrap(); assert_eq!(got, groups(&[0..3, 100..104, 1_000..1_001])); // A gap the fills avoid closes last, however narrow. - let (_, got) = fit(Vec::new(), &fills, &groups(&[1..2]), budget(fits)).unwrap(); + let mut avoid = RangeSet::new(); + avoid.insert(1..2); + let (_, got) = fit(Vec::new(), &fills, &avoid, budget(fits)).unwrap(); assert_eq!(got, groups(&[0..1, 2..104, 1_000..1_001])); } } diff --git a/rs/moq-net/tests/integration/range_union_fit.rs b/rs/moq-net/tests/integration/range_union_fit.rs index 826ca3730cd..57e113767da 100644 --- a/rs/moq-net/tests/integration/range_union_fit.rs +++ b/rs/moq-net/tests/integration/range_union_fit.rs @@ -163,9 +163,16 @@ async fn a_union_too_large_for_one_message_asks_a_covering_superset() { write_group(&track, BASE + 2 * VIEWERS + 2); write_group(&track, key); for want in [BASE + 2, BASE + 2 * VIEWERS + 2] { - let got = kio::clock::timeout(TIMEOUT, readers[1].recv_group()).await.unwrap().unwrap(); + let got = kio::clock::timeout(TIMEOUT, readers[1].recv_group()) + .await + .unwrap() + .unwrap(); assert_eq!(got.map(|group| group.start), Some(want)); } let got = kio::clock::timeout(TIMEOUT, live.recv_group()).await.unwrap().unwrap(); - assert_eq!(got.map(|group| group.start), Some(key), "the plain viewer keeps receiving"); + assert_eq!( + got.map(|group| group.start), + Some(key), + "the plain viewer keeps receiving" + ); } From f9ac784919dfcf74d0955d486c3f669d6cf7c831 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 19:02:45 -0800 Subject: [PATCH 07/10] fix(lite): drop the depth field main removed from SUBSCRIBE Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index a22b39f323d..728fedb43ce 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10721,7 +10721,6 @@ impl TrackServe { let msg = lite::Subscribe { instance: self.instance.clone(), node: self.node, - depth: self.depth, id: u64::MAX, broadcast: self.path.clone(), track: self.name.as_str().into(), From 82d04267f76e0f61d747a903a5745d2fba17ebd3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 19:03:28 -0800 Subject: [PATCH 08/10] quest: finish lite07-range-union, add its follow-ups Co-Authored-By: Claude Opus 5.5 --- quest/m1/gst-resume-late-flake.md | 16 ++++++++ quest/m1/lite07-idle-oldest.md | 25 +++++++++++++ quest/m1/precut-fixes/lite07-range-union.md | 41 --------------------- quest/m2/lite07-update-cost.md | 32 ++++++++++++++++ 4 files changed, 73 insertions(+), 41 deletions(-) create mode 100644 quest/m1/gst-resume-late-flake.md create mode 100644 quest/m1/lite07-idle-oldest.md delete mode 100644 quest/m1/precut-fixes/lite07-range-union.md create mode 100644 quest/m2/lite07-update-cost.md diff --git a/quest/m1/gst-resume-late-flake.md b/quest/m1/gst-resume-late-flake.md new file mode 100644 index 00000000000..9aed0070b4f --- /dev/null +++ b/quest/m1/gst-resume-late-flake.md @@ -0,0 +1,16 @@ +# [S] moq-gst's resume test holds under load + +## Goal + +`source::imp::session_tests::a_closed_publisher_session_resumes_on_the_next_start` +in `rs/moq-gst/src/source/imp.rs` passes however loaded the machine is. + +## Plan + +It failed once under a full `just check` on 2026-11-17 with "the sink dropped +1 as late: 7 of 10 rendered", and passed three runs alone, on a branch that +touched no moq-gst code ([moq-dev/moq#9045](https://github.com/moq-dev/moq/pull/9045)). +A sink judging lateness by the wall clock fails when the machine is busy. +Reproduce it under load first, then fix the root cause, such as a test clock +or a sink that does not drop late buffers in the test; no retry or looser +assertion. diff --git a/quest/m1/lite07-idle-oldest.md b/quest/m1/lite07-idle-oldest.md new file mode 100644 index 00000000000..fd1940f900f --- /dev/null +++ b/quest/m1/lite07-idle-oldest.md @@ -0,0 +1,25 @@ +# [S] A live viewer's fresh lite-07 copy asks only for the live edge + +## Goal + +A relay or client opening a lite-07 subscription for a viewer that asks only +for the live edge (`Range::live()`) asks its upstream for the live edge, not +for every group from the oldest the max age allows. + +## Plan + +Seen while testing the [range union fix](https://github.com/moq-dev/moq/pull/9045); +reproduce first. `TrackServe::wire_request` in `rs/moq-net/src/lite/subscriber.rs` +treats a copy whose demand is only open live or oldest ranges as idle and asks +from `producer.idle_newest()`. For a copy that never cached a group that is +`None`, and `Subscription::with_start(None)` turns the ranges into +`Range::span(None, None)`, which since +[moq-dev/moq#8638](https://github.com/moq-dev/moq/pull/8638) starts at +`Anchor::Oldest`. A publisher then saw `Oldest..` for a viewer through a relay +that asked for `Range::live()` alone, on a track whose max age is unlimited: +every group it holds. + +Recommended: only an idle copy that cached a group rewrites the start; one +with none keeps the subscriber's own ranges. Test: a `Range::live()` viewer +through a lite-07 relay, asserting the publisher's demand stays `Relative(0)..`. +Check the JS subscriber for the same rewrite. diff --git a/quest/m1/precut-fixes/lite07-range-union.md b/quest/m1/precut-fixes/lite07-range-union.md deleted file mode 100644 index d82e74f4e0e..00000000000 --- a/quest/m1/precut-fixes/lite07-range-union.md +++ /dev/null @@ -1,41 +0,0 @@ -# [M] A lite-07 relay's upstream ranges always fit, and an update costs only its edit - -## Goal - -A lite-07 relay never gives back the upstream copy every viewer reads through -because its viewers' ranges, merged, do not fit one message: when the exact -union is too large it asks for a covering superset. A SUBSCRIBE_UPDATE costs -work proportional to the ranges it edits, not to every range the -subscription holds. - -## Plan - -Found by the 2026-11-17 pre-cut correctness audit, from source; reproduce -first. Rechecked after [moq-dev/moq#8908](https://github.com/moq-dev/moq/pull/8908) -rewrote lite-07 to address groups by start: it changed the ranges' units, not -how they merge. lite-07 publishes for the first time at this cut, so this -gates it. - -- The union in `rs/moq-net/src/model/track/demand/ranges.rs` (`fold`) merges - only touching runs. One session sending two SUBSCRIBEs of about 10k - interleaved single-group ranges, each fitting a message, makes a union that - does not; the upstream SUBSCRIBE_UPDATE fails and the shared copy is given - back (`lite/subscriber.rs`), and each new route hits the same union, so - other sessions' viewers end with an error. Gap fills across many viewers - can fragment the union the same way with no hostile peer. `edit_keyed` in - `lite/subscribe.rs` also changes its stored ranges before the size check, - so a refused edit leaves them out of step with what was sent. Recommended: - when the exact union cannot fit, coarsen it by merging the smallest gaps - until it fits, and size-check before mutating. Test: a relay with those two - SUBSCRIBEs over a lite-07 upstream, asserting a third plain viewer keeps - receiving. -- `LiveRanges::apply` returns every live range (`numbered`), and - `TrackRun::update` in `lite/publisher.rs` rebuilds its lookups and hands the - whole list to the track under the lock every subscriber of it shares, so a - stream of empty updates against a 32k-range subscription copies megabytes - each. Recommended: apply only the edit, keyed by Range ID, and skip the - track call when nothing changed. - -Caused by, for the fix PR's `## Caused by`: - -- [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872) - exact range unions and whole-set updates diff --git a/quest/m2/lite07-update-cost.md b/quest/m2/lite07-update-cost.md new file mode 100644 index 00000000000..feaba332447 --- /dev/null +++ b/quest/m2/lite07-update-cost.md @@ -0,0 +1,32 @@ +# [M] A lite-07 SUBSCRIBE_UPDATE costs its edit on the publisher's session too + +## Goal + +A SUBSCRIBE_UPDATE that edits k ranges of an n-range subscription costs +O(k log n) on the publisher, in the session's own task as well as under the +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. +- 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. From 6313526e8e8beb0b44c60726dfecdc510c85bca5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 19:41:16 -0800 Subject: [PATCH 09/10] quest: lite07-update-cost stays internal-only Co-Authored-By: Claude Opus 5.5 --- quest/m2/lite07-update-cost.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/quest/m2/lite07-update-cost.md b/quest/m2/lite07-update-cost.md index feaba332447..9f675dff365 100644 --- a/quest/m2/lite07-update-cost.md +++ b/quest/m2/lite07-update-cost.md @@ -30,3 +30,22 @@ one small message to buy, not unbounded work. 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. + +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: + +- 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. +- 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` + and the exported types stay as they are. +- If the work turns up a need for a public change after all, stop and ask: + the maintainer's condition was that this rework stays internal. From b01e1ee5d04fa265eb31865d003f0ff4d905f4bb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 03:38:24 -0800 Subject: [PATCH 10/10] chore(quest): the resume flake's fix keeps its lateness check Co-Authored-By: Claude Opus 5.5 --- quest/m1/gst-resume-late-flake.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/quest/m1/gst-resume-late-flake.md b/quest/m1/gst-resume-late-flake.md index 9aed0070b4f..a5674b0965b 100644 --- a/quest/m1/gst-resume-late-flake.md +++ b/quest/m1/gst-resume-late-flake.md @@ -11,6 +11,7 @@ It failed once under a full `just check` on 2026-11-17 with "the sink dropped 1 as late: 7 of 10 rendered", and passed three runs alone, on a branch that touched no moq-gst code ([moq-dev/moq#9045](https://github.com/moq-dev/moq/pull/9045)). A sink judging lateness by the wall clock fails when the machine is busy. -Reproduce it under load first, then fix the root cause, such as a test clock -or a sink that does not drop late buffers in the test; no retry or looser -assertion. +Reproduce it under load first, then fix the root cause, such as a test clock. +Keep the lateness check: `rendered` asserts every frame arrives on time, so a +sink that stops dropping late buffers must keep a separate lateness assertion. +No retry or looser assertion.