Skip to content

Commit f152a3c

Browse files
authored
Improve performance of stateroot computation when using kMap memory optimization (#3717)
* Copy kMap values into snapshot vtx in copyFrom. * Add a test.
1 parent bd02a5f commit f152a3c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

execution_chain/db/aristo/aristo_layers.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ proc copyFrom*(snapshot: var Snapshot, tx: AristoTxRef) =
192192
do:
193193
snapshot.vtx[rvid] = (vtx, VOID_HASH_KEY, tx.level)
194194

195+
for rvid, key in tx.kMap:
196+
snapshot.vtx.withValue(rvid, v):
197+
v[][1] = key
198+
195199
for k, v in tx.accLeaves:
196200
snapshot.acc[k] = (v, tx.level)
197201
for k, v in tx.stoLeaves:

tests/test_aristo/test_tx_frame.nim

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,55 @@ suite "Aristo TxFrame":
138138
tx3.kMap.len() == 0
139139
tx2.fetchStateRoot() == tx3.fetchStateRoot()
140140

141+
test "Frames using moveParentHashKeys parameter with snapshots":
142+
let
143+
tx0 = db.txFrameBegin(db.baseTxFrame())
144+
tx1 = db.txFrameBegin(tx0)
145+
146+
check:
147+
tx0.mergeAccountRecord(acc1[0], acc1[1]).isOk()
148+
tx1.mergeAccountRecord(acc2[0], acc2[1]).isOk()
149+
tx0.fetchStateRoot() != tx1.fetchStateRoot()
150+
tx0.kMap.len() == 0
151+
tx1.kMap.len() == 1
152+
tx1.checkpoint(1, skipSnapshot = false)
153+
154+
let tx2 = db.txFrameBegin(tx1, moveParentHashKeys = true)
155+
tx2.checkpoint(2, skipSnapshot = false)
156+
157+
block:
158+
var hashKey: HashKey
159+
for v in tx2.kMap.values():
160+
if v.isValid():
161+
hashKey = v
162+
check hashKey != default(HashKey)
163+
var snapshotContainsHashKey = false
164+
for k, v in tx2.snapshot.vtx:
165+
if v[1] == hashKey:
166+
snapshotContainsHashKey = true
167+
168+
check:
169+
snapshotContainsHashKey
170+
tx1.fetchStateRoot() == tx2.fetchStateRoot()
171+
172+
let tx3 = db.txFrameBegin(tx2, moveParentHashKeys = false)
173+
tx3.checkpoint(3, skipSnapshot = false)
174+
175+
block:
176+
var hashKey: HashKey
177+
for v in tx2.kMap.values():
178+
if v.isValid():
179+
hashKey = v
180+
check hashKey != default(HashKey)
181+
var snapshotContainsHashKey = false
182+
for k, v in tx3.snapshot.vtx:
183+
if v[1] == hashKey:
184+
snapshotContainsHashKey = true
185+
186+
check:
187+
snapshotContainsHashKey
188+
tx2.fetchStateRoot() == tx3.fetchStateRoot()
189+
141190
test "Frames using moveParentHashKeys parameter - moved from persist":
142191
let
143192
tx0 = db.txFrameBegin(db.baseTxFrame())

0 commit comments

Comments
 (0)