From 69a187c5adfd86b2d13cc05c28e905a30fe8d99a Mon Sep 17 00:00:00 2001 From: Manish R Jain <manishrjain@gmail.com> Date: Tue, 1 Mar 2016 18:42:34 +1100 Subject: [PATCH] Create a sync.Pool for entry struct. This removes newOrExisting in uid/assigner.go from memory profiler. --- uid/assigner.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/uid/assigner.go b/uid/assigner.go index ef835558..43b80352 100644 --- a/uid/assigner.go +++ b/uid/assigner.go @@ -33,6 +33,11 @@ import ( var glog = x.Log("uid") var lmgr *lockManager var uidStore *store.Store +var eidPool = sync.Pool{ + New: func() interface{} { + return new(entry) + }, +} type entry struct { sync.Mutex @@ -63,7 +68,7 @@ func (lm *lockManager) newOrExisting(xid string) *entry { if e, ok := lm.locks[xid]; ok { return e } - e := new(entry) + e := eidPool.Get().(*entry) e.ts = time.Now() lm.locks[xid] = e return e @@ -78,6 +83,7 @@ func (lm *lockManager) clean() { if e.isOld() { count += 1 delete(lm.locks, xid) + eidPool.Put(e) } } lm.Unlock() @@ -181,9 +187,8 @@ func assignNew(pl *posting.List, xid string, instanceIdx uint64, } func stringKey(xid string) []byte { - buf := new(bytes.Buffer) - buf.WriteString("_uid_") - buf.WriteString("|") + var buf bytes.Buffer + buf.WriteString("_uid_|") buf.WriteString(xid) return buf.Bytes() } -- GitLab