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

Unified Diff: lib/elemHide.js

Issue 29757591: Issue 6562 - Remove filter keys from the element hiding code (Closed)
Patch Set: Avoid checking currentDomain == "" more than necessary Created May 2, 2018, 4:34 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/filterListener.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHide.js
diff --git a/lib/elemHide.js b/lib/elemHide.js
index 9aa1ae3948540af980eda636d9530464c6951aa6..04f1f7aea289f3d2a3072a86bdddfb02a06fb64e 100644
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -25,48 +25,36 @@ const {ElemHideException} = require("./filterClasses");
const {FilterNotifier} = require("./filterNotifier");
/**
- * Lookup table, filters by their associated key
- * @type {Filter[]}
- */
-let filterByKey = [];
-
-/**
- * Lookup table, keys of the filters by filter
- * @type {Map.<Filter,number>}
- */
-let keyByFilter = new Map();
-
-/**
- * Nested lookup table, filter (or false if inactive) by filter key by domain.
+ * Lookup table, active flag, by filter by domain.
* (Only contains filters that aren't unconditionally matched for all domains.)
- * @type {Map.<string,Map.<number,(Filter|boolean)>>}
+ * @type {Map.<string,Map.<Filter,boolean>>}
*/
let filtersByDomain = new Map();
/**
- * Lookup table, filter key by selector. (Only used for selectors that are
+ * Lookup table, filter by selector. (Only used for selectors that are
* unconditionally matched for all domains.)
- * @type {Map.<string,number>}
+ * @type {Map.<string,Filter>}
*/
-let filterKeyBySelector = new Map();
+let filterBySelector = new Map();
/**
- * This array caches the keys of filterKeyBySelector table (selectors which
- * unconditionally apply on all domains). It will be null if the cache needs to
- * be rebuilt.
+ * This array caches the keys of filterBySelector table (selectors
+ * which unconditionally apply on all domains). It will be null if the
+ * cache needs to be rebuilt.
*/
let unconditionalSelectors = null;
/**
- * Object to be used instead when a filter has a blank domains property.
+ * Map to be used instead when a filter has a blank domains property.
*/
let defaultDomains = new Map([["", true]]);
/**
- * Set containing known element hiding exceptions
- * @type {Set.<string>}
+ * Set containing known element hiding and exception filters
+ * @type {Set.<ElemHideBase>}
*/
-let knownExceptions = new Set();
+let knownFilters = new Set();
/**
* Lookup table, lists of element hiding exceptions by selector
@@ -84,39 +72,42 @@ let ElemHide = exports.ElemHide = {
*/
clear()
{
- for (let collection of [keyByFilter, filtersByDomain, filterKeyBySelector,
- knownExceptions, exceptions])
+ for (let collection of [filtersByDomain, filterBySelector,
+ knownFilters, exceptions])
{
collection.clear();
}
- filterByKey = [];
unconditionalSelectors = null;
FilterNotifier.emit("elemhideupdate");
},
- _addToFiltersByDomain(key, filter)
+ _addToFiltersByDomain(filter)
{
let domains = filter.domains || defaultDomains;
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(key, isIncluded ? filter : false);
+ filters.set(filter, isIncluded);
}
},
/**
* Add a new element hiding filter
- * @param {ElemHideFilter} filter
+ * @param {ElemHideBase} filter
*/
add(filter)
{
+ if (knownFilters.has(filter))
+ return;
+
if (filter instanceof ElemHideException)
{
- if (knownExceptions.has(filter.text))
- return;
-
let {selector} = filter;
let list = exceptions.get(selector);
if (list)
@@ -127,88 +118,66 @@ let ElemHide = exports.ElemHide = {
// If this is the first exception for a previously unconditionally
// applied element hiding selector we need to take care to update the
// lookups.
- let filterKey = filterKeyBySelector.get(selector);
- if (typeof filterKey != "undefined")
- {
- this._addToFiltersByDomain(filterKey, filterByKey[filterKey]);
- filterKeyBySelector.delete(selector);
- unconditionalSelectors = null;
- }
-
- knownExceptions.add(filter.text);
- }
- else
- {
- if (keyByFilter.has(filter))
- return;
-
- let key = filterByKey.push(filter) - 1;
- keyByFilter.set(filter, key);
-
- if (!(filter.domains || exceptions.has(filter.selector)))
+ let unconditionalFilterForSelector = filterBySelector.get(selector);
+ if (unconditionalFilterForSelector)
{
- // The new filter's selector is unconditionally applied to all domains
- filterKeyBySelector.set(filter.selector, key);
+ this._addToFiltersByDomain(unconditionalFilterForSelector);
+ filterBySelector.delete(selector);
unconditionalSelectors = null;
}
- else
- {
- // The new filter's selector only applies to some domains
- this._addToFiltersByDomain(key, filter);
- }
}
-
- FilterNotifier.emit("elemhideupdate");
- },
-
- _removeFilterKey(key, filter)
- {
- if (filterKeyBySelector.get(filter.selector) == key)
+ else if (!(filter.domains || exceptions.has(filter.selector)))
{
- filterKeyBySelector.delete(filter.selector);
+ // The new filter's selector is unconditionally applied to all domains
+ filterBySelector.set(filter.selector, filter);
unconditionalSelectors = null;
- return;
}
-
- // We haven't found this filter in unconditional filters, look in
- // filtersByDomain.
- let domains = filter.domains || defaultDomains;
- for (let domain of domains.keys())
+ else
{
- let filters = filtersByDomain.get(domain);
- if (filters)
- filters.delete(key);
+ // The new filter's selector only applies to some domains
+ this._addToFiltersByDomain(filter);
}
+
+ knownFilters.add(filter);
+ FilterNotifier.emit("elemhideupdate");
},
/**
* Removes an element hiding filter
- * @param {ElemHideFilter} filter
+ * @param {ElemHideBase} filter
*/
remove(filter)
{
+ if (!knownFilters.has(filter))
+ return;
+
+ // Whitelisting filters
if (filter instanceof ElemHideException)
{
- if (!knownExceptions.has(filter.text))
- return;
-
let list = exceptions.get(filter.selector);
let index = list.indexOf(filter);
if (index >= 0)
list.splice(index, 1);
- knownExceptions.delete(filter.text);
}
+ // Unconditially applied element hiding filters
+ else if (filterBySelector.get(filter.selector) === filter)
+ {
+ filterBySelector.delete(filter.selector);
+ unconditionalSelectors = null;
+ }
+ // Conditionally applied element hiding filters
else
{
- let key = keyByFilter.get(filter);
- if (typeof key == "undefined")
- return;
-
- delete filterByKey[key];
- keyByFilter.delete(filter);
- this._removeFilterKey(key, filter);
+ let domains = filter.domains || defaultDomains;
+ for (let domain of domains.keys())
+ {
+ let filters = filtersByDomain.get(domain);
+ if (filters)
+ filters.delete(filter);
+ }
}
+ knownFilters.delete(filter);
FilterNotifier.emit("elemhideupdate");
},
@@ -234,16 +203,6 @@ let ElemHide = exports.ElemHide = {
return null;
},
- /**
- * Retrieves an element hiding filter by the corresponding protocol key
- * @param {number} key
- * @return {Filter}
- */
- getFilterByKey(key)
- {
- return (key in filterByKey ? filterByKey[key] : null);
- },
-
/**
* Returns a list of selectors that apply on each website unconditionally.
* @returns {string[]}
@@ -251,7 +210,7 @@ let ElemHide = exports.ElemHide = {
getUnconditionalSelectors()
{
if (!unconditionalSelectors)
- unconditionalSelectors = [...filterKeyBySelector.keys()];
+ unconditionalSelectors = [...filterBySelector.keys()];
return unconditionalSelectors.slice();
},
@@ -293,32 +252,47 @@ let ElemHide = exports.ElemHide = {
selectors = this.getUnconditionalSelectors();
let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
- let seenFilters = new Set();
+ let excludedFilters = new Set();
+
let currentDomain = domain ? domain.toUpperCase() : "";
+ let currentDomainIsGeneric = currentDomain == "";
Manish Jethani 2018/05/02 18:11:40 Comparison with an empty string really should not
kzar 2018/05/03 15:36:00 OK, Done.
+
+ // 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 == "")
+ if (specificOnly && currentDomainIsGeneric)
break;
let filters = filtersByDomain.get(currentDomain);
if (filters)
{
- for (let [filterKey, filter] of filters)
+ for (let [filter, isIncluded] of filters)
{
- if (seenFilters.has(filterKey))
+ if (excludedFilters.size > 0 && excludedFilters.has(filter))
Manish Jethani 2018/05/02 18:11:40 We only need to do this check for included filters
kzar 2018/05/03 15:36:01 Good point, Done.
continue;
- seenFilters.add(filterKey);
- if (filter && !this.getException(filter, domain))
- selectors.push(filter.selector);
+ if (isIncluded)
+ {
+ if (!this.getException(filter, domain))
+ selectors.push(filter.selector);
+ }
+ else if (!currentDomainIsGeneric)
Manish Jethani 2018/05/02 18:11:40 currentDomain will always be non-empty if the filt
kzar 2018/05/03 15:36:00 OK, Done.
+ excludedFilters.add(filter);
}
}
- if (currentDomain == "")
+ if (currentDomainIsGeneric)
break;
let nextDot = currentDomain.indexOf(".");
- currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
+ if (nextDot == -1)
+ {
+ currentDomain = "";
+ currentDomainIsGeneric = true;
+ }
+ else
+ currentDomain = currentDomain.substr(nextDot + 1);
}
return selectors;
« no previous file with comments | « no previous file | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld