mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-14 06:15:57 +00:00
f034af422a
Each branch node may have up to 16 sub-items - currently, these are given VertexID based when they are first needed leading to a mostly-random order of vertexid for each subitem. Here, we pre-allocate all 16 vertex ids such that when a branch subitem is filled, it already has a vertexid waiting for it. This brings several important benefits: * subitems are sorted and "close" in their id sequencing - this means that when rocksdb stores them, they are likely to end up in the same data block thus improving read efficiency * because the ids are consequtive, we can store just the starting id and a bitmap representing which subitems are in use - this reduces disk space usage for branches allowing more of them fit into a single disk read, further improving disk read and caching performance - disk usage at block 18M is down from 84 to 78gb! * the in-memory footprint of VertexRef reduced allowing more instances to fit into caches and less memory to be used overall. Because of the increased locality of reference, it turns out that we no longer need to iterate over the entire database to efficiently generate the hash key database because the normal computation is now faster - this significantly benefits "live" chain processing as well where each dirtied key must be accompanied by a read of all branch subitems next to it - most of the performance benefit in this branch comes from this locality-of-reference improvement. On a sample resync, there's already ~20% improvement with later blocks seeing increasing benefit (because the trie is deeper in later blocks leading to more benefit from branch read perf improvements) ``` blocks: 18729664, baseline: 190h43m49s, contender: 153h59m0s Time (total): -36h44m48s, -19.27% ``` Note: clients need to be resynced as the PR changes the on-disk format R.I.P. little bloom filter - your life in the repo was short but valuable
169 lines
5.5 KiB
Nim
169 lines
5.5 KiB
Nim
# nimbus-eth1
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
|
# Licensed under either of
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
# http://opensource.org/licenses/MIT)
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
# except according to those terms.
|
|
|
|
## Read vertex record on the layered Aristo DB delta architecture
|
|
## ==============================================================
|
|
|
|
{.push raises: [].}
|
|
|
|
import
|
|
std/tables,
|
|
results,
|
|
"."/[aristo_desc, aristo_layers]
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Public functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
proc getTuvUbe*(
|
|
db: AristoDbRef;
|
|
): Result[VertexID,AristoError] =
|
|
## Get the ID generator state from the unfiltered backened if available.
|
|
let be = db.backend
|
|
if not be.isNil:
|
|
return be.getTuvFn()
|
|
err(GetTuvNotFound)
|
|
|
|
proc getLstUbe*(
|
|
db: AristoDbRef;
|
|
): Result[SavedState,AristoError] =
|
|
## Get the last saved state
|
|
let be = db.backend
|
|
if not be.isNil:
|
|
return be.getLstFn()
|
|
err(GetLstNotFound)
|
|
|
|
proc getVtxUbe*(
|
|
db: AristoDbRef;
|
|
rvid: RootedVertexID;
|
|
flags: set[GetVtxFlag] = {};
|
|
): Result[VertexRef,AristoError] =
|
|
## Get the vertex from the unfiltered backened if available.
|
|
let be = db.backend
|
|
if not be.isNil:
|
|
return be.getVtxFn(rvid, flags)
|
|
err GetVtxNotFound
|
|
|
|
proc getKeyUbe*(
|
|
db: AristoDbRef;
|
|
rvid: RootedVertexID;
|
|
flags: set[GetVtxFlag];
|
|
): Result[(HashKey, VertexRef),AristoError] =
|
|
## Get the Merkle hash/key from the unfiltered backend if available.
|
|
let be = db.backend
|
|
if not be.isNil:
|
|
return be.getKeyFn(rvid, flags)
|
|
err GetKeyNotFound
|
|
|
|
# ------------------
|
|
|
|
proc getTuvBE*(
|
|
db: AristoDbRef;
|
|
): Result[VertexID,AristoError] =
|
|
## Get the ID generator state the `backened` layer if available.
|
|
if not db.balancer.isNil:
|
|
return ok(db.balancer.vTop)
|
|
db.getTuvUbe()
|
|
|
|
proc getVtxBE*(
|
|
db: AristoDbRef;
|
|
rvid: RootedVertexID;
|
|
flags: set[GetVtxFlag] = {};
|
|
): Result[(VertexRef, int),AristoError] =
|
|
## Get the vertex from the (filtered) backened if available.
|
|
if not db.balancer.isNil:
|
|
db.balancer.sTab.withValue(rvid, w):
|
|
if w[].isValid:
|
|
return ok (w[], -1)
|
|
return err(GetVtxNotFound)
|
|
ok (? db.getVtxUbe(rvid, flags), -2)
|
|
|
|
proc getKeyBE*(
|
|
db: AristoDbRef;
|
|
rvid: RootedVertexID;
|
|
flags: set[GetVtxFlag];
|
|
): Result[((HashKey, VertexRef), int),AristoError] =
|
|
## Get the merkle hash/key from the (filtered) backend if available.
|
|
if not db.balancer.isNil:
|
|
db.balancer.kMap.withValue(rvid, w):
|
|
if w[].isValid:
|
|
return ok(((w[], nil), -1))
|
|
db.balancer.sTab.withValue(rvid, s):
|
|
if s[].isValid:
|
|
return ok(((VOID_HASH_KEY, s[]), -1))
|
|
return err(GetKeyNotFound)
|
|
ok ((?db.getKeyUbe(rvid, flags)), -2)
|
|
|
|
# ------------------
|
|
|
|
proc getVtxRc*(
|
|
db: AristoDbRef;
|
|
rvid: RootedVertexID;
|
|
flags: set[GetVtxFlag] = {};
|
|
): Result[(VertexRef, int),AristoError] =
|
|
## Cascaded attempt to fetch a vertex from the cache layers or the backend.
|
|
##
|
|
block body:
|
|
# If the vertex marked is to be deleted on the backend, a `VertexRef(nil)`
|
|
# entry is kept in the local table in which case it is returned as the
|
|
# error symbol `GetVtxNotFound`.
|
|
let vtx = db.layersGetVtx(rvid).valueOr:
|
|
break body
|
|
if vtx[0].isValid:
|
|
return ok vtx
|
|
else:
|
|
return err(GetVtxNotFound)
|
|
|
|
db.getVtxBE(rvid, flags)
|
|
|
|
proc getVtx*(db: AristoDbRef; rvid: RootedVertexID, flags: set[GetVtxFlag] = {}): VertexRef =
|
|
## Cascaded attempt to fetch a vertex from the cache layers or the backend.
|
|
## The function returns `nil` on error or failure.
|
|
##
|
|
db.getVtxRc(rvid).valueOr((VertexRef(nil), 0))[0]
|
|
|
|
proc getKeyRc*(
|
|
db: AristoDbRef; rvid: RootedVertexID, flags: set[GetVtxFlag]): Result[((HashKey, VertexRef), int),AristoError] =
|
|
## Cascaded attempt to fetch a Merkle hash from the cache layers or the
|
|
## backend. This function will never return a `VOID_HASH_KEY` but rather
|
|
## some `GetKeyNotFound` or `GetKeyUpdateNeeded` error.
|
|
##
|
|
block body:
|
|
let key = db.layersGetKey(rvid).valueOr:
|
|
break body
|
|
# If there is a zero key value, the entry is either marked for being
|
|
# updated or for deletion on the database. So check below.
|
|
if key[0].isValid:
|
|
return ok ((key[0], nil), key[1])
|
|
|
|
# The zero key value does not refer to an update mark if there is no
|
|
# valid vertex (either on the cache or the backend whatever comes first.)
|
|
let vtx = db.layersGetVtx(rvid).valueOr:
|
|
# There was no vertex on the cache. So there must be one the backend (the
|
|
# reason for the key label to exists, at all.)
|
|
return err(GetKeyNotFound)
|
|
if vtx[0].isValid:
|
|
return ok ((VOID_HASH_KEY, vtx[0]), vtx[1])
|
|
else:
|
|
# The vertex is to be deleted. So is the value key.
|
|
return err(GetKeyNotFound)
|
|
|
|
db.getKeyBE(rvid, flags)
|
|
|
|
proc getKey*(db: AristoDbRef; rvid: RootedVertexID): HashKey =
|
|
## Cascaded attempt to fetch a vertex from the cache layers or the backend.
|
|
## The function returns `nil` on error or failure.
|
|
##
|
|
(db.getKeyRc(rvid, {}).valueOr(((VOID_HASH_KEY, nil), 0)))[0][0]
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# End
|
|
# ------------------------------------------------------------------------------
|