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) {
var i, j, hash, bits, pos, pos1, pos2, bit, oct;
var m1, m2, v1, v2, mhi, mlo;
if (typeof val === 'string')
val = new Buffer(val, enc);
@ -266,19 +267,22 @@ RollingFilter.prototype.add = function add(val, enc) {
if (this.generation === 4)
this.generation = 1;
m1 = (this.generation & 1) * 0xffffffff;
m2 = (this.generation >>> 1) * 0xffffffff;
for (i = 0; i < this.items; i += 2) {
pos1 = i * 8;
pos2 = (i + 1) * 8;
for (j = 0; j < 64; j++) {
bit = j % 8;
oct = (j - bit) / 8;
bits = (this.filter[pos1 + oct] >>> bit) & 1;
bits |= ((this.filter[pos2 + oct] >>> bit) & 1) << 1;
if (bits === this.generation) {
this.filter[pos1 + oct] &= ~((this.generation & 1) << bit);
this.filter[pos2 + oct] &= ~((this.generation >>> 1) << bit);
}
}
v1 = read(this.filter, pos1);
v2 = read(this.filter, pos2);
mhi = (v1.hi ^ m1) | (v2.hi ^ m2);
mlo = (v1.lo ^ m1) | (v2.lo ^ m2);
v1.hi &= mhi;
v1.lo &= mlo;
v2.hi &= mhi;
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));
}
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
*/