瀏覽代碼

core, ethdb: reuse database batches (#15989)

* leveldb: Update leveldb to 211f780 (poolfix)

* core, ethdb: reuse database batches
Martin Holst Swende 7 年之前
父節點
當前提交
017b9f7eac
共有 4 個文件被更改,包括 16 次插入1 次删除
  1. 1 1
      core/blockchain.go
  2. 8 0
      ethdb/database.go
  3. 2 0
      ethdb/interface.go
  4. 5 0
      ethdb/memory_database.go

+ 1 - 1
core/blockchain.go

@@ -749,7 +749,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
 				return 0, err
 			}
 			bytes += batch.ValueSize()
-			batch = bc.chainDb.NewBatch()
+			batch.Reset()
 		}
 	}
 	if batch.ValueSize() > 0 {

+ 8 - 0
ethdb/database.go

@@ -299,6 +299,10 @@ func (b *ldbBatch) ValueSize() int {
 	return b.size
 }
 
+func (b *ldbBatch) Reset() {
+	b.b.Reset()
+}
+
 type table struct {
 	db     Database
 	prefix string
@@ -358,3 +362,7 @@ func (tb *tableBatch) Write() error {
 func (tb *tableBatch) ValueSize() int {
 	return tb.batch.ValueSize()
 }
+
+func (tb *tableBatch) Reset() {
+	tb.batch.Reset()
+}

+ 2 - 0
ethdb/interface.go

@@ -41,4 +41,6 @@ type Batch interface {
 	Putter
 	ValueSize() int // amount of data in the batch
 	Write() error
+	// Reset resets the batch for reuse
+	Reset()
 }

+ 5 - 0
ethdb/memory_database.go

@@ -123,3 +123,8 @@ func (b *memBatch) Write() error {
 func (b *memBatch) ValueSize() int {
 	return b.size
 }
+
+func (b *memBatch) Reset() {
+	b.writes = b.writes[:0]
+	b.size = 0
+}