From 292e077550b5804cfbae09e5929c4c2fa82716bf Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 20 May 2016 16:21:33 -0700 Subject: [PATCH] optimization for rolling filter. --- lib/bcoin/bloom.js | 17 +++++++++++++---- lib/bcoin/pool.js | 2 +- test/bloom-test.js | 6 +++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/bcoin/bloom.js b/lib/bcoin/bloom.js index fc86c1d5..01e22a5c 100644 --- a/lib/bcoin/bloom.js +++ b/lib/bcoin/bloom.js @@ -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; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 44cc8bd4..f3723e1a 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -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; diff --git a/test/bloom-test.js b/test/bloom-test.js index a2bc211e..4691cc55 100644 --- a/test/bloom-test.js +++ b/test/bloom-test.js @@ -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); }