Skip to content

serialize websocket requests directly#28323

Merged
jif-oai merged 1 commit into
mainfrom
jif/direct-websocket-request-serialization
Jun 15, 2026
Merged

serialize websocket requests directly#28323
jif-oai merged 1 commit into
mainfrom
jif/direct-websocket-request-serialization

Conversation

@jif-oai

@jif-oai jif-oai commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Why

Responses WebSocket requests were encoded in two steps: first into a full serde_json::Value, then again into the JSON string sent over the socket.

That walks the full request twice and keeps an extra JSON tree alive. These requests can contain the complete conversation history and tool schemas, so the extra work grows with the request size.

What changed

  • serialize ResponsesWsRequest directly to the wire string
  • pass that string through the existing WebSocket stream and send path
  • keep the existing error mapping, tracing, send timeout, and telemetry behavior
  • compare the new wire JSON with the previous to_value payload in a focused test

Performance

I measured both paths in an optimized temporary test using a 6,324,180-byte request: 4 MiB of history plus 256 tools with 8 KiB descriptions. Each path ran 100 times.

  • previous to_value + to_string: 209 ms total, 2.09 ms per request
  • direct to_string: 174 ms total, 1.74 ms per request
  • difference: about 17% faster, or 0.35 ms per request

The direct path also removes one full temporary serde_json::Value tree. For this mostly string-backed payload, that avoids roughly one payload-sized copy plus the JSON node overhead. The exact memory saving depends on the request shape.

The temporary benchmark was removed before committing.

Validation

  • just test -p codex-api — 125 passed
  • just fix -p codex-api

@jif-oai

jif-oai commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@jif-oai jif-oai changed the title [codex] serialize websocket requests directly serialize websocket requests directly Jun 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: fe58231e02

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@jif-oai jif-oai marked this pull request as ready for review June 15, 2026 14:12
Comment thread codex-rs/codex-api/src/endpoint/responses_websocket.rs
@jif-oai jif-oai merged commit baddb5e into main Jun 15, 2026
31 checks passed
@jif-oai jif-oai deleted the jif/direct-websocket-request-serialization branch June 15, 2026 16:33
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants