Latest Results
perf: skip Python Response wrapping for bare dict/list/str/bytes returns (#1384)
* perf: skip Python Response wrapping for bare dict/list/str/bytes returns
Handlers that return a dict/list/str/bytes no longer allocate a Python-side
`Response(...)` or fresh `Headers({...})` per request. The router now lets
those values flow through to the Rust executor, which:
- downcasts `#[pyclass] Response` directly (0 getattr calls) when the user
did return one,
- serializes bare dicts/lists via a cached `orjson.dumps`,
- wraps bare str/bytes with prebuilt content-type headers,
- falls back to the existing `FromPyObject<Response>` chain for subclasses.
Also skips the per-request `contextvars.Context()` allocation when no
middleware is registered — the #1380 cross-phase context is only needed
when `before_request`/`after_request` hooks can mutate `ContextVar`s.
Local micro-benchmark on `/json` (workers=1, processes=1, single-threaded
urllib client): 4013 rps -> 5728 rps (+43%).
All 339 integration tests and 23 unit tests pass (6 pre-existing
`test_openapi_schema.py` failures unrelated to this change).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* review: address CodeRabbit feedback
- cargo fmt: wrap the `get_description_from_pyobject` call that rustfmt
flagged on the PyResponse fast path.
- `downcast_exact::<PyResponse>()` so user Response subclasses with
overridden properties fall through to the getattr slow path, preserving
their custom read semantics.
- `downcast` (not `downcast_exact`) for PyDict/PyList/PyString/PyBytes so
primitive subclasses match the Python router's `isinstance(...)` gate
instead of falling through to the FromPyObject chain (which would fail
with TypeError).
- Revert the conditional per-request `contextvars.Context()`: with it
skipped, sync handlers calling `ContextVar.set(...)` would leak values
into the next request on the same worker thread, weakening the #1380
isolation guarantee. The J1-J4 fast paths alone still deliver the win
(~+43% locally on /json with workers=1, processes=1).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Latest Branches
0%
pre-commit-ci-update-config 0%
0%
fix/openapi-schema-generation-v2 © 2026 CodSpeed Technology