Skip to content

Commit 9bc9fbf

Browse files
authored
Implement grpc.Compressor.DecompressedSize for snappy to optimize memory allocations (#5213)
Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
1 parent 4d433cc commit 9bc9fbf

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [ENHANCEMENT] Update Go version to 1.20.1. #5159
2424
* [ENHANCEMENT] Distributor: Reuse byte slices when serializing requests from distributors to ingesters. #5193
2525
* [ENHANCEMENT] Query Frontend: Add number of chunks and samples fetched in query stats. #5198
26+
* [ENHANCEMENT] Implement grpc.Compressor.DecompressedSize for snappy to optimize memory allocations. #5213
2627
* [FEATURE] Querier/Query Frontend: support Prometheus /api/v1/status/buildinfo API. #4978
2728
* [FEATURE] Ingester: Add active series to all_user_stats page. #4972
2829
* [FEATURE] Ingester: Added `-blocks-storage.tsdb.head-chunks-write-queue-size` allowing to configure the size of the in-memory queue used before flushing chunks to the disk . #5000

pkg/util/grpcencoding/snappy/snappy.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ func (c *compressor) Decompress(r io.Reader) (io.Reader, error) {
5151
return reader{dr, &c.readersPool}, nil
5252
}
5353

54+
// If a Compressor implements DecompressedSize(compressedBytes []byte) int,
55+
// gRPC will call it to determine the size of the buffer allocated for the
56+
// result of decompression.
57+
// Return -1 to indicate unknown size.
58+
//
59+
// This is an EXPERIMENTAL feature of grpc-go.
60+
func (c *compressor) DecompressedSize(compressedBytes []byte) int {
61+
decompressedSize, err := snappy.DecodedLen(compressedBytes)
62+
if err != nil {
63+
return -1
64+
}
65+
return decompressedSize
66+
}
67+
5468
type writeCloser struct {
5569
writer *snappy.Writer
5670
pool *sync.Pool

0 commit comments

Comments
 (0)