Skip to content

Commit

Permalink
Revert lazy implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mjfh committed Sep 2, 2024
1 parent 84a72c8 commit 5cbc039
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 101 deletions.
94 changes: 35 additions & 59 deletions nimbus/db/aristo/aristo_delete/delete_subtree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,41 @@

import
eth/common,
results,
".."/[aristo_desc, aristo_get, aristo_layers],
./delete_helpers

# ------------------------------------------------------------------------------
# Private heplers
# ------------------------------------------------------------------------------

proc collectStoTreeLazily(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
accPath: Hash256; # Accounts cache designator
stoPath: NibblesBuf; # Current storage path
proc delSubTreeNow(
db: AristoDbRef;
rvid: RootedVertexID;
): Result[void,AristoError] =
## Delete sub-tree now
let (vtx, _) = db.getVtxRc(rvid).valueOr:
if error == GetVtxNotFound:
return ok()
return err(error)

if vtx.vType == Branch:
for n in 0..15:
if vtx.bVid[n].isValid:
? db.delSubTreeNow((rvid.root,vtx.bVid[n]))

db.disposeOfVtx(rvid)

ok()


proc delStoTreeNow(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
accPath: Hash256; # Accounts cache designator
stoPath: NibblesBuf; # Current storage path
): Result[void,AristoError] =
## Collect vertex/vid and delete cache entries.
## Implementation of *delete* sub-trie.

let (vtx, _) = db.getVtxRc(rvid).valueOr:
if error == GetVtxNotFound:
return ok()
Expand All @@ -36,80 +56,36 @@ proc collectStoTreeLazily(
of Branch:
for i in 0..15:
if vtx.bVid[i].isValid:
? db.collectStoTreeLazily(
? db.delStoTreeNow(
(rvid.root, vtx.bVid[i]), accPath,
stoPath & vtx.ePfx & NibblesBuf.nibble(byte i))

of Leaf:
let stoPath = Hash256(data: (stoPath & vtx.lPfx).getBytes())
db.layersPutStoLeaf(AccountKey.mixUp(accPath, stoPath), nil)

# There is no useful approach avoiding to walk the whole tree for updating
# the storage data access cache.
#
# The alternative of stopping here and clearing the whole cache did degrade
# performance significantly in some tests on mainnet when importing `era1`.
#
# The cache it was seen
# * filled up to maximum size most of the time
# * at the same time having no `stoPath` hit at all (so there was nothing
# to be cleared.)
#
ok()


proc disposeOfSubTree(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
) =
## Evaluate results from `collectSubTreeLazyImpl()` or ftom
## `collectStoTreeLazyImpl)`.
##
let vtx = db.getVtxRc(rvid).value[0]
if vtx.vType == Branch:
for n in 0..15:
if vtx.bVid[n].isValid:
db.top.delTree.add (rvid.root,vtx.bVid[n])

# Delete top of tree now.
db.disposeOfVtx(rvid)

ok()

# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------

proc delSubTreeImpl*(
db: AristoDbRef; # Database, top layer
root: VertexID; # Root vertex
db: AristoDbRef;
root: VertexID;
): Result[void,AristoError] =
## Delete all the `subRoots`if there are a few, only. Otherwise
## mark it for deleting later.
discard db.getVtxRc((root,root)).valueOr:
if error == GetVtxNotFound:
return ok()
return err(error)

db.disposeOfSubTree((root,root))

ok()
db.delSubTreeNow (root,root)


proc delStoTreeImpl*(
db: AristoDbRef; # Database, top layer
rvid: RootedVertexID; # Root vertex
accPath: Hash256;
): Result[void,AristoError] =
## Collect vertex/vid and cache entry.
discard db.getVtxRc(rvid).valueOr:
if error == GetVtxNotFound:
return ok()
return err(error)

? db.collectStoTreeLazily(rvid, accPath, NibblesBuf())

db.disposeOfSubTree(rvid)

ok()
## Implementation of *delete* sub-trie.
db.delStoTreeNow(rvid, accPath, NibblesBuf())

# ------------------------------------------------------------------------------
# End
Expand Down
26 changes: 0 additions & 26 deletions nimbus/db/aristo/aristo_delta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@ import
logScope:
topics = "aristo-delta"

# ------------------------------------------------------------------------------
# Private functions
# ------------------------------------------------------------------------------

proc toStr(rvid: RootedVertexID): string =
"$" & rvid.root.uint64.toHex & ":" & rvid.vid.uint64.toHex

proc delSubTree(db: AristoDbRef; writer: PutHdlRef; rvid: RootedVertexID) =
## Collect subtrees marked for deletion
let (vtx,_) = db.getVtxRc(rvid).valueOr:
notice "Descending for deletion stopped", rvid=(rvid.toStr), error
return
for vid in vtx.subVids:
db.delSubTree(writer, (rvid.root, vid))
db.backend.putVtxFn(writer, rvid, VertexRef(nil))
db.backend.putKeyFn(writer, rvid, VOID_HASH_KEY)
# Make sure the `rvid` is not mentioned here, anymore for further update.
db.balancer.sTab.del rvid
db.balancer.kMap.del rvid

# ------------------------------------------------------------------------------
# Public functions, save to backend
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -109,12 +89,6 @@ proc deltaPersistent*(

# Store structural single trie entries
let writeBatch = ? be.putBegFn()
# This one must come first in order to avoid duplicate `sTree[]` or
# `kMap[]` instructions, in the worst case overwiting previously deleted
# entries.
for rvid in db.balancer.delTree:
db.delSubTree(writeBatch, rvid)
# Now the standard `sTree[]` and `kMap[]` instructions.
for rvid, vtx in db.balancer.sTab:
be.putVtxFn(writeBatch, rvid, vtx)
for rvid, key in db.balancer.kMap:
Expand Down
4 changes: 0 additions & 4 deletions nimbus/db/aristo/aristo_delta/delta_merge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ proc deltaMerge*(
result = LayerRef(
sTab: lower.sTab, # shallow copy (entries will not be modified)
kMap: lower.kMap,
delTree: lower.delTree,
accLeaves: lower.accLeaves,
stoLeaves: lower.stoLeaves,
vTop: upper.vTop)
Expand All @@ -68,9 +67,6 @@ proc deltaMerge*(
if not upper.kMap.hasKey(rvid):
upper.kMap[rvid] = key

for rvid in lower.delTree:
upper.delTree.add rvid

for (accPath,leafVtx) in lower.accLeaves.pairs:
if not upper.accLeaves.hasKey(accPath):
upper.accLeaves[accPath] = leafVtx
Expand Down
4 changes: 0 additions & 4 deletions nimbus/db/aristo/aristo_delta/delta_reverse.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ proc revFilter*(
else:
return err((rvid.vid,rc.error))

# Reverse changes for `delTree[]` list.
for rvid in filter.delTree:
? db.revSubTree(rev, rvid)

ok(rev)

# ------------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions nimbus/db/aristo/aristo_desc/desc_structural.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ type
kMap*: Table[RootedVertexID,HashKey] ## Merkle hash key mapping
vTop*: VertexID ## Last used vertex ID

delTree*: seq[RootedVertexID] ## Not yet fully deleted sub-trees

accLeaves*: Table[Hash256, VertexRef] ## Account path -> VertexRef
stoLeaves*: Table[Hash256, VertexRef] ## Storage path -> VertexRef

Expand Down
6 changes: 0 additions & 6 deletions nimbus/db/aristo/aristo_layers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func isEmpty*(ly: LayerRef): bool =
## tables are empty. The field `txUid` is ignored, here.
ly.sTab.len == 0 and
ly.kMap.len == 0 and
ly.delTree.len == 0 and
ly.accLeaves.len == 0 and
ly.stoLeaves.len == 0

Expand All @@ -187,8 +186,6 @@ func layersMergeOnto*(src: LayerRef; trg: var LayerObj) =
for (vid,key) in src.kMap.pairs:
trg.kMap[vid] = key
trg.vTop = src.vTop
for rvid in src.delTree:
trg.delTree.add rvid
for (accPath,leafVtx) in src.accLeaves.pairs:
trg.accLeaves[accPath] = leafVtx
for (mixPath,leafVtx) in src.stoLeaves.pairs:
Expand All @@ -207,7 +204,6 @@ func layersCc*(db: AristoDbRef; level = high(int)): LayerRef =
sTab: layers[0].sTab.dup, # explicit dup for ref values
kMap: layers[0].kMap,
vTop: layers[^1].vTop,
delTree: layers[0].delTree,
accLeaves: layers[0].accLeaves,
stoLeaves: layers[0].stoLeaves)

Expand All @@ -217,8 +213,6 @@ func layersCc*(db: AristoDbRef; level = high(int)): LayerRef =
result.sTab[vid] = vtx
for (vid,key) in layers[n].kMap.pairs:
result.kMap[vid] = key
for rvid in layers[n].delTree:
result.delTree.add rvid
for (accPath,vtx) in layers[n].accLeaves.pairs:
result.accLeaves[accPath] = vtx
for (mixPath,vtx) in layers[n].stoLeaves.pairs:
Expand Down

0 comments on commit 5cbc039

Please sign in to comment.