lru: refactor.
This commit is contained in:
parent
d7ac63755f
commit
578ff8f684
@ -263,11 +263,11 @@ function ChainDB(chain, options) {
|
|||||||
this.coinWindow = 25 << 20;
|
this.coinWindow = 25 << 20;
|
||||||
|
|
||||||
this.coinCache = new bcoin.lru.nil();
|
this.coinCache = new bcoin.lru.nil();
|
||||||
this.cacheHash = new bcoin.lru(this.cacheWindow, 1);
|
this.cacheHash = new bcoin.lru(this.cacheWindow);
|
||||||
this.cacheHeight = new bcoin.lru(this.cacheWindow, 1);
|
this.cacheHeight = new bcoin.lru(this.cacheWindow);
|
||||||
|
|
||||||
if (this.options.coinCache)
|
if (this.options.coinCache)
|
||||||
this.coinCache = new bcoin.lru(this.coinWindow);
|
this.coinCache = new bcoin.lru(this.coinWindow, getSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.inherits(ChainDB, AsyncObject);
|
utils.inherits(ChainDB, AsyncObject);
|
||||||
@ -1864,6 +1864,10 @@ function ipair(prefix, num) {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSize(value) {
|
||||||
|
return 80 + value.length;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -22,34 +22,16 @@ function LRU(maxSize, getSize) {
|
|||||||
return new LRU(maxSize, getSize);
|
return new LRU(maxSize, getSize);
|
||||||
|
|
||||||
this.maxSize = maxSize;
|
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.size = 0;
|
||||||
this.head = null;
|
this.head = null;
|
||||||
this.tail = 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.
|
* Calculate size of an item.
|
||||||
* @private
|
* @private
|
||||||
@ -58,22 +40,12 @@ LRU.prototype._createGetSize = function _createGetSize(size) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
LRU.prototype._getSize = function _getSize(item) {
|
LRU.prototype._getSize = function _getSize(item) {
|
||||||
var keySize = item.key.length * 2;
|
var keySize;
|
||||||
|
|
||||||
if (this.getSize)
|
if (this.getSize) {
|
||||||
return this.getSize(item.key, item.value);
|
keySize = Math.floor(item.key.length * 1.375);
|
||||||
|
return 120 + keySize + this.getSize(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;
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
@ -93,7 +65,7 @@ LRU.prototype._compact = function _compact() {
|
|||||||
if (this.size <= this.maxSize)
|
if (this.size <= this.maxSize)
|
||||||
break;
|
break;
|
||||||
this.size -= this._getSize(item);
|
this.size -= this._getSize(item);
|
||||||
delete this.data[item.key];
|
delete this.map[item.key];
|
||||||
next = item.next;
|
next = item.next;
|
||||||
item.prev = null;
|
item.prev = null;
|
||||||
item.next = null;
|
item.next = null;
|
||||||
@ -117,7 +89,7 @@ LRU.prototype.reset = function reset() {
|
|||||||
var item, next;
|
var item, next;
|
||||||
|
|
||||||
for (item = this.head; item; item = next) {
|
for (item = this.head; item; item = next) {
|
||||||
delete this.data[item.key];
|
delete this.map[item.key];
|
||||||
next = item.next;
|
next = item.next;
|
||||||
item.prev = null;
|
item.prev = null;
|
||||||
item.next = null;
|
item.next = null;
|
||||||
@ -141,7 +113,7 @@ LRU.prototype.set = function set(key, value) {
|
|||||||
|
|
||||||
key = key + '';
|
key = key + '';
|
||||||
|
|
||||||
item = this.data[key];
|
item = this.map[key];
|
||||||
|
|
||||||
if (item) {
|
if (item) {
|
||||||
this.size -= this._getSize(item);
|
this.size -= this._getSize(item);
|
||||||
@ -155,7 +127,7 @@ LRU.prototype.set = function set(key, value) {
|
|||||||
|
|
||||||
item = new LRUItem(key, value);
|
item = new LRUItem(key, value);
|
||||||
|
|
||||||
this.data[key] = item;
|
this.map[key] = item;
|
||||||
|
|
||||||
this._appendList(item);
|
this._appendList(item);
|
||||||
|
|
||||||
@ -175,7 +147,7 @@ LRU.prototype.get = function get(key) {
|
|||||||
|
|
||||||
key = key + '';
|
key = key + '';
|
||||||
|
|
||||||
item = this.data[key];
|
item = this.map[key];
|
||||||
|
|
||||||
if (!item)
|
if (!item)
|
||||||
return;
|
return;
|
||||||
@ -193,7 +165,7 @@ LRU.prototype.get = function get(key) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
LRU.prototype.has = 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 + '';
|
key = key + '';
|
||||||
|
|
||||||
item = this.data[key];
|
item = this.map[key];
|
||||||
|
|
||||||
if (!item)
|
if (!item)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
this.size -= this._getSize(item);
|
this.size -= this._getSize(item);
|
||||||
|
|
||||||
delete this.data[key];
|
delete this.map[key];
|
||||||
|
|
||||||
this._removeList(item);
|
this._removeList(item);
|
||||||
|
|
||||||
|
|||||||
@ -258,7 +258,7 @@ function TXDB(wallet) {
|
|||||||
this.locked = {};
|
this.locked = {};
|
||||||
|
|
||||||
this.locker = new bcoin.locker(this);
|
this.locker = new bcoin.locker(this);
|
||||||
this.coinCache = new bcoin.lru(10000, 1);
|
this.coinCache = new bcoin.lru(10000);
|
||||||
|
|
||||||
this.current = null;
|
this.current = null;
|
||||||
this.balance = null;
|
this.balance = null;
|
||||||
|
|||||||
@ -171,9 +171,9 @@ function WalletDB(options) {
|
|||||||
this.writeLock = new bcoin.locker.mapped(this);
|
this.writeLock = new bcoin.locker.mapped(this);
|
||||||
this.txLock = new bcoin.locker(this);
|
this.txLock = new bcoin.locker(this);
|
||||||
|
|
||||||
this.walletCache = new bcoin.lru(10000, 1);
|
this.walletCache = new bcoin.lru(10000);
|
||||||
this.accountCache = new bcoin.lru(10000, 1);
|
this.accountCache = new bcoin.lru(10000);
|
||||||
this.pathCache = new bcoin.lru(100000, 1);
|
this.pathCache = new bcoin.lru(100000);
|
||||||
|
|
||||||
// Try to optimize for up to 1m addresses.
|
// Try to optimize for up to 1m addresses.
|
||||||
// We use a regular bloom filter here
|
// We use a regular bloom filter here
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user