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: Separate out subdomains generator function Created May 13, 2018, 7:41 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
Index: lib/elemHideEmulation.js
===================================================================
--- a/lib/elemHideEmulation.js
+++ b/lib/elemHideEmulation.js
@@ -17,64 +17,126 @@
"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* subdomains(domain)
+{
+ yield* mapIt(regExpIt(/([^|]*)\|/g, domain.replace(/([^.]+\.)/g, "$'|")),
+ ([, subdomain]) => subdomain);
+}
+
+function* filtersForDomain(domain)
+{
+ yield* filtersByDomain.get(domain) || [];
+
+ yield* flattenIt(mapIt(subdomains(domain),
+ subdomain => filtersByDomain.get(subdomain) || []));
+}
/**
* 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();
Manish Jethani 2018/05/13 20:39:48 The logic here is similar to lib/elemHide.js Even
+ 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;
« lib/coreUtils.js ('K') | « lib/coreUtils.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld