Skip to content

perf(rust): avoid rebuilding and copying JSON payloads - #2711

Open
mohamedmansour wants to merge 4 commits into
github:mainfrom
mohamedmansour:mohamedmansour-rust-memory-optimization
Open

mohamedmansour wants to merge 4 commits into
github:mainfrom
mohamedmansour:mohamedmansour-rust-memory-optimization

Conversation

@mohamedmansour

@mohamedmansour mohamedmansour commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

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.

Metric Upstream 0dd9d432 This PR 98e4c4f6
Median SDK processing time 276.1 ms 157.1 ms
Allocated bytes per event 6.83 MB 3.86 MB
Allocation operations per event 39,113 25,735

43% 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.

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>
Copilot AI balanced review requested due to automatic review settings September 17, 2026 17:56
@mohamedmansour
mohamedmansour requested a review from a team as a code owner September 17, 2026 17:56
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread rust/tests/session_test.rs
Comment thread rust/src/session.rs
mohamedmansour and others added 2 commits September 17, 2026 11:05
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>
@mohamedmansour mohamedmansour changed the title perf(rust): avoid redundant notification payload copies perf(rust): avoid rebuilding and copying JSON payloads Sep 18, 2026
@mohamedmansour

Copy link
Copy Markdown
Contributor Author

@stephentoub can you please look at this perf improvement please, found by integrating into our app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants