perf: Skip request-body compression for already-compressed content types - #987
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #987 +/- ##
==========================================
+ Coverage 94.65% 94.70% +0.04%
==========================================
Files 58 58
Lines 5259 5323 +64
==========================================
+ Hits 4978 5041 +63
- Misses 281 282 +1
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.
…on-for-compressed-types # Conflicts: # tests/unit/test_utils.py
B4nan
approved these changes
Aug 3, 2026
vdusek
added a commit
that referenced
this pull request
Aug 4, 2026
…est bodies (#997) A caller-supplied `Content-Encoding` now marks the request body as already encoded, so the client forwards it untouched instead of dropping the header (since #987) or overwriting it (before #987). ## How it works - Precedence in `_prepare_request_call`: caller `Content-Encoding` -> content-type heuristic -> size threshold -> compress. The header alone is the "hands off" signal, as in `apify-client-js`, so no opt-out parameter lands on the public `HttpClient` ABC. Lookups go through a new case-insensitive `_get_header`. - The header is forwarded verbatim, so encodings the client ships no compressor for (`deflate`) work too, and `identity` becomes a per-request opt-out. - `KeyValueStoreClient.set_record` gains a `content_encoding` argument (sync and async). Without it the behavior is barely reachable, as no public resource method took per-request headers. ## Notes - Not breaking: #987 and #988 are both still in the unreleased 3.1.2 section, so the header-dropping behavior never shipped. - Three tests from #987 flip. Two collapse into one parametrized `test_prepare_request_call_keeps_caller_content_encoding` covering their cases plus `identity` and `deflate`. - A `Content-Encoding` passed to the client constructor counts as caller-supplied on every request, as in JS, where axios config headers include the client defaults. - The compression guide drops a claim that file-like values are streamed and never compressed. Since #965 they're read into memory and compressed like any other body. - No integration test: the round trip needs a real token, so the API-side premise (a `PUT` record honors the header and serves the record with it) rests on the API docs. The unit tests cover the client side in full. Closes #996 *✍️ Drafted by Claude Code*
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