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 Sep 12, 2024
1 parent d04888e commit a07195d
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

// shuffleRand holds the random source to shuffle the queues.
shuffleRand *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,
shuffleRand: 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.shuffleRand.Seed(time.Now().UnixNano())
p.shuffleRand.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 a07195d

Please sign in to comment.