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](0897edb001/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.
This commit is contained in:
Victor Shyba 2023-03-14 04:17:04 -03:00 committed by GitHub
parent efd541028c
commit 158fb2b606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 32 additions and 16 deletions

View File

@ -14,7 +14,7 @@
package utils
import (
"crypto/sha256"
"github.com/minio/sha256-simd"
"hash"
"sync"
)

View File

@ -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"

View File

@ -17,9 +17,9 @@
package vm
import (
"crypto/sha256"
"encoding/binary"
"errors"
"github.com/minio/sha256-simd"
"math/big"
"github.com/holiman/uint256"

View File

@ -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.

View File

@ -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
@ -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()
@ -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 {

View File

@ -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"

View File

@ -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"

View File

@ -20,7 +20,7 @@ import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"github.com/minio/sha256-simd"
"reflect"
"strings"
"testing"

View File

@ -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"

View File

@ -19,8 +19,8 @@ package p2p
import (
"context"
"crypto/ecdsa"
"crypto/sha256"
"errors"
"github.com/minio/sha256-simd"
"io"
"math/rand"
"net"

View File

@ -1,7 +1,7 @@
package vtree
import (
"crypto/sha256"
"github.com/minio/sha256-simd"
"math/big"
"math/rand"
"testing"