Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/elemHide.js

Issue 29773570: Issue 6652 - Implement fast selector lookups for unknown domains (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Merge getConditionalGenericSelectors into getConditionalSelectorsForDomain Created May 9, 2018, 9:31 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHide.js
===================================================================
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -22,17 +22,17 @@
*/
const {ElemHideException} = require("./filterClasses");
const {FilterNotifier} = require("./filterNotifier");
/**
* Lookup table, active flag, by filter by domain.
* (Only contains filters that aren't unconditionally matched for all domains.)
- * @type {Map.<string,Map.<Filter,boolean>>}
+ * @type {Map.<string,?Map.<Filter,boolean>>}
*/
let filtersByDomain = new Map();
/**
* Lookup table, filter by selector. (Only used for selectors that are
* unconditionally matched for all domains.)
* @type {Map.<string,Filter>}
*/
@@ -53,20 +53,107 @@
/**
* Set containing known element hiding and exception filters
* @type {Set.<ElemHideBase>}
*/
let knownFilters = new Set();
/**
* Lookup table, lists of element hiding exceptions by selector
- * @type {Map.<string,Filter>}
+ * @type {Map.<string,Filter[]>}
*/
let exceptions = new Map();
+/*
+ * Lookup table, lists of generic element hiding exceptions by selector
+ * @type {Map.<string,Filter[]>}
+ */
+let genericExceptions = new Map();
Manish Jethani 2018/05/09 21:35:44 This needs to be a map actually, because we want t
+
+/**
+ * Checks whether a filter has an exception
+ * @param {Filter} filter
+ * @param {string} [domain]
+ * @returns {boolean}
+ */
+function hasException(filter, domain)
+{
+ if (!domain)
+ return genericExceptions.has(filter.selector);
+
+ return !!ElemHide.getException(filter, domain);
+}
+
+/*
+ * Returns a list of domain-specific filters matching a domain
+ * @param {string} [domain]
+ * @returns {Array.<?Map.<Filter,boolean>>}
+ */
+function getSpecificFiltersForDomain(domain)
+{
+ let filtersList = [];
+
+ if (domain)
+ domain = domain.toUpperCase();
+
+ while (domain)
+ {
+ // Note that we also push null values into the list, because
+ // ElemHide.getSelectorsForDomain still needs to know if there are any
+ // entries for the domain.
+ let filters = filtersByDomain.get(domain);
+ if (typeof filters != "undefined")
+ filtersList.push(filters);
+
+ let nextDot = domain.indexOf(".");
+ domain = nextDot == -1 ? null : domain.substring(nextDot + 1);
+ }
+
+ return filtersList;
+}
+
+/**
+ * Returns a list of selectors from a given list of filters that apply on a
+ * domain
+ * @param {string} [domain]
+ * @param {Array.<?Map.<Filter,boolean>>} filtersList
+ * @param {boolean} genericOnly
+ * @returns {string[]}
+ */
+function getConditionalSelectorsForDomain(domain, filtersList, genericOnly)
+{
+ let selectors = [];
+
+ let excluded = !genericOnly ? new Set() : null;
+ let domainForExceptionCheck = !genericOnly ? domain : null;
+
+ // This code is a performance hot-spot, which is why we've made certain
+ // micro-optimisations. Please be careful before making changes.
+ for (let i = 0; i < filtersList.length; i++)
+ {
+ if (!filtersList[i])
+ continue;
+
+ for (let [filter, isIncluded] of filtersList[i])
+ {
+ if (!isIncluded)
+ {
+ excluded.add(filter);
+ }
+ else if ((!excluded || excluded.size == 0 || !excluded.has(filter)) &&
+ !hasException(filter, domainForExceptionCheck))
+ {
+ selectors.push(filter.selector);
+ }
+ }
+ }
+
+ return selectors;
+}
+
/**
* Returns a list of selectors that apply on each website unconditionally.
* @returns {string[]}
*/
function getUnconditionalSelectors()
{
if (!unconditionalSelectors)
unconditionalSelectors = [...filterBySelector.keys()];
@@ -80,58 +167,86 @@
*/
let ElemHide = exports.ElemHide = {
/**
* Removes all known filters
*/
clear()
{
for (let collection of [filtersByDomain, filterBySelector,
- knownFilters, exceptions])
+ knownFilters, exceptions,
+ genericExceptions])
{
collection.clear();
}
unconditionalSelectors = null;
FilterNotifier.emit("elemhideupdate");
},
_addToFiltersByDomain(filter)
{
let domains = filter.domains || defaultDomains;
- for (let [domain, isIncluded] of domains)
+ if (filter instanceof ElemHideException)
{
- // There's no need to note that a filter is generically disabled.
- if (!isIncluded && domain == "")
- continue;
+ for (let domain of domains.keys())
+ {
+ // Add an entry for each domain, but without any filters. This makes
+ // the domain "known" and helps us avoid certain optimizations that
+ // would otherwise yield incorrect results.
+ if (domain != "" && !filtersByDomain.has(domain))
+ filtersByDomain.set(domain, null);
+ }
+ }
+ else
+ {
+ for (let [domain, isIncluded] of domains)
+ {
+ // There's no need to note that a filter is generically disabled.
+ if (!isIncluded && domain == "")
+ continue;
- let filters = filtersByDomain.get(domain);
- if (!filters)
- filtersByDomain.set(domain, filters = new Map());
- filters.set(filter, isIncluded);
+ let filters = filtersByDomain.get(domain);
+ if (!filters)
+ filtersByDomain.set(domain, filters = new Map());
+ filters.set(filter, isIncluded);
+ }
}
},
/**
* Add a new element hiding filter
* @param {ElemHideBase} filter
*/
add(filter)
{
if (knownFilters.has(filter))
return;
if (filter instanceof ElemHideException)
{
- let {selector} = filter;
+ let {selector, domains} = filter;
+
let list = exceptions.get(selector);
if (list)
list.push(filter);
else
exceptions.set(selector, [filter]);
+ if (domains)
+ this._addToFiltersByDomain(filter);
+
+ if (filter.isGeneric())
+ {
+ list = genericExceptions.get(selector);
+ if (list)
+ list.push(filter);
+ else
+ genericExceptions.set(selector, [filter]);
+ }
+
// If this is the first exception for a previously unconditionally
// applied element hiding selector we need to take care to update the
// lookups.
let unconditionalFilterForSelector = filterBySelector.get(selector);
if (unconditionalFilterForSelector)
{
this._addToFiltersByDomain(unconditionalFilterForSelector);
filterBySelector.delete(selector);
@@ -165,16 +280,27 @@
// Whitelisting filters
if (filter instanceof ElemHideException)
{
let list = exceptions.get(filter.selector);
let index = list.indexOf(filter);
if (index >= 0)
list.splice(index, 1);
+
+ if (filter.isGeneric())
+ {
+ list = genericExceptions.get(filter.selector);
+ index = list.indexOf(filter);
+ if (index >= 0)
+ list.splice(index, 1);
+
+ if (list.length == 0)
+ genericExceptions.delete(filter.selector);
+ }
}
// Unconditially applied element hiding filters
else if (filterBySelector.get(filter.selector) == filter)
{
filterBySelector.delete(filter.selector);
unconditionalSelectors = null;
}
// Conditionally applied element hiding filters
@@ -240,54 +366,28 @@
* @param {number} [criteria]
* One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
* ElemHide.SPECIFIC_ONLY.
* @returns {string[]}
* List of selectors.
*/
getSelectorsForDomain(domain, criteria)
{
- let selectors = [];
-
if (typeof criteria == "undefined")
criteria = ElemHide.ALL_MATCHING;
- let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
- let excluded = new Set();
- let currentDomain = domain ? domain.toUpperCase() : "";
+ let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY;
+ let filtersList = getSpecificFiltersForDomain(domain);
- // This code is a performance hot-spot, which is why we've made certain
- // micro-optimisations. Please be careful before making changes.
- while (true)
- {
- if (specificOnly && currentDomain == "")
- break;
+ let genericOnly = filtersList.length == 0;
- let filters = filtersByDomain.get(currentDomain);
- if (filters)
- {
- for (let [filter, isIncluded] of filters)
- {
- if (!isIncluded)
- {
- excluded.add(filter);
- }
- else if ((excluded.size == 0 || !excluded.has(filter)) &&
- !this.getException(filter, domain))
- {
- selectors.push(filter.selector);
- }
- }
- }
+ if (!specificOnly)
+ filtersList.push(filtersByDomain.get(""));
- if (currentDomain == "")
- break;
-
- let nextDot = currentDomain.indexOf(".");
- currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
- }
+ let selectors = getConditionalSelectorsForDomain(domain, filtersList,
+ genericOnly);
if (criteria < ElemHide.NO_UNCONDITIONAL)
selectors = getUnconditionalSelectors().concat(selectors);
return selectors;
}
};
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld