perf(cursor): reduce SSE delta allocations#166
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Code Review
This pull request optimizes the Cursor SSE framer by serializing token deltas directly into a reusable byte buffer and preserving the buffer's capacity when extracting output. The review feedback suggests further reducing allocations by defining a dedicated struct for content_block_stop events instead of using the serde_json::json! macro, and updating the benchmark to run multiple iterations on the same framer instance to accurately measure the capacity preservation optimization.
Merging this PR will improve performance by ×3.1
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | frame_cursor_sse |
241.2 µs | 78.8 µs | ×3.1 |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing amondnet/perf-reduce-per-delta-allocations-in-the-cursor (7a5a41e) with main (32bbeb0)
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request optimizes SSE framing in CursorSseFramer by replacing dynamic JSON serialization with strongly-typed structs and writing directly to a reusable byte buffer. It also refactors take_output to preserve buffer capacity for reuse and updates benchmarks to measure repeated flushes. The reviewer suggests adding an into_output(self) method to consume the framer and avoid unnecessary allocations in non-streaming paths where the framer is immediately dropped.
|
/gemini review |
Greptile SummaryThis PR reduces per-delta heap allocations in the Cursor SSE framing hot path by introducing typed
Confidence Score: 5/5Safe to merge — the optimization is entirely internal to the Cursor SSE framer, does not touch the public API, and the wire output is unchanged. The delta-emit hot paths are correctly refactored to use typed Serialize structs and direct buffer writes. The new take_output swap preserves framer-side capacity as intended, and the empty-path short-circuit correctly avoids unnecessary allocations. New tests verify JSON escaping for quotes and newlines, the capacity-preservation invariant, and the empty-flush case. No behavioral regressions are present. No files require special attention. Important Files Changed
Sequence Diagram%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant Caller
participant Framer as CursorSseFramer
participant Buf as output Vec
Caller->>Framer: emit_text_delta(text)
Framer->>Buf: extend "event: content_block_delta\ndata: "
Framer->>Buf: serde_json::to_writer (JSON appended in-place)
Framer->>Buf: extend "\n\n"
Caller->>Framer: take_output()
Note over Framer,Buf: swap — framer keeps empty Vec with old capacity
Framer-->>Caller: Vec with framed SSE bytes
Caller->>Framer: emit_text_delta(text) [next batch]
Note over Buf: writes reuse retained capacity
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant Caller
participant Framer as CursorSseFramer
participant Buf as output Vec
Caller->>Framer: emit_text_delta(text)
Framer->>Buf: extend "event: content_block_delta\ndata: "
Framer->>Buf: serde_json::to_writer (JSON appended in-place)
Framer->>Buf: extend "\n\n"
Caller->>Framer: take_output()
Note over Framer,Buf: swap — framer keeps empty Vec with old capacity
Framer-->>Caller: Vec with framed SSE bytes
Caller->>Framer: emit_text_delta(text) [next batch]
Note over Buf: writes reuse retained capacity
Reviews (1): Last reviewed commit: "chore(cursor): avoid final output alloca..." | Re-trigger Greptile |
|



Summary
Reduce per-delta allocations while translating Cursor SSE events, improving streaming response processing efficiency without changing the wire format or streaming semantics.
Milestone / spec
Not applicable — this is a focused performance optimization.
Checklist
cargo buildpassescargo test --all-features --workspacepassescargo clippy --all-targets --all-features -- -D warningscleancargo fmt --all --checkcleandocs/updated if this change deviates from itREADME.md/site/as applicable (wiki/is generated; don't hand-edit)Notes for reviewers
The optimization reduces temporary allocations in Cursor SSE delta handling while preserving existing output behavior. Benchmark changes update the affected gateway benchmark expectations.
Verification
cargo fmt --all --checkcargo clippy --all-targets --all-features -- -D warningscargo test --all-features --workspaceNo GitHub issue is linked to this change.
Summary by cubic
Reduces per-delta allocations in Cursor SSE framing by serializing events directly into a reusable buffer and avoids a final allocation by returning the buffer at completion. Wire format and streaming semantics are unchanged.
append_sse_eventto write SSE frames directly into the output buffer;format_sse_event_bytesnow delegates to it.serde::Serializestructs andserde_json::to_writerfor fewer allocations and correct escaping.take_outputswaps buffers and preserves capacity;frame_cursor_streamnow usesinto_outputfor a zero-copy final handoff.frame_cursor_ssebench to flush periodically to exercise buffer reuse; added tests for JSON escaping and capacity preservation.Written for commit 7a5a41e. Summary will update on new commits.