Skip to content

Commit

Permalink
feat: follow the latest k8s code and optimize parallel performance
Browse files Browse the repository at this point in the history
  • Loading branch information
slipegg committed Oct 17, 2024
1 parent d25f196 commit e25741f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/util/parallelize/parallelism.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,28 @@ package parallelize

import (
"context"
"math"

"k8s.io/client-go/util/workqueue"
)

const parallelism = 16

// chunkSizeFor returns a chunk size for the given number of items to use for
// parallel work. The size aims to produce good CPU utilization.
// returns max(1, min(sqrt(n), n/Parallelism))
func chunkSizeFor(n, parallelism int) int {
s := int(math.Sqrt(float64(n)))

if r := n/parallelism + 1; s > r {
s = r
} else if s < 1 {
s = 1
}
return s
}

// Until is a wrapper around workqueue.ParallelizeUntil to use in scheduling algorithms.
func Until(ctx context.Context, pieces int, doWorkPiece workqueue.DoWorkPieceFunc) {
workqueue.ParallelizeUntil(ctx, parallelism, pieces, doWorkPiece)
workqueue.ParallelizeUntil(ctx, parallelism, pieces, doWorkPiece, workqueue.WithChunkSize(chunkSizeFor(pieces, parallelism)))
}

0 comments on commit e25741f

Please sign in to comment.