Jacek Sieka f034af422a
Pre-allocate vids for branches (#2882)
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
2024-12-04 11:42:04 +01:00

175 lines
5.0 KiB
Nim

# nimbus-eth1
# Copyright (c) 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.
{.push raises: [].}
import
std/sets,
eth/common,
results,
".."/[aristo_desc, aristo_get, aristo_hike, aristo_layers, aristo_utils],
#./part_debug,
./part_desc
# ------------------------------------------------------------------------------
# Private functions
# ------------------------------------------------------------------------------
proc newCtx(ps: PartStateRef; hike: Hike): Result[PartStateCtx,AristoError] =
## ..
doAssert 0 <= hike.legs[^1].nibble
let
wp = hike.legs[^1].wp
nibble = hike.legs[^1].nibble
fromVid = wp.vtx.bVid(uint8 nibble)
if not ps.isPerimeter(fromVid) or ps.isExtension(fromVid):
return err(PartCtxNotAvailable)
let
vtx2 = wp.vtx.dup
psc = PartStateCtx(
ps: ps,
location: (hike.root,wp.vid),
nibble: nibble,
fromVid: fromVid)
# Update database so that is space for adding a new sub-tree here
discard vtx2.setUsed(uint8 nibble, false)
ps.db.layersPutVtx(psc.location,vtx2)
ok psc
proc removedCompletedNode(
ps: PartStateRef;
rvid: RootedVertexID;
key: HashKey;
): bool =
let vtx = ps.db.getVtx rvid
if vtx.isNil:
return false
var subVids: seq[VertexID]
for vid in vtx.subVids():
# Only accept perimeter nodes with all links on the database (i.e. links
# must nor refere t a core node.)
if not ps.db.getVtx((rvid.root, vid)).isValid or ps.isCore(vid):
return false # not complete
subVids.add vid
# No need to keep that core vertex any longer
ps.delCore(rvid.root, key)
for vid in subVids:
ps.del vid
true
proc removeCompletedNodes(ps: PartStateRef; rvid: RootedVertexID) =
let key = ps[rvid.vid]
if ps.removedCompletedNode(rvid, key):
# Rather stupid loop to clear additional nodes. Note that the set
# of core nodes is assumed to be small, i.e. not more than a hand full.
while true:
ps.core.withValue(rvid.root, coreKeys):
block removeItem:
for cKey in coreKeys[]:
let rv = ps[cKey]
if ps.removedCompletedNode(rv, cKey):
break removeItem # continue `while`
return # done
do: return # done
# -------------------
proc ctxAcceptChange(psc: PartStateCtx): Result[bool,AristoError] =
## Apply `psc` context if there was a change on the targeted vertex,
## otherwise restore. Returns `true` exactly if there was a change on
## the database which could be applied.
##
let
ps = psc.ps
db = ps.db
(vtx,_) = ? db.getVtxRc psc.location
toVid = vtx.bVid(uint8 psc.nibble)
if not toVid.isValid:
# Nothing changed, so restore
let vtx2 = vtx.dup
doAssert false, "TODO vtx2.bVid[psc.nibble] = psc.fromVid"
db.layersPutVtx(psc.location, vtx2)
ok(false)
elif toVid != psc.fromVid:
# Replace `fromVid` by `toVid` in state descriptor `ps`
let key = ps.move(psc.fromVid, toVid)
doAssert key.isValid
ps.changed.incl key
# Remove parent vertex it it has become complete.
ps.removeCompletedNodes(psc.location)
ok(true)
else:
ok(false)
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
proc ctxMergeBegin*(
ps: PartStateRef;
root: VertexID;
path: openArray[byte];
): Result[PartStateCtx,AristoError] =
## This function clears the way for mering some payload at the argument
## path `(root,path)`.
var hike: Hike
path.hikeUp(root,ps.db, Opt.none(VertexRef), hike).isOkOr:
if error[1] != HikeDanglingEdge:
return err error[1] # Cannot help here
return ps.newCtx hike
ok PartStateCtx(nil) # Nothing to do
proc ctxMergeBegin*(
ps: PartStateRef;
accPath: Hash32;
): Result[PartStateCtx,AristoError] =
## Variant of `partMergeBegin()` for different path representation
ps.ctxMergeBegin(VertexID(1), accPath.data)
proc ctxMergeCommit*(psc: PartStateCtx): Result[bool,AristoError] =
##
if psc.isNil:
return ok(false) # Nothing to do
if psc.ps.isNil:
return err(PartCtxStaleDescriptor)
let yn = ? psc.ctxAcceptChange()
psc[].reset
ok(yn)
proc ctxMergeRollback*(psc: PartStateCtx): Result[void,AristoError] =
## ..
if psc.isNil:
return ok() # Nothing to do
if psc.ps.isNil:
return err(PartCtxStaleDescriptor)
let yn = ? psc.ctxAcceptChange()
psc[].reset
if yn: err(PartVtxSlotWasModified) else: ok()
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------