Skip to content

Commit 89754de

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 efd794b commit 89754de

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

execution_chain/core/chain/forked_chain.nim

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ proc updateBase(c: ForkedChainRef, base: BlockRef): uint =
314314
# No update, return
315315
return
316316

317+
block:
318+
# Write block contents to txFrame at the last moment - otherwise, they would
319+
# stay both in BlockRef and TxFrame memory
320+
# TODO probably makes sense to do it the other way around, removing blk
321+
# from BlockRef
322+
var blk = base
323+
while blk.isOk:
324+
c.writeBaggage(blk.blk, blk.hash, blk.txFrame, blk.receipts)
325+
blk = blk.parent
326+
317327
# State root sanity check is performed to verify, before writing to disk,
318328
# that optimistically checked blocks indeed end up being stored with a
319329
# consistent state root.
@@ -484,7 +494,7 @@ proc validateBlock(c: ForkedChainRef,
484494
# Update the snapshot before processing the block so that any vertexes in snapshots
485495
# from lower levels than the baseTxFrame are removed from the snapshot before running
486496
# the stateroot computation.
487-
c.updateSnapshot(blk, txFrame)
497+
c.updateSnapshot(blk.header.number, txFrame)
488498

489499
var receipts = c.processBlock(parent, txFrame, blk, blkHash, finalized).valueOr:
490500
txFrame.dispose()

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
@@ -129,7 +129,7 @@ proc replayBlock(fc: ForkedChainRef;
129129
# Update the snapshot before processing the block so that any vertexes in snapshots
130130
# from lower levels than the baseTxFrame are removed from the snapshot before running
131131
# the stateroot computation.
132-
fc.updateSnapshot(blk.blk, txFrame)
132+
fc.updateSnapshot(blk.blk.header.number, txFrame)
133133

134134
var receipts = fc.processBlock(parent, txFrame, blk.blk, blk.hash, false).valueOr:
135135
txFrame.dispose()

0 commit comments

Comments
 (0)