perf: replace O(n²) splice with O(n) filter in filterHeartbeatMessages#16
Open
yuanrengu wants to merge 1 commit into
Open
perf: replace O(n²) splice with O(n) filter in filterHeartbeatMessages#16yuanrengu wants to merge 1 commit into
yuanrengu wants to merge 1 commit into
Conversation
splice(i, 1) in the outer loop and splice(j, 1) in the inner content loop each shift all subsequent elements, making the function O(n²) in the number of messages and content blocks. For sessions with many heartbeat tool calls this becomes a measurable hotspot. Replace with a filter-then-rebuild approach: - Tool/toolResult messages: skip heartbeats by continue (omit from kept array) - Assistant content blocks: filter out heartbeat tool_use blocks in O(n) - Rebuild messages array in-place from kept list Behavior is identical: same heartbeat detection logic, same removal count semantics (one removed increment per message regardless of block count), same in-place mutation contract. Signed-off-by: yuanrengu <heyonggang0811@126.com>
Collaborator
|
Hi @yuanrengu, thanks for the PR! 👍 The splice → filter optimization in Thanks for the contribution! 🙏 |
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
Replace two levels of
splice()calls infilterHeartbeatMessageswith a filter-then-rebuild approach, reducing time complexity from O(n²) to O(n).The function filters heartbeat tool calls from the messages array before LLM input. Previously it used
messages.splice(i, 1)in the outer loop andcontent.splice(j, 1)in the inner loop — both O(n) per call due to element shifting.Changes
setMessageContentLocal(msg, newContent)helper for consistent content reassignment acrossmsg.type === "message"and normal message shapessplice(i, 1)→ forward iteration intokept[]array, skipping removed messages viacontinuesplice(j, 1)→content.filter(block => !isHeartbeatToolUseBlock(block))messagesarray in-place fromkeptlist at the endBehavior
No functional change — same heartbeat detection, same count semantics, same in-place mutation contract:
Test plan
npx tsdownbuilds successfully