1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 15:32:28 +02:00

Enable origin-hit coalescing optimisation for modifier filters

Related commit:
- b265f2644d

The optimization in the commit above was meant to
improve the performance of lookup operations of
modifier filters, but I forgot to enable the
optimisation for that class of filters.

This means this commit brings another significant
performance gain on top of the previous commit, as
shown by the built-in benchmark.

Additionally a few minor code rearrangements.
This commit is contained in:
Raymond Hill 2020-11-06 18:24:46 -05:00
parent c38d7f5bf9
commit 1d679143d2
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -345,6 +345,7 @@ let filterClassIdGenerator = 0;
const registerFilterClass = function(ctor) { const registerFilterClass = function(ctor) {
const fid = filterClassIdGenerator++; const fid = filterClassIdGenerator++;
ctor.fid = ctor.prototype.fid = fid; ctor.fid = ctor.prototype.fid = fid;
ctor.fidstr = `${fid}`;
filterClasses[fid] = ctor; filterClasses[fid] = ctor;
}; };
@ -370,7 +371,7 @@ const filterUnitFromCompiled = function(args) {
if ( keygen === undefined ) { if ( keygen === undefined ) {
return filterUnits.push(ctor.fromCompiled(args)) - 1; return filterUnits.push(ctor.fromCompiled(args)) - 1;
} }
let key = `${ctor.fid}`; let key = ctor.fidstr;
const keyargs = keygen(args); const keyargs = keygen(args);
if ( keyargs !== undefined ) { if ( keyargs !== undefined ) {
key += `\t${keyargs}`; key += `\t${keyargs}`;
@ -383,9 +384,7 @@ const filterUnitFromCompiled = function(args) {
return iunit; return iunit;
}; };
const filterFromSelfie = function(args) { const filterFromSelfie = args => filterClasses[args[0]].fromSelfie(args);
return filterClasses[args[0]].fromSelfie(args);
};
/******************************************************************************/ /******************************************************************************/
@ -1618,20 +1617,17 @@ const FilterCollection = class {
return [ ctor.fid, fdata ]; return [ ctor.fid, fdata ];
} }
static fromCompiled(ctor, args) { static fromCompiled(args, bucket) {
let iprev = 0, i0 = 0; const units = args[1];
const n = args[1].length; const n = units.length;
for ( let i = 0; i < n; i++ ) { let iunit, inext = 0;
const iunit = filterUnitFromCompiled(args[1][i]); let i = n;
const inext = filterSequenceAdd(iunit, 0); while ( i-- ) {
if ( iprev !== 0 ) { iunit = filterUnitFromCompiled(units[i]);
filterSequences[iprev+1] = inext; inext = filterSequenceAdd(iunit, inext);
} else {
i0 = inext;
}
iprev = inext;
} }
return new ctor(i0, args[1].length); bucket.i = inext;
return bucket;
} }
static fromSelfie(args, bucket) { static fromSelfie(args, bucket) {
@ -1655,11 +1651,11 @@ const FilterCompositeAny = class extends FilterCollection {
} }
static compile(fdata) { static compile(fdata) {
return FilterCollection.compile(FilterCompositeAny, fdata); return super.compile(FilterCompositeAny, fdata);
} }
static fromCompiled(args) { static fromCompiled(args) {
return FilterCollection.fromCompiled(FilterCompositeAny, args); return super.fromCompiled(args, new FilterCompositeAny());
} }
static fromSelfie(args, bucket) { static fromSelfie(args, bucket) {
@ -1738,11 +1734,11 @@ const FilterCompositeAll = class extends FilterCollection {
} }
static compile(fdata) { static compile(fdata) {
return FilterCollection.compile(FilterCompositeAll, fdata); return super.compile(FilterCompositeAll, fdata);
} }
static fromCompiled(args) { static fromCompiled(args) {
return FilterCollection.fromCompiled(FilterCompositeAll, args); return super.fromCompiled(args, new FilterCompositeAll());
} }
static fromSelfie(args, bucket) { static fromSelfie(args, bucket) {
@ -2097,15 +2093,15 @@ const FilterBucket = class extends FilterCollection {
return super.fromSelfie(args[2], bucket); return super.fromSelfie(args[2], bucket);
} }
optimize() { optimize(optimizeBits = 0b11) {
if ( this.n >= 3 ) { if ( this.n >= 3 && (optimizeBits & 0b01) !== 0 ) {
const f = this.optimizePatternTests(); const f = this.optimizePatternTests();
if ( f !== undefined ) { if ( f !== undefined ) {
if ( this.i === 0 ) { return f; } if ( this.i === 0 ) { return f; }
this.unshift(filterUnitFromFilter(f)); this.unshift(filterUnitFromFilter(f));
} }
} }
if ( this.n >= 10 ) { if ( this.n >= 10 && (optimizeBits & 0b10) !== 0 ) {
const f = this.optimizeOriginHitTests(); const f = this.optimizeOriginHitTests();
if ( f !== undefined ) { if ( f !== undefined ) {
if ( this.i === 0 ) { return f; } if ( this.i === 0 ) { return f; }
@ -2973,11 +2969,13 @@ FilterContainer.prototype.freeze = function() {
// Skip modify realm, since bidi-trie does not (yet) support matchAll(). // Skip modify realm, since bidi-trie does not (yet) support matchAll().
for ( const [ catBits, bucket ] of this.categories ) { for ( const [ catBits, bucket ] of this.categories ) {
if ( (catBits & ActionBitsMask) === ModifyAction ) { continue; } const optimizeBits = (catBits & ActionBitsMask) === ModifyAction
? 0b10
: 0b11;
for ( const iunit of bucket.values() ) { for ( const iunit of bucket.values() ) {
const f = units[iunit]; const f = units[iunit];
if ( f instanceof FilterBucket === false ) { continue; } if ( f instanceof FilterBucket === false ) { continue; }
const g = f.optimize(); const g = f.optimize(optimizeBits);
if ( g !== undefined ) { if ( g !== undefined ) {
units[iunit] = g; units[iunit] = g;
} }