core/vm: refactor memory resize

This commit is contained in:
MariusVanDerWijden 2025-10-30 12:41:21 +01:00
parent 243407a3aa
commit 7e30dfb18c
2 changed files with 12 additions and 2 deletions

View file

@ -76,12 +76,15 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) {
val.PutUint256(m.store[offset:])
}
// Resize resizes the memory to size
func (m *Memory) Resize(size uint64) {
if uint64(m.Len()) < size {
if uint64(cap(m.store)) > size {
m.store = m.store[:size]
} else {
m.store = append(m.store, make([]byte, size-uint64(m.Len()))...)
}
}
}
// GetCopy returns offset + size as a new slice
func (m *Memory) GetCopy(offset, size uint64) (cpy []byte) {

View file

@ -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))
}
}