core, ethdb, triedb: add batch close

This commit is contained in:
Gary Rong 2026-01-29 14:20:36 +08:00
parent 344d01e2be
commit 8913016e2d
9 changed files with 30 additions and 0 deletions

View file

@ -1276,6 +1276,8 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
func (bc *BlockChain) writeHeadBlock(block *types.Block) { func (bc *BlockChain) writeHeadBlock(block *types.Block) {
// Add the block to the canonical chain number scheme and mark as the head // Add the block to the canonical chain number scheme and mark as the head
batch := bc.db.NewBatch() batch := bc.db.NewBatch()
defer batch.Close()
rawdb.WriteHeadHeaderHash(batch, block.Hash()) rawdb.WriteHeadHeaderHash(batch, block.Hash())
rawdb.WriteHeadFastBlockHash(batch, block.Hash()) rawdb.WriteHeadFastBlockHash(batch, block.Hash())
rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64()) rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64())
@ -1650,6 +1652,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
batch = bc.db.NewBatch() batch = bc.db.NewBatch()
start = time.Now() start = time.Now()
) )
defer batch.Close()
rawdb.WriteBlock(batch, block) rawdb.WriteBlock(batch, block)
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts) rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts)
rawdb.WritePreimages(batch, statedb.Preimages()) rawdb.WritePreimages(batch, statedb.Preimages())
@ -2628,6 +2632,8 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Header) error
// Delete useless indexes right now which includes the non-canonical // Delete useless indexes right now which includes the non-canonical
// transaction indexes, canonical chain indexes which above the head. // transaction indexes, canonical chain indexes which above the head.
batch := bc.db.NewBatch() batch := bc.db.NewBatch()
defer batch.Close()
for _, tx := range types.HashDifference(deletedTxs, rebirthTxs) { for _, tx := range types.HashDifference(deletedTxs, rebirthTxs) {
rawdb.DeleteTxLookupEntry(batch, tx) rawdb.DeleteTxLookupEntry(batch, tx)
} }

View file

@ -253,6 +253,11 @@ func (b *tableBatch) Reset() {
b.batch.Reset() b.batch.Reset()
} }
// Close closes the batch and releases all associated resources.
func (b *tableBatch) Close() {
b.batch.Close()
}
// tableReplayer is a wrapper around a batch replayer which truncates // tableReplayer is a wrapper around a batch replayer which truncates
// the added prefix. // the added prefix.
type tableReplayer struct { type tableReplayer struct {

View file

@ -1333,6 +1333,7 @@ func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorag
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
return nil, err return nil, err
} }
batch.Close()
} }
if !ret.empty() { if !ret.empty() {
// If snapshotting is enabled, update the snapshot tree with this new version // If snapshotting is enabled, update the snapshot tree with this new version

View file

@ -37,6 +37,9 @@ type Batch interface {
// Replay replays the batch contents. // Replay replays the batch contents.
Replay(w KeyValueWriter) error Replay(w KeyValueWriter) error
// Close closes the batch and releases all associated resources.
Close()
} }
// Batcher wraps the NewBatch method of a backing data store. // Batcher wraps the NewBatch method of a backing data store.

View file

@ -518,6 +518,9 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error {
return b.b.Replay(&replayer{writer: w}) return b.b.Replay(&replayer{writer: w})
} }
// Close closes the batch and releases all associated resources.
func (b *batch) Close() {}
// replayer is a small wrapper to implement the correct replay methods. // replayer is a small wrapper to implement the correct replay methods.
type replayer struct { type replayer struct {
writer ethdb.KeyValueWriter writer ethdb.KeyValueWriter

View file

@ -338,6 +338,9 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error {
return nil return nil
} }
// Close closes the batch and releases all associated resources.
func (b *batch) Close() {}
// iterator can walk over the (potentially partial) keyspace of a memory key // iterator can walk over the (potentially partial) keyspace of a memory key
// value store. Internally it is a deep copy of the entire iterated state, // value store. Internally it is a deep copy of the entire iterated state,
// sorted by keys. // sorted by keys.

View file

@ -727,6 +727,12 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error {
} }
} }
// Close closes the batch and releases all associated resources. After it is
// closed, any subsequent operations on this batch are undefined.
func (b *batch) Close() {
b.b.Close()
}
// pebbleIterator is a wrapper of underlying iterator in storage engine. // pebbleIterator is a wrapper of underlying iterator in storage engine.
// The purpose of this structure is to implement the missing APIs. // The purpose of this structure is to implement the missing APIs.
// //

View file

@ -880,6 +880,7 @@ func (b *spongeBatch) ValueSize() int { return 100 }
func (b *spongeBatch) Write() error { return nil } func (b *spongeBatch) Write() error { return nil }
func (b *spongeBatch) Reset() {} func (b *spongeBatch) Reset() {}
func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil } func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil }
func (b *spongeBatch) Close() {}
// TestCommitSequence tests that the trie.Commit operation writes the elements // TestCommitSequence tests that the trie.Commit operation writes the elements
// of the trie in the expected order. // of the trie in the expected order.

View file

@ -180,6 +180,8 @@ func (b *buffer) flush(root common.Hash, db ethdb.KeyValueStore, freezers []ethd
b.flushErr = err b.flushErr = err
return return
} }
batch.Close()
commitBytesMeter.Mark(int64(size)) commitBytesMeter.Mark(int64(size))
commitNodesMeter.Mark(int64(nodes)) commitNodesMeter.Mark(int64(nodes))
commitAccountsMeter.Mark(int64(accounts)) commitAccountsMeter.Mark(int64(accounts))