Skip to content

perf: Skip request-body compression for already-compressed content types - #987

Open
vdusek wants to merge 6 commits into
masterfrom
feat/skip-compression-for-compressed-types
Open

perf: Skip request-body compression for already-compressed content types#987
vdusek wants to merge 6 commits into
masterfrom
feat/skip-compression-for-compressed-types

Conversation

@vdusek

@vdusek vdusek commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

What

_prepare_request_call now checks the request's Content-Type before compressing the body and skips compression for media types that already carry their own compression:

  • any image/*, audio/*, or video/* type,
  • archives (application/zip, application/gzip, application/x-7z-compressed, rar, bzip2, xz, zstd, jar),
  • web fonts (font/woff, font/woff2).

Types with a +json/+xml structured suffix stay compressible, so image/svg+xml is not caught by the image/ prefix. The media type is normalized before matching (parameters stripped, lowercased, trimmed). When compression is skipped, any caller-supplied Content-Encoding header 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_record with a 200 MB incompressible payload:

Content type Peak RSS Wall / CPU
application/octet-stream (unchanged) 604 MB - 3.02x 7.8 s / 6.9 s user
image/png (new path) 403 MB - 2.02x 1.05 s / 0.40 s user

application/octet-stream is 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 no content_type is 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 bytes body to impit's content=, 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

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels 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.66%. Comparing base (297a53d) to head (928a160).

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              
Flag Coverage Δ
integration 92.54% <92.00%> (-0.01%) ⬇️
unit 83.96% <100.00%> (+0.06%) ⬆️

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 08:14
@vdusek
vdusek requested a review from szaganek as a code owner August 3, 2026 08:14
@vdusek
vdusek requested a review from Mantisus August 3, 2026 08:14

@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. Only a small suggestion.

)
"""Exact media types whose payloads carry their own compression."""

COMPRESSIBLE_MEDIA_TYPES = frozenset(

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.

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'}

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. 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.

3 participants