repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

perf(core): registry hands provider requests an Arc<[ToolMetadata]> snapshot; mock turn 208 -> 42 allocations

9d9850652080b0bf662056d5e6bbc3547c29b456

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

+30 / −12 text lines

Diff layout

smith-core/src/tools.rs

Modified

+23 / −6 text lines

1111use std::fs;
1212use std::io::{BufRead, BufReader, Write};
1313use std::path::{Path, PathBuf};
14use std::sync::Arc;
1415use std::sync::atomic::{AtomicUsize, Ordering};
1617const HARD_OUTPUT_CAP: usize = 256 * 1024;
2324#[derive(Clone, Debug, Default)]
2425pub struct ToolRegistry {
2526 /// Tools by name.
26 pub tools: HashMap<String, ToolMetadata>,
27 tools: HashMap<String, ToolMetadata>,
28 /// Name-ordered snapshot handed to every provider request; rebuilt on
29 /// registration, shared afterwards.
30 snapshot: Arc<[ToolMetadata]>,
2933impl ToolRegistry {
3640 /// Register or replace a tool definition.
3741 pub fn register(&mut self, meta: ToolMetadata) {
3842 self.tools.insert(meta.name.clone(), meta);
43 let mut ordered: Vec<ToolMetadata> = self.tools.values().cloned().collect();
44 ordered.sort_by(|left, right| left.name.cmp(&right.name));
45 self.snapshot = ordered.into();
46 }
48 /// The registered tool, when present.
49 #[must_use]
50 pub fn get(&self, name: &str) -> Option<&ToolMetadata> {
51 self.tools.get(name)
52 }
54 /// The name-ordered tool definitions, for provider requests.
55 #[must_use]
56 pub fn snapshot(&self) -> Arc<[ToolMetadata]> {
57 Arc::clone(&self.snapshot)
3958 }
4160 /// A registry with the seven built-in coding tools and real schemas.
179198 /// The registered tool definitions, for provider requests.
180199 #[must_use]
181 pub fn registered_tools(&self) -> Vec<ToolMetadata> {
182 let mut tools: Vec<ToolMetadata> = self.registry.tools.values().cloned().collect();
183 tools.sort_by(|left, right| left.name.cmp(&right.name));
184 tools
200 pub fn registered_tools(&self) -> Arc<[ToolMetadata]> {
201 self.registry.snapshot()
185202 }
187204 /// A handle that aborts in-flight and subsequent effects of this session.
221238 /// Returns [`SmithError::Tool`] for unknown tools, non-object input,
222239 /// missing required fields, or mismatched field types.
223240 pub fn validate(&self, name: &str, input: &serde_json::Value) -> Result<()> {
224 let meta = self.registry.tools.get(name).ok_or_else(|| {
241 let meta = self.registry.get(name).ok_or_else(|| {
225242 tool_err(
226243 "TOOL_NOT_REGISTERED",
227244 format!("tool '{name}' not registered"),

smith-harness/tests/alloc_bounds.rs

Modified

+1 / −1 text lines

1010/// One mock text turn on a fresh session: request build (one decode of the
1111/// user message), stream consumption, two recorded entries encoded once into
1212/// the arena, one single-buffer frame write each.
13const MOCK_TURN_ALLOCATIONS: u64 = 208;
13const MOCK_TURN_ALLOCATIONS: u64 = 42;
1515fn mock_turn(tag: &str) -> (u64, u64) {
1616 let dir = std::env::temp_dir().join(format!("smith_alloc_{tag}_{}", std::process::id()));

smith/src/provider.rs

Modified

+6 / −5 text lines

3030 pub model: String,
3131 /// Conversation so far, oldest first.
3232 pub messages: Vec<Message>,
33 /// Tools the model may call, validated against model capabilities.
34 pub tools: Vec<ToolMetadata>,
33 /// Tools the model may call, validated against model capabilities;
34 /// an immutable snapshot shared with the registry, never copied per turn.
35 pub tools: Arc<[ToolMetadata]>,
3536 /// Optional parameters.
3637 pub params: ProviderParams,
4344 Self {
4445 model: model.into(),
4546 messages,
46 tools: Vec::new(),
47 tools: Arc::new([]),
4748 params: ProviderParams::default(),
4849 }
4950 }
5152 /// Attach tools.
5253 #[must_use]
53 pub fn with_tools(mut self, tools: Vec<ToolMetadata>) -> Self {
54 self.tools = tools;
54 pub fn with_tools(mut self, tools: impl Into<Arc<[ToolMetadata]>>) -> Self {
55 self.tools = tools.into();
5556 self
5657 }