Skip to content

Commit a49754a

Browse files
MariusVanDerWijdenhowjmay
authored andcommitted
core/filtermaps: do not derive full receipts during rendering (ethereum#31716)
This changes the filtermaps to only pull up the raw receipts, not the derived receipts which saves a lot of allocations. During normal execution this will reduce the allocations of the whole geth node by ~15%.
1 parent 4813061 commit a49754a

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

core/blockchain_reader.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
234234
return receipts
235235
}
236236

237+
func (bc *BlockChain) GetRawReceiptsByHash(hash common.Hash) types.Receipts {
238+
number := rawdb.ReadHeaderNumber(bc.db, hash)
239+
if number == nil {
240+
return nil
241+
}
242+
return rawdb.ReadRawReceipts(bc.db, hash, *number)
243+
}
244+
237245
// GetUnclesInChain retrieves all the uncles from a given block backwards until
238246
// a specific distance is reached.
239247
func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header {

core/filtermaps/chain_view.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type blockchain interface {
2929
GetHeader(hash common.Hash, number uint64) *types.Header
3030
GetCanonicalHash(number uint64) common.Hash
3131
GetReceiptsByHash(hash common.Hash) types.Receipts
32+
GetRawReceiptsByHash(hash common.Hash) types.Receipts
3233
}
3334

3435
// ChainView represents an immutable view of a chain with a block id and a set
@@ -102,10 +103,23 @@ func (cv *ChainView) Receipts(number uint64) types.Receipts {
102103
blockHash := cv.BlockHash(number)
103104
if blockHash == (common.Hash{}) {
104105
log.Error("Chain view: block hash unavailable", "number", number, "head", cv.headNumber)
106+
return nil
105107
}
106108
return cv.chain.GetReceiptsByHash(blockHash)
107109
}
108110

111+
// RawReceipts returns the set of receipts belonging to the block at the given
112+
// block number. Does not derive the fields of the receipts, should only be
113+
// used during creation of the filter maps, please use cv.Receipts during querying.
114+
func (cv *ChainView) RawReceipts(number uint64) types.Receipts {
115+
blockHash := cv.BlockHash(number)
116+
if blockHash == (common.Hash{}) {
117+
log.Error("Chain view: block hash unavailable", "number", number, "head", cv.headNumber)
118+
return nil
119+
}
120+
return cv.chain.GetRawReceiptsByHash(blockHash)
121+
}
122+
109123
// SharedRange returns the block range shared by two chain views.
110124
func (cv *ChainView) SharedRange(cv2 *ChainView) common.Range[uint64] {
111125
cv.lock.Lock()

core/filtermaps/indexer_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ func (tc *testChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
515515
return tc.receipts[hash]
516516
}
517517

518+
func (tc *testChain) GetRawReceiptsByHash(hash common.Hash) types.Receipts {
519+
tc.lock.RLock()
520+
defer tc.lock.RUnlock()
521+
522+
return tc.receipts[hash]
523+
}
524+
518525
func (tc *testChain) addBlocks(count, maxTxPerBlock, maxLogsPerReceipt, maxTopicsPerLog int, random bool) {
519526
tc.lock.Lock()
520527
blockGen := func(i int, gen *core.BlockGen) {

core/filtermaps/map_renderer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func (f *FilterMaps) newLogIteratorFromMapBoundary(mapIndex uint32, startBlock,
693693
return nil, fmt.Errorf("iterator entry point %d after target chain head block %d", startBlock, f.targetView.HeadNumber())
694694
}
695695
// get block receipts
696-
receipts := f.targetView.Receipts(startBlock)
696+
receipts := f.targetView.RawReceipts(startBlock)
697697
if receipts == nil {
698698
return nil, fmt.Errorf("receipts not found for start block %d", startBlock)
699699
}
@@ -760,7 +760,7 @@ func (l *logIterator) next() error {
760760
if l.delimiter {
761761
l.delimiter = false
762762
l.blockNumber++
763-
l.receipts = l.chainView.Receipts(l.blockNumber)
763+
l.receipts = l.chainView.RawReceipts(l.blockNumber)
764764
if l.receipts == nil {
765765
return fmt.Errorf("receipts not found for block %d", l.blockNumber)
766766
}

eth/filters/filter_system_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ func (b *testBackend) GetReceiptsByHash(hash common.Hash) types.Receipts {
8080
return r
8181
}
8282

83+
func (b *testBackend) GetRawReceiptsByHash(hash common.Hash) types.Receipts {
84+
if number := rawdb.ReadHeaderNumber(b.db, hash); number != nil {
85+
return rawdb.ReadRawReceipts(b.db, hash, *number)
86+
}
87+
return nil
88+
}
89+
8390
func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
8491
var (
8592
hash common.Hash

0 commit comments

Comments
 (0)