Skip to content

perf(core,memory): reduce heap allocations in message processing#913

Merged
bug-ops merged 2 commits intomainfrom
feat/861-allocation-reduction
Feb 25, 2026
Merged

perf(core,memory): reduce heap allocations in message processing#913
bug-ops merged 2 commits intomainfrom
feat/861-allocation-reduction

Conversation

@bug-ops
Copy link
Owner

@bug-ops bug-ops commented Feb 25, 2026

Summary

  • remove_tool_responses_middle_out: take Vec<Message> by value — eliminates one redundant full-vec clone per progressive-compaction iteration (was cloned on both early-return path and result construction path)
  • Middle-out removal tracking: replace HashSet<usize> with Vec::with_capacity(to_remove) — linear contains is faster for N ≤ 20 and avoids hash table overhead
  • consolidate_summaries: replace .collect::<Vec<_>>().join("\n\n") with String::with_capacity + write! loop — eliminates intermediate Vec<String> allocation
  • load_history / load_history_filtered / message_by_id / messages_by_ids: skip serde_json::from_str when parts_json == "[]" — fast-paths the common empty-parts case
  • persist_message fast-path (Fast-path empty-parts serialization in persist_message #885) was already in place via .filter(|m| !m.parts.is_empty())

Test plan

  • cargo +nightly fmt --check — clean
  • cargo clippy --workspace -- -D warnings — 0 warnings
  • cargo nextest run --workspace --lib --bins — 2797/2797 passed
  • All 7 remove_tool_responses_middle_out tests verified
  • load_history tests verified

Closes #884, #885, #886, #887, #888
Part of epic #861

- remove_tool_responses_middle_out: take Vec<Message> by value to
  eliminate one redundant clone per progressive-compaction iteration
- middle-out removal tracking: replace HashSet<usize> with
  Vec::with_capacity(to_remove); linear scan is faster for N <= 20
- consolidate_summaries: replace collect::<Vec<_>>().join() with
  String::with_capacity + write! loop to skip intermediate allocation
- load_history / load_history_filtered / message_by_id / messages_by_ids:
  skip serde_json::from_str when parts_json == "[]" (common case)

Closes #884, #886, #887, #888. #885 was already handled.
…anges (#861)

- remove_tool_responses_middle_out: add fraction=0.0 edge case and
  ToolOutput part compaction tests
- messages.rs: add explicit fast-path coverage for load_history,
  load_history_filtered, message_by_id, messages_by_ids when
  parts_json == "[]" and non-empty parts round-trip test
@bug-ops bug-ops force-pushed the feat/861-allocation-reduction branch from 8d1e43b to ca2450b Compare February 25, 2026 21:58
@bug-ops bug-ops enabled auto-merge (squash) February 25, 2026 22:02
@bug-ops bug-ops merged commit 18e55c9 into main Feb 25, 2026
25 checks passed
@bug-ops bug-ops deleted the feat/861-allocation-reduction branch February 25, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core documentation Improvements or additions to documentation memory Persistence and memory performance Performance optimization rust size/L

Projects

None yet

1 participant