Skip to content

Bound the size of each chunk the Inflater returns - #865

Merged
sferik merged 2 commits into
mainfrom
bound-inflated-chunk-size
Nov 25, 2026
Merged

sferik merged 2 commits into
mainfrom
bound-inflated-chunk-size

Conversation

@sferik

@sferik sferik commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

With auto_inflate on, Inflater#readpartial inflated a whole read from the connection at once. Deflate can expand data by roughly 1032:1, so a single 16 KiB read of highly compressed data could come back as one chunk of about 16 MiB. That pretty much defeats the point of streaming a body with readpartial or Body#each when you're trying to keep memory bounded.

This hands zlib 4 KiB of compressed input at a time, which caps each chunk at about 4 MiB. I first tried 1 KiB, but every slice is a separate zlib call with a fixed cost, and that made inflating a large gzipped body about 1.7x slower. At 4 KiB it's about 1.25x. Leftover input from a read gets used up before the connection is read again, and readpartial keeps going until a slice actually produces output, so it still never returns an empty string. When the caller passes an outbuf, I copy the read, because a caller that reuses it could otherwise overwrite input that hasn't been inflated yet. Without one, the connection already returns a fresh string, so there's nothing to copy, and a read that fits in one slice goes to zlib whole.

This doesn't limit Body#to_s, which still reads the whole body by design, same as with an uncompressed response.

The new tests in test/http/response/inflater_test.rb include a 16 MiB gzip bomb delivered in a single read, and three of the six fail against the old inflater. I didn't see any meaningful slowdown in a quick benchmark. Mutant still skips Inflater because of the zlib segfaults, so this isn't covered by mutation testing.

Inflater#readpartial inflated a whole read from the connection at
once. Deflate can expand data by roughly 1032:1, so one 16 KiB read of
highly compressed data could come back as a chunk of about 16 MiB,
which defeats streaming a body with readpartial or Body#each as a way
to keep memory bounded.

Compressed input is now handed to zlib 1 KiB at a time, so each chunk
is at most about 1 MiB. Input left over from a read is consumed before
the connection is read again, and readpartial keeps going until a
slice produces output, so it never returns an empty string. Each read
is copied, since a caller that reuses its outbuf could otherwise
overwrite input that has not been inflated yet.
@sferik
sferik force-pushed the bound-inflated-chunk-size branch from 9d16f87 to 71fc294 Compare November 25, 2026 17:36
@github-actions

This comment has been minimized.

Each slice is a call to zlib, which has a fixed cost, so 1 KiB slices
made inflating a large gzipped body about 1.7x slower. 4 KiB slices
cap each chunk at about 4 MiB instead of 1 MiB, still a quarter of the
old worst case, and cost about 1.25x on a large body.

A read is now copied only when the caller passed an outbuf, which is
the only case where it can be overwritten while input is pending. And
a read that fits in one slice goes to zlib whole, without a byteslice.
@github-actions

Copy link
Copy Markdown

🔻 Duel: 1 benchmark failed

Compared 41de179 (merged with main) with 23212ed (where HEAD left origin/main).

Benchmark Failure Most extra CPU in
client: GET a gzipped body with auto_inflate cpu +4.0%, allocations 635 → 653 Class#new +1.0%
Details: 31 benchmarks in 74s, 3 unsettled

client: GET a gzipped body with auto_inflate: worse cpu, allocations in every measurement

cpu: 367.6µs → 382.6µs an iteration (1.04x)
allocations: 635 → 653 an iteration
where the extra CPU went (as a share of the baseline's: in each frame itself, and with what it called):
    +1.0%   +1.8%  Class#new
    +0.6%   +0.8%  HTTP::Options#assign_options
    +0.3%   +0.0%  Enumerable#filter_map
    +0.3%   +0.3%  String#downcase
    +0.3%   +1.0%  HTTP::Request::Builder#make_request_uri
    +0.2%   -0.2%  HTTP::Response::Parser::Handler#on_header_field
    +0.2%   +0.2%  String#dup
    +0.2%   +0.2%  Thread#[]
    -3.7%          in garbage collection, apart from the code
Benchmark Verdict Timings (ratio [interval]) Counts an iteration
client: GET a gzipped body with auto_inflate regressed cpu 1.040 [1.021, 1.056] slower allocations 635 → 653
headers: look up a response's headers unsettled cpu 1.005 [0.994, 1.028] unsettled
request: encode a multipart upload of 16 KB unsettled cpu 1.001 [0.981, 1.034] unsettled
response: parse a chunked body of 16 KB unsettled cpu 1.007 [0.996, 1.020] unsettled
client: GET a JSON response same cpu 1.005 [0.990, 1.016]
client: POST JSON and parse the reply same cpu 1.002 [0.991, 1.014]
client: stream a 128 KB body same cpu 1.003 [0.991, 1.018]
client: GET a chunked body same cpu 1.003 [0.980, 1.016]
client: GET on a persistent connection same cpu 1.001 [0.984, 1.018]
client: follow a redirect same cpu 0.997 [0.978, 1.011]
client: GET over a loopback TCP connection same
headers: build 5 from a hash same cpu 0.998 [0.965, 1.010]
headers: build 50 from a hash same cpu 1.000 [0.988, 1.013]
headers: merge request headers same cpu 0.996 [0.987, 1.007]
options: chain headers, auth, accept, timeout same cpu 0.996 [0.982, 1.008]
options: merge a request's options same cpu 0.999 [0.987, 1.012]
request: build a GET same cpu 1.005 [0.985, 1.017]
request: build a GET with query params same cpu 1.004 [0.985, 1.017]
request: build a POST with a JSON body same cpu 0.996 [0.980, 1.010]
request: build a POST with a form same cpu 0.997 [0.985, 1.011]
request: write a GET same cpu 0.999 [0.988, 1.013]
request: write a POST with an 8 KB body same cpu 0.999 [0.976, 1.012]
request: write a chunked body of 16 KB same cpu 0.994 [0.979, 1.010]
response: parse a small response same cpu 0.997 [0.985, 1.005]
response: parse 30 headers and a JSON body same cpu 0.990 [0.958, 1.002]
response: build one and read its metadata same cpu 0.996 [0.990, 1.004]
uri: parse a typical URL same cpu 0.999 [0.984, 1.017]
uri: normalize a typical URL same cpu 1.003 [0.990, 1.018]
uri: normalize case, port and dot segments same cpu 0.998 [0.975, 1.014]
uri: normalize a URL with a 2 KB query same cpu 1.003 [0.992, 1.014]
uri: join a relative redirect same cpu 0.999 [0.982, 1.019]

An unsettled timing was too close to the limit to settle. It doesn't fail the run, but one that's unsettled in run after run is noisy.

Across the board:

  • cpu 1.000 [0.996, 1.003] same (limit 1.0050, 30 benchmarks)

A timing is slower when a 99.0% confidence interval for its ratio, the candidate's to the baseline's, is above 1.02. How Duel decides

@sferik sferik added the duel: accept Accept a performance regression that Duel found label Nov 25, 2026
@sferik
sferik merged commit d9058fc into main Nov 25, 2026
14 of 15 checks passed
@sferik
sferik deleted the bound-inflated-chunk-size branch November 25, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duel: accept Accept a performance regression that Duel found

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant