Lazily allocate GZipMiddleware compression resources
Merged
KL
3mo ago+20+20+01234567890123456789-3-3-0123456789
Summary
- lazily allocate
BytesIOandGzipFileonly when a response is compressed - retain context-managed, reverse-order cleanup on normal completion and application errors
- add regression coverage for responses below
minimum_size, existingContent-Encoding,text/event-stream, andhttp.response.pathsend - update the gzip benchmark to use the lazy compression context
Motivation
GZipResponder previously constructed the gzip buffer and compressor before it knew whether the response should be compressed. Bypassed responses therefore paid the compressor's allocation cost even though their body was forwarded unchanged.
This keeps the public GZipMiddleware configuration and response behavior unchanged while avoiding those allocations for bypassed responses. CodSpeed's memory-mode bypass benchmarks will measure the resulting allocation reduction.
Validation
scripts/checkuv run pytest— 989 passed, 2 skipped, 2 xfailed- focused gzip tests and representative CodSpeed benchmark cases on Python 3.14