From 28b37142a55f8f105475ac4380b4e49f3a160e93 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 31 Oct 2019 19:07:45 +1100 Subject: Optimize a bunch of stuff --- funcs.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'funcs.go') diff --git a/funcs.go b/funcs.go index 4967e83..47c6273 100644 --- a/funcs.go +++ b/funcs.go @@ -1,14 +1,26 @@ package stm +import ( + "sync" +) + +var ( + txPool = sync.Pool{New: func() interface{} { + tx := &Tx{ + reads: make(map[*Var]uint64), + writes: make(map[*Var]interface{}), + } + tx.cond.L = &globalLock + return tx + }} +) + // Atomically executes the atomic function fn. func Atomically(fn func(*Tx)) interface{} { -retry: // run the transaction - tx := &Tx{ - reads: make(map[*Var]uint64), - writes: make(map[*Var]interface{}), - } - tx.cond.L = &globalLock + tx := txPool.Get().(*Tx) +retry: + tx.reset() var ret interface{} if func() (retry bool) { defer func() { @@ -40,6 +52,7 @@ retry: // commit the write log and broadcast that variables have changed tx.commit() globalLock.Unlock() + tx.recycle() return ret } -- cgit v1.2.3