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

Unified Diff: lib/elemHideEmulation.js

Issue 29780560: Issue 6665 - Optimize element hiding emulation filter lookups (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created May 13, 2018, 7:28 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 | « lib/coreUtils.js ('k') | test/filterListener.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/elemHideEmulation.js
===================================================================
--- a/lib/elemHideEmulation.js
+++ b/lib/elemHideEmulation.js
@@ -17,64 +17,122 @@
"use strict";
/**
* @fileOverview Element hiding emulation implementation.
*/
const {ElemHide} = require("./elemHide");
-const {Filter} = require("./filterClasses");
+const {filterIt, mapIt, flattenIt, regExpIt} = require("./coreUtils");
+
+/**
+ * Map of element hiding emulation filters by domain
+ * @type {Map.<string,Set.<ElemHideEmulationFilter>>}
+ */
+let filtersByDomain = new Map();
+
+/**
+ * Set containing known element hiding emulation filters
+ * @type {Set.<ElemHideEmulation>}
+ */
+let knownFilters = new Set();
-let filters = new Set();
+function* filterDomains(filter)
+{
+ yield* mapIt(filterIt(filter.domains || [], ([domain]) => domain != ""),
+ ([domain, include]) => [domain, include,
+ filtersByDomain.get(domain)]);
+}
+
+function* filtersForDomain(domain)
+{
+ yield* filtersByDomain.get(domain) || [];
+
+ let subdomainIt =
+ mapIt(regExpIt(/([^|]*)\|/g, domain.replace(/([^.]+\.)/g, "$'|")),
+ ([, subdomain]) => filtersByDomain.get(subdomain) || []);
+ yield* flattenIt(subdomainIt);
+}
/**
* Container for element hiding emulation filters
* @class
*/
let ElemHideEmulation = {
/**
* Removes all known filters
*/
clear()
{
- filters.clear();
+ filtersByDomain.clear();
},
/**
* Add a new element hiding emulation filter
* @param {ElemHideEmulationFilter} filter
*/
add(filter)
{
- filters.add(filter.text);
+ if (knownFilters.has(filter))
+ return;
+
+ for (let [domain, include, filters] of filterDomains(filter))
+ {
+ if (filters)
+ filters.set(filter, include);
+ else
+ filtersByDomain.set(domain, new Map([[filter, include]]));
+ }
+
+ knownFilters.add(filter);
},
/**
* Removes an element hiding emulation filter
* @param {ElemHideEmulationFilter} filter
*/
remove(filter)
{
- filters.delete(filter.text);
+ if (!knownFilters.has(filter))
+ return;
+
+ for (let [domain, , filters] of filterDomains(filter))
+ {
+ if (filters)
+ {
+ filters.delete(filter);
+
+ if (filters.size == 0)
+ filtersByDomain.delete(domain);
+ }
+ }
+
+ knownFilters.delete(filter);
},
/**
* Returns a list of all rules active on a particular domain
* @param {string} domain
* @return {ElemHideEmulationFilter[]}
*/
getRulesForDomain(domain)
{
let result = [];
- for (let text of filters.values())
+
+ let excludeSet = new Set();
+ for (let [filter, include] of filtersForDomain(domain.toUpperCase()))
{
- let filter = Filter.fromText(text);
- if (filter.isActiveOnDomain(domain) &&
- !ElemHide.getException(filter, domain))
+ if (!include)
+ {
+ excludeSet.add(filter);
+ }
+ else if ((excludeSet.size == 0 || !excludeSet.has(filter)) &&
+ !ElemHide.getException(filter, domain))
{
result.push(filter);
}
}
+
return result;
}
};
exports.ElemHideEmulation = ElemHideEmulation;
« no previous file with comments | « lib/coreUtils.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld