aboutsummaryrefslogtreecommitdiff
path: root/funcs.go
diff options
context:
space:
mode:
authorMatt Joiner <anacrolix@gmail.com>2019-10-31 19:07:45 +1100
committerMatt Joiner <anacrolix@gmail.com>2019-10-31 19:07:45 +1100
commit28b37142a55f8f105475ac4380b4e49f3a160e93 (patch)
tree1d1f813d25432712fc13128e7dcf4b56674eb5cd /funcs.go
parentMerge branch 'master' into var-conds (diff)
downloadstm-28b37142a55f8f105475ac4380b4e49f3a160e93.tar.gz
stm-28b37142a55f8f105475ac4380b4e49f3a160e93.tar.xz
Optimize a bunch of stuff
Diffstat (limited to 'funcs.go')
-rw-r--r--funcs.go25
1 files changed, 19 insertions, 6 deletions
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
}