Skip to content
1 change: 0 additions & 1 deletion quest/m2/perf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,5 @@ revisions on the same host and worker count, and profiles with
- [An idle export poll is cheap](/quest/m2/perf/export-idle-poll.md) - an export poll nothing woke skips the track fill and catalog work it repeats today
- [Mesh session cost](/quest/m2/perf/mesh-session-cost.md) - moq-net's per-session announce, route sync, and scope work drops, swept over sessions and announces
- [Cluster route memory](/quest/m2/perf/cluster-route-memory.md) - a full-mesh cluster's ~3.7 KB per route per broadcast shrinks, the in-memory hop is profiled, and `bench-profile lock` tolerates lost records
- [Lite request allocations](/quest/m2/perf/lite-request-allocs.md) - `Path::concat` allocates once and the lite request handler keeps one track-name copy
- [Lock-free single-delivery writes](/quest/m2/perf/single-delivery-write.md) - a frame with one delivery writes without a lock, recovering #6922's remaining FETCH cost
- [Small hot-path waste](/quest/m2/perf/hot-path-waste.md) - one track-name copy per lite subscription, no full-history `#drift` walk, and one header buffer per JS object
20 changes: 0 additions & 20 deletions quest/m2/perf/lite-request-allocs.md

This file was deleted.

124 changes: 119 additions & 5 deletions rs/moq-net-tests/tests/announce_allocs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Allocation counting for the announce hot paths: decoding the paths a peer
//! announces, and revisiting a cursor whose winning route is unchanged. Also the
//! messages around them: a decoded field allocates only once kept, and encoding
//! copies nothing.
//! announces, joining them, and revisiting a cursor whose winning route is
//! unchanged. Also the messages around them: a decoded field allocates only once
//! kept, encoding copies nothing, and a served request keeps one copy of its track
//! name.
//!
//! Every route change at a prefix revisits each cursor presenting it, and on a
//! dense mesh most of those visits find the cursor's winner unchanged: a worse
Expand All @@ -13,9 +14,20 @@ use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;

use moq_net::{
Hop, Hops, Path, Pattern, Patterns,
Client, Hop, Hops, Path, Pattern, Patterns, Server, Version,
fuzz::{decode_path, decode_subscribe, encode_path, encode_subscribe},
origin,
kio, origin,
time::{Driver, Instant},
track,
transport::memory,
};

#[path = "../../moq-net/tests/integration/support/mod.rs"]
mod support;

use support::{
harness::{now, spawn, subscribed},
mock::create_mock_session_pair,
};

thread_local! {
Expand All @@ -25,6 +37,13 @@ thread_local! {
/// `const` initialised, so reading it from inside the allocator cannot allocate
/// and recurse.
static ALLOCS: Cell<usize> = const { Cell::new(0) };

/// A byte size to watch, and the fresh allocations of exactly that size *this*
/// thread made. Size 0 watches nothing, since nothing allocates zero bytes.
static SIZED: Cell<(usize, usize)> = const { Cell::new((0, 0)) };

/// The size [`SIZED`] watches while a [`Watched`] driver is polled.
static WATCH: Cell<usize> = const { Cell::new(0) };
}

struct Counting;
Expand All @@ -36,6 +55,12 @@ unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// `try_with`, because TLS is unavailable while a thread is being torn down.
let _ = ALLOCS.try_with(|allocs| allocs.set(allocs.get() + 1));
let _ = SIZED.try_with(|sized| {
let (size, count) = sized.get();
if size == layout.size() {
sized.set((size, count + 1));
}
});
unsafe { System.alloc(layout) }
}

Expand Down Expand Up @@ -143,6 +168,19 @@ fn a_decoded_path_allocates_only_its_buffer() {
}
}

/// Joining two paths copies them straight into the one buffer the result keeps, or
/// inline when short, rather than through a `String` first.
#[test]
fn a_joined_path_allocates_only_its_buffer() {
for path in [SHORT, LONG] {
let (head, tail) = path.split_once('/').unwrap();
let (head, tail) = (Path::new(head).unwrap(), Path::new(tail).unwrap());
assert_eq!(head.join(&tail).unwrap().as_str(), path);
let joined = allocs(|| head.join(&tail).unwrap());
assert_eq!(joined, path_buffers(path), "allocations joining {path:?}");
}
}

/// Encoding a path writes its fields straight from its text, whatever the wire.
#[test]
fn an_encoded_path_allocates_nothing() {
Expand Down Expand Up @@ -178,3 +216,79 @@ fn a_decoded_subscribe_borrows_its_track_name() {
}
}
}

/// A session driver that counts the allocations sized [`WATCH`] only while it is
/// polled, so one end of a session is counted apart from the other on the same thread.
struct Watched<D>(D);

impl<D: Driver> Driver for Watched<D> {
fn poll(&mut self, now: Instant, waiter: &kio::Waiter) -> moq_net::Result<Option<Instant>> {
SIZED.with(|sized| sized.set((WATCH.with(Cell::get), sized.get().1)));
let next = self.0.poll(now, waiter);
SIZED.with(|sized| sized.set((0, sized.get().1)));
next
}
}

/// A served request keeps one copy of its track name: the one its owned message
/// made, which moves into the log context as the request starts, rather than a
/// second one for the log context alone.
///
/// Counts the publisher's allocations sized exactly like the name across a whole
/// subscription, so the name is an unusual length: a `String` copy of it is sized
/// that way, while the model's shared `Arc<str>` copies carry a header.
#[kio::test]
async fn a_lite_request_keeps_one_copy_of_its_track_name() {
const TRACK: &str = "video/hd/a-track-name-of-an-odd-length";

// Moq-lite 05 and later ask for the track's info on a TRACK stream of its own
// before the SUBSCRIBE, so the publisher serves two requests to 03's one.
for (version, requests) in [("moq-lite-03", 1), ("moq-lite-07-wip", 2)] {
let version: Version = version.parse().unwrap();
let (publisher, driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap()));
spawn(driver);
let broadcast = publisher.publish("room", Default::default()).unwrap();
let track = broadcast.create_track(TRACK, track::Info::default()).unwrap();
let (viewer, driver) = origin::Producer::new(origin::Config::new(Hop::new(2).unwrap()));
spawn(driver);

let mut transport = memory::Config::default();
transport.protocol = Some(version.alpn().to_string());
let (client, server) = create_mock_session_pair(transport);
let connect = Client::new()
.with_versions(version.into())
.with_subscriber(viewer.clone());
let accept = Server::new()
.with_versions(version.into())
.with_publisher(publisher.consume());
// Both at once: each handshake waits on the other's driver.
let (client, server) = futures::join!(
async {
let (session, driver) = connect.connect(now(), client).await.unwrap();
spawn(driver);
session
},
async {
let (session, driver) = accept.accept(now(), server).await.unwrap();
spawn(Watched(driver));
session
}
);

let remote = viewer
.consume()
.follow("room", None)
.unwrap()
.available()
.await
.unwrap();
WATCH.with(|watch| watch.set(TRACK.len()));
SIZED.with(|sized| sized.set((0, 0)));
let subscriber = remote.track(TRACK).unwrap().subscribe(None).await.unwrap();
subscribed(&track).await;
WATCH.with(|watch| watch.set(0));
let copies = SIZED.with(|sized| sized.replace((0, 0)).1);
assert_eq!(copies, requests, "{version}: the publisher's copies of the track name");
drop((subscriber, client, server));
}
}
Loading
Loading