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

Convert filterUnits into a const variable

Making filterUnits constant allows to no longer
mind how the array is accessed in loops.
This commit is contained in:
Raymond Hill 2020-11-08 10:30:47 -05:00
parent dd8cce48a1
commit 96bfe3c9a7
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -280,9 +280,10 @@ const isSeparatorChar = c => (charClassMap[c] & CHAR_CLASS_SEPARATOR) !== 0;
/******************************************************************************/ /******************************************************************************/
let filterUnits = [ null ]; const filterUnits = [ null ];
let filterSequences = new Uint32Array(131072); // Initial size should be enough for default set of filter lists.
let filterSequences = new Uint32Array(163840);
let filterSequenceWritePtr = 3; let filterSequenceWritePtr = 3;
const filterSequenceAdd = function(a, b) { const filterSequenceAdd = function(a, b) {
@ -1644,10 +1645,9 @@ const FilterCollection = class {
const FilterCompositeAny = class extends FilterCollection { const FilterCompositeAny = class extends FilterCollection {
match() { match() {
const sequences = filterSequences; const sequences = filterSequences;
const units = filterUnits;
let i = this.i; let i = this.i;
while ( i !== 0 ) { while ( i !== 0 ) {
if ( units[sequences[i+0]].match() ) { return true; } if ( filterUnits[sequences[i+0]].match() ) { return true; }
i = sequences[i+1]; i = sequences[i+1];
} }
return false; return false;
@ -1676,10 +1676,11 @@ registerFilterClass(FilterCompositeAny);
const FilterCompositeAll = class extends FilterCollection { const FilterCompositeAll = class extends FilterCollection {
match() { match() {
const sequences = filterSequences; const sequences = filterSequences;
const units = filterUnits;
let i = this.i; let i = this.i;
while ( i !== 0 ) { while ( i !== 0 ) {
if ( units[sequences[i+0]].match() !== true ) { return false; } if ( filterUnits[sequences[i+0]].match() !== true ) {
return false;
}
i = sequences[i+1]; i = sequences[i+1];
} }
return true; return true;
@ -2048,10 +2049,9 @@ const FilterBucket = class extends FilterCollection {
match() { match() {
const sequences = filterSequences; const sequences = filterSequences;
const units = filterUnits;
let i = this.i; let i = this.i;
while ( i !== 0 ) { while ( i !== 0 ) {
if ( units[sequences[i+0]].match() ) { if ( filterUnits[sequences[i+0]].match() ) {
this.$matchedUnit = sequences[i+0]; this.$matchedUnit = sequences[i+0];
return true; return true;
} }
@ -2062,11 +2062,10 @@ const FilterBucket = class extends FilterCollection {
matchAndFetchModifiers(env) { matchAndFetchModifiers(env) {
const sequences = filterSequences; const sequences = filterSequences;
const units = filterUnits;
let i = this.i; let i = this.i;
while ( i !== 0 ) { while ( i !== 0 ) {
env.iunit = sequences[i+0]; env.iunit = sequences[i+0];
units[env.iunit].matchAndFetchModifiers(env); filterUnits[env.iunit].matchAndFetchModifiers(env);
i = sequences[i+1]; i = sequences[i+1];
} }
} }
@ -2114,12 +2113,11 @@ const FilterBucket = class extends FilterCollection {
} }
optimizePatternTests() { optimizePatternTests() {
const units = filterUnits;
const sequences = filterSequences; const sequences = filterSequences;
let n = 0; let n = 0;
let i = this.i; let i = this.i;
do { do {
if ( units[sequences[i+0]].isBidiTrieable ) { n += 1; } if ( filterUnits[sequences[i+0]].isBidiTrieable ) { n += 1; }
i = sequences[i+1]; i = sequences[i+1];
} while ( i !== 0 && n < 3 ); } while ( i !== 0 && n < 3 );
if ( n < 3 ) { return; } if ( n < 3 ) { return; }
@ -2129,7 +2127,7 @@ const FilterBucket = class extends FilterCollection {
for (;;) { for (;;) {
const iunit = sequences[i+0]; const iunit = sequences[i+0];
const inext = sequences[i+1]; const inext = sequences[i+1];
if ( units[iunit].isBidiTrieable ) { if ( filterUnits[iunit].isBidiTrieable ) {
ftrie.addUnitToTrie(iunit); ftrie.addUnitToTrie(iunit);
if ( iprev !== 0 ) { if ( iprev !== 0 ) {
sequences[iprev+1] = inext; sequences[iprev+1] = inext;
@ -2147,10 +2145,9 @@ const FilterBucket = class extends FilterCollection {
} }
optimizeOriginHitTests() { optimizeOriginHitTests() {
const units = filterUnits;
let candidateCount = -10; let candidateCount = -10;
const shouldPreTest = this.forEach(iunit => { const shouldPreTest = this.forEach(iunit => {
if ( units[iunit].hasOriginHit !== true ) { return; } if ( filterUnits[iunit].hasOriginHit !== true ) { return; }
candidateCount += 1; candidateCount += 1;
if ( candidateCount === 0 ) { return true; } if ( candidateCount === 0 ) { return true; }
}); });
@ -2163,7 +2160,7 @@ const FilterBucket = class extends FilterCollection {
for (;;) { for (;;) {
const iunit = sequences[i+0]; const iunit = sequences[i+0];
const inext = sequences[i+1]; const inext = sequences[i+1];
const f = units[iunit]; const f = filterUnits[iunit];
if ( f.hasOriginHit ) { if ( f.hasOriginHit ) {
domainOpts.push(f.domainOpt); domainOpts.push(f.domainOpt);
bucket.unshift(iunit); bucket.unshift(iunit);
@ -2869,7 +2866,7 @@ FilterContainer.prototype.reset = function() {
bidiTrie.reset(); bidiTrie.reset();
filterArgsToUnit.clear(); filterArgsToUnit.clear();
filterUnits = filterUnits.slice(0, FILTER_UNITS_MIN); filterUnits.length = FILTER_UNITS_MIN;
filterSequenceWritePtr = FILTER_SEQUENCES_MIN; filterSequenceWritePtr = FILTER_SEQUENCES_MIN;
// Cancel potentially pending optimization run. // Cancel potentially pending optimization run.
@ -2889,7 +2886,6 @@ FilterContainer.prototype.reset = function() {
FilterContainer.prototype.freeze = function() { FilterContainer.prototype.freeze = function() {
const filterBucketId = FilterBucket.fid; const filterBucketId = FilterBucket.fid;
const unserialize = µb.CompiledLineIO.unserialize; const unserialize = µb.CompiledLineIO.unserialize;
const units = filterUnits;
const t0 = Date.now(); const t0 = Date.now();
@ -2918,7 +2914,7 @@ FilterContainer.prototype.freeze = function() {
iunit = filterFromCtor(FilterHostnameDict); iunit = filterFromCtor(FilterHostnameDict);
bucket.set(this.dotTokenHash, iunit); bucket.set(this.dotTokenHash, iunit);
} }
units[iunit].add(fdata); filterUnits[iunit].add(fdata);
continue; continue;
} }
@ -2927,7 +2923,7 @@ FilterContainer.prototype.freeze = function() {
iunit = filterFromCtor(FilterJustOrigin); iunit = filterFromCtor(FilterJustOrigin);
bucket.set(this.anyTokenHash, iunit); bucket.set(this.anyTokenHash, iunit);
} }
units[iunit].add(fdata); filterUnits[iunit].add(fdata);
continue; continue;
} }
@ -2936,7 +2932,7 @@ FilterContainer.prototype.freeze = function() {
iunit = filterFromCtor(FilterHTTPSJustOrigin); iunit = filterFromCtor(FilterHTTPSJustOrigin);
bucket.set(this.anyHTTPSTokenHash, iunit); bucket.set(this.anyHTTPSTokenHash, iunit);
} }
units[iunit].add(fdata); filterUnits[iunit].add(fdata);
continue; continue;
} }
@ -2945,7 +2941,7 @@ FilterContainer.prototype.freeze = function() {
iunit = filterFromCtor(FilterHTTPJustOrigin); iunit = filterFromCtor(FilterHTTPJustOrigin);
bucket.set(this.anyHTTPTokenHash, iunit); bucket.set(this.anyHTTPTokenHash, iunit);
} }
units[iunit].add(fdata); filterUnits[iunit].add(fdata);
continue; continue;
} }
@ -2957,13 +2953,13 @@ FilterContainer.prototype.freeze = function() {
bucket.set(tokenHash, inewunit); bucket.set(tokenHash, inewunit);
continue; continue;
} }
let f = units[iunit]; let f = filterUnits[iunit];
if ( f.fid === filterBucketId ) { if ( f.fid === filterBucketId ) {
f.unshift(inewunit); f.unshift(inewunit);
continue; continue;
} }
const ibucketunit = filterFromCtor(FilterBucket); const ibucketunit = filterFromCtor(FilterBucket);
f = units[ibucketunit]; f = filterUnits[ibucketunit];
f.unshift(iunit); f.unshift(iunit);
f.unshift(inewunit); f.unshift(inewunit);
bucket.set(tokenHash, ibucketunit); bucket.set(tokenHash, ibucketunit);
@ -2980,7 +2976,7 @@ FilterContainer.prototype.freeze = function() {
this.optimizeTimerId = undefined; this.optimizeTimerId = undefined;
for ( const [ catBits, bucket ] of this.categories ) { for ( const [ catBits, bucket ] of this.categories ) {
for ( const [ th, iunit ] of bucket ) { for ( const [ th, iunit ] of bucket ) {
const f = units[iunit]; const f = filterUnits[iunit];
if ( f instanceof FilterBucket === false ) { continue; } if ( f instanceof FilterBucket === false ) { continue; }
const optimizeBits = const optimizeBits =
(th === this.noTokenHash) || (th === this.noTokenHash) ||
@ -2989,7 +2985,7 @@ FilterContainer.prototype.freeze = function() {
: 0b01; : 0b01;
const g = f.optimize(optimizeBits); const g = f.optimize(optimizeBits);
if ( g !== undefined ) { if ( g !== undefined ) {
units[iunit] = g; filterUnits[iunit] = g;
} }
} }
} }
@ -3100,9 +3096,13 @@ FilterContainer.prototype.fromSelfie = function(path) {
this.blockFilterCount = selfie.blockFilterCount; this.blockFilterCount = selfie.blockFilterCount;
this.discardedCount = selfie.discardedCount; this.discardedCount = selfie.discardedCount;
urlTokenizer.fromSelfie(selfie.urlTokenizer); urlTokenizer.fromSelfie(selfie.urlTokenizer);
filterUnits = selfie.filterUnits.map(f => {
f !== null ? filterFromSelfie(f) : null const fselfies = selfie.filterUnits;
); for ( let i = 0, n = fselfies.length; i < n; i++ ) {
const f = fselfies[i];
filterUnits[i] = f !== null ? filterFromSelfie(f) : null;
}
}
for ( const [ catBits, bucket ] of selfie.categories ) { for ( const [ catBits, bucket ] of selfie.categories ) {
this.categories.set(catBits, new Map(bucket)); this.categories.set(catBits, new Map(bucket));
} }
@ -3363,7 +3363,6 @@ FilterContainer.prototype.matchAndFetchModifiers = function(
results, results,
}; };
const units = filterUnits;
const tokenHashes = urlTokenizer.getTokens(bidiTrie); const tokenHashes = urlTokenizer.getTokens(bidiTrie);
let i = 0; let i = 0;
for (;;) { for (;;) {
@ -3375,28 +3374,28 @@ FilterContainer.prototype.matchAndFetchModifiers = function(
const iunit = bucket00.get(th); const iunit = bucket00.get(th);
if ( iunit !== undefined ) { if ( iunit !== undefined ) {
env.bits = catBits00; env.iunit = iunit; env.bits = catBits00; env.iunit = iunit;
units[iunit].matchAndFetchModifiers(env); filterUnits[iunit].matchAndFetchModifiers(env);
} }
} }
if ( bucket01 !== undefined ) { if ( bucket01 !== undefined ) {
const iunit = bucket01.get(th); const iunit = bucket01.get(th);
if ( iunit !== undefined ) { if ( iunit !== undefined ) {
env.bits = catBits01; env.iunit = iunit; env.bits = catBits01; env.iunit = iunit;
units[iunit].matchAndFetchModifiers(env); filterUnits[iunit].matchAndFetchModifiers(env);
} }
} }
if ( bucket10 !== undefined ) { if ( bucket10 !== undefined ) {
const iunit = bucket10.get(th); const iunit = bucket10.get(th);
if ( iunit !== undefined ) { if ( iunit !== undefined ) {
env.bits = catBits10; env.iunit = iunit; env.bits = catBits10; env.iunit = iunit;
units[iunit].matchAndFetchModifiers(env); filterUnits[iunit].matchAndFetchModifiers(env);
} }
} }
if ( bucket11 !== undefined ) { if ( bucket11 !== undefined ) {
const iunit = bucket11.get(th); const iunit = bucket11.get(th);
if ( iunit !== undefined ) { if ( iunit !== undefined ) {
env.bits = catBits11; env.iunit = iunit; env.bits = catBits11; env.iunit = iunit;
units[iunit].matchAndFetchModifiers(env); filterUnits[iunit].matchAndFetchModifiers(env);
} }
} }
i += 2; i += 2;
@ -3520,7 +3519,6 @@ FilterContainer.prototype.realmMatchString = function(
return false; return false;
} }
const units = filterUnits;
let catBits = 0, iunit = 0; let catBits = 0, iunit = 0;
// Pure hostname-based filters // Pure hostname-based filters
@ -3528,25 +3526,25 @@ FilterContainer.prototype.realmMatchString = function(
if ( if (
(bucket00 !== undefined) && (bucket00 !== undefined) &&
(iunit = bucket00.get(tokenHash) || 0) !== 0 && (iunit = bucket00.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits00; catBits = catBits00;
} else if ( } else if (
(bucket01 !== undefined) && (bucket01 !== undefined) &&
(iunit = bucket01.get(tokenHash) || 0) !== 0 && (iunit = bucket01.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits01; catBits = catBits01;
} else if ( } else if (
(bucket10 !== undefined) && (bucket10 !== undefined) &&
(iunit = bucket10.get(tokenHash) || 0) !== 0 && (iunit = bucket10.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits10; catBits = catBits10;
} else if ( } else if (
(bucket11 !== undefined) && (bucket11 !== undefined) &&
(iunit = bucket11.get(tokenHash) || 0) !== 0 && (iunit = bucket11.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits11; catBits = catBits11;
} }
@ -3561,7 +3559,7 @@ FilterContainer.prototype.realmMatchString = function(
if ( if (
(bucket00 !== undefined) && (bucket00 !== undefined) &&
(iunit = bucket00.get(tokenHash) || 0) !== 0 && (iunit = bucket00.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits00; catBits = catBits00;
break; break;
@ -3569,7 +3567,7 @@ FilterContainer.prototype.realmMatchString = function(
if ( if (
(bucket01 !== undefined) && (bucket01 !== undefined) &&
(iunit = bucket01.get(tokenHash) || 0) !== 0 && (iunit = bucket01.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits01; catBits = catBits01;
break; break;
@ -3577,7 +3575,7 @@ FilterContainer.prototype.realmMatchString = function(
if ( if (
(bucket10 !== undefined) && (bucket10 !== undefined) &&
(iunit = bucket10.get(tokenHash) || 0) !== 0 && (iunit = bucket10.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits10; catBits = catBits10;
break; break;
@ -3585,7 +3583,7 @@ FilterContainer.prototype.realmMatchString = function(
if ( if (
(bucket11 !== undefined) && (bucket11 !== undefined) &&
(iunit = bucket11.get(tokenHash) || 0) !== 0 && (iunit = bucket11.get(tokenHash) || 0) !== 0 &&
(units[iunit].match() === true) (filterUnits[iunit].match() === true)
) { ) {
catBits = catBits11; catBits = catBits11;
break; break;
@ -4003,12 +4001,11 @@ FilterContainer.prototype.test = function(docURL, type, url) {
*/ */
FilterContainer.prototype.bucketHistogram = function() { FilterContainer.prototype.bucketHistogram = function() {
const units = filterUnits;
const results = []; const results = [];
for ( const [ bits, category ] of this.categories ) { for ( const [ bits, category ] of this.categories ) {
for ( const [ th, iunit ] of category ) { for ( const [ th, iunit ] of category ) {
const token = urlTokenizer.stringFromTokenHash(th); const token = urlTokenizer.stringFromTokenHash(th);
const f = units[iunit]; const f = filterUnits[iunit];
if ( f instanceof FilterBucket ) { if ( f instanceof FilterBucket ) {
results.push({ bits: bits.toString(16), token, size: f.size, f }); results.push({ bits: bits.toString(16), token, size: f.size, f });
continue; continue;