diff options
author | Matt Joiner <anacrolix@gmail.com> | 2019-10-31 19:07:45 +1100 |
---|---|---|
committer | Matt Joiner <anacrolix@gmail.com> | 2019-10-31 19:07:45 +1100 |
commit | 28b37142a55f8f105475ac4380b4e49f3a160e93 (patch) | |
tree | 1d1f813d25432712fc13128e7dcf4b56674eb5cd /funcs.go | |
parent | Merge branch 'master' into var-conds (diff) | |
download | stm-28b37142a55f8f105475ac4380b4e49f3a160e93.tar.gz stm-28b37142a55f8f105475ac4380b4e49f3a160e93.tar.xz |
Optimize a bunch of stuff
Diffstat (limited to 'funcs.go')
-rw-r--r-- | funcs.go | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -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 } |