Skip to content

Commit 1208845

Browse files
authored
Merge pull request #2390 from CortexFoundation/dev
reuse global hash buffer
2 parents 3b889ab + 2297b53 commit 1208845

4 files changed

Lines changed: 55 additions & 18 deletions

File tree

core/rawdb/database.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ func SafeDeleteRange(db ctxcdb.KeyValueStore, start, end []byte, hashScheme bool
607607

608608
var (
609609
count, deleted, skipped int
610-
buff = crypto.NewKeccakState()
611610
startTime = time.Now()
612611
)
613612

@@ -620,7 +619,7 @@ func SafeDeleteRange(db ctxcdb.KeyValueStore, start, end []byte, hashScheme bool
620619

621620
for it.Next() && bytes.Compare(end, it.Key()) > 0 {
622621
// Prevent deletion for trie nodes in hash mode
623-
if len(it.Key()) != 32 || crypto.HashData(buff, it.Value()) != common.BytesToHash(it.Key()) {
622+
if len(it.Key()) != 32 || crypto.Keccak256Hash(it.Value()) != common.BytesToHash(it.Key()) {
624623
if err := batch.Delete(it.Key()); err != nil {
625624
return err
626625
}

core/state/state_object.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"io"
2323
"maps"
2424
"math/big"
25-
"sync"
25+
//"sync"
2626
"time"
2727

2828
"github.com/CortexFoundation/CortexTheseus/common"
@@ -35,11 +35,11 @@ import (
3535

3636
// hasherPool holds a pool of hashers used by state objects during concurrent
3737
// trie updates.
38-
var hasherPool = sync.Pool{
39-
New: func() interface{} {
40-
return crypto.NewKeccakState()
41-
},
42-
}
38+
//var hasherPool = sync.Pool{
39+
// New: func() interface{} {
40+
// return crypto.NewKeccakState()
41+
// },
42+
//}
4343

4444
type Storage map[common.Hash]common.Hash
4545

@@ -344,8 +344,8 @@ func (s *stateObject) updateTrie() (Trie, error) {
344344
)
345345
// Insert all the pending storage updates into the trie
346346

347-
hasher := hasherPool.Get().(crypto.KeccakState)
348-
defer hasherPool.Put(hasher)
347+
//hasher := hasherPool.Get().(crypto.KeccakState)
348+
//defer hasherPool.Put(hasher)
349349

350350
// Perform trie updates before deletions. This prevents resolution of unnecessary trie nodes
351351
// in circumstances similar to the following:
@@ -390,7 +390,8 @@ func (s *stateObject) updateTrie() (Trie, error) {
390390
}
391391
s.db.storagesLock.Unlock()
392392
}
393-
khash := crypto.HashData(hasher, key[:])
393+
//khash := crypto.HashData(hasher, key[:])
394+
khash := crypto.Keccak256Hash(key[:])
394395
storage[khash] = v // encoded will be nil if it's deleted
395396

396397
// Cache the original value of mutated storage slots

core/state/statedb.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ type StateDB struct {
8282
db Database
8383
prefetcher *triePrefetcher
8484
trie Trie
85-
hasher crypto.KeccakState
86-
snap snapshot.Snapshot // Nil if snapshot is not available
85+
//hasher crypto.KeccakState
86+
snap snapshot.Snapshot // Nil if snapshot is not available
8787

8888
// originalRoot is the pre-state root, before any changes were made.
8989
// It will be updated when the Commit is called.
@@ -186,7 +186,7 @@ func New(root common.Hash, db Database) (*StateDB, error) {
186186
journal: newJournal(),
187187
accessList: newAccessList(),
188188
transientStorage: newTransientStorage(),
189-
hasher: crypto.NewKeccakState(),
189+
//hasher: crypto.NewKeccakState(),
190190
}
191191
if snaps := sdb.db.Snapshot(); snaps != nil {
192192
sdb.snap = snaps.Snapshot(root)
@@ -645,7 +645,8 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
645645
var data *types.StateAccount
646646
if s.snap != nil {
647647
start := time.Now()
648-
acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes()))
648+
addrHash := crypto.Keccak256Hash(addr.Bytes())
649+
acc, err := s.snap.Account(addrHash)
649650
s.SnapshotAccountReads += time.Since(start)
650651
if err == nil {
651652
if acc == nil {
@@ -781,9 +782,9 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
781782
func (s *StateDB) Copy() *StateDB {
782783
// Copy all the basic fields, initialize the memory ones
783784
state := &StateDB{
784-
db: s.db,
785-
trie: s.db.CopyTrie(s.trie),
786-
hasher: crypto.NewKeccakState(),
785+
db: s.db,
786+
trie: s.db.CopyTrie(s.trie),
787+
//hasher: crypto.NewKeccakState(),
787788
originalRoot: s.originalRoot,
788789
accounts: copySet(s.accounts),
789790
storages: copy2DSet(s.storages),

crypto/crypto_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package crypto
1919
import (
2020
"bytes"
2121
"crypto/ecdsa"
22+
"crypto/rand"
2223
"encoding/hex"
2324
"math/big"
2425
"os"
@@ -254,3 +255,38 @@ func TestPythonIntegration(t *testing.T) {
254255
t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0)
255256
t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1)
256257
}
258+
259+
// goos: darwin
260+
// goarch: arm64
261+
// pkg: github.com/ethereum/go-ethereum/crypto
262+
// cpu: Apple M1 Pro
263+
// BenchmarkKeccak256Hash
264+
// BenchmarkKeccak256Hash-8 931095 1270 ns/op 32 B/op 1 allocs/op
265+
func BenchmarkKeccak256Hash(b *testing.B) {
266+
var input [512]byte
267+
rand.Read(input[:])
268+
269+
b.ReportAllocs()
270+
for i := 0; i < b.N; i++ {
271+
Keccak256Hash(input[:])
272+
}
273+
}
274+
275+
// goos: darwin
276+
// goarch: arm64
277+
// pkg: github.com/ethereum/go-ethereum/crypto
278+
// cpu: Apple M1 Pro
279+
// BenchmarkHashData
280+
// BenchmarkHashData-8 793386 1278 ns/op 32 B/op 1 allocs/op
281+
func BenchmarkHashData(b *testing.B) {
282+
var (
283+
input [512]byte
284+
buffer = NewKeccakState()
285+
)
286+
rand.Read(input[:])
287+
288+
b.ReportAllocs()
289+
for i := 0; i < b.N; i++ {
290+
HashData(buffer, input[:])
291+
}
292+
}

0 commit comments

Comments
 (0)