Преглед на файлове

policies: pool rand.Rands (#1403)

Reduce allocs when doing query host selection by pooling rands.

updates #1401
Chris Bannister преди 5 години
родител
ревизия
81b8263d9f
променени са 1 файла, в които са добавени 12 реда и са изтрити 2 реда
  1. 12 2
      policies.go

+ 12 - 2
policies.go

@@ -343,12 +343,21 @@ func (r *roundRobinHostPolicy) KeyspaceChanged(KeyspaceUpdateEvent) {}
 func (r *roundRobinHostPolicy) SetPartitioner(partitioner string)   {}
 func (r *roundRobinHostPolicy) Init(*Session)                       {}
 
+var (
+	randPool = sync.Pool{
+		New: func() interface{} {
+			return rand.New(randSource())
+		},
+	}
+)
+
 func (r *roundRobinHostPolicy) Pick(qry ExecutableQuery) NextHost {
 	src := r.hosts.get()
 	hosts := make([]*HostInfo, len(src))
 	copy(hosts, src)
 
-	rand := rand.New(randSource())
+	rand := randPool.Get().(*rand.Rand)
+	defer randPool.Put(rand)
 	rand.Shuffle(len(hosts), func(i, j int) {
 		hosts[i], hosts[j] = hosts[j], hosts[i]
 	})
@@ -881,7 +890,8 @@ func (d *dcAwareRR) Pick(q ExecutableQuery) NextHost {
 
 	// TODO: use random chose-2 but that will require plumbing information
 	// about connection/host load to here
-	r := rand.New(randSource())
+	r := randPool.Get().(*rand.Rand)
+	defer randPool.Put(r)
 	for _, l := range [][]*HostInfo{hosts[:len(local)], hosts[len(local):]} {
 		r.Shuffle(len(l), func(i, j int) {
 			l[i], l[j] = l[j], l[i]