Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions triedb/pathdb/history_index_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
)

const (
indexBlockDescSize = 14 // The size of index block descriptor
indexBlockEntriesCap = 4096 // The maximum number of entries can be grouped in a block
indexBlockRestartLen = 256 // The restart interval length of index block
historyIndexBatch = 1_000_000 // The number of state history indexes for constructing or deleting as batch
indexBlockDescSize = 14 // The size of index block descriptor
indexBlockEntriesCap = 4096 // The maximum number of entries can be grouped in a block
indexBlockRestartLen = 256 // The restart interval length of index block
historyIndexBatch = 512 * 1024 // The number of state history indexes for constructing or deleting as batch
)

// indexBlockDesc represents a descriptor for an index block, which contains a
Expand Down
21 changes: 20 additions & 1 deletion triedb/pathdb/history_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const (
stateHistoryIndexVersion = stateHistoryIndexV0 // the current state index version
trienodeHistoryIndexV0 = uint8(0) // initial version of trienode index structure
trienodeHistoryIndexVersion = trienodeHistoryIndexV0 // the current trienode index version

// estimations for calculating the batch size for atomic database commit
estimatedStateHistoryIndexSize = 3 // The average size of each state history index entry is approximately 2–3 bytes
estimatedTrienodeHistoryIndexSize = 3 // The average size of each trienode history index entry is approximately 2-3 bytes
estimatedIndexBatchSizeFactor = 32 // The factor counts for the write amplification for each entry
)

// indexVersion returns the latest index version for the given history type.
Expand Down Expand Up @@ -150,6 +155,20 @@ func (b *batchIndexer) process(h history, id uint64) error {
return b.finish(false)
}

// makeBatch constructs a database batch based on the number of pending entries.
// The batch size is roughly estimated to minimize repeated resizing rounds,
// as accurately predicting the exact size is technically challenging.
func (b *batchIndexer) makeBatch() ethdb.Batch {
var size int
switch b.typ {
case typeStateHistory:
size = estimatedStateHistoryIndexSize
case typeTrienodeHistory:
size = estimatedTrienodeHistoryIndexSize
Comment on lines +163 to +167
Copy link
Member

@gballet gballet Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no default here? is using 0 future-proof?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tracked the use, but I didn't go all the way down. I see pebble uses the size to do something like mallocgc, and then does slice arithmetic on it. I would return a save value just to be sure it doesn't panic.,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the default here. It's only possible to have state history or trienode history, we can panic for unknown type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k, I was hoping for a warning, but I agree that this is not a concern right now. It's just the future-proofiness I was considering. Nvm I'll merge.

}
return b.db.NewBatchWithSize(size * estimatedIndexBatchSizeFactor * b.pending)
}

// finish writes the accumulated state indexes into the disk if either the
// memory limitation is reached or it's requested forcibly.
func (b *batchIndexer) finish(force bool) error {
Expand All @@ -160,7 +179,7 @@ func (b *batchIndexer) finish(force bool) error {
return nil
}
var (
batch = b.db.NewBatch()
batch = b.makeBatch()
batchMu sync.RWMutex
start = time.Now()
eg errgroup.Group
Expand Down