Skip to content

Commit

Permalink
perf: Reuse rand for shuffling queues
Browse files Browse the repository at this point in the history
Instead of creating a new `rand.Rand` every time we need the next
queues, we create it once and reuse it.
  • Loading branch information
l0nax committed Oct 16, 2024
1 parent d04888e commit f4d037f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type processor struct {

starting chan<- *workerInfo
finished chan<- *base.TaskMessage

// queueShuffleRand holds the random source to shuffle the queues.
queueShuffleRand *rand.Rand
}

type processorParams struct {
Expand Down Expand Up @@ -119,6 +122,7 @@ func newProcessor(params processorParams) *processor {
shutdownTimeout: params.shutdownTimeout,
starting: params.starting,
finished: params.finished,
queueShuffleRand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

Expand Down Expand Up @@ -404,8 +408,8 @@ func (p *processor) queues() []string {
names = append(names, qname)
}
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] })
p.queueShuffleRand.Seed(time.Now().UnixNano())
p.queueShuffleRand.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] })
return uniq(names, len(p.queueConfig))
}

Expand Down

0 comments on commit f4d037f

Please sign in to comment.