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@
This commit is contained in:
David Hill 2014-03-20 11:07:40 -04:00
parent c570830104
commit 1b51d58e55
2 changed files with 12 additions and 7 deletions

View file

@ -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 // fetchBlockShaByHeight returns a block hash based on its height in the
// block chain. // block chain.
func (db *LevelDb) fetchBlockShaByHeight(height int64) (rsha *btcwire.ShaHash, err error) { func (db *LevelDb) fetchBlockShaByHeight(height int64) (rsha *btcwire.ShaHash, err error) {
var sha *btcwire.ShaHash key := int64ToKey(height)
sha, _, err = db.getBlkByHeight(height)
blkVal, err := db.lDb.Get(key, db.ro)
if err != nil { 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 // 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 endidx = endHeight
} }
var shalist []btcwire.ShaHash shalist := make([]btcwire.ShaHash, 0, endidx-startHeight)
for height := startHeight; height < endidx; height++ { for height := startHeight; height < endidx; height++ {
// TODO(drahn) fix blkFile from height // TODO(drahn) fix blkFile from height

View file

@ -101,7 +101,7 @@ func OpenDB(args ...interface{}) (btcdb.Db, error) {
blockforward: blockforward:
for { for {
sha, _, err := ldb.getBlkByHeight(testblock) sha, err := ldb.fetchBlockShaByHeight(testblock)
if err == nil { if err == nil {
// block is found // block is found
lastSha = sha lastSha = sha
@ -125,7 +125,7 @@ blockforward:
blocknarrow: blocknarrow:
for lastknownblock != -1 { for lastknownblock != -1 {
testblock = (lastknownblock + nextunknownblock) / 2 testblock = (lastknownblock + nextunknownblock) / 2
sha, _, err := ldb.getBlkByHeight(testblock) sha, err := ldb.fetchBlockShaByHeight(testblock)
if err == nil { if err == nil {
lastknownblock = testblock lastknownblock = testblock
lastSha = sha lastSha = sha