perf: Skip request-body compression for small payloads - #988
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #988 +/- ##
==========================================
- Coverage 94.72% 94.71% -0.01%
==========================================
Files 58 58
Lines 5323 5332 +9
==========================================
+ Hits 5042 5050 +8
- 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:
|
szaganek
approved these changes
Aug 3, 2026
Co-authored-by: Edyta <142720610+szaganek@users.noreply.github.com>
B4nan
approved these changes
Aug 3, 2026
…hreshold-934 # Conflicts: # docs/02_concepts/13_http_compression.mdx # src/apify_client/_consts.py # src/apify_client/http_clients/_base.py # src/apify_client/http_clients/_impit.py # tests/unit/test_http_clients.py
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.
Until now the Python client compressed every request body regardless of size. It now skips compression below 1024 B.
Closes #934
Why 1024 B
>=), and the same byte-based measurement (MIN_COMPRESS_BYTESinapify-client-js/src/utils.ts), so both clients now put identical bytes on the wire.run.charge40 B to 59 B,kvs.set_record27 B to 45 B, single-itemdataset.push_items10 B to 30 B. gzip breaks even only at ~88 B of realistic JSON, and high-entropy bodies such as binary key-value store records inflate at any size, by ~23 B for gzip and ~4 B for brotli.Compressing everything instead, the reverse direction raised in the issue, doesn't pay off. Over 1200 realistic bodies it inflates 749 of them while total bytes move only from −62.1% to −63.8%, and that gain sits entirely in the 512–1024 B band where no packet is saved anyway. At 1024 B nothing inflates.
Changes
MIN_COMPRESSION_SIZE = 1024._prepare_request_callcompresses only at or above it, measured on the encoded bytes so a multibytestris judged correctly.Content-Encodingis now dropped on a skipped body, where it would otherwise survive and mislabel an uncompressed payload.asyncio.to_threadhop for any body it won't compress, the hop costing 36–68 µs against 5–12 µs of compression._is_body_worth_compressingsits next to the rule it mirrors so the two can't drift apart. Ajson=body still hops, its size being unknown until serialized.Verification
Against the live API, 8 value shapes round-trip byte-identical with and without compression, small uncompressed bodies are accepted by
dataset.push_items,rq.add_request,rq.batch_add_requests,dataset.update,schedules().create,schedule.update, andwebhooks().create, and latency is unchanged.Tests that compressed tiny bodies were passing vacuously. They now cover the 0/1/1023/1024/1025 boundary for both gzip and brotli, the byte-vs-character threshold, the dropped header, and the thread-hop decisions.
test_run_charge'scompressionaxis was the suite's only end-to-end compression coverage and had gone vacuous on its 39-byte body, so it's replaced by a test asserting an above-threshold body reaches the server compressed under the configured algorithm.✍️ Drafted by Claude Code