From 8078452405c93888334a21e58bf969d3e2af852f Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Thu, 9 Mar 2023 00:45:34 -0300 Subject: [PATCH 1/6] reuse memory buffer --- core/vm/interpreter.go | 16 +++++++++++++--- core/vm/memory.go | 9 ++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 4fef7499c3b..f26e272bbd8 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -18,6 +18,7 @@ package vm import ( "hash" + "sync" "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" @@ -43,6 +44,12 @@ type Config struct { ExtraEips []int // Additional EIPS that are to be enabled } +var pool = sync.Pool{ + New: func() any { + return NewMemory() + }, +} + func (vmConfig *Config) HasEip3860(rules *chain.Rules) bool { for _, eip := range vmConfig.ExtraEips { if eip == 3860 { @@ -192,8 +199,8 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( in.returnData = nil var ( - op OpCode // current opcode - mem = NewMemory() // bound memory + op OpCode // current opcode + mem = pool.Get().(*Memory) locStack = stack.New() callContext = &ScopeContext{ Memory: mem, @@ -215,6 +222,8 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( // Don't move this deferrred function, it's placed before the capturestate-deferred method, // so that it get's executed _after_: the capturestate needs the stacks before // they are returned to the pools + mem.Reset() + defer pool.Put(mem) defer stack.ReturnNormalStack(locStack) contract.Input = input @@ -304,7 +313,8 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( err = nil // clear stop token error } - return res, err + ret = append(ret, res...) + return } // Depth returns the current call stack depth. diff --git a/core/vm/memory.go b/core/vm/memory.go index 14ebad8b7b1..6f877d95787 100644 --- a/core/vm/memory.go +++ b/core/vm/memory.go @@ -30,7 +30,9 @@ type Memory struct { // NewMemory returns a new memory model. func NewMemory() *Memory { - return &Memory{} + return &Memory{ + store: make([]byte, 0, 4*1024), + } } // Set sets offset + size to value @@ -77,6 +79,11 @@ func (m *Memory) Resize(size uint64) { m.store = append(m.store, zeroes[:l]...) } +func (m *Memory) Reset() { + m.lastGasCost = 0 + m.store = m.store[:0] +} + // GetCopy returns offset + size as a new slice func (m *Memory) GetCopy(offset, size int64) (cpy []byte) { if size == 0 { From 88c6ec43307b7bb8687af0f609f3032ff74ab32d Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Thu, 9 Mar 2023 00:49:56 -0300 Subject: [PATCH 2/6] simplify set32 --- core/vm/memory.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/vm/memory.go b/core/vm/memory.go index 6f877d95787..0b6a0d29e4e 100644 --- a/core/vm/memory.go +++ b/core/vm/memory.go @@ -58,9 +58,8 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) { panic("invalid memory: store empty") } // Zero the memory area - copy(m.store[offset:offset+32], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) - // Fill in relevant bits - val.WriteToSlice(m.store[offset:]) + copy(m.store[offset:offset+32], zeroes) + val.WriteToSlice(m.store) } // zeroes - pre-allocated zeroes for Resize() From 8b4b833b80bd889481235072475505eaa0442e67 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Thu, 9 Mar 2023 00:52:04 -0300 Subject: [PATCH 3/6] use minio/sha256-simd --- cl/utils/crypto.go | 2 +- cmd/erigon-cl/core/state/state.go | 2 +- core/vm/contracts.go | 2 +- crypto/ecies/ecies_test.go | 2 +- crypto/ecies/params.go | 2 +- p2p/discover/v5wire/crypto_test.go | 2 +- p2p/discover/v5wire/encoding.go | 2 +- p2p/server_test.go | 2 +- turbo/trie/vtree/verkle_utils_test.go | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cl/utils/crypto.go b/cl/utils/crypto.go index 23104caf2fb..bf2acc47e0d 100644 --- a/cl/utils/crypto.go +++ b/cl/utils/crypto.go @@ -14,7 +14,7 @@ package utils import ( - "crypto/sha256" + "github.com/minio/sha256-simd" "hash" "sync" ) diff --git a/cmd/erigon-cl/core/state/state.go b/cmd/erigon-cl/core/state/state.go index 369a1606b79..65b12e4c2ef 100644 --- a/cmd/erigon-cl/core/state/state.go +++ b/cmd/erigon-cl/core/state/state.go @@ -1,7 +1,7 @@ package state import ( - "crypto/sha256" + "github.com/minio/sha256-simd" "encoding/binary" lru "github.com/hashicorp/golang-lru" diff --git a/core/vm/contracts.go b/core/vm/contracts.go index c3a4beb2327..9227f65f771 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -17,9 +17,9 @@ package vm import ( - "crypto/sha256" "encoding/binary" "errors" + "github.com/minio/sha256-simd" "math/big" "github.com/holiman/uint256" diff --git a/crypto/ecies/ecies_test.go b/crypto/ecies/ecies_test.go index 7aa0339212f..0c0497bca92 100644 --- a/crypto/ecies/ecies_test.go +++ b/crypto/ecies/ecies_test.go @@ -33,7 +33,7 @@ import ( "bytes" "crypto/elliptic" "crypto/rand" - "crypto/sha256" + "github.com/minio/sha256-simd" "encoding/hex" "flag" "fmt" diff --git a/crypto/ecies/params.go b/crypto/ecies/params.go index 0c63c138196..a408fd6fda7 100644 --- a/crypto/ecies/params.go +++ b/crypto/ecies/params.go @@ -37,7 +37,7 @@ import ( "crypto/aes" "crypto/cipher" "crypto/elliptic" - "crypto/sha256" + "github.com/minio/sha256-simd" "crypto/sha512" "fmt" "hash" diff --git a/p2p/discover/v5wire/crypto_test.go b/p2p/discover/v5wire/crypto_test.go index 7f28528c164..76c5677c6ab 100644 --- a/p2p/discover/v5wire/crypto_test.go +++ b/p2p/discover/v5wire/crypto_test.go @@ -20,7 +20,7 @@ import ( "bytes" "crypto/ecdsa" "crypto/elliptic" - "crypto/sha256" + "github.com/minio/sha256-simd" "reflect" "strings" "testing" diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index 3b8023022c0..b7c7eb31fb6 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -22,7 +22,7 @@ import ( "crypto/cipher" "crypto/ecdsa" crand "crypto/rand" - "crypto/sha256" + "github.com/minio/sha256-simd" "encoding/binary" "errors" "fmt" diff --git a/p2p/server_test.go b/p2p/server_test.go index 1decb3ead07..d4991dee05a 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -19,7 +19,7 @@ package p2p import ( "context" "crypto/ecdsa" - "crypto/sha256" + "github.com/minio/sha256-simd" "errors" "io" "math/rand" diff --git a/turbo/trie/vtree/verkle_utils_test.go b/turbo/trie/vtree/verkle_utils_test.go index 6b029f4b1d1..385b79fe6b4 100644 --- a/turbo/trie/vtree/verkle_utils_test.go +++ b/turbo/trie/vtree/verkle_utils_test.go @@ -1,7 +1,7 @@ package vtree import ( - "crypto/sha256" + "github.com/minio/sha256-simd" "math/big" "math/rand" "testing" From 4a64fa33fd9edf0fcd044dc8e96a330b0be684e1 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Thu, 9 Mar 2023 10:39:42 -0300 Subject: [PATCH 4/6] fix offset on set32 --- core/vm/memory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/vm/memory.go b/core/vm/memory.go index 0b6a0d29e4e..0f7f541a83b 100644 --- a/core/vm/memory.go +++ b/core/vm/memory.go @@ -59,7 +59,7 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) { } // Zero the memory area copy(m.store[offset:offset+32], zeroes) - val.WriteToSlice(m.store) + val.WriteToSlice(m.store[offset:]) } // zeroes - pre-allocated zeroes for Resize() From b938fd9c857e9ecb0b6be42933ca495070724466 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Thu, 9 Mar 2023 10:40:29 -0300 Subject: [PATCH 5/6] gofmt imports --- cmd/erigon-cl/core/state/state.go | 2 +- crypto/ecies/ecies_test.go | 2 +- crypto/ecies/params.go | 2 +- p2p/discover/v5wire/encoding.go | 2 +- p2p/server_test.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/erigon-cl/core/state/state.go b/cmd/erigon-cl/core/state/state.go index 65b12e4c2ef..10c69f16a44 100644 --- a/cmd/erigon-cl/core/state/state.go +++ b/cmd/erigon-cl/core/state/state.go @@ -1,8 +1,8 @@ package state import ( - "github.com/minio/sha256-simd" "encoding/binary" + "github.com/minio/sha256-simd" lru "github.com/hashicorp/golang-lru" libcommon "github.com/ledgerwatch/erigon-lib/common" diff --git a/crypto/ecies/ecies_test.go b/crypto/ecies/ecies_test.go index 0c0497bca92..b04820dbb88 100644 --- a/crypto/ecies/ecies_test.go +++ b/crypto/ecies/ecies_test.go @@ -33,10 +33,10 @@ import ( "bytes" "crypto/elliptic" "crypto/rand" - "github.com/minio/sha256-simd" "encoding/hex" "flag" "fmt" + "github.com/minio/sha256-simd" "math/big" "os" "testing" diff --git a/crypto/ecies/params.go b/crypto/ecies/params.go index a408fd6fda7..ddde8eda218 100644 --- a/crypto/ecies/params.go +++ b/crypto/ecies/params.go @@ -37,9 +37,9 @@ import ( "crypto/aes" "crypto/cipher" "crypto/elliptic" - "github.com/minio/sha256-simd" "crypto/sha512" "fmt" + "github.com/minio/sha256-simd" "hash" ethcrypto "github.com/ledgerwatch/erigon/crypto" diff --git a/p2p/discover/v5wire/encoding.go b/p2p/discover/v5wire/encoding.go index b7c7eb31fb6..fc01c27d3c6 100644 --- a/p2p/discover/v5wire/encoding.go +++ b/p2p/discover/v5wire/encoding.go @@ -22,10 +22,10 @@ import ( "crypto/cipher" "crypto/ecdsa" crand "crypto/rand" - "github.com/minio/sha256-simd" "encoding/binary" "errors" "fmt" + "github.com/minio/sha256-simd" "hash" "github.com/ledgerwatch/erigon/common/mclock" diff --git a/p2p/server_test.go b/p2p/server_test.go index d4991dee05a..030be85564b 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -19,8 +19,8 @@ package p2p import ( "context" "crypto/ecdsa" - "github.com/minio/sha256-simd" "errors" + "github.com/minio/sha256-simd" "io" "math/rand" "net" From 337bca700da52787611c229948a98fa7a060a61e Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Sun, 12 Mar 2023 23:48:22 -0300 Subject: [PATCH 6/6] specify complete offset --- core/vm/memory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/vm/memory.go b/core/vm/memory.go index 0f7f541a83b..9a9f72c6667 100644 --- a/core/vm/memory.go +++ b/core/vm/memory.go @@ -59,7 +59,7 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) { } // Zero the memory area copy(m.store[offset:offset+32], zeroes) - val.WriteToSlice(m.store[offset:]) + val.WriteToSlice(m.store[offset : offset+32]) } // zeroes - pre-allocated zeroes for Resize()