perf: replace deepcopy with shallow copy in graph/swarm (~600x faster state management)#2274
Closed
AnnasMazhar wants to merge 1 commit into
Closed
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
a46211b to
1e23190
Compare
Author
|
Closing this to follow the contribution guidelines properly. Will open an issue first, then resubmit with hatch fmt/lint verification. |
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
copy.deepcopy()with targeted shallow copies inGraphNodeandSwarmNodestate save/restore operations.Why
deepcopyrecursively copies every nested object. For a typical conversation (20 messages, 10 tools), this costs 0.218ms per call. In a 5-node graph, that's 1.1ms of pure copy overhead per execution.The executor only:
This means a shallow copy of the list + shallow copy of each message dict is semantically equivalent but ~600x faster.
Benchmark
Changes
_copy_messages()and_copy_model_state()helpers ingraph.pycopy.deepcopy()calls ingraph.pyandswarm.pyswarm.pySafety
The shallow copy is safe because:
agent.py:1123only callsself.messages.append(message)— never mutates existing messagesIf a future change mutates message dicts in-place, this would need to be reverted. Added docstrings explaining the invariant.