Skip to content

Commit d5909c0

Browse files
committed
reduce memory allocations by reusing VertexID instance sometimes
wip
1 parent 8db34f1 commit d5909c0

File tree

7 files changed

+66
-64
lines changed

7 files changed

+66
-64
lines changed

execution_chain/db/aristo/aristo_compute.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import
1515
chronicles,
1616
eth/common/[accounts_rlp, base_rlp, hashes_rlp],
1717
results,
18-
"."/[aristo_desc, aristo_get],
18+
"."/[aristo_desc, aristo_get, aristo_layers],
1919
./aristo_desc/desc_backend
2020

2121
type WriteBatch = tuple[writer: PutHdlRef, count: int, depth: int, prefix: uint64]
@@ -85,8 +85,7 @@ proc putKeyAtLevel(
8585
else:
8686
debug "Writing computeKey cache", keys = batch.count, accounts = batch.progress
8787
else:
88-
db.deltaAtLevel(level).sTab[rvid] = vtx
89-
db.deltaAtLevel(level).kMap[rvid] = key
88+
db.deltaAtLevel(level).layersPutKey(rvid, vtx, key)
9089

9190
ok()
9291

execution_chain/db/aristo/aristo_delete.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ proc deleteImpl(
119119
ok(nil)
120120
else:
121121
# Clear the removed leaf from the branch (that still contains other children)
122-
let brDup = brVtx.dup
122+
let brDup = db.layersPutDup((hike.root, br.vid), brVtx)
123123
discard brDup.setUsed(uint8 hike.legs[^2].nibble, false)
124-
db.layersPutVtx((hike.root, br.vid), brDup)
125124

126125
ok(nil)
127126

@@ -204,9 +203,8 @@ proc deleteStorageData*(
204203
# If there was only one item (that got deleted), update the account as well
205204
if stoHike.legs.len == 1:
206205
# De-register the deleted storage tree from the account record
207-
let leaf = AccLeafRef(wpAcc.vtx).dup # Dup on modify
206+
let leaf = db.layersPutDup((accHike.root, wpAcc.vid), AccLeafRef(wpAcc.vtx)) # Dup on modify
208207
leaf.stoID.isValid = false
209-
db.layersPutVtx((accHike.root, wpAcc.vid), leaf)
210208

211209
ok()
212210

@@ -237,9 +235,8 @@ proc deleteStorageTree*(
237235
?db.delStoTreeImpl(stoID.vid, accPath)
238236

239237
# De-register the deleted storage tree from the accounts record
240-
let leaf = accVtx.dup # Dup on modify
238+
let leaf = db.layersPutDup((accHike.root, wpAcc.vid), accVtx) # Dup on modify
241239
leaf.stoID.isValid = false
242-
db.layersPutVtx((accHike.root, wpAcc.vid), leaf)
243240
ok()
244241

245242
# ------------------------------------------------------------------------------

execution_chain/db/aristo/aristo_desc/desc_structural.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ template dup*(vtx: BranchRef): BranchRef =
252252
template dup*(vtx: ExtBranchRef): ExtBranchRef =
253253
ExtBranchRef(VertexRef(vtx).dup())
254254

255+
proc `$`*(vtx: VertexRef): string =
256+
if vtx == nil:
257+
"VertexRef(nil)"
258+
else:
259+
case vtx.vType
260+
of AccLeaf:
261+
$(AccLeafRef(vtx)[])
262+
of StoLeaf:
263+
$(StoLeafRef(vtx)[])
264+
of Branch:
265+
$(BranchRef(vtx)[])
266+
of ExtBranch:
267+
$(ExtBranchRef(vtx)[])
268+
269+
255270
# ------------------------------------------------------------------------------
256271
# End
257272
# ------------------------------------------------------------------------------

execution_chain/db/aristo/aristo_layers.nim

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ func layersPutVtx*(
8686
if db.snapshot.level.isSome():
8787
db.snapshot.vtx[rvid] = (vtx, VOID_HASH_KEY, db.level)
8888

89+
func layersPutDup*[T: VertexRef](
90+
db: AristoTxRef;
91+
rvid: RootedVertexID;
92+
vtx: T;
93+
): T =
94+
## Store a (potentally empty) vertex on the top layer
95+
var vtxDup = vtx
96+
db.sTab.withValue(rvid, cur):
97+
if addr(vtx[]) != addr(cur[][]):
98+
vtxDup = vtxDup.dup()
99+
cur[] = vtxDup
100+
do:
101+
vtxDup = vtxDup.dup()
102+
db.sTab[rvid] = vtxDup
103+
104+
db.kMap.del(rvid)
105+
106+
if db.snapshot.level.isSome():
107+
db.snapshot.vtx[rvid] = (VertexRef(vtxDup), VOID_HASH_KEY, db.level)
108+
vtxDup
109+
89110
func layersResVtx*(
90111
db: AristoTxRef;
91112
rvid: RootedVertexID;
@@ -101,16 +122,16 @@ func layersPutKey*(
101122
key: HashKey;
102123
) =
103124
## Store a (potentally void) hash key on the top layer
104-
db.sTab[rvid] = vtx
125+
db.sTab[rvid] = if rvid notin db.sTab: vtx.dup() else: vtx
105126
db.kMap[rvid] = key
106127
107128
if db.snapshot.level.isSome():
108129
db.snapshot.vtx[rvid] = (vtx, key, db.level)
109130
110131
func layersResKey*(db: AristoTxRef; rvid: RootedVertexID, vtx: VertexRef) =
111-
## Shortcut for `db.layersPutKey(vid, VOID_HASH_KEY)`. It is sort of the
112-
## equivalent of a delete function.
113-
db.layersPutVtx(rvid, vtx)
132+
## Shortcut for `db.layersPutKey(vid, VOID_HASH_KEY)` which resets the hash
133+
## key cache for the given rvid / vtx
134+
db.layersPutVtx(rvid, if rvid notin db.sTab: vtx.dup() else: vtx)
114135
115136
func layersResKeys*(db: AristoTxRef; hike: Hike, skip: int) =
116137
## Reset all cached keys along the given hike

execution_chain/db/aristo/aristo_merge.nim

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ proc mergePayloadImpl[LeafType, T](
6969
7070
template resetKeys() =
7171
# Reset cached hashes of touched verticies
72-
for i in 2..vids.len:
72+
for i in 1..vids.len:
7373
db.layersResKey((root, vids[^i]), vtxs[^i])
7474
7575
while pos < path.len:
7676
# Clear existing merkle keys along the traversal path
77-
vids.add cur
78-
vtxs.add vtx
7977
var psuffix = path.slice(pos)
8078
let n = psuffix.sharedPrefixLen(vtx.pfx)
8179
case vtx.vType
@@ -87,13 +85,15 @@ proc mergePayloadImpl[LeafType, T](
8785
when payload is AristoAccount:
8886
if AccLeafRef(vtx).account == payload:
8987
return err(MergeNoAction)
90-
let leafVtx = db.layersPutLeaf((root, cur), psuffix, payload)
88+
let leafVtx = db.layersPutDup((root, cur), AccLeafRef(vtx))
89+
leafVtx.account = payload
9190
leafVtx.stoID = AccLeafRef(vtx).stoID
9291
9392
else:
9493
if StoLeafRef(vtx).stoData == payload:
9594
return err(MergeNoAction)
96-
let leafVtx = db.layersPutLeaf((root, cur), psuffix, payload)
95+
let leafVtx = db.layersPutDup((root, cur), StoLeafRef(vtx))
96+
leafVtx.stoData = payload
9797
(leafVtx, nil, nil)
9898
else:
9999
# Turn leaf into a branch (or extension) then insert the two leaves
@@ -141,6 +141,8 @@ proc mergePayloadImpl[LeafType, T](
141141
next = BranchRef(vtx).bVid(nibble)
142142
143143
if next.isValid:
144+
vids.add cur
145+
vtxs.add vtx
144146
cur = next
145147
psuffix = psuffix.slice(n + 1)
146148
pos += n + 1
@@ -153,11 +155,10 @@ proc mergePayloadImpl[LeafType, T](
153155
# There's no vertex at the branch point - insert the payload as a new
154156
# leaf and update the existing branch
155157

156-
let brDup = vtx.dup()
157-
let local = BranchRef(brDup).setUsed(nibble, true)
158-
db.layersPutVtx((root, cur), brDup)
159-
160-
let leafVtx = db.layersPutLeaf((root, local), psuffix.slice(n + 1), payload)
158+
let
159+
brDup = db.layersPutDup((root, cur), BranchRef(vtx))
160+
local = BranchRef(brDup).setUsed(nibble, true)
161+
leafVtx = db.layersPutLeaf((root, local), psuffix.slice(n + 1), payload)
161162

162163
resetKeys()
163164
return ok((leafVtx, nil, nil))
@@ -238,7 +239,8 @@ proc mergeStorageData*(
238239
return err(MergeStoAccMissing)
239240

240241
let
241-
stoID = AccLeafRef(accHike.legs[^1].wp.vtx).stoID
242+
accVtx = AccLeafRef(accHike.legs[^1].wp.vtx)
243+
stoID = accVtx.stoID
242244

243245
# Provide new storage ID when needed
244246
useID =
@@ -270,9 +272,8 @@ proc mergeStorageData*(
270272

271273
if not stoID.isValid:
272274
# Make sure that there is an account that refers to that storage trie
273-
let leaf = AccLeafRef(accHike.legs[^1].wp.vtx).dup # Dup on modify
275+
let leaf = db.layersPutDup((STATE_ROOT_VID, accHike.legs[^1].wp.vid), accVtx) # Dup on modify
274276
leaf.stoID = useID
275-
db.layersPutVtx((STATE_ROOT_VID, accHike.legs[^1].wp.vid), leaf)
276277

277278
ok()
278279

execution_chain/db/kvt/kvt_layers.nim

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,6 @@ func layersPut*(db: KvtTxRef; key: openArray[byte]; data: openArray[byte]) =
6565
proc mergeAndReset*(trg, src: KvtTxRef) =
6666
mergeAndReset(trg.sTab, src.sTab)
6767

68-
# ------------------------------------------------------------------------------
69-
# Public functions
70-
# ------------------------------------------------------------------------------
71-
72-
func layersCc*(db: KvtDbRef; level = high(int)): KvtTxRef =
73-
## Provide a collapsed copy of layers up to a particular transaction level.
74-
## If the `level` argument is too large, the maximum transaction level is
75-
## returned. For the result layer, the `txUid` value set to `0`.
76-
# let layers = if db.stack.len <= level: db.stack & @[db.top]
77-
# else: db.stack[0 .. level]
78-
79-
# # Set up initial layer (bottom layer)
80-
# result = LayerRef(sTab: layers[0].sTab)
81-
82-
# # Consecutively merge other layers on top
83-
# for n in 1 ..< layers.len:
84-
# for (key,val) in layers[n].sTab.pairs:
85-
# result.sTab[key] = val
86-
discard # TODO
87-
8868
# ------------------------------------------------------------------------------
8969
# Public iterators
9070
# ------------------------------------------------------------------------------

tests/test_aristo/test_blobify.nim

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,14 @@ import unittest2, std/sequtils, ../../execution_chain/db/aristo/aristo_blobify
1515
suite "Aristo blobify":
1616
test "VertexRef roundtrip":
1717
let
18-
leafAccount = AccLeafRef(
19-
vType: AccLeaf,
20-
pfx: NibblesBuf.nibble(1),
21-
account: AristoAccount(nonce: 100, balance: 123.u256),
22-
stoID: (isValid: true, vid: VertexID(5))
23-
)
24-
leafStoData = StoLeafRef(
25-
vType: StoLeaf,
26-
pfx: NibblesBuf.nibble(3),
27-
stoData: 42.u256,
28-
)
29-
branch = BranchRef(vType: Branch, startVid: VertexID(0x334452), used: 0x43'u16)
30-
31-
extension = ExtBranchRef(
32-
vType: ExtBranch,
33-
pfx: NibblesBuf.nibble(2),
34-
startVid: VertexID(0x55),
35-
used: 0x12'u16,
18+
leafAccount = AccLeafRef.init(
19+
NibblesBuf.nibble(1),
20+
AristoAccount(nonce: 100, balance: 123.u256),
21+
(isValid: true, vid: VertexID(5)),
3622
)
23+
leafStoData = StoLeafRef.init(NibblesBuf.nibble(3), 42.u256)
24+
branch = BranchRef.init(VertexID(0x334452), 0x43'u16)
25+
extension = ExtBranchRef.init(NibblesBuf.nibble(2), VertexID(0x55), 0x12'u16)
3726

3827
key = HashKey.fromBytes(repeat(0x34'u8, 32))[]
3928

0 commit comments

Comments
 (0)