optimization for rolling filter.
This commit is contained in:
parent
14b2b528f3
commit
292e077550
@ -216,14 +216,17 @@ function RollingFilter(items, rate) {
|
||||
|
||||
this.n = Math.max(1, Math.min(Math.round(logRate / Math.log(0.5)), 50));
|
||||
this.limit = (items + 1) / 2 | 0;
|
||||
|
||||
max = this.limit * 3;
|
||||
this.size = -1 * this.n * max / Math.log(1.0 - Math.exp(logRate / this.n));
|
||||
this.size = Math.ceil(this.size);
|
||||
|
||||
this.items = ((this.size + 63) / 64 | 0) << 1;
|
||||
this.filter = new Buffer(this.items * 8);
|
||||
|
||||
this.tweak = (Math.random() * 0x100000000) >>> 0;
|
||||
|
||||
this.reset();
|
||||
this.filter = new Buffer(this.items * 8);
|
||||
this.filter.fill(0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -242,6 +245,9 @@ RollingFilter.prototype.hash = function hash(val, n) {
|
||||
*/
|
||||
|
||||
RollingFilter.prototype.reset = function reset() {
|
||||
if (this.entries === 0)
|
||||
return;
|
||||
|
||||
this.entries = 0;
|
||||
this.generation = 1;
|
||||
this.filter.fill(0);
|
||||
@ -318,6 +324,9 @@ RollingFilter.prototype.add = function add(val, enc) {
|
||||
RollingFilter.prototype.test = function test(val, enc) {
|
||||
var i, hash, bits, pos, pos1, pos2, bit, oct;
|
||||
|
||||
if (this.entries === 0)
|
||||
return false;
|
||||
|
||||
if (typeof val === 'string')
|
||||
val = new Buffer(val, enc);
|
||||
|
||||
@ -333,8 +342,8 @@ RollingFilter.prototype.test = function test(val, enc) {
|
||||
pos1 += oct;
|
||||
pos2 += oct;
|
||||
|
||||
bits = (this.filter[pos1] >> bit) & 1;
|
||||
bits |= (this.filter[pos2] >> bit) & 1;
|
||||
bits = (this.filter[pos1] >>> bit) & 1;
|
||||
bits |= (this.filter[pos2] >>> bit) & 1;
|
||||
|
||||
if (bits === 0)
|
||||
return false;
|
||||
|
||||
@ -1647,7 +1647,7 @@ Pool.prototype._sendRequests = function _sendRequests(peer) {
|
||||
size = 500;
|
||||
else if (this.chain.height <= 150000)
|
||||
size = 250;
|
||||
else if (this.chain.height <= 170000)
|
||||
else if (this.chain.height <= 250000)
|
||||
size = 20;
|
||||
else
|
||||
size = 10;
|
||||
|
||||
@ -79,7 +79,7 @@ describe('Bloom', function() {
|
||||
var j = i;
|
||||
do {
|
||||
var str = 'foobar' + j;
|
||||
assert(filter.test(str, 'ascii') === true, str + i);
|
||||
assert(filter.test(str, 'ascii') === true, str);
|
||||
assert(filter.test(str + '-', 'ascii') === false, str);
|
||||
} while (j--);
|
||||
}
|
||||
@ -99,7 +99,7 @@ describe('Bloom', function() {
|
||||
var j = i;
|
||||
do {
|
||||
var str = 'foobar' + j;
|
||||
assert(filter.test(str, 'ascii') === true, str + ' GOOD');
|
||||
assert(filter.test(str, 'ascii') === true, str);
|
||||
assert(filter.test(str + '-', 'ascii') === false, str);
|
||||
} while (j-- > 25);
|
||||
assert(filter.test('foobar 24', 'ascii') === false);
|
||||
@ -110,7 +110,7 @@ describe('Bloom', function() {
|
||||
var j = i;
|
||||
do {
|
||||
var str = 'foobar' + j;
|
||||
assert(filter.test(str, 'ascii') === true, str + ' GOOD');
|
||||
assert(filter.test(str, 'ascii') === true, str);
|
||||
assert(filter.test(str + '-', 'ascii') === false, str);
|
||||
} while (j-- > 50);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user