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

Code review of session filters commit

Related commit:
- 59c9a34d34
This commit is contained in:
Raymond Hill 2019-09-25 11:21:34 -04:00
parent 28aee88a7b
commit 46d36cb0b0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
5 changed files with 76 additions and 63 deletions

View File

@ -204,6 +204,9 @@ const FilterContainer = function() {
// specific filters
this.specificFilters = new µb.staticExtFilteringEngine.HostnameBasedDB(2);
// temporary filters
this.sessionFilterDB = new µb.staticExtFilteringEngine.SessionDB();
// low generic cosmetic filters, organized by id/class then simple/complex.
this.lowlyGeneric = Object.create(null);
this.lowlyGeneric.id = {
@ -827,7 +830,7 @@ FilterContainer.prototype.randomAlphaToken = function() {
/******************************************************************************/
FilterContainer.prototype.getSession = function() {
return this.specificFilters.session;
return this.sessionFilterDB;
};
/******************************************************************************/
@ -997,7 +1000,9 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
}
// Retrieve temporary filters
this.specificFilters.session.retrieve([ dummySet, exceptionSet ]);
if ( this.sessionFilterDB.isNotEmpty ) {
this.sessionFilterDB.retrieve([ null, exceptionSet ]);
}
// Retrieve filters with a non-empty hostname
this.specificFilters.retrieve(

View File

@ -28,10 +28,11 @@
const pselectors = new Map();
const duplicates = new Set();
let filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(2),
acceptedCount = 0,
discardedCount = 0,
docRegister;
const filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(2);
const sessionFilterDB = new µb.staticExtFilteringEngine.SessionDB();
let acceptedCount = 0;
let discardedCount = 0;
let docRegister;
const api = {
get acceptedCount() {
@ -335,7 +336,7 @@
};
api.getSession = function() {
return filterDB.session;
return sessionFilterDB;
};
api.retrieve = function(details) {
@ -354,7 +355,9 @@
const procedurals = new Set();
const exceptions = new Set();
filterDB.session.retrieve([ new Set(), exceptions ]);
if ( sessionFilterDB.isNotEmpty ) {
sessionFilterDB.retrieve([ null, exceptions ]);
}
filterDB.retrieve(
hostname,
[ plains, exceptions, procedurals, exceptions ]
@ -415,7 +418,7 @@
};
api.fromSelfie = function(selfie) {
filterDB = new µb.staticExtFilteringEngine.HostnameBasedDB(2, selfie);
filterDB.fromSelfie(selfie);
pselectors.clear();
};

View File

@ -1451,14 +1451,11 @@ const reloadTab = function(ev) {
span.className = 'filter';
span.textContent = selector;
fragment.appendChild(span);
let isTemporaryException = false;
if ( match[0] === '#@#' ) {
isTemporaryException = await messaging.send('loggerUI', {
what: 'hasTemporaryException',
filter,
});
receiver.classList.toggle('exceptored', isTemporaryException);
}
const isTemporaryException = await messaging.send('loggerUI', {
what: 'hasTemporaryException',
filter,
});
receiver.classList.toggle('exceptored', isTemporaryException);
if ( match[0] === '##' || isTemporaryException ) {
receiver.children[2].style.visibility = '';
}

View File

@ -24,14 +24,15 @@
/******************************************************************************/
µBlock.scriptletFilteringEngine = (function() {
const µb = µBlock,
duplicates = new Set(),
scriptletCache = new µb.MRUCache(32),
reEscapeScriptArg = /[\\'"]/g;
const µb = µBlock;
const duplicates = new Set();
const scriptletCache = new µb.MRUCache(32);
const reEscapeScriptArg = /[\\'"]/g;
let acceptedCount = 0,
discardedCount = 0,
scriptletDB = new µb.staticExtFilteringEngine.HostnameBasedDB(1);
const scriptletDB = new µb.staticExtFilteringEngine.HostnameBasedDB(1);
const sessionScriptletDB = new µb.staticExtFilteringEngine.SessionDB();
let acceptedCount = 0;
let discardedCount = 0;
const api = {
get acceptedCount() {
@ -343,7 +344,7 @@
};
api.getSession = function() {
return scriptletDB.session;
return sessionScriptletDB;
};
const scriptlets$ = new Set();
@ -371,7 +372,9 @@
scriptlets$.clear();
exceptions$.clear();
scriptletDB.session.retrieve([ scriptlets$, exceptions$ ]);
if ( sessionScriptletDB.isNotEmpty ) {
sessionScriptletDB.retrieve([ null, exceptions$ ]);
}
scriptletDB.retrieve(hostname, [ scriptlets$, exceptions$ ]);
if ( request.entity !== '' ) {
scriptletDB.retrieve(
@ -464,7 +467,7 @@
};
api.fromSelfie = function(selfie) {
scriptletDB = new µb.staticExtFilteringEngine.HostnameBasedDB(1, selfie);
scriptletDB.fromSelfie(selfie);
};
api.benchmark = async function() {

View File

@ -511,7 +511,6 @@
//--------------------------------------------------------------------------
api.HostnameBasedDB = class {
constructor(nBits, selfie = undefined) {
this.nBits = nBits;
this.timer = undefined;
@ -524,41 +523,6 @@
// Array of strings (selectors and pseudo-selectors)
this.strSlots = [];
this.size = 0;
// Temporary set
this.session = {
collection: new Map(),
add: function(bits, s) {
const bucket = this.collection.get(bits);
if ( bucket === undefined ) {
this.collection.set(bits, new Set([ s ]));
} else {
bucket.add(s);
}
},
remove: function(bits, s) {
const bucket = this.collection.get(bits);
if ( bucket === undefined ) { return; }
bucket.delete(s);
if ( bucket.size !== 0 ) { return; }
this.collection.delete(bits);
},
retrieve(out) {
const mask = out.length - 1;
for ( const [ bits, bucket ] of this.collection ) {
for ( const s of bucket ) {
out[bits & mask].add(s);
}
}
},
has(bits, s) {
const selectors = this.collection.get(bits);
return selectors !== undefined && selectors.has(s);
},
clear() {
this.collection.clear();
},
};
if ( selfie !== undefined ) {
this.fromSelfie(selfie);
}
@ -675,6 +639,47 @@
}
};
api.SessionDB = class {
constructor() {
this.db = new Map();
}
add(bits, s) {
const bucket = this.db.get(bits);
if ( bucket === undefined ) {
this.db.set(bits, new Set([ s ]));
} else {
bucket.add(s);
}
}
remove(bits, s) {
const bucket = this.db.get(bits);
if ( bucket === undefined ) { return; }
bucket.delete(s);
if ( bucket.size !== 0 ) { return; }
this.db.delete(bits);
}
retrieve(out) {
const mask = out.length - 1;
for ( const [ bits, bucket ] of this.db ) {
const i = bits & mask;
if ( out[i] instanceof Object === false ) { continue; }
for ( const s of bucket ) {
out[i].add(s);
}
}
}
has(bits, s) {
const selectors = this.db.get(bits);
return selectors !== undefined && selectors.has(s);
}
clear() {
this.db.clear();
}
get isNotEmpty() {
return this.db.size !== 0;
}
};
//--------------------------------------------------------------------------
// Public methods
//--------------------------------------------------------------------------