optimization for rolling filter.

This commit is contained in:
Christopher Jeffrey 2016-05-20 16:21:33 -07:00
parent 14b2b528f3
commit 292e077550
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 17 additions and 8 deletions

View File

@ -216,14 +216,17 @@ function RollingFilter(items, rate) {
this.n = Math.max(1, Math.min(Math.round(logRate / Math.log(0.5)), 50)); this.n = Math.max(1, Math.min(Math.round(logRate / Math.log(0.5)), 50));
this.limit = (items + 1) / 2 | 0; this.limit = (items + 1) / 2 | 0;
max = this.limit * 3; max = this.limit * 3;
this.size = -1 * this.n * max / Math.log(1.0 - Math.exp(logRate / this.n)); this.size = -1 * this.n * max / Math.log(1.0 - Math.exp(logRate / this.n));
this.size = Math.ceil(this.size); this.size = Math.ceil(this.size);
this.items = ((this.size + 63) / 64 | 0) << 1; this.items = ((this.size + 63) / 64 | 0) << 1;
this.filter = new Buffer(this.items * 8);
this.tweak = (Math.random() * 0x100000000) >>> 0; 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() { RollingFilter.prototype.reset = function reset() {
if (this.entries === 0)
return;
this.entries = 0; this.entries = 0;
this.generation = 1; this.generation = 1;
this.filter.fill(0); this.filter.fill(0);
@ -318,6 +324,9 @@ RollingFilter.prototype.add = function add(val, enc) {
RollingFilter.prototype.test = function test(val, enc) { RollingFilter.prototype.test = function test(val, enc) {
var i, hash, bits, pos, pos1, pos2, bit, oct; var i, hash, bits, pos, pos1, pos2, bit, oct;
if (this.entries === 0)
return false;
if (typeof val === 'string') if (typeof val === 'string')
val = new Buffer(val, enc); val = new Buffer(val, enc);
@ -333,8 +342,8 @@ RollingFilter.prototype.test = function test(val, enc) {
pos1 += oct; pos1 += oct;
pos2 += oct; pos2 += oct;
bits = (this.filter[pos1] >> bit) & 1; bits = (this.filter[pos1] >>> bit) & 1;
bits |= (this.filter[pos2] >> bit) & 1; bits |= (this.filter[pos2] >>> bit) & 1;
if (bits === 0) if (bits === 0)
return false; return false;

View File

@ -1647,7 +1647,7 @@ Pool.prototype._sendRequests = function _sendRequests(peer) {
size = 500; size = 500;
else if (this.chain.height <= 150000) else if (this.chain.height <= 150000)
size = 250; size = 250;
else if (this.chain.height <= 170000) else if (this.chain.height <= 250000)
size = 20; size = 20;
else else
size = 10; size = 10;

View File

@ -79,7 +79,7 @@ describe('Bloom', function() {
var j = i; var j = i;
do { do {
var str = 'foobar' + j; 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); assert(filter.test(str + '-', 'ascii') === false, str);
} while (j--); } while (j--);
} }
@ -99,7 +99,7 @@ describe('Bloom', function() {
var j = i; var j = i;
do { do {
var str = 'foobar' + j; 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); assert(filter.test(str + '-', 'ascii') === false, str);
} while (j-- > 25); } while (j-- > 25);
assert(filter.test('foobar 24', 'ascii') === false); assert(filter.test('foobar 24', 'ascii') === false);
@ -110,7 +110,7 @@ describe('Bloom', function() {
var j = i; var j = i;
do { do {
var str = 'foobar' + j; 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); assert(filter.test(str + '-', 'ascii') === false, str);
} while (j-- > 50); } while (j-- > 50);
} }