Skip to content

Commit a5e46d2

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 fe162f0 commit a5e46d2

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

execution_chain/core/chain/forked_chain.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ proc updateBase(c: ForkedChainRef, base: BlockRef): uint =
315315
return
316316

317317
let startTime = Moment.now()
318+
block:
319+
# Write block contents to txFrame at the last moment - otherwise, they would
320+
# stay both in BlockRef and TxFrame memory
321+
# TODO probably makes sense to do it the other way around, removing blk
322+
# from BlockRef
323+
var blk = base
324+
while blk.isOk:
325+
c.writeBaggage(blk.blk, blk.hash, blk.txFrame, blk.receipts)
326+
blk = blk.parent
318327

319328
# State root sanity check is performed to verify, before writing to disk,
320329
# that optimistically checked blocks indeed end up being stored with a
@@ -513,7 +522,7 @@ proc validateBlock(c: ForkedChainRef,
513522
# Update the snapshot before processing the block so that any vertexes in snapshots
514523
# from lower levels than the baseTxFrame are removed from the snapshot before running
515524
# the stateroot computation.
516-
c.updateSnapshot(parent.blk, parentFrame)
525+
c.updateSnapshot(parent.blk.header.number, parentFrame)
517526

518527
var receipts = c.processBlock(parent, txFrame, blk, blkHash, finalized).valueOr:
519528
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(parent.blk, parentFrame)
132+
fc.updateSnapshot(parent.blk.header.number, parentFrame)
133133

134134
# Set finalized to true in order to skip the stateroot check when replaying the
135135
# block because the blocks should have already been checked previously during

0 commit comments

Comments
 (0)