Skip to content

perf(indexgateway): Only calculate chunk groups if we're going to return them - #24113

Open
DanHopperGrafana wants to merge 2 commits into
mainfrom
dahoppe/only-build-chunk-groups-when-needed
Open

perf(indexgateway): Only calculate chunk groups if we're going to return them#24113
DanHopperGrafana wants to merge 2 commits into
mainfrom
dahoppe/only-build-chunk-groups-when-needed

Conversation

@DanHopperGrafana

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:
Two commits:

  1. Small refactor identified in chore: Remove dead code relating to blooms in Gateway.GetShards #24103 (review), and add a benchmark.
  2. Optimisation - don't do work that we're going to throw away.

Benchmark results:

                                                          │ before.txt  │              after.txt              │
                                                          │   sec/op    │   sec/op     vs base                │
BuildShardsResponse/series=10000/chunks_per_series=100-12   54.28m ± 3%   34.34m ± 1%  -36.73% (p=0.000 n=10)
BuildShardsResponse/series=100000/chunks_per_series=10-12   89.12m ± 1%   69.67m ± 1%  -21.82% (p=0.000 n=10)
BuildShardsResponse/series=1000000/chunks_per_series=1-12   317.1m ± 1%   292.6m ± 2%   -7.73% (p=0.000 n=10)
geomean                                                     115.3m        88.79m       -23.01%

                                                          │  before.txt  │              after.txt               │
                                                          │     B/op     │     B/op      vs base                │
BuildShardsResponse/series=10000/chunks_per_series=100-12   226.5Mi ± 0%   157.3Mi ± 0%  -30.58% (p=0.000 n=10)
BuildShardsResponse/series=100000/chunks_per_series=10-12   266.4Mi ± 0%   197.1Mi ± 0%  -26.00% (p=0.000 n=10)
BuildShardsResponse/series=1000000/chunks_per_series=1-12   400.6Mi ± 0%   331.3Mi ± 0%  -17.30% (p=0.000 n=10)
geomean                                                     289.2Mi        217.4Mi       -24.82%

                                                          │  before.txt   │              after.txt              │
                                                          │   allocs/op   │  allocs/op   vs base                │
BuildShardsResponse/series=10000/chunks_per_series=100-12   1087.75k ± 0%   85.22k ± 0%  -92.17% (p=0.000 n=10)
BuildShardsResponse/series=100000/chunks_per_series=10-12    1508.5k ± 0%   506.0k ± 0%  -66.45% (p=0.000 n=10)
BuildShardsResponse/series=1000000/chunks_per_series=1-12     2.024M ± 0%   1.021M ± 0%  -49.53% (p=0.000 n=10)
geomean                                                       1.492M        353.2k       -76.33%

Which issue(s) this PR fixes: N/A

Special notes for your reviewer: I recommend reviewing the commits separately.

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • Title matches the required conventional commits format, see here
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/setup/upgrade/_index.md
  • If the change is deprecating or removing a configuration option, update the deprecated-config.yaml and deleted-config.yaml files respectively in the tools/deprecated-config-checker directory. Example PR

Benchmark:
```
                                                          │ before.txt  │              after.txt              │
                                                          │   sec/op    │   sec/op     vs base                │
BuildShardsResponse/series=10000/chunks_per_series=100-12   54.28m ± 3%   34.34m ± 1%  -36.73% (p=0.000 n=10)
BuildShardsResponse/series=100000/chunks_per_series=10-12   89.12m ± 1%   69.67m ± 1%  -21.82% (p=0.000 n=10)
BuildShardsResponse/series=1000000/chunks_per_series=1-12   317.1m ± 1%   292.6m ± 2%   -7.73% (p=0.000 n=10)
geomean                                                     115.3m        88.79m       -23.01%

                                                          │  before.txt  │              after.txt               │
                                                          │     B/op     │     B/op      vs base                │
BuildShardsResponse/series=10000/chunks_per_series=100-12   226.5Mi ± 0%   157.3Mi ± 0%  -30.58% (p=0.000 n=10)
BuildShardsResponse/series=100000/chunks_per_series=10-12   266.4Mi ± 0%   197.1Mi ± 0%  -26.00% (p=0.000 n=10)
BuildShardsResponse/series=1000000/chunks_per_series=1-12   400.6Mi ± 0%   331.3Mi ± 0%  -17.30% (p=0.000 n=10)
geomean                                                     289.2Mi        217.4Mi       -24.82%

                                                          │  before.txt   │              after.txt              │
                                                          │   allocs/op   │  allocs/op   vs base                │
BuildShardsResponse/series=10000/chunks_per_series=100-12   1087.75k ± 0%   85.22k ± 0%  -92.17% (p=0.000 n=10)
BuildShardsResponse/series=100000/chunks_per_series=10-12    1508.5k ± 0%   506.0k ± 0%  -66.45% (p=0.000 n=10)
BuildShardsResponse/series=1000000/chunks_per_series=1-12     2.024M ± 0%   1.021M ± 0%  -49.53% (p=0.000 n=10)
geomean                                                       1.492M        353.2k       -76.33%
```
@DanHopperGrafana
DanHopperGrafana requested a review from a team as a code owner August 20, 2026 15:42

@joe-elliott joe-elliott left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some Qs and thoughts, but i'm good on the merge. nice improvement!

give me a ping if you make changes and want a second review

seen[model.Fingerprint(ref.Fingerprint)] = struct{}{}
}
resp.Statistics.Index.TotalStreams = int64(len(seen))
seen := make(map[model.Fingerprint]struct{}, 1024)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on the impact of perfectly counting streams? is this useful? costly?

is there a proxy value or approx count that would work just as well?

@@ -2,8 +2,10 @@ package indexgateway

import (
"context"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that you have broken them out there is an opportunity to test chunkGroupsForShards and buildShardsResponse directly

for _, s := range shards {
from := sort.Search(len(filtered), func(i int) bool {
return filtered[i].Fingerprint >= uint64(s.Bounds.Min)
from := sort.Search(len(chunkRefs), func(i int) bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is outside the point of this PR, but does this actually work? are chunkRefs guaranteed to be sorted?

they come from the call GetChunkRefsWithSizingInfo and they do not appear to be sorted in at least this implementation

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.

2 participants