From b11bf582c51fdf3888433575a104d301a095f337 Mon Sep 17 00:00:00 2001 From: Mikael Lindlof Date: Sun, 17 May 2020 13:02:24 +0100 Subject: [PATCH] Improve chain state init efficiency Remove unnecessary slice of all block indexes and remove DB iteration over all block indexes that used to determined the size of the slice. --- blockchain/chainio.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/blockchain/chainio.go b/blockchain/chainio.go index 8d76d3ca..c456c006 100644 --- a/blockchain/chainio.go +++ b/blockchain/chainio.go @@ -1149,18 +1149,9 @@ func (b *BlockChain) initChainState() error { blockIndexBucket := dbTx.Metadata().Bucket(blockIndexBucketName) - // Determine how many blocks will be loaded into the index so we can - // allocate the right amount. - var blockCount int32 - cursor := blockIndexBucket.Cursor() - for ok := cursor.First(); ok; ok = cursor.Next() { - blockCount++ - } - blockNodes := make([]blockNode, blockCount) - var i int32 var lastNode *blockNode - cursor = blockIndexBucket.Cursor() + cursor := blockIndexBucket.Cursor() for ok := cursor.First(); ok; ok = cursor.Next() { header, status, err := deserializeBlockRow(cursor.Value()) if err != nil { @@ -1193,7 +1184,7 @@ func (b *BlockChain) initChainState() error { // Initialize the block node for the block, connect it, // and add it to the block index. - node := &blockNodes[i] + node := new(blockNode) initBlockNode(node, header, parent) node.status = status b.index.addNode(node)