trie: use batch reset instead of recreation

This commit is contained in:
Péter Szilágyi 2018-01-30 19:11:01 +02:00
parent abd99834db
commit a7bae8c11c
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D

View file

@ -262,12 +262,12 @@ func (db *Database) Commit(node common.Hash) error {
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
return err return err
} }
batch = db.diskdb.NewBatch() batch.Reset()
} }
} }
// Move the trie itself into the batch, flushing if enough data is accumulated // Move the trie itself into the batch, flushing if enough data is accumulated
nodes, storage := len(db.nodes), db.nodesSize+db.preimagesSize nodes, storage := len(db.nodes), db.nodesSize+db.preimagesSize
if err := db.commit(node, &batch); err != nil { if err := db.commit(node, batch); err != nil {
log.Error("Failed to commit trie from trie database", "err", err) log.Error("Failed to commit trie from trie database", "err", err)
db.lock.RUnlock() db.lock.RUnlock()
return err return err
@ -299,7 +299,7 @@ func (db *Database) Commit(node common.Hash) error {
} }
// commit is the private locked version of Commit. // commit is the private locked version of Commit.
func (db *Database) commit(hash common.Hash, batch *ethdb.Batch) error { func (db *Database) commit(hash common.Hash, batch ethdb.Batch) error {
// If the node does not exist, it's a previously committed node // If the node does not exist, it's a previously committed node
node, ok := db.nodes[hash] node, ok := db.nodes[hash]
if !ok { if !ok {
@ -310,15 +310,15 @@ func (db *Database) commit(hash common.Hash, batch *ethdb.Batch) error {
return err return err
} }
} }
if err := (*batch).Put(hash[:], node.blob); err != nil { if err := batch.Put(hash[:], node.blob); err != nil {
return err return err
} }
// If we've reached an optimal match size, commit and start over // If we've reached an optimal match size, commit and start over
if (*batch).ValueSize() >= ethdb.IdealBatchSize { if batch.ValueSize() >= ethdb.IdealBatchSize {
if err := (*batch).Write(); err != nil { if err := batch.Write(); err != nil {
return err return err
} }
(*batch) = db.diskdb.NewBatch() batch.Reset()
} }
return nil return nil
} }