repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by bugabinga
smith-core/tests/alloc_bounds.rs
RawFile outline
//! Allocation-count bounds on session hot paths (`SMH-SPEC-SPEC0001`,
//! memory accounting). Bounds only tighten.
use smith::message::{Message, Role};
use smith_alloc::{Counting, Scope, Stats, enter, snapshot};
use smith_core::session::{Entry, Session};
#[global_allocator]
static ALLOC: Counting = Counting;
/// `fork_at` over 1000 entries; clones every entry on the chain today.
const FORK_AT_1000_ALLOCATIONS: u64 = 2020;
fn linear_session(len: usize) -> Session {
let mut session = Session::new();
for turn in 0..len {
let entry = Entry::message(
Message::with_text(Role::User, format!("turn {turn}")),
session.selected_id(),
);
session.append_to_active(entry);
}
session
}
fn fork_cost(session: &Session, scope: Scope) -> Stats {
let leaf = session.selected_id().expect("non-empty session");
let before = snapshot(scope);
let guard = enter(scope);
let fork = session.fork_at(leaf).expect("leaf is known");
drop(guard);
let after = snapshot(scope);
assert_eq!(
fork.active_branch().entries.len(),
session.active_branch().entries.len()
);
drop(fork);
Stats {
allocations: after.allocations - before.allocations,
bytes: after.bytes - before.bytes,
live: after.live - before.live,
peak: after.peak,
}
}
#[test]
fn fork_at_over_1000_entries_is_bounded_and_reproducible() {
let session = linear_session(1000);
let first = fork_cost(&session, Scope::Session);
let second = fork_cost(&session, Scope::Session);
assert!(
first.allocations <= FORK_AT_1000_ALLOCATIONS,
"fork_at allocated {} times, bound is {FORK_AT_1000_ALLOCATIONS}",
first.allocations
);
assert_eq!(
(first.allocations, first.bytes),
(second.allocations, second.bytes)
);
}