Skip to content

Commit 158fb2b

Browse files
authored
Optimize memory buffer, simplify set32, use sha256-simd (#7060)
Hi, I'm syncing Gnosis on a Celeron N5100 to get familiar with the codebase. In the process I managed to optimize some things from profiling. Since I'm not yet on the project Discord, I decided to open this PR as a suggestion. This pass all tests here and gave me a nice boost for that platform, although I didn't have time to benchmark it yet. * reuse VM Memory objects with sync.Pool. It starts with 4k as `evmone` [code suggested](https://github.com/ethereum/evmone/blob/0897edb00179ac3d27f27c82749119003df851e7/lib/evmone/execution_state.hpp#L49) as a good value. * set32 simplification: mostly cosmetic * sha256-simd: Celeron has SHA instructions. We should probably do the same for torrent later, but this already helped as it is very CPU bound on such a low end processor. Maybe that helps ARM as well.
1 parent efd5410 commit 158fb2b

File tree

11 files changed

+32
-16
lines changed

11 files changed

+32
-16
lines changed

cl/utils/crypto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package utils
1515

1616
import (
17-
"crypto/sha256"
17+
"github.com/minio/sha256-simd"
1818
"hash"
1919
"sync"
2020
)

cmd/erigon-cl/core/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package state
22

33
import (
4-
"crypto/sha256"
54
"encoding/binary"
5+
"github.com/minio/sha256-simd"
66

77
lru2 "github.com/hashicorp/golang-lru/v2"
88
libcommon "github.com/ledgerwatch/erigon-lib/common"

core/vm/contracts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package vm
1818

1919
import (
20-
"crypto/sha256"
2120
"encoding/binary"
2221
"errors"
22+
"github.com/minio/sha256-simd"
2323
"math/big"
2424

2525
"github.com/holiman/uint256"

core/vm/interpreter.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package vm
1818

1919
import (
2020
"hash"
21+
"sync"
2122

2223
"github.com/ledgerwatch/erigon-lib/chain"
2324
libcommon "github.com/ledgerwatch/erigon-lib/common"
@@ -43,6 +44,12 @@ type Config struct {
4344
ExtraEips []int // Additional EIPS that are to be enabled
4445
}
4546

47+
var pool = sync.Pool{
48+
New: func() any {
49+
return NewMemory()
50+
},
51+
}
52+
4653
func (vmConfig *Config) HasEip3860(rules *chain.Rules) bool {
4754
for _, eip := range vmConfig.ExtraEips {
4855
if eip == 3860 {
@@ -192,8 +199,8 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
192199
in.returnData = nil
193200

194201
var (
195-
op OpCode // current opcode
196-
mem = NewMemory() // bound memory
202+
op OpCode // current opcode
203+
mem = pool.Get().(*Memory)
197204
locStack = stack.New()
198205
callContext = &ScopeContext{
199206
Memory: mem,
@@ -215,6 +222,8 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
215222
// Don't move this deferrred function, it's placed before the capturestate-deferred method,
216223
// so that it get's executed _after_: the capturestate needs the stacks before
217224
// they are returned to the pools
225+
mem.Reset()
226+
defer pool.Put(mem)
218227
defer stack.ReturnNormalStack(locStack)
219228
contract.Input = input
220229

@@ -304,7 +313,8 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
304313
err = nil // clear stop token error
305314
}
306315

307-
return res, err
316+
ret = append(ret, res...)
317+
return
308318
}
309319

310320
// Depth returns the current call stack depth.

core/vm/memory.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ type Memory struct {
3030

3131
// NewMemory returns a new memory model.
3232
func NewMemory() *Memory {
33-
return &Memory{}
33+
return &Memory{
34+
store: make([]byte, 0, 4*1024),
35+
}
3436
}
3537

3638
// Set sets offset + size to value
@@ -56,9 +58,8 @@ func (m *Memory) Set32(offset uint64, val *uint256.Int) {
5658
panic("invalid memory: store empty")
5759
}
5860
// Zero the memory area
59-
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})
60-
// Fill in relevant bits
61-
val.WriteToSlice(m.store[offset:])
61+
copy(m.store[offset:offset+32], zeroes)
62+
val.WriteToSlice(m.store[offset : offset+32])
6263
}
6364

6465
// zeroes - pre-allocated zeroes for Resize()
@@ -77,6 +78,11 @@ func (m *Memory) Resize(size uint64) {
7778
m.store = append(m.store, zeroes[:l]...)
7879
}
7980

81+
func (m *Memory) Reset() {
82+
m.lastGasCost = 0
83+
m.store = m.store[:0]
84+
}
85+
8086
// GetCopy returns offset + size as a new slice
8187
func (m *Memory) GetCopy(offset, size int64) (cpy []byte) {
8288
if size == 0 {

crypto/ecies/ecies_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import (
3333
"bytes"
3434
"crypto/elliptic"
3535
"crypto/rand"
36-
"crypto/sha256"
3736
"encoding/hex"
3837
"flag"
3938
"fmt"
39+
"github.com/minio/sha256-simd"
4040
"math/big"
4141
"os"
4242
"testing"

crypto/ecies/params.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import (
3737
"crypto/aes"
3838
"crypto/cipher"
3939
"crypto/elliptic"
40-
"crypto/sha256"
4140
"crypto/sha512"
4241
"fmt"
42+
"github.com/minio/sha256-simd"
4343
"hash"
4444

4545
ethcrypto "github.com/ledgerwatch/erigon/crypto"

p2p/discover/v5wire/crypto_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"bytes"
2121
"crypto/ecdsa"
2222
"crypto/elliptic"
23-
"crypto/sha256"
23+
"github.com/minio/sha256-simd"
2424
"reflect"
2525
"strings"
2626
"testing"

p2p/discover/v5wire/encoding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import (
2222
"crypto/cipher"
2323
"crypto/ecdsa"
2424
crand "crypto/rand"
25-
"crypto/sha256"
2625
"encoding/binary"
2726
"errors"
2827
"fmt"
28+
"github.com/minio/sha256-simd"
2929
"hash"
3030

3131
"github.com/ledgerwatch/erigon/common/mclock"

p2p/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package p2p
1919
import (
2020
"context"
2121
"crypto/ecdsa"
22-
"crypto/sha256"
2322
"errors"
23+
"github.com/minio/sha256-simd"
2424
"io"
2525
"math/rand"
2626
"net"

0 commit comments

Comments
 (0)