From 7e72889e253a390a19b6746213504813a112a0a6 Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 25 Jun 2020 17:24:33 +0200 Subject: [PATCH 1/2] Reduced memory allocations in readIndexRange() Signed-off-by: Marco Pracucci --- pkg/store/bucket.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index 82513889f0..33e9d68918 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -1319,11 +1319,15 @@ func (b *bucketBlock) readIndexRange(ctx context.Context, off, length int64) ([] } defer runutil.CloseWithLogOnErr(b.logger, r, "readIndexRange close range reader") - c, err := ioutil.ReadAll(r) - if err != nil { + // Preallocate the buffer with the exact size so we don't waste allocations + // while progressively growing an initial small buffer. The buffer capacity + // is increased by MinRead to avoid extra allocations due to how ReadFrom() + // internally works. + buf := bytes.NewBuffer(make([]byte, 0, length+bytes.MinRead)) + if _, err := buf.ReadFrom(r); err != nil { return nil, errors.Wrap(err, "read range") } - return c, nil + return buf.Bytes(), nil } func (b *bucketBlock) readChunkRange(ctx context.Context, seq int, off, length int64) (*[]byte, error) { From 389ad432758a7d795abb54e2bb581ec8fa7995e6 Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 25 Jun 2020 17:32:13 +0200 Subject: [PATCH 2/2] Updated CHANGELOG Signed-off-by: Marco Pracucci --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1f6035145..51bbfd1564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel - [#2705](https://github.com/thanos-io/thanos/pull/2705) minio-go: Added support for `af-south-1` and `eu-south-1` regions. - [#2728](https://github.com/thanos-io/thanos/pull/2728) Query: Fixed panics when using larger number of replica labels with short series label sets. - [#2787](https://github.com/thanos-io/thanos/pull/2787) Update Prometheus mod to pull in prometheus/prometheus#7414. +- [#2807](https://github.com/thanos-io/thanos/pull/2807) Store: decreased memory allocations while querying block's index. ### Changed