diff --git a/core/vm/memory.go b/core/vm/memory.go index 5e11e83748f..de58c748718 100644 --- a/core/vm/memory.go +++ b/core/vm/memory.go @@ -79,7 +79,11 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) { // Resize resizes the memory to size func (m *Memory) Resize(size uint64) { if uint64(m.Len()) < size { - m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) + if uint64(cap(m.store)) > size { + m.store = m.store[:size] + } else { + m.store = append(m.store, make([]byte, size-uint64(m.Len()))...) + } } } diff --git a/core/vm/memory_test.go b/core/vm/memory_test.go index 41389b729aa..3890d18cb5a 100644 --- a/core/vm/memory_test.go +++ b/core/vm/memory_test.go @@ -83,3 +83,10 @@ func TestMemoryCopy(t *testing.T) { } } } + +func BenchmarkResize(b *testing.B) { + memory := NewMemory() + for i := range b.N { + memory.Resize(uint64(i)) + } +}