perf: replace deepcopy with shallow copy in graph/swarm state management#2276
Closed
AnnasMazhar wants to merge 1 commit intostrands-agents:mainfrom
Closed
perf: replace deepcopy with shallow copy in graph/swarm state management#2276AnnasMazhar wants to merge 1 commit intostrands-agents:mainfrom
AnnasMazhar wants to merge 1 commit intostrands-agents:mainfrom
Conversation
Replace copy.deepcopy() with targeted shallow copies in GraphNode and SwarmNode state save/restore. The executor only appends new messages and replaces content blocks — it does not mutate existing message dicts in-place, making shallow copies safe. Benchmark (20 messages, 10 tools, 5-node graph): Before: 1.1ms per graph execution (deepcopy) After: 0.002ms per graph execution (shallow copy) Speedup: ~600x on state management overhead For a 20-node complex workflow, this reduces copy overhead from 9ms to 0.01ms per execution. Added tests: - test_copy_messages_isolation: verifies append and key replacement on copy do not affect original - test_copy_model_state_isolation: verifies scalar and tools list modifications on copy do not affect original
Author
|
Closing this — after deeper analysis I found that the executor DOES mutate existing message dicts in-place:
A shallow copy ( A safe optimization would need to deep-copy only the Sorry for the premature PR. |
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.
Closes #2275
Summary
Replace
copy.deepcopy()with targeted shallow copies inGraphNodeandSwarmNodestate save/restore.Motivation
deepcopyon a typical conversation (20 messages, 10 tools) costs 0.218ms per call. In a 5-node graph that's 1.1ms of pure copy overhead. See #2275 for full benchmark.Changes
_copy_messages()helper:[msg.copy() for msg in messages](~67x faster)_copy_model_state()helper: shallow dict copy + tools list copy (~70x faster)copy.deepcopy()calls acrossgraph.pyandswarm.pySafety
Safe because the executor only:
agent.py:1123) — never mutates existing message dictsTesting
hatch fmt --formatter✓hatch fmt --linter✓ (ruff + mypy pass)test_copy_messages_isolation,test_copy_model_state_isolation