perf: fix O(N^2) accumulation in MessageStream and BetaMessageStream#997
Open
claygeo wants to merge 2 commits intoanthropics:mainfrom
Open
perf: fix O(N^2) accumulation in MessageStream and BetaMessageStream#997claygeo wants to merge 2 commits intoanthropics:mainfrom
claygeo wants to merge 2 commits intoanthropics:mainfrom
Conversation
Replace object spread with direct in-place mutation in #accumulateMessage for all content_block_delta types (text, citations, input_json, thinking, signature, compaction). Object spread copied all properties on every delta event, forcing O(N^2) work for N deltas. Direct mutation is O(N) total and is consistent with the existing message_delta handling pattern. Fixes anthropics#995 Fixes anthropics#933
Add object spread when pushing content blocks in BetaMessageStream to match MessageStream behavior. Since we now mutate content blocks in place during delta accumulation, this prevents the original event object from being modified.
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
Replaces object spread with direct in-place mutation in
#accumulateMessagefor allcontent_block_deltatypes in bothMessageStreamandBetaMessageStream. The previous implementation created a new content block object via{ ...snapshotContent, prop: newValue }on every delta event, causing O(N^2) work for N deltas.Delta types fixed:
text_delta:{ ...obj, text: old + new }→obj.text = old + newcitations_delta:[...existing, new]→Array.push()(O(N^2) → O(N) for array copy)input_json_delta: spread +definePropertyeach time →definePropertyonce, then direct assignmentthinking_delta:{ ...obj, thinking: old + new }→obj.thinking = old + newsignature_delta:{ ...obj, signature: val }→obj.signature = valcompaction_delta(Beta only):{ ...obj, content: old + new }→obj.content = old + newWhy this is safe: The
message_deltacase already uses direct mutation (snapshot.stop_reason = event.delta.stop_reason). All content block TypeScript types are mutable interfaces (noreadonlymodifiers). The snapshot is stored in a private field and events emit references to the same object.Also fixes: BetaMessageStream's
content_block_startwas missing the defensive copy ({ ...event.content_block }) that MessageStream has. Added for consistency since we now mutate content blocks in place.Fixes #995
Fixes #933
Relationship to PR #936
PR #936 addresses only
input_json_deltawith a lazy getter approach. This PR fixes all 6 delta types in both files using a simpler, consistent mutation pattern.Test plan
MessageStream.test.ts,BetaMessageStream.test.ts,streaming.test.ts)tsc --noEmitpasses with no type errors