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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29342830: Issue 4057 - Further speedup ElemHide.getSelectorsforDomain (Closed)
Left Patch Set: Speed up ElemHide.remove Created May 25, 2016, 4:54 a.m.
Right Patch Set: Addressed nits Created May 25, 2016, 10:50 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/elemHide.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 */ 54 */
55 var filtersByDomain = Object.create(null); 55 var filtersByDomain = Object.create(null);
56 56
57 /** 57 /**
58 * Lookup table, filters by selector. (Only contains filters that have a 58 * Lookup table, filters by selector. (Only contains filters that have a
59 * selector that is unconditionally matched for all domains.) 59 * selector that is unconditionally matched for all domains.)
60 */ 60 */
61 var filtersBySelector = Object.create(null); 61 var filtersBySelector = Object.create(null);
62 62
63 /** 63 /**
64 * Array of selectors which unconditionally apply to all domains. 64 * This array caches the keys of filtersBySelector table (selectors which
65 */ 65 * unconditionally apply on all domains). It will be null if the cache needs to
66 var unconditionalSelectors = []; 66 * be rebuilt.
67 67 */
68 /** 68 var unconditionalSelectors = null;
69 * Indicates that the unconditionalSelectors Array needs to be regenerated.
70 */
71 var unconditionalSelectorsDirty = false;
72 69
73 /** 70 /**
74 * Object to be used instead when a filter has a blank domains property. 71 * Object to be used instead when a filter has a blank domains property.
75 */ 72 */
76 var defaultDomains = Object.create(null); 73 var defaultDomains = Object.create(null);
77 defaultDomains[""] = true; 74 defaultDomains[""] = true;
78 75
79 /** 76 /**
80 * Lookup table, keys are known element hiding exceptions 77 * Lookup table, keys are known element hiding exceptions
81 * @type Object 78 * @type Object
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 128
132 /** 129 /**
133 * Removes all known filters 130 * Removes all known filters
134 */ 131 */
135 clear: function() 132 clear: function()
136 { 133 {
137 filterByKey = []; 134 filterByKey = [];
138 keyByFilter = Object.create(null); 135 keyByFilter = Object.create(null);
139 filtersByDomain = Object.create(null); 136 filtersByDomain = Object.create(null);
140 filtersBySelector = Object.create(null); 137 filtersBySelector = Object.create(null);
141 unconditionalSelectors = []; 138 unconditionalSelectors = null;
142 unconditionalSelectorsDirty = false;
143 knownExceptions = Object.create(null); 139 knownExceptions = Object.create(null);
144 exceptions = Object.create(null); 140 exceptions = Object.create(null);
145 ElemHide.isDirty = false; 141 ElemHide.isDirty = false;
146 ElemHide.unapply(); 142 ElemHide.unapply();
147 }, 143 },
148 144
149 _addToFiltersByDomain: function(filter) 145 _addToFiltersByDomain: function(filter)
150 { 146 {
151 let key = keyByFilter[filter.text]; 147 let key = keyByFilter[filter.text];
152 let domains = filter.domains || defaultDomains; 148 let domains = filter.domains || defaultDomains;
(...skipping 30 matching lines...) Expand all
183 { 179 {
184 // If this is the first exception for a previously unconditionally 180 // If this is the first exception for a previously unconditionally
185 // applied element hiding selector we need to take care to update the 181 // applied element hiding selector we need to take care to update the
186 // lookups. 182 // lookups.
187 let unconditionalFilters = filtersBySelector[selector]; 183 let unconditionalFilters = filtersBySelector[selector];
188 if (unconditionalFilters) 184 if (unconditionalFilters)
189 { 185 {
190 for (let f of unconditionalFilters) 186 for (let f of unconditionalFilters)
191 this._addToFiltersByDomain(f); 187 this._addToFiltersByDomain(f);
192 delete filtersBySelector[selector]; 188 delete filtersBySelector[selector];
193 unconditionalSelectorsDirty = true; 189 unconditionalSelectors = null;
194 } 190 }
195 } 191 }
196 192
197 knownExceptions[filter.text] = true; 193 knownExceptions[filter.text] = true;
198 } 194 }
199 else 195 else
200 { 196 {
201 if (filter.text in keyByFilter) 197 if (filter.text in keyByFilter)
202 return; 198 return;
203 199
204 let key = filterByKey.push(filter) - 1; 200 let key = filterByKey.push(filter) - 1;
205 keyByFilter[filter.text] = key; 201 keyByFilter[filter.text] = key;
206 202
207 if (usingGetSelectorsForDomain) 203 if (usingGetSelectorsForDomain)
208 { 204 {
209 // The new filter's selector is unconditionally applied to all domains 205 if (!(filter.domains || filter.selector in exceptions))
210 if (!filter.domains && !exceptions[filter.selector]) 206 {
211 { 207 // The new filter's selector is unconditionally applied to all domains
212 let filters = filtersBySelector[filter.selector]; 208 let filters = filtersBySelector[filter.selector];
213 if (filters) 209 if (filters)
214 { 210 {
215 filters.push(filter); 211 filters.push(filter);
216 } 212 }
217 else 213 else
218 { 214 {
219 filtersBySelector[filter.selector] = [filter]; 215 filtersBySelector[filter.selector] = [filter];
220 unconditionalSelectorsDirty = true; 216 unconditionalSelectors = null;
221 } 217 }
222 } 218 }
223 // The new filter's selector only applies to some domains
224 else 219 else
225 { 220 {
221 // The new filter's selector only applies to some domains
226 this._addToFiltersByDomain(filter); 222 this._addToFiltersByDomain(filter);
227 } 223 }
228 } 224 }
229 225
230 ElemHide.isDirty = true; 226 ElemHide.isDirty = true;
231 } 227 }
232 }, 228 },
233 229
234 /** 230 /**
235 * Removes an element hiding filter 231 * Removes an element hiding filter
(...skipping 28 matching lines...) Expand all
264 if (filters) 260 if (filters)
265 { 261 {
266 if (filters.length > 1) 262 if (filters.length > 1)
267 { 263 {
268 let index = filters.indexOf(filter); 264 let index = filters.indexOf(filter);
269 filters.splice(index, 1); 265 filters.splice(index, 1);
270 } 266 }
271 else 267 else
272 { 268 {
273 delete filtersBySelector[filter.selector]; 269 delete filtersBySelector[filter.selector];
274 unconditionalSelectorsDirty = true; 270 unconditionalSelectors = null;
275 } 271 }
276 } 272 }
277 else 273 else
278 { 274 {
279 let domains = filter.domains || defaultDomains; 275 let domains = filter.domains || defaultDomains;
280 for (let domain in domains) 276 for (let domain in domains)
281 { 277 {
282 let filters = filtersByDomain[domain]; 278 let filters = filtersByDomain[domain];
283 if (filters) 279 if (filters)
284 delete filters[key]; 280 delete filters[key];
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 490
495 /** 491 /**
496 * Returns a list of all selectors active on a particular domain, must not be 492 * Returns a list of all selectors active on a particular domain, must not be
497 * used in Firefox (when usingGetSelectorsForDomain is false). 493 * used in Firefox (when usingGetSelectorsForDomain is false).
498 */ 494 */
499 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 495 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly)
500 { 496 {
501 if (!usingGetSelectorsForDomain) 497 if (!usingGetSelectorsForDomain)
502 throw new Error("getSelectorsForDomain can not be used in Firefox!"); 498 throw new Error("getSelectorsForDomain can not be used in Firefox!");
503 499
504 let selectors = []; 500 if (!unconditionalSelectors)
501 unconditionalSelectors = Object.keys(filtersBySelector);
502 let selectors = specificOnly ? [] : unconditionalSelectors.slice();
505 503
506 let seenFilters = Object.create(null); 504 let seenFilters = Object.create(null);
507 let currentDomain = domain ? domain.toUpperCase() : ""; 505 let currentDomain = domain ? domain.toUpperCase() : "";
508 while (true) 506 while (true)
509 { 507 {
510 if (specificOnly && currentDomain == "") 508 if (specificOnly && currentDomain == "")
511 break; 509 break;
512 510
513 let filters = filtersByDomain[currentDomain]; 511 let filters = filtersByDomain[currentDomain];
514 if (filters) 512 if (filters)
(...skipping 10 matching lines...) Expand all
525 } 523 }
526 } 524 }
527 525
528 if (currentDomain == "") 526 if (currentDomain == "")
529 break; 527 break;
530 528
531 let nextDot = currentDomain.indexOf("."); 529 let nextDot = currentDomain.indexOf(".");
532 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 530 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
533 } 531 }
534 532
535 if (specificOnly) 533 return selectors;
536 return selectors;
537
538 if (unconditionalSelectorsDirty)
539 {
540 unconditionalSelectors = Object.keys(filtersBySelector);
541 unconditionalSelectorsDirty = false;
542 }
543 return unconditionalSelectors.concat(selectors);
544 } 534 }
545 }; 535 };
LEFTRIGHT
« no previous file | test/elemHide.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld