optimize new generations with rolling filter.

This commit is contained in:
Christopher Jeffrey 2016-05-20 16:03:07 -07:00
parent 381c1ca1f1
commit d8800632ea
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -255,6 +255,7 @@ RollingFilter.prototype.reset = function reset() {
RollingFilter.prototype.add = function add(val, enc) { RollingFilter.prototype.add = function add(val, enc) {
var i, j, hash, bits, pos, pos1, pos2, bit, oct; var i, j, hash, bits, pos, pos1, pos2, bit, oct;
var m1, m2, v1, v2, mhi, mlo;
if (typeof val === 'string') if (typeof val === 'string')
val = new Buffer(val, enc); val = new Buffer(val, enc);
@ -266,19 +267,22 @@ RollingFilter.prototype.add = function add(val, enc) {
if (this.generation === 4) if (this.generation === 4)
this.generation = 1; this.generation = 1;
m1 = (this.generation & 1) * 0xffffffff;
m2 = (this.generation >>> 1) * 0xffffffff;
for (i = 0; i < this.items; i += 2) { for (i = 0; i < this.items; i += 2) {
pos1 = i * 8; pos1 = i * 8;
pos2 = (i + 1) * 8; pos2 = (i + 1) * 8;
for (j = 0; j < 64; j++) { v1 = read(this.filter, pos1);
bit = j % 8; v2 = read(this.filter, pos2);
oct = (j - bit) / 8; mhi = (v1.hi ^ m1) | (v2.hi ^ m2);
bits = (this.filter[pos1 + oct] >>> bit) & 1; mlo = (v1.lo ^ m1) | (v2.lo ^ m2);
bits |= ((this.filter[pos2 + oct] >>> bit) & 1) << 1; v1.hi &= mhi;
if (bits === this.generation) { v1.lo &= mlo;
this.filter[pos1 + oct] &= ~((this.generation & 1) << bit); v2.hi &= mhi;
this.filter[pos2 + oct] &= ~((this.generation >>> 1) << bit); v2.lo &= mlo;
} write(this.filter, v1, pos1);
} write(this.filter, v2, pos2);
} }
} }
@ -459,6 +463,18 @@ function rotl32(w, b) {
return (w << b) | (w >>> (32 - b)); return (w << b) | (w >>> (32 - b));
} }
function read(data, off) {
return {
hi: data.readUInt32LE(off + 4, true),
lo: data.readUInt32LE(off, true)
};
}
function write(data, value, off) {
data.writeUInt32LE(value.hi, off + 4, true);
data.writeUInt32LE(value.lo, off, true);
}
/* /*
* Expose * Expose
*/ */