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

Side by Side Diff: lib/elemHide.js

Issue 4529242486341632: Issue 235 - Improved performance of ElemHide.getSelectorsForDomain() (Closed)
Patch Set: Couple of overall improvements Created June 23, 2015, 1:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/contentPolicy.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 filter selectors by domain which they are applied to
Wladimir Palant 2015/06/23 13:57:09 Nit: the word "blocking" makes no sense here.
Thomas Greiner 2015/07/02 09:32:49 Done.
63 * @type Object
64 */
65 let selectorsByDomain = Object.create(null);
66
67 /**
68 * Indicates whether stylesheets can be used for element hiding
69 * @type Boolean
70 */
71 let canUseStylesheets = ("nsIStyleSheetService" in Ci);
72
73 /**
62 * Element hiding component 74 * Element hiding component
63 * @class 75 * @class
64 */ 76 */
65 let ElemHide = exports.ElemHide = 77 let ElemHide = exports.ElemHide =
66 { 78 {
67 /** 79 /**
68 * Indicates whether filters have been added or removed since the last apply() call. 80 * Indicates whether filters have been added or removed since the last apply() call.
69 * @type Boolean 81 * @type Boolean
70 */ 82 */
71 isDirty: false, 83 isDirty: false,
(...skipping 26 matching lines...) Expand all
98 110
99 /** 111 /**
100 * Removes all known filters 112 * Removes all known filters
101 */ 113 */
102 clear: function() 114 clear: function()
103 { 115 {
104 filterByKey = Object.create(null); 116 filterByKey = Object.create(null);
105 keyByFilter = Object.create(null); 117 keyByFilter = Object.create(null);
106 knownExceptions = Object.create(null); 118 knownExceptions = Object.create(null);
107 exceptions = Object.create(null); 119 exceptions = Object.create(null);
120 selectorsByDomain = Object.create(null);
108 ElemHide.isDirty = false; 121 ElemHide.isDirty = false;
109 ElemHide.unapply(); 122 ElemHide.unapply();
110 }, 123 },
111 124
112 /** 125 /**
113 * Add a new element hiding filter 126 * Add a new element hiding filter
114 * @param {ElemHideFilter} filter 127 * @param {ElemHideFilter} filter
115 */ 128 */
116 add: function(filter) 129 add: function(filter)
117 { 130 {
118 if (filter instanceof ElemHideException) 131 if (filter instanceof ElemHideException)
119 { 132 {
120 if (filter.text in knownExceptions) 133 if (filter.text in knownExceptions)
121 return; 134 return;
122 135
123 let selector = filter.selector; 136 let selector = filter.selector;
124 if (!(selector in exceptions)) 137 if (!(selector in exceptions))
125 exceptions[selector] = []; 138 exceptions[selector] = [];
126 exceptions[selector].push(filter); 139 exceptions[selector].push(filter);
127 knownExceptions[filter.text] = true; 140 knownExceptions[filter.text] = true;
128 } 141 }
129 else 142 else
130 { 143 {
131 if (filter.text in keyByFilter) 144 if (filter.text in keyByFilter)
132 return; 145 return;
133 146
147 if (!canUseStylesheets)
148 {
149 let domains = filter.domains;
150 if (domains === null)
151 {
152 domains = Object.create(null);
153 domains[""] = true;
154 }
155
156 for (let domain in domains)
157 {
158 if (!(domain in selectorsByDomain))
159 {
160 selectorsByDomain[domain] = Object.create(null);
161 selectorsByDomain[domain].$length = 0;
162 }
163
164 let selectors = selectorsByDomain[domain];
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];
Thomas Greiner 2015/06/23 13:28:41 I made this change here because I noticed that thi
Wladimir Palant 2015/06/23 13:57:09 Unfortunately, the result still isn't correct. Con
Thomas Greiner 2015/06/23 15:11:41 I see your point and please correct me if I'm wron
Wladimir Palant 2015/06/29 09:09:09 The problem is: you would create a behavior here t
Thomas Greiner 2015/06/29 13:03:44 Generally, I might be able to solve this by splitt
Thomas Greiner 2015/07/02 09:32:49 Done. See comment below for further details.
173 selectors[filter.selector].count++;
174
175 selectors.$length++;
176 }
177 }
178
134 let key; 179 let key;
135 do { 180 do {
136 key = Math.random().toFixed(15).substr(5); 181 key = Math.random().toFixed(15).substr(5);
137 } while (key in filterByKey); 182 } while (key in filterByKey);
138 183
139 filterByKey[key] = filter; 184 filterByKey[key] = filter;
140 keyByFilter[filter.text] = key; 185 keyByFilter[filter.text] = key;
141 ElemHide.isDirty = true; 186 ElemHide.isDirty = true;
142 } 187 }
143 }, 188 },
(...skipping 13 matching lines...) Expand all
157 let index = list.indexOf(filter); 202 let index = list.indexOf(filter);
158 if (index >= 0) 203 if (index >= 0)
159 list.splice(index, 1); 204 list.splice(index, 1);
160 delete knownExceptions[filter.text]; 205 delete knownExceptions[filter.text];
161 } 206 }
162 else 207 else
163 { 208 {
164 if (!(filter.text in keyByFilter)) 209 if (!(filter.text in keyByFilter))
165 return; 210 return;
166 211
212 if (!canUseStylesheets)
213 {
214 let domains = filter.domains;
215 if (!domains)
216 {
217 domains = Object.create(null);
218 domains[""] = true;
219 }
220
221 for (let domain in domains)
222 {
223 if (domain in selectorsByDomain
224 && filter.selector in selectorsByDomain[domain]
225 && !--selectorsByDomain[domain][filter.selector].$length)
226 {
227 delete selectorsByDomain[domain][filter.selector];
228 if (!--selectorsByDomain[domain].$length)
229 delete selectorsByDomain[domain];
230 }
231 }
232 }
233
167 let key = keyByFilter[filter.text]; 234 let key = keyByFilter[filter.text];
168 delete filterByKey[key]; 235 delete filterByKey[key];
169 delete keyByFilter[filter.text]; 236 delete keyByFilter[filter.text];
170 ElemHide.isDirty = true; 237 ElemHide.isDirty = true;
171 } 238 }
172 }, 239 },
173 240
174 /** 241 /**
175 * 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
176 * domain. 243 * domain.
177 */ 244 */
178 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 245 getException: function(/**String*/ selector, /**String*/ docDomain) /**ElemHid eException*/
179 { 246 {
180 if (!(filter.selector in exceptions)) 247 if (!(selector in exceptions))
181 return null; 248 return null;
182 249
183 let list = exceptions[filter.selector]; 250 let list = exceptions[selector];
184 for (let i = list.length - 1; i >= 0; i--) 251 for (let i = list.length - 1; i >= 0; i--)
185 if (list[i].isActiveOnDomain(docDomain)) 252 if (list[i].isActiveOnDomain(docDomain))
186 return list[i]; 253 return list[i];
187 254
188 return null; 255 return null;
189 }, 256 },
190 257
191 /** 258 /**
192 * Will be set to true if apply() is running (reentrance protection). 259 * Will be set to true if apply() is running (reentrance protection).
193 * @type Boolean 260 * @type Boolean
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 } 342 }
276 } 343 }
277 344
278 FilterNotifier.triggerListeners("elemhideupdate"); 345 FilterNotifier.triggerListeners("elemhideupdate");
279 } 346 }
280 }.bind(this)); 347 }.bind(this));
281 348
282 this._applying = true; 349 this._applying = true;
283 }, 350 },
284 351
285 _generateCSSContent: function*() 352 _generateCSSContent: function*()
Thomas Greiner 2015/06/23 13:28:41 This was added when rebasing
286 { 353 {
287 // Grouping selectors by domains 354 // Grouping selectors by domains
288 let domains = Object.create(null); 355 let domains = Object.create(null);
289 let hasFilters = false; 356 let hasFilters = false;
290 for (let key in filterByKey) 357 for (let key in filterByKey)
291 { 358 {
292 let filter = filterByKey[key]; 359 let filter = filterByKey[key];
293 let domain = filter.selectorDomain || ""; 360 let domain = filter.selectorDomain || "";
294 361
295 let list; 362 let list;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 432
366 /** 433 /**
367 * Retrieves an element hiding filter by the corresponding protocol key 434 * Retrieves an element hiding filter by the corresponding protocol key
368 */ 435 */
369 getFilterByKey: function(/**String*/ key) /**Filter*/ 436 getFilterByKey: function(/**String*/ key) /**Filter*/
370 { 437 {
371 return (key in filterByKey ? filterByKey[key] : null); 438 return (key in filterByKey ? filterByKey[key] : null);
372 }, 439 },
373 440
374 /** 441 /**
375 * Returns a list of all selectors active on a particular domain (currently 442 * Returns a list of all selectors active on a particular domain (it cannot
376 * used only in Chrome, Opera and Safari). 443 * be used in Firefox).
377 */ 444 */
378 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 445 getSelectorsForDomain: function(/**String*/ docDomain, /**Boolean*/ specificOn ly)
379 { 446 {
380 let result = []; 447 if (canUseStylesheets)
381 let keys = Object.getOwnPropertyNames(filterByKey); 448 throw new Error("Use of getSelectorsForDomain is limited to platforms whic h cannot inject stylesheets");
382 for (let key of keys) 449
450 let selectors = [];
451 let domain = docDomain.toUpperCase();
452
453 let ignoredSelectors = Object.create(null);
454 while (true)
383 { 455 {
384 let filter = filterByKey[key]; 456 if (domain in selectorsByDomain && (domain != "" || !specificOnly))
385 if (specificOnly && (!filter.domains || filter.domains[""])) 457 {
386 continue; 458 let domainSelectors = selectorsByDomain[domain];
459 let filterSelectors = Object.getOwnPropertyNames(domainSelectors);
460 for (let filterSelector of filterSelectors)
461 {
462 if (filterSelector == "$length" || filterSelector in ignoredSelectors)
463 continue;
387 464
388 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) 465 if (domainSelectors[filterSelector].isActive)
389 result.push(filter.selector); 466 {
467 if (!this.getException(filterSelector, docDomain))
468 {
469 selectors.push(filterSelector);
470 ignoredSelectors[filterSelector] = null;
471 }
472 }
473 else
474 ignoredSelectors[filterSelector] = null;
Thomas Greiner 2015/07/02 09:32:49 Basically, all I did was remove the else-case here
475 }
476 }
477
478 if (domain == "")
479 break;
480
481 let nextDot = domain.indexOf(".");
482 domain = (nextDot < 0) ? "" : domain.substr(nextDot + 1);
390 } 483 }
391 return result; 484
485 return selectors;
392 } 486 }
393 }; 487 };
OLDNEW
« no previous file with comments | « lib/contentPolicy.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld