mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
core/vm: reuse Memory instances
This commit is contained in:
parent
37590b2c55
commit
f62d48649f
2 changed files with 23 additions and 1 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue