Skip to content

Commit

Permalink
Shortcut callback execution if there are 1 or fewer elements (#11446)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:
In the case where we have zero elements, we can exit early; with 1
element there's no need for concurrency.
This will provide a small optimisation and also make our
profiles/execution traces easier to read.

We have a fairly significant amount of requests which don't require
concurrency.
  • Loading branch information
Danny Kopping committed Dec 12, 2023
1 parent cdf1f77 commit c7447e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkg/storage/stores/shipper/indexshipper/downloads/index_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,28 @@ func (t *indexSet) ForEachConcurrent(ctx context.Context, callback index.ForEach
}
defer t.indexMtx.rUnlock()

logger := util_log.WithContext(ctx, t.logger)
level.Debug(logger).Log("index-files-count", len(t.index))

if len(t.index) == 0 {
return nil
}

// shortcut; if there's only one index, there's no need for bounded concurrency
if len(t.index) == 1 {
for i := range t.index {
idx := t.index[i]
return callback(t.userID == "", idx)
}
}

//nolint:ineffassign,staticcheck
g, ctx := errgroup.WithContext(ctx)
if t.maxConcurrent == 0 {
panic("maxConcurrent cannot be 0, indexSet is being initialized without setting maxConcurrent")
}
g.SetLimit(t.maxConcurrent)

logger := util_log.WithContext(ctx, t.logger)
level.Debug(logger).Log("index-files-count", len(t.index))

for i := range t.index {
idx := t.index[i]
g.Go(func() error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ type IndexIter interface {
type IndexSlice []Index

func (xs IndexSlice) For(ctx context.Context, maxConcurrent int, fn func(context.Context, Index) error) error {
if len(xs) == 0 {
return nil
}

// shortcut; if there's only one slice, there's no need for bounded concurrency
if len(xs) == 1 {
return fn(ctx, xs[0])
}

g, ctx := errgroup.WithContext(ctx)
if maxConcurrent == 0 {
panic("maxConcurrent cannot be 0, IndexIter is being called with a maxConcurrent of 0")
Expand Down

0 comments on commit c7447e2

Please sign in to comment.