Skip to content

Commit b88b94e

Browse files
rjl493456442jorgemmsilva
authored andcommitted
core, triedb/pathdb: calculate the size for batch pre-allocation (ethereum#29106)
* core, triedb/pathdb: calculate the size for batch pre-allocation * triedb/pathdb: address comment
1 parent 80e089f commit b88b94e

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

core/rawdb/schema.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ var (
113113
skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header
114114

115115
// Path-based storage scheme of merkle patricia trie.
116-
trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node
117-
trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node
116+
TrieNodeAccountPrefix = []byte("A") // TrieNodeAccountPrefix + hexPath -> trie node
117+
TrieNodeStoragePrefix = []byte("O") // TrieNodeStoragePrefix + accountHash + hexPath -> trie node
118118
stateIDPrefix = []byte("L") // stateIDPrefix + state root -> state id
119119

120120
PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage
@@ -265,15 +265,15 @@ func stateIDKey(root common.Hash) []byte {
265265
return append(stateIDPrefix, root.Bytes()...)
266266
}
267267

268-
// accountTrieNodeKey = trieNodeAccountPrefix + nodePath.
268+
// accountTrieNodeKey = TrieNodeAccountPrefix + nodePath.
269269
func accountTrieNodeKey(path []byte) []byte {
270-
return append(trieNodeAccountPrefix, path...)
270+
return append(TrieNodeAccountPrefix, path...)
271271
}
272272

273-
// storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath.
273+
// storageTrieNodeKey = TrieNodeStoragePrefix + accountHash + nodePath.
274274
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
275-
buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path))
276-
n := copy(buf, trieNodeStoragePrefix)
275+
buf := make([]byte, len(TrieNodeStoragePrefix)+common.HashLength+len(path))
276+
n := copy(buf, TrieNodeStoragePrefix)
277277
n += copy(buf[n:], accountHash.Bytes())
278278
copy(buf[n:], path)
279279
return buf
@@ -294,16 +294,16 @@ func IsLegacyTrieNode(key []byte, val []byte) bool {
294294
// account trie node in path-based state scheme, and returns the resolved
295295
// node path if so.
296296
func ResolveAccountTrieNodeKey(key []byte) (bool, []byte) {
297-
if !bytes.HasPrefix(key, trieNodeAccountPrefix) {
297+
if !bytes.HasPrefix(key, TrieNodeAccountPrefix) {
298298
return false, nil
299299
}
300300
// The remaining key should only consist a hex node path
301301
// whose length is in the range 0 to 64 (64 is excluded
302302
// since leaves are always wrapped with shortNode).
303-
if len(key) >= len(trieNodeAccountPrefix)+common.HashLength*2 {
303+
if len(key) >= len(TrieNodeAccountPrefix)+common.HashLength*2 {
304304
return false, nil
305305
}
306-
return true, key[len(trieNodeAccountPrefix):]
306+
return true, key[len(TrieNodeAccountPrefix):]
307307
}
308308

309309
// IsAccountTrieNode reports whether a provided database entry is an account
@@ -317,20 +317,20 @@ func IsAccountTrieNode(key []byte) bool {
317317
// trie node in path-based state scheme, and returns the resolved account hash
318318
// and node path if so.
319319
func ResolveStorageTrieNode(key []byte) (bool, common.Hash, []byte) {
320-
if !bytes.HasPrefix(key, trieNodeStoragePrefix) {
320+
if !bytes.HasPrefix(key, TrieNodeStoragePrefix) {
321321
return false, common.Hash{}, nil
322322
}
323323
// The remaining key consists of 2 parts:
324324
// - 32 bytes account hash
325325
// - hex node path whose length is in the range 0 to 64
326-
if len(key) < len(trieNodeStoragePrefix)+common.HashLength {
326+
if len(key) < len(TrieNodeStoragePrefix)+common.HashLength {
327327
return false, common.Hash{}, nil
328328
}
329-
if len(key) >= len(trieNodeStoragePrefix)+common.HashLength+common.HashLength*2 {
329+
if len(key) >= len(TrieNodeStoragePrefix)+common.HashLength+common.HashLength*2 {
330330
return false, common.Hash{}, nil
331331
}
332-
accountHash := common.BytesToHash(key[len(trieNodeStoragePrefix) : len(trieNodeStoragePrefix)+common.HashLength])
333-
return true, accountHash, key[len(trieNodeStoragePrefix)+common.HashLength:]
332+
accountHash := common.BytesToHash(key[len(TrieNodeStoragePrefix) : len(TrieNodeStoragePrefix)+common.HashLength])
333+
return true, accountHash, key[len(TrieNodeStoragePrefix)+common.HashLength:]
334334
}
335335

336336
// IsStorageTrieNode reports whether a provided database entry is a storage

triedb/pathdb/nodebuffer.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ func (b *nodebuffer) setSize(size int, db ethdb.KeyValueStore, clean *fastcache.
204204
return b.flush(db, clean, id, false)
205205
}
206206

207+
// allocBatch returns a database batch with pre-allocated buffer.
208+
func (b *nodebuffer) allocBatch(db ethdb.KeyValueStore) ethdb.Batch {
209+
var metasize int
210+
for owner, nodes := range b.nodes {
211+
if owner == (common.Hash{}) {
212+
metasize += len(nodes) * len(rawdb.TrieNodeAccountPrefix) // database key prefix
213+
} else {
214+
metasize += len(nodes) * (len(rawdb.TrieNodeStoragePrefix) + common.HashLength) // database key prefix + owner
215+
}
216+
}
217+
return db.NewBatchWithSize((metasize + int(b.size)) * 11 / 10) // extra 10% for potential pebble internal stuff
218+
}
219+
207220
// flush persists the in-memory dirty trie node into the disk if the configured
208221
// memory threshold is reached. Note, all data must be written atomically.
209222
func (b *nodebuffer) flush(db ethdb.KeyValueStore, clean *fastcache.Cache, id uint64, force bool) error {
@@ -217,7 +230,7 @@ func (b *nodebuffer) flush(db ethdb.KeyValueStore, clean *fastcache.Cache, id ui
217230
}
218231
var (
219232
start = time.Now()
220-
batch = db.NewBatchWithSize(int(b.size))
233+
batch = b.allocBatch(db)
221234
)
222235
nodes := writeNodes(batch, b.nodes, clean)
223236
rawdb.WritePersistentStateID(batch, id)

0 commit comments

Comments
 (0)