repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

build(xtask): scoped lint fits bin-only and lib-only packages; smith-alloc::measure replaces duplicated delta logic

8398eaec934217ff3e69c09ddff224dacdd0a54e

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (4)

+54 / −24 text lines

Diff layout

smith-alloc/src/lib.rs

Modified

+22 / −0 text lines

143143 }
146/// Run `work` charged to `scope` and report what it cost: `allocations`
147/// and `bytes` are the deltas across the run, `live` and `peak` the scope's
148/// values right after it.
149///
150/// Counters are process-wide; concurrent measurements of one scope mix.
151pub fn measure<T>(scope: Scope, work: impl FnOnce() -> T) -> (T, Stats) {
152 let before = snapshot(scope);
153 let guard = enter(scope);
154 let out = work();
155 drop(guard);
156 let after = snapshot(scope);
157 (
158 out,
159 Stats {
160 allocations: after.allocations - before.allocations,
161 bytes: after.bytes - before.bytes,
162 live: after.live,
163 peak: after.peak,
164 },
165 )
166}
146168/// Every scope with its counters.
147169pub fn snapshot_all() -> [(Scope, Stats); SCOPES.len()] {
148170 SCOPES.map(|scope| (scope, snapshot(scope)))

smith-core/tests/alloc_bounds.rs

Modified

+3 / −13 text lines

22//! memory accounting). Bounds only tighten.
44use smith::message::{Message, Role};
5use smith_alloc::{Counting, Scope, Stats, enter, snapshot};
5use smith_alloc::{Counting, Scope, measure};
66use smith_core::frame::Frame;
77use smith_core::session::{EntryContent, EntryFrame, Session};
88use std::sync::Mutex;
3636 let _serial = MEASURING
3737 .lock()
3838 .unwrap_or_else(std::sync::PoisonError::into_inner);
39 let before: Stats = snapshot(Scope::Session);
40 let guard = enter(Scope::Session);
41 let out = run();
42 drop(guard);
43 let after = snapshot(Scope::Session);
44 (
45 (
46 after.allocations - before.allocations,
47 after.bytes - before.bytes,
48 ),
49 out,
50 )
39 let (out, cost) = measure(Scope::Session, run);
40 ((cost.allocations, cost.bytes), out)
5343/// Cost of one `fork_at` of the leaf.

smith-harness/tests/alloc_bounds.rs

Modified

+3 / −10 text lines

11//! Allocation-count bounds on one agent turn (`SMH-SPEC-SPEC0001`, memory
22//! accounting). Bounds only tighten.
4use smith_alloc::{Counting, Scope, enter, snapshot};
4use smith_alloc::{Counting, Scope, measure};
55use smith_harness::{RuntimeConfig, build_agent, mock_text_reply};
77#[global_allocator]
1717 let _ = std::fs::remove_dir_all(&dir);
1818 std::fs::create_dir_all(&dir).unwrap();
1919 let mut agent = build_agent(&RuntimeConfig::mock(&dir, vec![mock_text_reply("hi")])).unwrap();
20 let before = snapshot(Scope::Turn);
21 let guard = enter(Scope::Turn);
22 let outcome = agent.run_turn("hello");
23 drop(guard);
24 let after = snapshot(Scope::Turn);
20 let (outcome, cost) = measure(Scope::Turn, || agent.run_turn("hello"));
2521 assert_eq!(outcome.unwrap().text, "hi");
2622 let _ = std::fs::remove_dir_all(&dir);
27 (
28 after.allocations - before.allocations,
29 after.bytes - before.bytes,
30 )
23 (cost.allocations, cost.bytes)
3326#[test]

xtask/src/main.rs

Modified

+26 / −1 text lines

208208/// Production targets carry the full strict gate.
209209fn clippy_prod(extra: &[OsString]) -> ExitCode {
210 clippy_phase(&["--lib", "--bins"], &[], extra)
210 clippy_phase(&prod_targets(extra), &[], extra)
211}
213/// `--lib` and `--bins` as the scoped package actually has them; cargo
214/// rejects a target flag the package cannot satisfy.
215fn prod_targets(extra: &[OsString]) -> Vec<&'static str> {
216 let package = extra
217 .iter()
218 .position(|arg| arg == "-p" || arg == "--package")
219 .and_then(|at| extra.get(at + 1))
220 .map(|name| name.to_string_lossy().into_owned());
221 let Some(package) = package else {
222 return vec!["--lib", "--bins"];
223 };
224 let src = Path::new(&package).join("src");
225 let mut targets = Vec::new();
226 if src.join("lib.rs").is_file() {
227 targets.push("--lib");
228 }
229 if src.join("main.rs").is_file() || Path::new(&package).join("src/bin").is_dir() {
230 targets.push("--bins");
231 }
232 if targets.is_empty() {
233 targets = vec!["--lib", "--bins"];
234 }
235 targets
213238/// Test targets may assert, panic, and print: outcomes are the point.