Skip to content

Implement grpc.Compressor.DecompressedSize for snappy to optimize memory allocations #5213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [ENHANCEMENT] Update Go version to 1.20.1. #5159
* [ENHANCEMENT] Distributor: Reuse byte slices when serializing requests from distributors to ingesters. #5193
* [ENHANCEMENT] Query Frontend: Add number of chunks and samples fetched in query stats. #5198
* [ENHANCEMENT] Implement grpc.Compressor.DecompressedSize for snappy to optimize memory allocations. #5213
* [FEATURE] Querier/Query Frontend: support Prometheus /api/v1/status/buildinfo API. #4978
* [FEATURE] Ingester: Add active series to all_user_stats page. #4972
* [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
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/grpcencoding/snappy/snappy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func (c *compressor) Decompress(r io.Reader) (io.Reader, error) {
return reader{dr, &c.readersPool}, nil
}

// If a Compressor implements DecompressedSize(compressedBytes []byte) int,
// gRPC will call it to determine the size of the buffer allocated for the
// result of decompression.
// Return -1 to indicate unknown size.
//
// This is an EXPERIMENTAL feature of grpc-go.
func (c *compressor) DecompressedSize(compressedBytes []byte) int {
decompressedSize, err := snappy.DecodedLen(compressedBytes)
if err != nil {
return -1
}
return decompressedSize
}

type writeCloser struct {
writer *snappy.Writer
pool *sync.Pool
Expand Down