Skip to content

Commit

Permalink
Merge pull request #1350 from josephschorr/avoid-slice-realloc
Browse files Browse the repository at this point in the history
Avoid reallocation of slice in preloaded task runner
  • Loading branch information
vroldanbet committed May 24, 2023
2 parents 2fb89c4 + 7ab3be6 commit 140b41e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/graph/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func dispatchAllAsync[T any](
resultChan chan<- CheckResult,
concurrencyLimit uint16,
) {
tr := newPreloadedTaskRunner(ctx, concurrencyLimit)
tr := newPreloadedTaskRunner(ctx, concurrencyLimit, len(children))
for _, currentChild := range children {
currentChild := currentChild
tr.add(func(ctx context.Context) error {
Expand Down
4 changes: 2 additions & 2 deletions internal/graph/preloadedtaskrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type preloadedTaskRunner struct {
tasks []TaskFunc
}

func newPreloadedTaskRunner(ctx context.Context, concurrencyLimit uint16) *preloadedTaskRunner {
func newPreloadedTaskRunner(ctx context.Context, concurrencyLimit uint16, initialCapacity int) *preloadedTaskRunner {
// Ensure a concurrency level of at least 1.
if concurrencyLimit <= 0 {
concurrencyLimit = 1
Expand All @@ -33,7 +33,7 @@ func newPreloadedTaskRunner(ctx context.Context, concurrencyLimit uint16) *prelo
ctx: ctxWithCancel,
cancel: cancel,
sem: make(chan token, concurrencyLimit),
tasks: make([]TaskFunc, 0),
tasks: make([]TaskFunc, 0, initialCapacity),
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/graph/preloadedtaskrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestPreloadedTaskRunnerCompletesAllTasks(t *testing.T) {
defer goleak.VerifyNone(t)

tr := newPreloadedTaskRunner(context.Background(), 2)
tr := newPreloadedTaskRunner(context.Background(), 2, 5)
wg := sync.WaitGroup{}

for i := 0; i < 5; i++ {
Expand All @@ -44,7 +44,7 @@ func TestPreloadedTaskRunnerCancelsEarlyDueToError(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tr := newPreloadedTaskRunner(ctx, 3)
tr := newPreloadedTaskRunner(ctx, 3, 10)
completed := sync.Map{}

for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestPreloadedTaskRunnerCancelsEarlyDueToCancel(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tr := newPreloadedTaskRunner(ctx, 3)
tr := newPreloadedTaskRunner(ctx, 3, 10)
completed := sync.Map{}

for i := 0; i < 10; i++ {
Expand Down

0 comments on commit 140b41e

Please sign in to comment.