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

Unified Diff: lib/elemHide.js

Issue 29342830: Issue 4057 - Further speedup ElemHide.getSelectorsforDomain (Closed)
Patch Set: Created May 20, 2016, 5:54 a.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/cssRules.js ('k') | no next file » | 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 6e2f1b56acbb6e67e79f779503b469f9d8829903..e78d9c655e6e27575582c98944fd15a189f0c768 100644
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -40,17 +40,31 @@ var filterByKey = Object.create(null);
var keyByFilter = Object.create(null);
/**
- * Nested lookup table, filter (or false if inactive) by filter text by domain
- * @type Object
+ * Indicates whether we are using getSelectorsByDomain and maintaining the
+ * required lookup tables. (Will be false for Firefox)
+ * @type Boolean
*/
-var filtersByDomain = Object.create(null);
+var usingGetSelectorsByDomain = !("nsIStyleSheetService" in Ci);
/**
- * Indicates whether we are using (and maintaining) the filtersByDomain lookup.
- * (Will be false for Firefox)
- * @type Boolean
+ * Lookup table, filter selector by filter ID.
+ */
+var selectorByFilterId = [];
+
+/**
+ * Lookup table, active filter IDs by domain.
+ */
+var activeFilterIdsByDomain = Object.create(null);
+
+/**
+ * Lookup table, inactive filter IDs by domain.
+ */
+var inactiveFilterIdsByDomain = Object.create(null);
+
+/**
+ * Lookup table, filter ID by filter text.
*/
-var usingFiltersByDomain = !("nsIStyleSheetService" in Ci);
+var filterIdByFilterText = Object.create(null);
/**
* Object to be used instead when a filter has a blank domains property.
@@ -118,7 +132,10 @@ var ElemHide = exports.ElemHide =
{
filterByKey = Object.create(null);
keyByFilter = Object.create(null);
- filtersByDomain = Object.create(null);
+ selectorByFilterId = [];
+ activeFilterIdsByDomain = Object.create(null);
+ inactiveFilterIdsByDomain = Object.create(null);
+ filterIdByFilterText = Object.create(null);
knownExceptions = Object.create(null);
exceptions = Object.create(null);
ElemHide.isDirty = false;
@@ -155,19 +172,30 @@ var ElemHide = exports.ElemHide =
filterByKey[key] = filter;
keyByFilter[filter.text] = key;
- if (usingFiltersByDomain)
+ if (usingGetSelectorsByDomain)
{
+ let filterId = filterIdByFilterText[filter.text];
+ if (filterId == undefined)
kzar 2016/05/20 08:11:12 I guess we can remove filterIdByFilterText lookup
kzar 2016/05/20 10:55:17 Nevermind, pretty sure we need it
+ {
+ filterId = selectorByFilterId.push(filter.selector) - 1;
+ filterIdByFilterText[filter.text] = filterId;
+ }
+
let domainMatches = filter.domains || defaultDomains;
+
for (let domain in domainMatches)
{
- let filters = filtersByDomain[domain];
- if (!filters)
- filters = filtersByDomain[domain] = Object.create(null);
-
+ let lookup;
if (domainMatches[domain])
- filters[filter.text] = filter;
+ lookup = activeFilterIdsByDomain;
else
- filters[filter.text] = false;
+ lookup = inactiveFilterIdsByDomain;
+
+ let filterIds = lookup[domain];
+ if (filterIds == undefined)
+ filterIds = lookup[domain] = [];
+
+ filterIds.push(filterId);
}
}
@@ -208,12 +236,12 @@ var ElemHide = exports.ElemHide =
* Checks whether an exception rule is registered for a filter on a particular
* domain.
*/
- getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideException*/
+ getException: function(/**String*/ selector, /**String*/ docDomain) /**ElemHideException*/
{
- if (!(filter.selector in exceptions))
+ if (!(selector in exceptions))
return null;
- let list = exceptions[filter.selector];
+ let list = exceptions[selector];
for (let i = list.length - 1; i >= 0; i--)
if (list[i].isActiveOnDomain(docDomain))
return list[i];
@@ -410,30 +438,35 @@ var ElemHide = exports.ElemHide =
*/
getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly)
{
- if (!usingFiltersByDomain)
+ if (!usingGetSelectorsByDomain)
throw new Error("getSelectorsForDomain can not be used in Firefox!");
let selectors = [];
- let seenFilters = Object.create(null);
+ let seenFilterIds = Object.create(null);
let currentDomain = domain ? domain.toUpperCase() : "";
while (true)
{
if (specificOnly && currentDomain == "")
break;
- let filters = filtersByDomain[currentDomain];
- if (filters)
+ let inactiveFilterIds = inactiveFilterIdsByDomain[currentDomain];
+ if (inactiveFilterIds)
+ for (let filterId of inactiveFilterIds)
+ seenFilterIds[filterId] = true;
+
+ let activeFilterIds = activeFilterIdsByDomain[currentDomain];
+ if (activeFilterIds)
{
- for (let filterText in filters)
+ for (let filterId of activeFilterIds)
{
- if (filterText in seenFilters)
+ if (filterId in seenFilterIds)
continue;
- seenFilters[filterText] = true;
+ seenFilterIds[filterId] = true;
- let filter = filters[filterText];
- if (filter && !this.getException(filter, domain))
- selectors.push(filter.selector);
+ let selector = selectorByFilterId[filterId];
+ if (!this.getException(selector, domain))
+ selectors.push(selector);
}
}
« no previous file with comments | « lib/cssRules.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld