perf(rust): avoid rebuilding and copying JSON payloads - #2711
Open
mohamedmansour wants to merge 4 commits into
Open
mohamedmansour wants to merge 4 commits into
mohamedmansour wants to merge 4 commits into
Conversation
Consume owned router parameters and borrow events during session dispatch to avoid two redundant deep copies. Add an allocation benchmark and subscriber ownership regression coverage. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep the ownership optimization focused: remove stats_alloc and benchmark documentation, and extend the existing subscriber test with a small payload instead of adding a separate large scenario. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The promised benchmark artifacts and malformed-notification recovery coverage are missing.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Optimizes Rust notification routing by consuming JSON payloads and borrowing session events to reduce copies.
Changes:
- Removes redundant session and telemetry payload clones.
- Verifies subscriber payload independence.
- Preserves malformed-payload diagnostics.
File summaries
| File | Description |
|---|---|
rust/src/router.rs |
Consumes notification parameters during deserialization. |
rust/src/session.rs |
Borrows events during dispatch. |
rust/tests/session_test.rs |
Tests independent subscriber payload ownership. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Send an invalid event before the valid payload in the existing subscriber test, without adding fixtures or helpers. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Separate payload ownership from metadata deserialization in JSON-RPC messages and session notifications, avoiding recursive Value container reconstruction. Preserve optional/null semantics, envelope validation, diagnostics, and existing subscriber ownership. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.
Problem
The Rust SDK copied payloads and rebuilt already-parsed JSON containers when decoding event metadata. Nested tool results made this unnecessary work especially expensive.
How we solved it
Move owned payloads through JSON-RPC and session routing instead of copying or reconstructing them. Deserialize the metadata separately, preserving validation, event ordering, and independent subscriber payloads.
No public API changes, new dependencies, or benchmark files.
Performance
Local release profiling of a synthetic 512-row tool result: 74,976 framed bytes, 256 events, one subscriber.
0dd9d43298e4c4f643% less processing time and 44% less allocation traffic. Gains depend on payload shape: flat 64-KiB text showed 16% less processing time and 25% less allocation traffic.
Measured on Apple M4 Pro, Rust 1.94.0, using seven timing runs and separate allocation instrumentation. These are SDK-pipeline measurements, not app-wide latency or process-memory/RSS savings.