Skip to content

Commit

Permalink
Optimize memory buffer, simplify set32, use sha256-simd (#7060)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shyba authored Mar 14, 2023
1 parent efd5410 commit 158fb2b
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cl/utils/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package utils

import (
"crypto/sha256"
"github.com/minio/sha256-simd"
"hash"
"sync"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/erigon-cl/core/state/state.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package state

import (
"crypto/sha256"
"encoding/binary"
"github.com/minio/sha256-simd"

lru2 "github.com/hashicorp/golang-lru/v2"
libcommon "github.com/ledgerwatch/erigon-lib/common"
Expand Down
2 changes: 1 addition & 1 deletion core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package vm

import (
"crypto/sha256"
"encoding/binary"
"errors"
"github.com/minio/sha256-simd"
"math/big"

"github.com/holiman/uint256"
Expand Down
16 changes: 13 additions & 3 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vm

import (
"hash"
"sync"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions core/vm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -56,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[offset : offset+32])
}

// zeroes - pre-allocated zeroes for Resize()
Expand All @@ -77,6 +78,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 {
Expand Down
2 changes: 1 addition & 1 deletion crypto/ecies/ecies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
"bytes"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"github.com/minio/sha256-simd"
"math/big"
"os"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion crypto/ecies/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/sha256"
"crypto/sha512"
"fmt"
"github.com/minio/sha256-simd"
"hash"

ethcrypto "github.com/ledgerwatch/erigon/crypto"
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v5wire/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"github.com/minio/sha256-simd"
"reflect"
"strings"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v5wire/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"crypto/cipher"
"crypto/ecdsa"
crand "crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"github.com/minio/sha256-simd"
"hash"

"github.com/ledgerwatch/erigon/common/mclock"
Expand Down
2 changes: 1 addition & 1 deletion p2p/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package p2p
import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"errors"
"github.com/minio/sha256-simd"
"io"
"math/rand"
"net"
Expand Down
2 changes: 1 addition & 1 deletion turbo/trie/vtree/verkle_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vtree

import (
"crypto/sha256"
"github.com/minio/sha256-simd"
"math/big"
"math/rand"
"testing"
Expand Down

0 comments on commit 158fb2b

Please sign in to comment.