2023-05-30 11:47:47 +00:00
|
|
|
# nimbus-eth1
|
Core db update storage root management for sub tries (#1964)
* Aristo: Re-phrase `LayerDelta` and `LayerFinal` as object references
why:
Avoids copying in some cases
* Fix copyright header
* Aristo: Verify `leafTie.root` function argument for `merge()` proc
why:
Zero root will lead to inconsistent DB entry
* Aristo: Update failure condition for hash labels compiler `hashify()`
why:
Node need not be rejected as long as links are on the schedule. In
that case, `redo[]` is to become `wff.base[]` at a later stage.
This amends an earlier fix, part of #1952 by also testing against
the target nodes of the `wff.base[]` sets.
* Aristo: Add storage root glue record to `hashify()` schedule
why:
An account leaf node might refer to a non-resolvable storage root ID.
Storage root node chains will end up at the storage root. So the link
`storage-root->account-leaf` needs an extra item in the schedule.
* Aristo: fix error code returned by `fetchPayload()`
details:
Final error code is implied by the error code form the `hikeUp()`
function.
* CoreDb: Discard `createOk` argument in API `getRoot()` function
why:
Not needed for the legacy DB. For the `Arsto` DB, a lazy approach is
implemented where a stprage root node is created on-the-fly.
* CoreDb: Prevent `$$` logging in some cases
why:
Logging the function `$$` is not useful when it is used for internal
use, i.e. retrieving an an error text for logging.
* CoreDb: Add `tryHashFn()` to API for pretty printing
why:
Pretty printing must not change the hashification status for the
`Aristo` DB. So there is an independent API wrapper for getting the
node hash which never updated the hashes.
* CoreDb: Discard `update` argument in API `hash()` function
why:
When calling the API function `hash()`, the latest state is always
wanted. For a version that uses the current state as-is without checking,
the function `tryHash()` was added to the backend.
* CoreDb: Update opaque vertex ID objects for the `Aristo` backend
why:
For `Aristo`, vID objects encapsulate a numeric `VertexID`
referencing a vertex (rather than a node hash as used on the
legacy backend.) For storage sub-tries, there might be no initial
vertex known when the descriptor is created. So opaque vertex ID
objects are supported without a valid `VertexID` which will be
initalised on-the-fly when the first item is merged.
* CoreDb: Add pretty printer for opaque vertex ID objects
* Cosmetics, printing profiling data
* CoreDb: Fix segfault in `Aristo` backend when creating MPT descriptor
why:
Missing initialisation error
* CoreDb: Allow MPT to inherit shared context on `Aristo` backend
why:
Creates descriptors with different storage roots for the same
shared `Aristo` DB descriptor.
* Cosmetics, update diagnostic message items for `Aristo` backend
* Fix Copyright year
2024-01-11 19:11:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-30 11:47:47 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-05-30 21:21:15 +00:00
|
|
|
## Aristo DB -- Patricia Trie builder, raw node insertion
|
|
|
|
## ======================================================
|
|
|
|
##
|
2023-10-27 21:36:51 +00:00
|
|
|
## This module merges `PathID` values as hexary lookup paths into the
|
2023-05-30 21:21:15 +00:00
|
|
|
## `Patricia Trie`. When changing vertices (aka nodes without Merkle hashes),
|
|
|
|
## associated (but separated) Merkle hashes will be deleted unless locked.
|
|
|
|
## Instead of deleting locked hashes error handling is applied.
|
|
|
|
##
|
|
|
|
## Also, nodes (vertices plus merkle hashes) can be added which is needed for
|
|
|
|
## boundary proofing after `snap/1` download. The vertices are split from the
|
|
|
|
## nodes and stored as-is on the table holding `Patricia Trie` entries. The
|
|
|
|
## hashes are stored iin a separate table and the vertices are labelled
|
|
|
|
## `locked`.
|
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-06-18 11:14:02 +00:00
|
|
|
std/typetraits,
|
|
|
|
eth/common,
|
2023-09-12 18:45:12 +00:00
|
|
|
results,
|
2024-09-19 08:39:06 +00:00
|
|
|
"."/[aristo_desc, aristo_fetch, aristo_get, aristo_layers, aristo_vid]
|
2023-05-30 11:47:47 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
proc layersPutLeaf(
|
|
|
|
db: AristoDbRef, rvid: RootedVertexID, path: NibblesBuf, payload: LeafPayload
|
|
|
|
): VertexRef =
|
|
|
|
let vtx = VertexRef(vType: Leaf, pfx: path, lData: payload)
|
|
|
|
db.layersPutVtx(rvid, vtx)
|
|
|
|
vtx
|
|
|
|
|
|
|
|
proc mergePayloadImpl(
|
|
|
|
db: AristoDbRef, # Database, top layer
|
|
|
|
root: VertexID, # MPT state root
|
|
|
|
path: openArray[byte], # Leaf item to add to the database
|
|
|
|
leaf: Opt[VertexRef],
|
|
|
|
payload: LeafPayload, # Payload value
|
|
|
|
): Result[(VertexRef, VertexRef, VertexRef), AristoError] =
|
|
|
|
## Merge the argument `(root,path)` key-value-pair into the top level vertex
|
|
|
|
## table of the database `db`. The `path` argument is used to address the
|
|
|
|
## leaf vertex with the payload. It is stored or updated on the database
|
|
|
|
## accordingly.
|
|
|
|
##
|
|
|
|
var
|
|
|
|
path = NibblesBuf.fromBytes(path)
|
|
|
|
cur = root
|
|
|
|
(vtx, _) = db.getVtxRc((root, cur)).valueOr:
|
|
|
|
if error != GetVtxNotFound:
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
# We're at the root vertex and there is no data - this must be a fresh
|
|
|
|
# VertexID!
|
|
|
|
return ok (db.layersPutLeaf((root, cur), path, payload), nil, nil)
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
vids: ArrayBuf[NibblesBuf.high + 1, VertexID]
|
|
|
|
vtxs: ArrayBuf[NibblesBuf.high + 1, VertexRef]
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
template resetKeys() =
|
|
|
|
# Reset cached hashes of touched verticies
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
for i in 2..vids.len:
|
|
|
|
db.layersResKey((root, vids[^i]), vtxs[^i])
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
while path.len > 0:
|
|
|
|
# Clear existing merkle keys along the traversal path
|
Store keys together with node data (#2849)
Currently, computed hash keys are stored in a separate column family
with respect to the MPT data they're generated from - this has several
disadvantages:
* A lot of space is wasted because the lookup key (`RootedVertexID`) is
repeated in both tables - this is 30% of the `AriKey` content!
* rocksdb must maintain in-memory bloom filters and LRU caches for said
keys, doubling its "minimal efficient cache size"
* An extra disk traversal must be made to check for existence of cached
hash key
* Doubles the amount of files on disk due to each column family being
its own set of files
Here, the two CFs are joined such that both key and data is stored in
`AriVtx`. This means:
* we save ~30% disk space on repeated lookup keys
* we save ~2gb of memory overhead that can be used to cache data instead
of indices
* we can skip storing hash keys for MPT leaf nodes - these are trivial
to compute and waste a lot of space - previously they had to present in
the `AriKey` CF to avoid having to look in two tables on the happy path.
* There is a small increase in write amplification because when a hash
value is updated for a branch node, we must write both key and branch
data - previously we would write only the key
* There's a small shift in CPU usage - instead of performing lookups in
the database, hashes for leaf nodes are (re)-computed on the fly
* We can return to slightly smaller on-disk SST files since there's
fewer of them, which should reduce disk traffic a bit
Internally, there are also other advantages:
* when clearing keys, we no longer have to store a zero hash in memory -
instead, we deduce staleness of the cached key from the presence of an
updated VertexRef - this saves ~1gb of mem overhead during import
* hash key cache becomes dedicated to branch keys since leaf keys are no
longer stored in memory, reducing churn
* key computation is a lot faster thanks to the skipped second disk
traversal - a key computation for mainnet can be completed in 11 hours
instead of ~2 days (!) thanks to better cache usage and less read
amplification - with additional improvements to the on-disk format, we
can probably get rid of the initial full traversal method of seeding the
key cache on first start after import
All in all, this PR reduces the size of a mainnet database from 160gb to
110gb and the peak memory footprint during import by ~1-2gb.
2024-11-20 08:56:27 +00:00
|
|
|
vids.add cur
|
|
|
|
vtxs.add vtx
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
let n = path.sharedPrefixLen(vtx.pfx)
|
|
|
|
case vtx.vType
|
|
|
|
of Leaf:
|
|
|
|
let res =
|
|
|
|
if n == vtx.pfx.len:
|
|
|
|
# Same path - replace the current vertex with a new payload
|
|
|
|
|
|
|
|
if vtx.lData == payload:
|
|
|
|
return err(MergeNoAction)
|
|
|
|
|
|
|
|
let leafVtx = if root == VertexID(1):
|
|
|
|
var payload = payload.dup()
|
|
|
|
# TODO can we avoid this hack? it feels like the caller should already
|
|
|
|
# have set an appropriate stoID - this "fixup" feels risky,
|
|
|
|
# specially from a caching point of view
|
|
|
|
payload.stoID = vtx.lData.stoID
|
|
|
|
db.layersPutLeaf((root, cur), path, payload)
|
|
|
|
else:
|
|
|
|
db.layersPutLeaf((root, cur), path, payload)
|
|
|
|
(leafVtx, nil, nil)
|
|
|
|
else:
|
|
|
|
# Turn leaf into a branch (or extension) then insert the two leaves
|
|
|
|
# into the branch
|
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 10:42:04 +00:00
|
|
|
let branch = VertexRef(vType: Branch, pfx: path.slice(0, n), startVid: db.vidFetch(16))
|
2024-09-19 08:39:06 +00:00
|
|
|
let other = block: # Copy of existing leaf node, now one level deeper
|
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 10:42:04 +00:00
|
|
|
let local = branch.setUsed(vtx.pfx[n], true)
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersPutLeaf((root, local), vtx.pfx.slice(n + 1), vtx.lData)
|
|
|
|
|
|
|
|
let leafVtx = block: # Newly inserted leaf node
|
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 10:42:04 +00:00
|
|
|
let local = branch.setUsed(path[n], true)
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersPutLeaf((root, local), path.slice(n + 1), payload)
|
|
|
|
|
|
|
|
# Put the branch at the vid where the leaf was
|
|
|
|
db.layersPutVtx((root, cur), branch)
|
|
|
|
|
|
|
|
# We need to return vtx here because its pfx member hasn't yet been
|
|
|
|
# sliced off and is therefore shared with the hike
|
|
|
|
(leafVtx, vtx, other)
|
|
|
|
|
|
|
|
resetKeys()
|
|
|
|
return ok(res)
|
|
|
|
of Branch:
|
|
|
|
if vtx.pfx.len == n:
|
|
|
|
# The existing branch is a prefix of the new entry
|
|
|
|
let
|
|
|
|
nibble = path[vtx.pfx.len]
|
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 10:42:04 +00:00
|
|
|
next = vtx.bVid(nibble)
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
if next.isValid:
|
|
|
|
cur = next
|
|
|
|
path = path.slice(n + 1)
|
|
|
|
vtx =
|
|
|
|
if leaf.isSome and leaf[].isValid and leaf[].pfx == path:
|
|
|
|
leaf[]
|
|
|
|
else:
|
|
|
|
(?db.getVtxRc((root, next)))[0]
|
|
|
|
|
|
|
|
else:
|
|
|
|
# There's no vertex at the branch point - insert the payload as a new
|
|
|
|
# leaf and update the existing branch
|
|
|
|
|
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 10:42:04 +00:00
|
|
|
let brDup = vtx.dup()
|
|
|
|
let local = brDup.setUsed(nibble, true)
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersPutVtx((root, cur), brDup)
|
|
|
|
|
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 10:42:04 +00:00
|
|
|
let
|
|
|
|
leafVtx = db.layersPutLeaf((root, local), path.slice(n + 1), payload)
|
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
resetKeys()
|
|
|
|
return ok((leafVtx, nil, nil))
|
|
|
|
else:
|
|
|
|
# Partial path match - we need to split the existing branch at
|
|
|
|
# the point of divergence, inserting a new branch
|
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 10:42:04 +00:00
|
|
|
let branch = VertexRef(vType: Branch, pfx: path.slice(0, n), startVid: db.vidFetch(16))
|
2024-09-19 08:39:06 +00:00
|
|
|
block: # Copy the existing vertex and add it to the new branch
|
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 10:42:04 +00:00
|
|
|
let local = branch.setUsed(vtx.pfx[n], true)
|
2024-09-19 08:39:06 +00:00
|
|
|
|
|
|
|
db.layersPutVtx(
|
|
|
|
(root, local),
|
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 10:42:04 +00:00
|
|
|
VertexRef(vType: Branch, pfx: vtx.pfx.slice(n + 1), startVid: vtx.startVid, used: vtx.used),
|
2024-09-19 08:39:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
let leafVtx = block: # add the new entry
|
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 10:42:04 +00:00
|
|
|
let local = branch.setUsed(path[n], true)
|
2024-09-19 08:39:06 +00:00
|
|
|
db.layersPutLeaf((root, local), path.slice(n + 1), payload)
|
|
|
|
|
|
|
|
db.layersPutVtx((root, cur), branch)
|
|
|
|
|
|
|
|
resetKeys()
|
|
|
|
return ok((leafVtx, nil, nil))
|
|
|
|
|
|
|
|
err(MergeHikeFailed)
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
proc mergeAccountRecord*(
|
2023-07-04 18:24:03 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2024-10-01 21:03:10 +00:00
|
|
|
accPath: Hash32; # Even nibbled byte path
|
2024-06-27 09:01:26 +00:00
|
|
|
accRec: AristoAccount; # Account data
|
2024-06-18 11:14:02 +00:00
|
|
|
): Result[bool,AristoError] =
|
2024-07-12 13:12:25 +00:00
|
|
|
## Merge the key-value-pair argument `(accKey,accRec)` as an account
|
2024-06-18 11:14:02 +00:00
|
|
|
## ledger value, i.e. the the sub-tree starting at `VertexID(1)`.
|
2024-02-01 21:27:48 +00:00
|
|
|
##
|
2024-07-12 13:12:25 +00:00
|
|
|
## On success, the function returns `true` if the `accRec` argument was
|
|
|
|
## not on the database already or different from `accRec`, and `false`
|
|
|
|
## otherwise.
|
2024-06-18 19:30:01 +00:00
|
|
|
##
|
2024-06-18 11:14:02 +00:00
|
|
|
let
|
2024-07-14 10:02:05 +00:00
|
|
|
pyl = LeafPayload(pType: AccountData, account: accRec)
|
2024-09-19 08:39:06 +00:00
|
|
|
updated = db.mergePayloadImpl(
|
|
|
|
VertexID(1), accPath.data, db.cachedAccLeaf(accPath), pyl).valueOr:
|
|
|
|
if error == MergeNoAction:
|
|
|
|
return ok false
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
# Update leaf cache both of the merged value and potentially the displaced
|
|
|
|
# leaf resulting from splitting a leaf into a branch with two leaves
|
|
|
|
db.layersPutAccLeaf(accPath, updated[0])
|
|
|
|
if updated[1].isValid:
|
2024-09-29 12:37:09 +00:00
|
|
|
let otherPath = Hash32(getBytes(
|
2024-09-19 08:39:06 +00:00
|
|
|
NibblesBuf.fromBytes(accPath.data).replaceSuffix(updated[1].pfx)))
|
|
|
|
db.layersPutAccLeaf(otherPath, updated[2])
|
2024-02-01 21:27:48 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
ok true
|
2023-09-15 15:23:53 +00:00
|
|
|
|
2024-06-18 11:14:02 +00:00
|
|
|
proc mergeStorageData*(
|
2024-02-01 21:27:48 +00:00
|
|
|
db: AristoDbRef; # Database, top layer
|
2024-10-01 21:03:10 +00:00
|
|
|
accPath: Hash32; # Needed for accounts payload
|
|
|
|
stoPath: Hash32; # Storage data path (aka key)
|
2024-08-14 08:54:44 +00:00
|
|
|
stoData: UInt256; # Storage data payload value
|
2024-06-27 19:21:01 +00:00
|
|
|
): Result[void,AristoError] =
|
|
|
|
## Store the `stoData` data argument on the storage area addressed by
|
|
|
|
## `(accPath,stoPath)` where `accPath` is the account key (into the MPT)
|
|
|
|
## and `stoPath` is the slot path of the corresponding storage area.
|
2024-06-18 11:14:02 +00:00
|
|
|
##
|
2024-09-19 08:39:06 +00:00
|
|
|
var accHike: Hike
|
|
|
|
db.fetchAccountHike(accPath,accHike).isOkOr:
|
|
|
|
return err(MergeStoAccMissing)
|
2024-06-18 11:14:02 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
let
|
|
|
|
stoID = accHike.legs[^1].wp.vtx.lData.stoID
|
2024-07-11 11:26:46 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
# Provide new storage ID when needed
|
|
|
|
useID =
|
|
|
|
if stoID.isValid: stoID # Use as is
|
|
|
|
elif stoID.vid.isValid: (true, stoID.vid) # Re-use previous vid
|
|
|
|
else: (true, db.vidFetch()) # Create new vid
|
|
|
|
mixPath = mixUp(accPath, stoPath)
|
|
|
|
# Call merge
|
|
|
|
pyl = LeafPayload(pType: StoData, stoData: stoData)
|
|
|
|
updated = db.mergePayloadImpl(
|
|
|
|
useID.vid, stoPath.data, db.cachedStoLeaf(mixPath), pyl).valueOr:
|
|
|
|
if error == MergeNoAction:
|
|
|
|
assert stoID.isValid # debugging only
|
|
|
|
return ok()
|
2024-07-11 11:26:46 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
return err(error)
|
2024-07-14 17:12:10 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
# Mark account path Merkle keys for update
|
|
|
|
db.layersResKeys(accHike)
|
2024-07-12 13:08:26 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
# Update leaf cache both of the merged value and potentially the displaced
|
|
|
|
# leaf resulting from splitting a leaf into a branch with two leaves
|
|
|
|
db.layersPutStoLeaf(mixPath, updated[0])
|
2024-07-11 11:26:46 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
if updated[1].isValid:
|
2024-09-29 12:37:09 +00:00
|
|
|
let otherPath = Hash32(getBytes(
|
2024-09-19 08:39:06 +00:00
|
|
|
NibblesBuf.fromBytes(stoPath.data).replaceSuffix(updated[1].pfx)))
|
|
|
|
db.layersPutStoLeaf(mixUp(accPath, otherPath), updated[2])
|
2024-07-11 11:26:46 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
if not stoID.isValid:
|
|
|
|
# Make sure that there is an account that refers to that storage trie
|
|
|
|
let leaf = accHike.legs[^1].wp.vtx.dup # Dup on modify
|
|
|
|
leaf.lData.stoID = useID
|
|
|
|
db.layersPutAccLeaf(accPath, leaf)
|
|
|
|
db.layersPutVtx((VertexID(1), accHike.legs[^1].wp.vid), leaf)
|
2024-06-18 11:14:02 +00:00
|
|
|
|
2024-09-19 08:39:06 +00:00
|
|
|
ok()
|
2023-06-09 11:17:37 +00:00
|
|
|
|
2023-05-30 11:47:47 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|