perf: Skip request-body compression for already-compressed content types - #987
Open
vdusek wants to merge 6 commits into
Open
perf: Skip request-body compression for already-compressed content types#987vdusek wants to merge 6 commits into
vdusek wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #987 +/- ##
==========================================
+ Coverage 94.64% 94.66% +0.02%
==========================================
Files 58 58
Lines 5248 5270 +22
==========================================
+ Hits 4967 4989 +22
Misses 281 281
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Mantisus
approved these changes
Aug 3, 2026
Mantisus
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. Only a small suggestion.
| ) | ||
| """Exact media types whose payloads carry their own compression.""" | ||
|
|
||
| COMPRESSIBLE_MEDIA_TYPES = frozenset( |
Collaborator
There was a problem hiding this comment.
I would also add audio/L24 and audio/midi
| # Anything left uncompressed goes out as-is - a file-like body included - so a caller-supplied encoding | ||
| # would misdescribe it. | ||
| if data is not None and not compressed: | ||
| headers = {key: value for key, value in headers.items() if key.lower() != 'content-encoding'} |
Collaborator
There was a problem hiding this comment.
Just out of curiosity. Am I correct in understanding that we don't allow a user to compress the data themselves and send a Content-Encoding header?
…on-for-compressed-types # Conflicts: # tests/unit/test_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_prepare_request_callnow checks the request'sContent-Typebefore compressing the body and skips compression for media types that already carry their own compression:image/*,audio/*, orvideo/*type,application/zip,application/gzip,application/x-7z-compressed, rar, bzip2, xz, zstd, jar),font/woff,font/woff2).Types with a
+json/+xmlstructured suffix stay compressible, soimage/svg+xmlis not caught by theimage/prefix. The media type is normalized before matching (parameters stripped, lowercased, trimmed). When compression is skipped, any caller-suppliedContent-Encodingheader is dropped as well — the body goes out verbatim, so the header would otherwise misdescribe it.Why
The client compressed every request body unconditionally. For already-compressed payloads that burns CPU and holds a second full copy of the body in memory, while typically producing output slightly larger than the input. Key-value store records — screenshots, video, archives — are exactly this case.
End-to-end
set_recordwith a 200 MB incompressible payload:application/octet-stream(unchanged)image/png(new path)application/octet-streamis deliberately not on the list: it is the catch-all for unknown binary, which may well be uncompressed data. It is also the encoder's default when nocontent_typeis passed, so the optimization only applies when the caller sets an accurate content type — documented in the compression concept page.The remaining 2x comes from handing a
bytesbody to impit'scontent=, tracked separately in #972.Notes
This overlaps with #934 (skip compression for small payloads) — both add a condition to the same block in
_prepare_request_call. Whichever lands second needs the two conditions combined.✍️ Drafted by Claude Code