From 19a0b4679a6e7827b7a276ea5da3b0c53a479253 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 30 Jul 2022 00:29:46 +0300 Subject: [PATCH] app/vmselect/netstorage: re-use random generator used for series shuffle in Result.RunParallel This should reduce CPU usage needed for rand.Rand initialization --- app/vmselect/netstorage/netstorage.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index c78f4a688a..27f338bdb6 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -182,10 +182,11 @@ func (rss *Results) RunParallel(qt *querytracer.Tracer, f func(rs *Result, worke tsws[i] = tsw } // 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) { tsws[i], tsws[j] = tsws[j], tsws[i] }) + putRand(r) // Spin up up to gomaxprocs local workers and split work equally among them. // 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 } +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 ( rowsReadPerSeries = metrics.NewHistogram(`vm_rows_read_per_series`) rowsReadPerQuery = metrics.NewHistogram(`vm_rows_read_per_query`)