app/vmselect/netstorage: re-use random generator used for series shuffle in Result.RunParallel

This should reduce CPU usage needed for rand.Rand initialization
This commit is contained in:
Aliaksandr Valialkin 2022-07-30 00:29:46 +03:00
parent 90649de0c4
commit 19a0b4679a
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -182,10 +182,11 @@ func (rss *Results) RunParallel(qt *querytracer.Tracer, f func(rs *Result, worke
tsws[i] = tsw tsws[i] = tsw
} }
// Shuffle tsws for providing the equal amount of work among workers. // Shuffle tsws for providing the equal amount of work among workers.
r := rand.New(rand.NewSource(int64(fasttime.UnixTimestamp()))) r := getRand()
r.Shuffle(len(tsws), func(i, j int) { r.Shuffle(len(tsws), func(i, j int) {
tsws[i], tsws[j] = tsws[j], tsws[i] tsws[i], tsws[j] = tsws[j], tsws[i]
}) })
putRand(r)
// Spin up up to gomaxprocs local workers and split work equally among them. // Spin up up to gomaxprocs local workers and split work equally among them.
// This guarantees linear scalability with the increase of gomaxprocs // This guarantees linear scalability with the increase of gomaxprocs
@ -240,6 +241,20 @@ func (rss *Results) RunParallel(qt *querytracer.Tracer, f func(rs *Result, worke
return firstErr return firstErr
} }
var randPool sync.Pool
func getRand() *rand.Rand {
v := randPool.Get()
if v == nil {
v = rand.New(rand.NewSource(int64(fasttime.UnixTimestamp())))
}
return v.(*rand.Rand)
}
func putRand(r *rand.Rand) {
randPool.Put(r)
}
var ( var (
rowsReadPerSeries = metrics.NewHistogram(`vm_rows_read_per_series`) rowsReadPerSeries = metrics.NewHistogram(`vm_rows_read_per_series`)
rowsReadPerQuery = metrics.NewHistogram(`vm_rows_read_per_query`) rowsReadPerQuery = metrics.NewHistogram(`vm_rows_read_per_query`)