From 1b51d58e556c384ac657d69b0c8100b942223ce6 Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 20 Mar 2014 11:07:40 -0400 Subject: [PATCH] In FetchHeightRange, preallocate the hash array. Change FetchBlockByHeight to do the database lookup itself instead of calling getBlkByHeight. This saves an allocation of the entire block, which it wasn't using. In OpenDB, use fetchBlockShaByHeight instead of getBlkByHeight since it only needs the sha. This saves memory on startup. ok drahn@ --- ldb/block.go | 15 ++++++++++----- ldb/leveldb.go | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ldb/block.go b/ldb/block.go index 6739114f..729ca943 100644 --- a/ldb/block.go +++ b/ldb/block.go @@ -247,13 +247,18 @@ func (db *LevelDb) FetchBlockShaByHeight(height int64) (sha *btcwire.ShaHash, er // fetchBlockShaByHeight returns a block hash based on its height in the // block chain. func (db *LevelDb) fetchBlockShaByHeight(height int64) (rsha *btcwire.ShaHash, err error) { - var sha *btcwire.ShaHash - sha, _, err = db.getBlkByHeight(height) + key := int64ToKey(height) + + blkVal, err := db.lDb.Get(key, db.ro) if err != nil { - return + log.Tracef("failed to find height %v", height) + return // exists ??? } - return sha, nil + var sha btcwire.ShaHash + sha.SetBytes(blkVal[0:32]) + + return &sha, nil } // FetchHeightRange looks up a range of blocks by the start and ending @@ -271,7 +276,7 @@ func (db *LevelDb) FetchHeightRange(startHeight, endHeight int64) (rshalist []bt endidx = endHeight } - var shalist []btcwire.ShaHash + shalist := make([]btcwire.ShaHash, 0, endidx-startHeight) for height := startHeight; height < endidx; height++ { // TODO(drahn) fix blkFile from height diff --git a/ldb/leveldb.go b/ldb/leveldb.go index 2209d833..34671dcd 100644 --- a/ldb/leveldb.go +++ b/ldb/leveldb.go @@ -101,7 +101,7 @@ func OpenDB(args ...interface{}) (btcdb.Db, error) { blockforward: for { - sha, _, err := ldb.getBlkByHeight(testblock) + sha, err := ldb.fetchBlockShaByHeight(testblock) if err == nil { // block is found lastSha = sha @@ -125,7 +125,7 @@ blockforward: blocknarrow: for lastknownblock != -1 { testblock = (lastknownblock + nextunknownblock) / 2 - sha, _, err := ldb.getBlkByHeight(testblock) + sha, err := ldb.fetchBlockShaByHeight(testblock) if err == nil { lastknownblock = testblock lastSha = sha