Skip to content

Commit 1c26462

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 240564e commit 1c26462

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

execution_chain/core/chain/forked_chain.nim

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ proc updateBase(c: ForkedChainRef, base: BlockRef): uint =
311311
# No update, return
312312
return
313313

314+
block:
315+
# Write block contents to txFrame at the last moment - otherwise, they would
316+
# stay both in BlockRef and TxFrame memory
317+
# TODO probably makes sense to do it the other way around, removing blk
318+
# from BlockRef
319+
var blk = base
320+
while blk.isOk:
321+
c.writeBaggage(blk.blk, blk.hash, blk.txFrame, blk.receipts)
322+
blk = blk.parent
323+
314324
c.com.db.persist(base.txFrame, Opt.some(base.stateRoot))
315325

316326
# Update baseTxFrame when we about to yield to the event loop
@@ -466,9 +476,7 @@ proc validateBlock(c: ForkedChainRef,
466476
txFrame.dispose()
467477
return err(error)
468478

469-
c.writeBaggage(blk, blkHash, txFrame, receipts)
470-
471-
c.updateSnapshot(blk, txFrame)
479+
c.updateSnapshot(blk.header.number, txFrame)
472480

473481
let newBlock = c.appendBlock(parent, blk, blkHash, txFrame, move(receipts))
474482

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)