Skip to content

perf(api): reduce response serialization copies - #582

Closed
CyMule wants to merge 1 commit into
mainfrom
perf/reduce-response-serialization-copies
Closed

perf(api): reduce response serialization copies#582
CyMule wants to merge 1 commit into
mainfrom
perf/reduce-response-serialization-copies

Conversation

@CyMule

@CyMule CyMule commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Serialize JSON responses directly to bytes with orjson.
  • Send multipart framing and payloads as separate ASGI body events.
  • Avoid concatenating multipart framing and encoded payloads into another response-sized allocation.
  • Add coverage for single-file, multi-file, and multipart responses.

Why

The current response path can materialize JSON as a string before encoding it again for the HTTP response. Multipart responses also concatenate framing and payload bytes, temporarily creating another large allocation.

Direct byte serialization and separate multipart events reduce these intermediate copies.

Impact

In a representative end-to-end partition API benchmark producing a 44.9 MB JSON response:

  • Peak process RSS decreased by 0.5 MiB.
  • Peak container memory decreased by 2.5 MiB.
  • Median latency changed by +1.5%, within observed benchmark noise.

A serializer-only benchmark showed an approximately 36% reduction in peak allocation, but this did not translate into a material end-to-end memory reduction for the tested request.

Risk

Switching JSON implementations can change byte-level formatting and handling of edge values. Clients consuming parsed JSON should be unaffected, but compatibility should be verified for non-standard numeric values and clients that expect byte-identical output.

No response-size limit is introduced.

Validation

  • uv lock --check — passed.
  • uv run pytest test_general/api/test_response_serialization.py -q — 3 passed.
  • uv run ruff check --select I prepline_general/api/general.py test_general/api/test_response_serialization.py — passed.

Review in cubic

@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedpypi/​orjson@​3.11.910010010010070

View full report

@socket-security

Copy link
Copy Markdown

Warning

Review the following alerts detected in dependencies.

According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.

Action Severity Alert  (click "▶" to expand/collapse)
Warn High
Obfuscated code: pypi orjson is 90.0% likely obfuscated

Confidence: 0.90

Location: Package overview

From: pyproject.tomlpypi/orjson@3.11.9

ℹ Read more on: This package | This alert | What is obfuscated code?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support@socket.dev.

Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore pypi/orjson@3.11.9. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

View full report

@CyMule CyMule closed this Jul 23, 2026
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.

1 participant