facett-core/tests/lane_is_installed_allocates_nothing.rs
//! **`lane::is_installed` allocates nothing** — the reason it exists.
//!
//! A pane chooses "GPU lane or CPU painter" every frame. Before `is_installed`, the
//! public way to ask was `lane::lanes()`, which clones every lane record (each with an
//! owned `String` name) to answer one bool, sixty times a second. This binary counts
//! the bytes the CALLING THREAD asks the allocator for across many calls and requires
//! zero — so "cheap" is a measurement here, not an adjective.
//!
//! The counter is per thread (a const-initialised thread-local, which itself never
//! allocates), so the test harness's other threads cannot move it.
//!
//! RED: answer `is_installed` with `lanes().iter().any(…)` and the count leaves zero.
//!
//! Run: `cargo test -p facett-core --test lane_is_installed_allocates_nothing`
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use facett_core::render::lane;
struct Counting;
thread_local! {
static ALLOCATED: Cell<usize> = const { Cell::new(0) };
}
// SAFETY: forwards every call to `System` unchanged; it only counts.
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let _ = ALLOCATED.try_with(|c| c.set(c.get() + layout.size()));
// SAFETY: same contract as the caller's.
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// SAFETY: same contract as the caller's.
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let _ = ALLOCATED.try_with(|c| c.set(c.get() + new_size));
// SAFETY: same contract as the caller's.
unsafe { System.realloc(ptr, layout, new_size) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn allocated_by(f: impl FnOnce()) -> usize {
let before = ALLOCATED.with(Cell::get);
f();
ALLOCATED.with(Cell::get) - before
}
#[test]
fn is_installed_allocates_nothing_per_call() {
// Populate the registry first (that may allocate — once per NAME, not per call).
for name in ["alloc::LaneA", "alloc::LaneB", "alloc::LaneC", "alloc::LaneD"] {
lane::note_installed(name);
}
let _ = lane::installed_flag("alloc::NeverInstalled");
let mut hits = 0usize;
let bytes = allocated_by(|| {
for _ in 0..10_000 {
hits += usize::from(lane::is_installed("alloc::LaneC"));
hits += usize::from(lane::is_installed("alloc::NeverInstalled"));
hits += usize::from(lane::is_installed("alloc::NoSuchLane"));
}
});
assert_eq!(hits, 10_000, "only LaneC is installed: one hit per round");
assert_eq!(bytes, 0, "30 000 is_installed calls asked the allocator for {bytes} bytes");
// …and the cached-flag form a paint path keeps in a `OnceLock` is a plain load.
let flag = lane::installed_flag("alloc::LaneA");
let bytes = allocated_by(|| {
for _ in 0..10_000 {
assert!(flag.load(std::sync::atomic::Ordering::Acquire));
}
});
assert_eq!(bytes, 0);
// The control: the old way to ask DOES allocate, so the counter can see it.
let bytes = allocated_by(|| {
let _ = lane::lanes().iter().any(|l| l.name == "alloc::LaneC" && l.installed);
});
assert!(bytes > 0, "the control must allocate, or this counter measures nothing");
}