Browse Source

Merge pull request #5406 from gyuho/clientv3_slice

clientv3/concurrency: preallocate slice in stm
Gyu-Ho Lee 9 years ago
parent
commit
82c6408f38
1 changed files with 10 additions and 6 deletions
  1. 10 6
      clientv3/concurrency/stm.go

+ 10 - 6
clientv3/concurrency/stm.go

@@ -139,11 +139,12 @@ func (s *stm) commit() *v3.TxnResponse {
 }
 
 // cmps guards the txn from updates to read set
-func (s *stm) cmps() (cmps []v3.Cmp) {
+func (s *stm) cmps() []v3.Cmp {
+	cmps := make([]v3.Cmp, 0, len(s.rset))
 	for k, rk := range s.rset {
 		cmps = append(cmps, isKeyCurrent(k, rk))
 	}
-	return
+	return cmps
 }
 
 func (s *stm) fetch(key string) *v3.GetResponse {
@@ -159,11 +160,12 @@ func (s *stm) fetch(key string) *v3.GetResponse {
 }
 
 // puts is the list of ops for all pending writes
-func (s *stm) puts() (puts []v3.Op) {
+func (s *stm) puts() []v3.Op {
+	puts := make([]v3.Op, 0, len(s.wset))
 	for _, v := range s.wset {
 		puts = append(puts, v.op)
 	}
-	return
+	return puts
 }
 
 func (s *stm) reset() {
@@ -201,12 +203,14 @@ func (s *stmSerializable) Rev(key string) int64 {
 	return s.stm.Rev(key)
 }
 
-func (s *stmSerializable) gets() (keys []string, ops []v3.Op) {
+func (s *stmSerializable) gets() ([]string, []v3.Op) {
+	keys := make([]string, 0, len(s.rset))
+	ops := make([]v3.Op, 0, len(s.rset))
 	for k := range s.rset {
 		keys = append(keys, k)
 		ops = append(ops, v3.OpGet(k))
 	}
-	return
+	return keys, ops
 }
 
 func (s *stmSerializable) commit() *v3.TxnResponse {