From c27466a7c5a45ff42bd81ce5f2d9254c68181f3a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 6 Jul 2016 16:35:23 -0700 Subject: [PATCH] avoid duplicate hash table lookups. --- lib/bcoin/chaindb.js | 23 +++++++++++++++++------ lib/bcoin/chainentry.js | 7 +++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 23d5a807..7f4fa6b3 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -317,6 +317,8 @@ ChainDB.prototype.getCache = function getCache(hash) { */ ChainDB.prototype.getHeight = function getHeight(hash, callback) { + var entry; + if (hash == null || hash < 0) { callback = utils.asyncify(callback); return callback(null, -1); @@ -332,9 +334,11 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) { return callback(null, -1); } - if (this.cacheHash.has(hash)) { + entry = this.cacheHash.get(hash); + + if (entry) { callback = utils.asyncify(callback); - return callback(null, this.cacheHash.get(hash).height); + return callback(null, entry.height); } this.db.fetch(layout.h(hash), function(data) { @@ -359,6 +363,8 @@ ChainDB.prototype.getHeight = function getHeight(hash, callback) { */ ChainDB.prototype.getHash = function getHash(height, callback) { + var entry; + if (height == null || height < 0) { callback = utils.asyncify(callback); return callback(null, null); @@ -369,9 +375,11 @@ ChainDB.prototype.getHash = function getHash(height, callback) { return callback(null, height); } - if (this.cacheHeight.has(height)) { + entry = this.cacheHeight.get(height); + + if (entry) { callback = utils.asyncify(callback); - return callback(null, this.cacheHeight.get(height).hash); + return callback(null, entry.hash); } this.db.fetch(layout.H(height), function(data) { @@ -445,6 +453,7 @@ ChainDB.prototype.getBoth = function getBoth(block, callback) { ChainDB.prototype.getEntry = function getEntry(hash, callback) { var self = this; + var entry; if (hash == null || hash < 0) return utils.nextTick(callback); @@ -456,8 +465,10 @@ ChainDB.prototype.getEntry = function getEntry(hash, callback) { if (!hash) return callback(); - if (self.cacheHash.has(hash)) - return callback(null, self.cacheHash.get(hash)); + entry = self.cacheHash.get(hash); + + if (entry) + return callback(null, entry); return self.db.fetch(layout.e(hash), function(data) { return bcoin.chainentry.fromRaw(self.chain, data); diff --git a/lib/bcoin/chainentry.js b/lib/bcoin/chainentry.js index f8204609..ac0543d2 100644 --- a/lib/bcoin/chainentry.js +++ b/lib/bcoin/chainentry.js @@ -179,6 +179,7 @@ ChainEntry.prototype.getRetargetAncestors = function getRetargetAncestors(callba ChainEntry.prototype.getAncestors = function getAncestors(max, callback) { var entry = this; var ancestors = []; + var cached; if (max === 0) return callback(null, []); @@ -193,12 +194,14 @@ ChainEntry.prototype.getAncestors = function getAncestors(max, callback) { if (ancestors.length >= max) return callback(null, ancestors); - if (!this.chain.db.hasCache(entry.prevBlock)) { + cached = this.chain.db.getCache(entry.prevBlock); + + if (!cached) { ancestors.pop(); break; } - entry = this.chain.db.getCache(entry.prevBlock); + entry = cached; } (function next(err, entry) {