Repository navigation
encoding/gzip: Read compressed messages through a bufio.Reader. - #9631
Open
corneliusweig wants to merge 1 commit into
Open
corneliusweig wants to merge 1 commit into
corneliusweig wants to merge 1 commit into
Conversation
Decompressing a gzip-compressed message spends a large part of its time fetching input bytes. compress/flate reads its input one byte at a time, and compress/gzip only adds a buffer when the input does not implement io.ByteReader. gRPC's message reader, mem.Reader, implements it, so every compressed byte costs a mem.Reader.ReadByte call, which makes three interface calls. For large payloads about 40% of decompression CPU is spent in ReadByte. Decompress now puts a bufio.Reader between the message and the gzip.Reader. Reading a byte becomes a slice index, and the message reader is only asked for whole blocks. The buffer is pooled together with the gzip.Reader. Removing ReadByte from mem.Reader would also avoid the slow call, but then compress/gzip allocates a new 4 KiB buffer for every message, which makes small messages about 2.5 times slower. Shortcomings: each pooled reader now holds a 4 KiB buffer, the compressed bytes are copied once more, and messages of up to about a hundred bytes do not get faster. Time to decompress one message that arrives in 16 KiB buffers, measured with v1.85.0 on linux/amd64 (median of 5 runs; allocations are the same before and after): | Payload | Before | After | Without ReadByte | |---------|---------|---------|------------------| | 0 B | 1.61 µs | 1.73 µs | 4.32 µs | | 128 B | 2.04 µs | 1.86 µs | 4.62 µs | | 1 KiB | 24.1 µs | 16.0 µs | 18.8 µs | | 64 KiB | 1.04 ms | 0.68 ms | 0.70 ms | | 4 MiB | 65.1 ms | 45.3 ms | 43.8 ms | The difference at 0 B is within the noise between runs. RELEASE NOTES: N/A Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9631 +/- ##
==========================================
+ Coverage 87.98% 88.07% +0.09%
==========================================
Files 438 438
Lines 31664 31666 +2
==========================================
+ Hits 27860 27891 +31
+ Misses 3804 3775 -29
🚀 New features to boost your workflow:
|
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.
Decompressing a gzip-compressed message spends a large part of its time fetching input bytes. compress/flate reads its input one byte at a time, and compress/gzip only adds a buffer when the input does not implement io.ByteReader. gRPC's message reader, mem.Reader, implements it, so every compressed byte costs a mem.Reader.ReadByte call, which makes three interface calls. For large payloads about 40% of decompression CPU is spent in ReadByte.
Decompress now puts a bufio.Reader between the message and the gzip.Reader. Reading a byte becomes a slice index, and the message reader is only asked for whole blocks. The buffer is pooled together with the gzip.Reader.
Removing ReadByte from mem.Reader would also avoid the slow call, but then compress/gzip allocates a new 4 KiB buffer for every message, which makes small messages about 2.5 times slower.
Shortcomings: each pooled reader now holds a 4 KiB buffer, the compressed bytes are copied once more, and messages of up to about a hundred bytes do not get faster.
Time to decompress one message that arrives in 16 KiB buffers, measured with v1.85.0 on linux/amd64 (median of 5 runs; allocations are the same before and after):
The difference at 0 B is within the noise between runs.
RELEASE NOTES: N/A
Thank you for your PR. Please read and follow
https://github.com/grpc/grpc-go/blob/master/CONTRIBUTING.md, especially the
"Guidelines for Pull Requests" section, and then delete this text before
entering your PR description.