Repository navigation
Bound the size of each chunk the Inflater returns - #865
Merged
Merged
Conversation
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
force-pushed
the
bound-inflated-chunk-size
branch
from
November 25, 2026 17:36
9d16f87 to
71fc294
Compare
This comment has been minimized.
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.
🔻 Duel: 1 benchmark failedCompared 41de179 (merged with
Details: 31 benchmarks in 74s, 3 unsettled
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:
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 |
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.
With
auto_inflateon,Inflater#readpartialinflated 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 withreadpartialorBody#eachwhen 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
readpartialkeeps going until a slice actually produces output, so it still never returns an empty string. When the caller passes anoutbuf, 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.rbinclude 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 skipsInflaterbecause of the zlib segfaults, so this isn't covered by mutation testing.