Skip to content

Commit a627026

Browse files
committed
Halve FC memory usage
Blocks are getting stored both in BlockRef and in TxFrame - this is the less invasive change that delays storing block contents in TxFrame until it's time to update the base - the better option for the future is likely to not store the full block in BlockRef (and instead load it from TxFrame on demand)
1 parent 9f75e08 commit a627026

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

execution_chain/core/chain/forked_chain.nim

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ something else needs attention! Shutting down to preserve the database - restart
327327
with --debug-eager-state-root."""
328328

329329
c.com.db.persist(base.txFrame)
330+
block:
331+
# Write block contents to txFrame at the last moment - otherwise, they would
332+
# stay both in BlockRef and TxFrame memory
333+
# TODO probably makes sense to do it the other way around, removing blk
334+
# from BlockRef
335+
var blk = base
336+
while blk.isOk:
337+
c.writeBaggage(blk.blk, blk.hash, blk.txFrame, blk.receipts)
338+
blk = blk.parent
339+
340+
c.com.db.persist(base.txFrame, Opt.some(base.stateRoot))
330341

331342
# Update baseTxFrame when we about to yield to the event loop
332343
# and prevent other modules accessing expired baseTxFrame.
@@ -481,9 +492,7 @@ proc validateBlock(c: ForkedChainRef,
481492
txFrame.dispose()
482493
return err(error)
483494

484-
c.writeBaggage(blk, blkHash, txFrame, receipts)
485-
486-
c.updateSnapshot(blk, txFrame)
495+
c.updateSnapshot(blk.header.number, txFrame)
487496

488497
let newBlock = c.appendBlock(parent, blk, blkHash, txFrame, move(receipts))
489498

execution_chain/core/chain/forked_chain/chain_private.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ proc writeBaggage*(c: ForkedChainRef,
3535
header.withdrawalsRoot.expect("WithdrawalsRoot should be verified before"),
3636
blk.withdrawals.get)
3737

38-
template updateSnapshot*(c: ForkedChainRef,
39-
blk: Block,
38+
proc updateSnapshot*(c: ForkedChainRef,
39+
number: BlockNumber,
4040
txFrame: CoreDbTxRef) =
4141
let pos = c.lastSnapshotPos
4242
c.lastSnapshotPos = (c.lastSnapshotPos + 1) mod c.lastSnapshots.len
@@ -51,7 +51,7 @@ template updateSnapshot*(c: ForkedChainRef,
5151
# Checkpoint creates a snapshot of ancestor changes in txFrame - it is an
5252
# expensive operation, specially when creating a new branch (ie when blk
5353
# is being applied to a block that is currently not a head)
54-
txFrame.checkpoint(blk.header.number)
54+
txFrame.checkpoint(number)
5555

5656
c.lastSnapshots[pos] = txFrame
5757

execution_chain/core/chain/forked_chain/chain_serialize.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ proc replayBlock(fc: ForkedChainRef;
131131
return err(error)
132132

133133
fc.writeBaggage(blk.blk, blk.hash, txFrame, receipts)
134-
fc.updateSnapshot(blk.blk, txFrame)
134+
fc.updateSnapshot(blk.blk.header.number, txFrame)
135135

136136
blk.txFrame = txFrame
137137
blk.receipts = move(receipts)

0 commit comments

Comments
 (0)