core/vm: reuse Memory instances

This commit is contained in:
lmittmann 2024-07-10 09:01:53 +02:00
parent 37590b2c55
commit f62d48649f
2 changed files with 23 additions and 1 deletions

View file

@ -198,6 +198,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
// they are returned to the pools
defer func() {
returnStack(stack)
mem.Free()
}()
contract.Input = input

View file

@ -17,9 +17,19 @@
package vm
import (
"sync"
"github.com/holiman/uint256"
)
var memoryPool = sync.Pool{
New: func() any {
return &Memory{
store: make([]byte, 0),
}
},
}
// Memory implements a simple memory model for the ethereum virtual machine.
type Memory struct {
store []byte
@ -28,7 +38,18 @@ type Memory struct {
// NewMemory returns a new memory model.
func NewMemory() *Memory {
return &Memory{}
return memoryPool.Get().(*Memory)
}
// Free returns the memory to the pool.
func (m *Memory) Free() {
// To reduce peak allocation, return only smaller memory instances to the pool.
const maxBufferSize = 16 << 10
if cap(m.store) <= maxBufferSize {
m.store = m.store[:0]
m.lastGasCost = 0
memoryPool.Put(m)
}
}
// Set sets offset + size to value