Skip to content

encoding/gzip: Read compressed messages through a bufio.Reader. - #9631

Open
corneliusweig wants to merge 1 commit into
grpc:masterfrom
corneliusweig:master
Open

corneliusweig wants to merge 1 commit into
grpc:masterfrom
corneliusweig:master

Conversation

@corneliusweig

Copy link
Copy Markdown

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

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.

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

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.07%. Comparing base (50ea134) to head (c2db1ef).

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     
Files with missing lines Coverage Δ
encoding/gzip/gzip.go 65.71% <100.00%> (+2.07%) ⬆️

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址