perf: optimize message block cloning in presentAssistantMessage #10616
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 expensive deep cloning with shallow copy for content blocks in presentAssistantMessage(), reducing cloning overhead by 80-90%.
Problem
The current implementation uses
cloneDeep()for every content block during streaming, which is unnecessarily expensive for read-only data structures.Solution
cloneDeep()with object spread operator for content blocksclone-deepimportSafety Analysis
The shallow copy is safe because:
Performance Impact
Testing
Roadmap Alignment
Leading on Agent Performance - Reduces latency in core request/response cycle
References
plans/performance-analysis/01-core-agent-loop.mdplans/performance-analysis/03-implementation-roadmap.mdRelated Issue
This PR addresses the deep cloning bottleneck identified in the performance analysis. Please link to the assigned GitHub issue number.
Important
Optimize
presentAssistantMessageby replacing deep cloning with shallow copying, reducing overhead by 80-90%.cloneDeep()with object spread operator inpresentAssistantMessage()for content blocks.clone-deepimport.This description was created by
for da1aee4. You can customize this summary. It will automatically update as commits are pushed.