From 578ff8f68443051b51fc0184fe79d4f95bea5b37 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 18 Aug 2016 00:11:40 -0700 Subject: [PATCH] lru: refactor. --- lib/bcoin/chaindb.js | 10 ++++--- lib/bcoin/lru.js | 62 ++++++++++++------------------------------- lib/bcoin/txdb.js | 2 +- lib/bcoin/walletdb.js | 6 ++--- 4 files changed, 28 insertions(+), 52 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 41835c13..09c03a7f 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -263,11 +263,11 @@ function ChainDB(chain, options) { this.coinWindow = 25 << 20; this.coinCache = new bcoin.lru.nil(); - this.cacheHash = new bcoin.lru(this.cacheWindow, 1); - this.cacheHeight = new bcoin.lru(this.cacheWindow, 1); + this.cacheHash = new bcoin.lru(this.cacheWindow); + this.cacheHeight = new bcoin.lru(this.cacheWindow); if (this.options.coinCache) - this.coinCache = new bcoin.lru(this.coinWindow); + this.coinCache = new bcoin.lru(this.coinWindow, getSize); } utils.inherits(ChainDB, AsyncObject); @@ -1864,6 +1864,10 @@ function ipair(prefix, num) { return key; } +function getSize(value) { + return 80 + value.length; +} + /* * Expose */ diff --git a/lib/bcoin/lru.js b/lib/bcoin/lru.js index f4bcdcac..b699d10b 100644 --- a/lib/bcoin/lru.js +++ b/lib/bcoin/lru.js @@ -22,34 +22,16 @@ function LRU(maxSize, getSize) { return new LRU(maxSize, getSize); this.maxSize = maxSize; - this.getSize = this._createGetSize(getSize); + this.getSize = getSize; - this.data = {}; + assert(!getSize || typeof getSize === 'function', 'Bad size callback.'); + + this.map = {}; this.size = 0; this.head = null; this.tail = null; } -/** - * Create a getSize callback. - * @private - * @param {Number} size - * @returns {Function} - */ - -LRU.prototype._createGetSize = function _createGetSize(size) { - if (!size) - return; - - if (typeof size === 'number') - return function() { return size; }; - - if (typeof size === 'function') - return size; - - assert(false, 'Bad getSize callback.'); -}; - /** * Calculate size of an item. * @private @@ -58,22 +40,12 @@ LRU.prototype._createGetSize = function _createGetSize(size) { */ LRU.prototype._getSize = function _getSize(item) { - var keySize = item.key.length * 2; + var keySize; - if (this.getSize) - return this.getSize(item.key, item.value); - - if (item.value == null) - return keySize + 1; - - if (typeof item.value === 'number') - return keySize + 4; - - if (typeof item.value === 'string') - return keySize + item.value.length * 2; - - if (typeof item.value.length === 'number') - return keySize + item.value.length; + if (this.getSize) { + keySize = Math.floor(item.key.length * 1.375); + return 120 + keySize + this.getSize(item.value); + } return 1; }; @@ -93,7 +65,7 @@ LRU.prototype._compact = function _compact() { if (this.size <= this.maxSize) break; this.size -= this._getSize(item); - delete this.data[item.key]; + delete this.map[item.key]; next = item.next; item.prev = null; item.next = null; @@ -117,7 +89,7 @@ LRU.prototype.reset = function reset() { var item, next; for (item = this.head; item; item = next) { - delete this.data[item.key]; + delete this.map[item.key]; next = item.next; item.prev = null; item.next = null; @@ -141,7 +113,7 @@ LRU.prototype.set = function set(key, value) { key = key + ''; - item = this.data[key]; + item = this.map[key]; if (item) { this.size -= this._getSize(item); @@ -155,7 +127,7 @@ LRU.prototype.set = function set(key, value) { item = new LRUItem(key, value); - this.data[key] = item; + this.map[key] = item; this._appendList(item); @@ -175,7 +147,7 @@ LRU.prototype.get = function get(key) { key = key + ''; - item = this.data[key]; + item = this.map[key]; if (!item) return; @@ -193,7 +165,7 @@ LRU.prototype.get = function get(key) { */ LRU.prototype.has = function get(key) { - return this.data[key] != null; + return this.map[key] != null; }; /** @@ -207,14 +179,14 @@ LRU.prototype.remove = function remove(key) { key = key + ''; - item = this.data[key]; + item = this.map[key]; if (!item) return false; this.size -= this._getSize(item); - delete this.data[key]; + delete this.map[key]; this._removeList(item); diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 4397381c..b3a38e42 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -258,7 +258,7 @@ function TXDB(wallet) { this.locked = {}; this.locker = new bcoin.locker(this); - this.coinCache = new bcoin.lru(10000, 1); + this.coinCache = new bcoin.lru(10000); this.current = null; this.balance = null; diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 7d9a9e14..d09e18c7 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -171,9 +171,9 @@ function WalletDB(options) { this.writeLock = new bcoin.locker.mapped(this); this.txLock = new bcoin.locker(this); - this.walletCache = new bcoin.lru(10000, 1); - this.accountCache = new bcoin.lru(10000, 1); - this.pathCache = new bcoin.lru(100000, 1); + this.walletCache = new bcoin.lru(10000); + this.accountCache = new bcoin.lru(10000); + this.pathCache = new bcoin.lru(100000); // Try to optimize for up to 1m addresses. // We use a regular bloom filter here