Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize memory buffer, simplify set32, use sha256-simd #7060

Merged
merged 6 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +1,7 @@
package state

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

lru "github.com/hashicorp/golang-lru"
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...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mabye good. But I need run some experiment with Grafana to see an impact.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look!
For the Celeron N5100 I'm left with github.com/ledgerwatch/erigon/crypto/bn256/cloudflare.gfpMul dominating 47% of the CPU profiling while before I had memory.go related calls. However I must admit that the SHA/SIMD change made a larger difference than the memory pool. This machine is at 100% CPU and no iowait as it has a good nvme on a slow CPU.

Another machine syncing Ethereum mainnet on Intel 11800h using mechanical HDD with nvme lvm cache just had a minor CPU usage reduction as it is severely limited by the slow HDD IO.

If desired, I can split the changes into smaller PRs.

return
}

// Depth returns the current call stack depth.
Expand Down
12 changes: 9 additions & 3 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,8 +58,7 @@ 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
copy(m.store[offset:offset+32], zeroes)
val.WriteToSlice(m.store[offset:])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not offset:offset+32?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, thanks. WriteToSlice will write 32 bytes but I agree that specifying explicitly reads better, it was just the way I found it.

}

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,7 +33,7 @@ import (
"bytes"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"github.com/minio/sha256-simd"
"encoding/hex"
"flag"
"fmt"
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,7 +37,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/sha256"
"github.com/minio/sha256-simd"
"crypto/sha512"
"fmt"
"hash"
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,7 +22,7 @@ import (
"crypto/cipher"
"crypto/ecdsa"
crand "crypto/rand"
"crypto/sha256"
"github.com/minio/sha256-simd"
"encoding/binary"
"errors"
"fmt"
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,7 +19,7 @@ package p2p
import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"github.com/minio/sha256-simd"
"errors"
"io"
"math/rand"
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