lru: refactor.

This commit is contained in:
Christopher Jeffrey 2016-08-18 00:11:40 -07:00
parent d7ac63755f
commit 578ff8f684
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 28 additions and 52 deletions

View File

@ -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
*/

View File

@ -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);

View File

@ -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;

View File

@ -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