Skip to content

perf: Skip request-body compression for small payloads - #988

Merged
vdusek merged 12 commits into
masterfrom
worktree-compress-threshold-934
Aug 3, 2026
Merged

perf: Skip request-body compression for small payloads#988
vdusek merged 12 commits into
masterfrom
worktree-compress-threshold-934

Conversation

@vdusek

@vdusek vdusek commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Until now the Python client compressed every request body regardless of size. It now skips compression below 1024 B.

Closes #934

Why 1024 B

  • Parity with the JS client. It uses the same threshold, the same operator (>=), and the same byte-based measurement (MIN_COMPRESS_BYTES in apify-client-js/src/utils.ts), so both clients now put identical bytes on the wire.
  • Small bodies grow. run.charge 40 B to 59 B, kvs.set_record 27 B to 45 B, single-item dataset.push_items 10 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.
  • No packet is saved below ~1 KB, and removing a packet is the only thing that buys latency. With ~500 B of request headers on a 1400 B MSS:
Raw body gzipped Segments raw to gzip Packet saved?
786 B 344 B 1 to 1 no
1071 B 447 B 2 to 1 yes
8250 B 2958 B 7 to 3 yes

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_call compresses only at or above it, measured on the encoded bytes so a multibyte str is judged correctly.
  • A caller-supplied Content-Encoding is now dropped on a skipped body, where it would otherwise survive and mislabel an uncompressed payload.
  • The async client skips the asyncio.to_thread hop for any body it won't compress, the hop costing 36–68 µs against 5–12 µs of compression. _is_body_worth_compressing sits next to the rule it mirrors so the two can't drift apart. A json= body still hops, its size being unknown until serialized.
  • Docs: new "Minimum body size" section. The page claimed the client compresses every request body.

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, and webhooks().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's compression axis 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

@vdusek vdusek added the t-tooling Issues with this label are in the ownership of the tooling team. label Aug 3, 2026
@vdusek vdusek self-assigned this Aug 3, 2026
@github-actions github-actions Bot added this to the 146th sprint - Tooling team milestone Aug 3, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label Aug 3, 2026
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.71%. Comparing base (808bdde) to head (d112162).
⚠️ Report is 2 commits behind head on master.

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     
Flag Coverage Δ
integration 92.06% <100.00%> (-0.40%) ⬇️
unit 84.45% <100.00%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek
vdusek marked this pull request as ready for review August 3, 2026 10:10
@vdusek
vdusek requested a review from szaganek as a code owner August 3, 2026 10:10
@vdusek
vdusek requested a review from Mantisus August 3, 2026 10:13
Comment thread docs/02_concepts/13_http_compression.mdx Outdated

@Mantisus Mantisus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vdusek
vdusek requested a review from B4nan August 3, 2026 14:40
…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
vdusek merged commit 6bd31b2 into master Aug 3, 2026
30 checks passed
@vdusek
vdusek deleted the worktree-compress-threshold-934 branch August 3, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skip request-body compression for small payloads

5 participants