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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 4529242486341632: Issue 235 - Improved performance of ElemHide.getSelectorsForDomain() (Closed)
Left Patch Set: Rebased to 5434ef6e1545 Created June 23, 2015, 12:52 p.m.
Right Patch Set: Always consider generic filters Created July 2, 2015, 9:22 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 | « lib/contentPolicy.js ('k') | no next file » | 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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 */ 52 */
53 let exceptions = Object.create(null); 53 let exceptions = Object.create(null);
54 54
55 /** 55 /**
56 * Currently applied stylesheet URL 56 * Currently applied stylesheet URL
57 * @type nsIURI 57 * @type nsIURI
58 */ 58 */
59 let styleURL = null; 59 let styleURL = null;
60 60
61 /** 61 /**
62 * Lookup table, blocking filters by domain which they are applied to 62 * Lookup table, filter selectors by domain which they are applied to
63 * @type Object 63 * @type Object
64 */ 64 */
65 let filtersByDomain = Object.create(null); 65 let selectorsByDomain = Object.create(null);
66 66
67 /** 67 /**
68 * Indicates whether stylesheets can be used for element hiding 68 * Indicates whether stylesheets can be used for element hiding
69 * @type Boolean 69 * @type Boolean
70 */ 70 */
71 let canUseStylesheets = ("nsIStyleSheetService" in Ci); 71 let canUseStylesheets = ("nsIStyleSheetService" in Ci);
72 72
73 /** 73 /**
74 * Element hiding component 74 * Element hiding component
75 * @class 75 * @class
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 110
111 /** 111 /**
112 * Removes all known filters 112 * Removes all known filters
113 */ 113 */
114 clear: function() 114 clear: function()
115 { 115 {
116 filterByKey = Object.create(null); 116 filterByKey = Object.create(null);
117 keyByFilter = Object.create(null); 117 keyByFilter = Object.create(null);
118 knownExceptions = Object.create(null); 118 knownExceptions = Object.create(null);
119 exceptions = Object.create(null); 119 exceptions = Object.create(null);
120 filtersByDomain = Object.create(null); 120 selectorsByDomain = Object.create(null);
121 ElemHide.isDirty = false; 121 ElemHide.isDirty = false;
122 ElemHide.unapply(); 122 ElemHide.unapply();
123 }, 123 },
124 124
125 /** 125 /**
126 * Add a new element hiding filter 126 * Add a new element hiding filter
127 * @param {ElemHideFilter} filter 127 * @param {ElemHideFilter} filter
128 */ 128 */
129 add: function(filter) 129 add: function(filter)
130 { 130 {
(...skipping 17 matching lines...) Expand all
148 { 148 {
149 let domains = filter.domains; 149 let domains = filter.domains;
150 if (domains === null) 150 if (domains === null)
151 { 151 {
152 domains = Object.create(null); 152 domains = Object.create(null);
153 domains[""] = true; 153 domains[""] = true;
154 } 154 }
155 155
156 for (let domain in domains) 156 for (let domain in domains)
157 { 157 {
158 if (!(domain in filtersByDomain)) 158 if (!(domain in selectorsByDomain))
159 { 159 {
160 filtersByDomain[domain] = Object.create(null); 160 selectorsByDomain[domain] = Object.create(null);
161 filtersByDomain[domain].$length = 0; 161 selectorsByDomain[domain].$length = 0;
162 } 162 }
163 163
164 filtersByDomain[domain][filter.text] = domains[domain]; 164 let selectors = selectorsByDomain[domain];
165 filtersByDomain[domain].$length++; 165 if (!(filter.selector in selectors))
166 {
167 selectors[filter.selector] = {
168 isActive: false,
169 count: 0
170 };
171 }
172 selectors[filter.selector].isActive |= domains[domain];
173 selectors[filter.selector].count++;
174
175 selectors.$length++;
166 } 176 }
167 } 177 }
168 178
169 let key; 179 let key;
170 do { 180 do {
171 key = Math.random().toFixed(15).substr(5); 181 key = Math.random().toFixed(15).substr(5);
172 } while (key in filterByKey); 182 } while (key in filterByKey);
173 183
174 filterByKey[key] = filter; 184 filterByKey[key] = filter;
175 keyByFilter[filter.text] = key; 185 keyByFilter[filter.text] = key;
(...skipping 27 matching lines...) Expand all
203 { 213 {
204 let domains = filter.domains; 214 let domains = filter.domains;
205 if (!domains) 215 if (!domains)
206 { 216 {
207 domains = Object.create(null); 217 domains = Object.create(null);
208 domains[""] = true; 218 domains[""] = true;
209 } 219 }
210 220
211 for (let domain in domains) 221 for (let domain in domains)
212 { 222 {
213 if (domain in filtersByDomain && filter.text in filtersByDomain[domain ]) 223 if (domain in selectorsByDomain
224 && filter.selector in selectorsByDomain[domain]
225 && !--selectorsByDomain[domain][filter.selector].$length)
214 { 226 {
215 delete filtersByDomain[domain][filter.text]; 227 delete selectorsByDomain[domain][filter.selector];
216 if (!--filtersByDomain[domain].$length) 228 if (!--selectorsByDomain[domain].$length)
217 delete filtersByDomain[domain]; 229 delete selectorsByDomain[domain];
218 } 230 }
219 } 231 }
220 } 232 }
221 233
222 let key = keyByFilter[filter.text]; 234 let key = keyByFilter[filter.text];
223 delete filterByKey[key]; 235 delete filterByKey[key];
224 delete keyByFilter[filter.text]; 236 delete keyByFilter[filter.text];
225 ElemHide.isDirty = true; 237 ElemHide.isDirty = true;
226 } 238 }
227 }, 239 },
228 240
229 /** 241 /**
230 * Checks whether an exception rule is registered for a filter on a particular 242 * Checks whether an exception rule is registered for a filter on a particular
231 * domain. 243 * domain.
232 */ 244 */
233 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 245 getException: function(/**String*/ selector, /**String*/ docDomain) /**ElemHid eException*/
234 { 246 {
235 if (!(filter.selector in exceptions)) 247 if (!(selector in exceptions))
236 return null; 248 return null;
237 249
238 let list = exceptions[filter.selector]; 250 let list = exceptions[selector];
239 for (let i = list.length - 1; i >= 0; i--) 251 for (let i = list.length - 1; i >= 0; i--)
240 if (list[i].isActiveOnDomain(docDomain)) 252 if (list[i].isActiveOnDomain(docDomain))
241 return list[i]; 253 return list[i];
242 254
243 return null; 255 return null;
244 }, 256 },
245 257
246 /** 258 /**
247 * Will be set to true if apply() is running (reentrance protection). 259 * Will be set to true if apply() is running (reentrance protection).
248 * @type Boolean 260 * @type Boolean
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 } 342 }
331 } 343 }
332 344
333 FilterNotifier.triggerListeners("elemhideupdate"); 345 FilterNotifier.triggerListeners("elemhideupdate");
334 } 346 }
335 }.bind(this)); 347 }.bind(this));
336 348
337 this._applying = true; 349 this._applying = true;
338 }, 350 },
339 351
340 _generateCSSContent: function() 352 _generateCSSContent: function*()
341 { 353 {
342 // Grouping selectors by domains 354 // Grouping selectors by domains
343 let domains = Object.create(null); 355 let domains = Object.create(null);
344 let hasFilters = false; 356 let hasFilters = false;
345 for (let key in filterByKey) 357 for (let key in filterByKey)
346 { 358 {
347 let filter = filterByKey[key]; 359 let filter = filterByKey[key];
348 let domain = filter.selectorDomain || ""; 360 let domain = filter.selectorDomain || "";
349 361
350 let list; 362 let list;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 */ 435 */
424 getFilterByKey: function(/**String*/ key) /**Filter*/ 436 getFilterByKey: function(/**String*/ key) /**Filter*/
425 { 437 {
426 return (key in filterByKey ? filterByKey[key] : null); 438 return (key in filterByKey ? filterByKey[key] : null);
427 }, 439 },
428 440
429 /** 441 /**
430 * Returns a list of all selectors active on a particular domain (it cannot 442 * Returns a list of all selectors active on a particular domain (it cannot
431 * be used in Firefox). 443 * be used in Firefox).
432 */ 444 */
433 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 445 getSelectorsForDomain: function(/**String*/ docDomain, /**Boolean*/ specificOn ly)
434 { 446 {
435 if (canUseStylesheets) 447 if (canUseStylesheets)
436 throw new Error("Use of getSelectorsForDomain is limited to platforms whic h cannot inject stylesheets"); 448 throw new Error("Use of getSelectorsForDomain is limited to platforms whic h cannot inject stylesheets");
437 449
438 let selectors = []; 450 let selectors = Object.create(null);
439 domain = domain.toUpperCase(); 451 let domain = docDomain.toUpperCase();
440
441 if (!specificOnly)
442 {
443 let filterTexts = filtersByDomain[""];
444 if (filterTexts)
445 {
446 for (let filterText in filterTexts)
447 {
448 if (filterTexts[filterText] && filterText != "$length")
449 {
450 let filter = Filter.fromText(filterText);
451 if (!this.getException(filter, domain))
452 selectors[selectors.length] = filter.selector;
453 }
454 }
455 }
456 }
457
458 let ignorableFilters = [];
459 while (true) 452 while (true)
460 { 453 {
461 if (domain && domain in filtersByDomain) 454 if (domain in selectorsByDomain && (domain != "" || !specificOnly))
462 { 455 {
463 let filterTexts = filtersByDomain[domain]; 456 let domainSelectors = selectorsByDomain[domain];
464 for (let filterText in filterTexts) 457 let filterSelectors = Object.getOwnPropertyNames(domainSelectors);
465 { 458 for (let filterSelector of filterSelectors)
466 if (ignorableFilters.indexOf(filterText) > -1) 459 {
460 if (filterSelector == "$length"
461 || filterSelector in selectors
462 || !domainSelectors[filterSelector].isActive
463 || this.getException(filterSelector, docDomain))
467 continue; 464 continue;
Wladimir Palant 2015/07/30 11:26:53 Even with the inconsistent logic changes you are i
468 465
469 if (filterTexts[filterText]) 466 selectors[filterSelector] = null;
470 { 467 }
471 if (filterText == "$length") 468 }
472 continue; 469
473 470 if (domain == "")
474 let filter = Filter.fromText(filterText); 471 break;
475 if (!this.getException(filter, domain))
476 selectors[selectors.length] = filter.selector;
477 }
478 else
479 ignorableFilters[ignorableFilters.length] = filterText;
480 }
481 }
482 472
483 let nextDot = domain.indexOf("."); 473 let nextDot = domain.indexOf(".");
484 if (nextDot < 0) 474 domain = (nextDot < 0) ? "" : domain.substr(nextDot + 1);
485 break; 475 }
486 domain = domain.substr(nextDot + 1); 476
487 } 477 return Object.getOwnPropertyNames(selectors);
488
489 return selectors;
490 } 478 }
491 }; 479 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld