aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/intpool.go
diff options
context:
space:
mode:
authorGuillaume Ballet <gballet@gmail.com>2018-07-03 18:06:42 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-07-03 18:06:42 +0800
commit4e5d1f1c39159de42511770bd390ad583ebd57a5 (patch)
treed1ce3fb28c78dc6bdc73026eacfee92fa71ce447 /core/vm/intpool.go
parentd57e85ecc91608a9095479365308a285f05c755d (diff)
downloaddexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.tar
dexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.tar.gz
dexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.tar.bz2
dexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.tar.lz
dexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.tar.xz
dexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.tar.zst
dexon-4e5d1f1c39159de42511770bd390ad583ebd57a5.zip
core/vm: reuse bigint pools across transactions (#17070)
* core/vm: A pool for int pools * core/vm: fix rebase issue * core/vm: push leftover stack items after execution, not before
Diffstat (limited to 'core/vm/intpool.go')
-rw-r--r--core/vm/intpool.go41
1 files changed, 40 insertions, 1 deletions
diff --git a/core/vm/intpool.go b/core/vm/intpool.go
index 5dbda18ee..917a78d56 100644
--- a/core/vm/intpool.go
+++ b/core/vm/intpool.go
@@ -16,7 +16,10 @@
package vm
-import "math/big"
+import (
+ "math/big"
+ "sync"
+)
var checkVal = big.NewInt(-42)
@@ -65,3 +68,39 @@ func (p *intPool) put(is ...*big.Int) {
p.pool.push(i)
}
}
+
+// The intPool pool's default capacity
+const poolDefaultCap = 25
+
+// intPoolPool manages a pool of intPools.
+type intPoolPool struct {
+ pools []*intPool
+ lock sync.Mutex
+}
+
+var poolOfIntPools = &intPoolPool{
+ pools: make([]*intPool, 0, poolDefaultCap),
+}
+
+// get is looking for an available pool to return.
+func (ipp *intPoolPool) get() *intPool {
+ ipp.lock.Lock()
+ defer ipp.lock.Unlock()
+
+ if len(poolOfIntPools.pools) > 0 {
+ ip := ipp.pools[len(ipp.pools)-1]
+ ipp.pools = ipp.pools[:len(ipp.pools)-1]
+ return ip
+ }
+ return newIntPool()
+}
+
+// put a pool that has been allocated with get.
+func (ipp *intPoolPool) put(ip *intPool) {
+ ipp.lock.Lock()
+ defer ipp.lock.Unlock()
+
+ if len(ipp.pools) < cap(ipp.pools) {
+ ipp.pools = append(ipp.pools, ip)
+ }
+}