Skip to content

perf: reduce unnecessary heap allocations in core paths#886

Merged
starpit merged 1 commit intoIBM:mainfrom
starpit:perf/reduce-heap-allocations
Feb 19, 2026
Merged

perf: reduce unnecessary heap allocations in core paths#886
starpit merged 1 commit intoIBM:mainfrom
starpit:perf/reduce-heap-allocations

Conversation

@starpit
Copy link
Copy Markdown
Member

@starpit starpit commented Feb 19, 2026

Summary

  • Message::content() -> &str: Eliminates a String clone on every call — heavily used in chat template formatting (N allocations per template application)
  • split_once('/') for model routing: Replaces splitn(2, '/').collect::<Vec<_>>() with zero-allocation split_once in both generate() and map()
  • pad() mutates in-place: Removes a spurious .clone() of an already-owned Vec<u32> and replaces .collect() rebuild with in-place pop/extend/push
  • Spec::metadata() -> &GenerateMetadata: Returns a reference instead of cloning the entire struct
  • Cow::Borrowed for TreeItem::children(): Avoids deep-cloning the child vector for Par/Seq/Plus/Cross variants during tree printing

Test plan

  • cargo check with default features
  • cargo check -p spnl --features tok
  • cargo check -p spnl --features spnl-api
  • cargo test -p spnl — 33/33 pass
  • cargo test -p spnl --features tok — 41/41 pass

🤖 Generated with Claude Code

- Return &str from Message::content() instead of cloning a String
- Replace splitn().collect::<Vec<_>>() with split_once() for model routing
- Fix pad() in tokenize to mutate in-place instead of cloning/rebuilding
- Return &GenerateMetadata from Spec::metadata() instead of cloning
- Use Cow::Borrowed in TreeItem::children() for vector Query variants

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Nick Mitchell <nickm@us.ibm.com>
@starpit starpit merged commit da434bc into IBM:main Feb 19, 2026
36 checks passed
@starpit starpit deleted the perf/reduce-heap-allocations branch February 19, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant