perf(core,memory): reduce heap allocations in message processing#913
Merged
perf(core,memory): reduce heap allocations in message processing#913
Conversation
This was
linked to
issues
Feb 25, 2026
498f5ef to
cd8ecbb
Compare
cd8ecbb to
8d1e43b
Compare
- 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
8d1e43b to
ca2450b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
remove_tool_responses_middle_out: takeVec<Message>by value — eliminates one redundant full-vec clone per progressive-compaction iteration (was cloned on both early-return path and result construction path)HashSet<usize>withVec::with_capacity(to_remove)— linearcontainsis faster for N ≤ 20 and avoids hash table overheadconsolidate_summaries: replace.collect::<Vec<_>>().join("\n\n")withString::with_capacity+write!loop — eliminates intermediateVec<String>allocationload_history/load_history_filtered/message_by_id/messages_by_ids: skipserde_json::from_strwhenparts_json == "[]"— fast-paths the common empty-parts casepersist_messagefast-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— cleancargo clippy --workspace -- -D warnings— 0 warningscargo nextest run --workspace --lib --bins— 2797/2797 passedremove_tool_responses_middle_outtests verifiedload_historytests verifiedCloses #884, #885, #886, #887, #888
Part of epic #861