From c4cb681b3a89ac91e061941b3334925acca15fbb Mon Sep 17 00:00:00 2001 From: Sahil-4555 Date: Tue, 30 Dec 2025 16:25:15 +0530 Subject: [PATCH] add --- core/vm/memory.go | 19 +++++++++++++------ core/vm/new_bench.txt | 7 +++++++ core/vm/old_bench.txt | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 core/vm/new_bench.txt create mode 100644 core/vm/old_bench.txt diff --git a/core/vm/memory.go b/core/vm/memory.go index 54bc2b2849..a6bde15d6d 100644 --- a/core/vm/memory.go +++ b/core/vm/memory.go @@ -79,13 +79,20 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) { // Resize grows the memory to the requested size. func (m *Memory) Resize(size uint64) { - 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)))...) - } + if size <= uint64(len(m.store)) { + return } + + if size <= uint64(cap(m.store)) { + prevLen := len(m.store) + m.store = m.store[:size] + clear(m.store[prevLen:]) + return + } + + store := make([]byte, size) + copy(store, m.store) + m.store = store } // GetCopy returns offset + size as a new slice diff --git a/core/vm/new_bench.txt b/core/vm/new_bench.txt new file mode 100644 index 0000000000..1cb4014e25 --- /dev/null +++ b/core/vm/new_bench.txt @@ -0,0 +1,7 @@ +goos: linux +goarch: amd64 +pkg: github.com/ethereum/go-ethereum/core/vm +cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz +BenchmarkResize-8 876326 89800 ns/op 442142 B/op 1 allocs/op +BenchmarkResize-8 signal: interrupt +FAIL github.com/ethereum/go-ethereum/core/vm 154.281s diff --git a/core/vm/old_bench.txt b/core/vm/old_bench.txt new file mode 100644 index 0000000000..53c0eff049 --- /dev/null +++ b/core/vm/old_bench.txt @@ -0,0 +1,16 @@ +goos: linux +goarch: amd64 +pkg: github.com/ethereum/go-ethereum/core/vm +cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz +BenchmarkResize-8 513681400 4.756 ns/op 5 B/op 0 allocs/op +BenchmarkResize-8 747650196 2.313 ns/op 6 B/op 0 allocs/op +BenchmarkResize-8 907757728 1.302 ns/op 5 B/op 0 allocs/op +BenchmarkResize-8 906696679 1.356 ns/op 5 B/op 0 allocs/op +BenchmarkResize-8 832139062 2.823 ns/op 5 B/op 0 allocs/op +BenchmarkResize-8 943258486 2.120 ns/op 6 B/op 0 allocs/op +BenchmarkResize-8 1000000000 1.619 ns/op 5 B/op 0 allocs/op +BenchmarkResize-8 982619973 1.388 ns/op 5 B/op 0 allocs/op +BenchmarkResize-8 761129438 1.351 ns/op 6 B/op 0 allocs/op +BenchmarkResize-8 943591894 1.643 ns/op 6 B/op 0 allocs/op +PASS +ok github.com/ethereum/go-ethereum/core/vm 37.469s