mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
triedb/pathdb: make batch with pre-allocated size
This commit is contained in:
parent
b6da120f9b
commit
882c1354f5
1 changed files with 20 additions and 1 deletions
|
|
@ -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 = 2 // The average size of each state history index entry is approximately 1–2 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.
|
||||
|
|
@ -152,6 +157,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
|
||||
}
|
||||
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 {
|
||||
|
|
@ -162,7 +181,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
|
||||
|
|
|
|||
Loading…
Reference in a new issue