mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32: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
|
// they are returned to the pools
|
||||||
defer func() {
|
defer func() {
|
||||||
returnStack(stack)
|
returnStack(stack)
|
||||||
|
mem.Free()
|
||||||
}()
|
}()
|
||||||
contract.Input = input
|
contract.Input = input
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,19 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/holiman/uint256"
|
"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.
|
// Memory implements a simple memory model for the ethereum virtual machine.
|
||||||
type Memory struct {
|
type Memory struct {
|
||||||
store []byte
|
store []byte
|
||||||
|
|
@ -28,7 +38,18 @@ type Memory struct {
|
||||||
|
|
||||||
// NewMemory returns a new memory model.
|
// NewMemory returns a new memory model.
|
||||||
func NewMemory() *Memory {
|
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
|
// Set sets offset + size to value
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue