core/vm: refactor memory resize #33056 (#1845)

This commit is contained in:
wit liu 2025-12-09 13:31:47 +08:00 committed by GitHub
parent 70755237e7
commit 20e6a3ef9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View file

@ -57,10 +57,14 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) {
val.PutUint256(m.store[offset:])
}
// Resize resizes the memory to size
// Resize grows the memory to the requested 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(len(m.store)) < size {
if uint64(cap(m.store)) >= size {
m.store = m.store[:size]
} else {
m.store = append(m.store, make([]byte, size-uint64(len(m.store)))...)
}
}
}

View file

@ -67,3 +67,10 @@ func TestMemoryCopy(t *testing.T) {
}
}
}
func BenchmarkResize(b *testing.B) {
memory := NewMemory()
for i := range b.N {
memory.Resize(uint64(i))
}
}