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

Side by Side Diff: lib/elemHide.js

Issue 29341426: Issue 235 - Speed up ElemHide.getSelectorsForDomain (Closed)
Patch Set: Cache filtersByDomain[domain] Created May 17, 2016, 10:04 a.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 | « no previous file | 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-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 22 matching lines...) Expand all
33 */ 33 */
34 var filterByKey = Object.create(null); 34 var filterByKey = Object.create(null);
35 35
36 /** 36 /**
37 * Lookup table, keys of the filters by filter text 37 * Lookup table, keys of the filters by filter text
38 * @type Object 38 * @type Object
39 */ 39 */
40 var keyByFilter = Object.create(null); 40 var keyByFilter = Object.create(null);
41 41
42 /** 42 /**
43 * Nested lookup table, filter (or false if inactive) by filter text by domain
44 * @type Object
45 */
46 var filtersByDomain = Object.create(null);
47
48 /**
49 * Indicates whether we are using (and maintaining) the filtersByDomain lookup.
50 * (Will be false for Firefox)
51 * @type Boolean
52 */
53 var usingFiltersByDomain = !("nsIStyleSheetService" in Ci);
54
55 /**
43 * Lookup table, keys are known element hiding exceptions 56 * Lookup table, keys are known element hiding exceptions
44 * @type Object 57 * @type Object
45 */ 58 */
46 var knownExceptions = Object.create(null); 59 var knownExceptions = Object.create(null);
47 60
48 /** 61 /**
49 * Lookup table, lists of element hiding exceptions by selector 62 * Lookup table, lists of element hiding exceptions by selector
50 * @type Object 63 * @type Object
51 */ 64 */
52 var exceptions = Object.create(null); 65 var exceptions = Object.create(null);
(...skipping 10 matching lines...) Expand all
63 */ 76 */
64 var ElemHide = exports.ElemHide = 77 var ElemHide = exports.ElemHide =
65 { 78 {
66 /** 79 /**
67 * 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.
68 * @type Boolean 81 * @type Boolean
69 */ 82 */
70 isDirty: false, 83 isDirty: false,
71 84
72 /** 85 /**
73 * Inidicates whether the element hiding stylesheet is currently applied. 86 * Indicates whether the element hiding stylesheet is currently applied.
74 * @type Boolean 87 * @type Boolean
75 */ 88 */
76 applied: false, 89 applied: false,
77 90
78 /** 91 /**
79 * Called on module startup. 92 * Called on module startup.
80 */ 93 */
81 init: function() 94 init: function()
82 { 95 {
83 Prefs.addListener(function(name) 96 Prefs.addListener(function(name)
84 { 97 {
85 if (name == "enabled") 98 if (name == "enabled")
86 ElemHide.apply(); 99 ElemHide.apply();
87 }); 100 });
88 onShutdown.add(() => ElemHide.unapply()); 101 onShutdown.add(() => ElemHide.unapply());
89 102
90 let styleFile = IO.resolveFilePath(Prefs.data_directory); 103 let styleFile = IO.resolveFilePath(Prefs.data_directory);
91 styleFile.append("elemhide.css"); 104 styleFile.append("elemhide.css");
92 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); 105 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
93 }, 106 },
94 107
95 /** 108 /**
96 * Removes all known filters 109 * Removes all known filters
97 */ 110 */
98 clear: function() 111 clear: function()
99 { 112 {
100 filterByKey = Object.create(null); 113 filterByKey = Object.create(null);
101 keyByFilter = Object.create(null); 114 keyByFilter = Object.create(null);
115 filtersByDomain = Object.create(null);
102 knownExceptions = Object.create(null); 116 knownExceptions = Object.create(null);
103 exceptions = Object.create(null); 117 exceptions = Object.create(null);
104 ElemHide.isDirty = false; 118 ElemHide.isDirty = false;
105 ElemHide.unapply(); 119 ElemHide.unapply();
106 }, 120 },
107 121
108 /** 122 /**
109 * Add a new element hiding filter 123 * Add a new element hiding filter
110 * @param {ElemHideFilter} filter 124 * @param {ElemHideFilter} filter
111 */ 125 */
(...skipping 15 matching lines...) Expand all
127 if (filter.text in keyByFilter) 141 if (filter.text in keyByFilter)
128 return; 142 return;
129 143
130 let key; 144 let key;
131 do { 145 do {
132 key = Math.random().toFixed(15).substr(5); 146 key = Math.random().toFixed(15).substr(5);
133 } while (key in filterByKey); 147 } while (key in filterByKey);
134 148
135 filterByKey[key] = filter; 149 filterByKey[key] = filter;
136 keyByFilter[filter.text] = key; 150 keyByFilter[filter.text] = key;
151
152 if (usingFiltersByDomain)
153 {
154 let domainMatches = filter.domains || {"": true};
Wladimir Palant 2016/05/18 13:38:06 You are iterating over object keys below but {"":
Sebastian Noack 2016/05/18 13:48:25 Also note that for..in loops on objects in hash-ta
kzar 2016/05/18 14:23:00 Done.
kzar 2016/05/18 14:23:00 The only way I know to check if a function is deop
155 for (let domain in domainMatches)
156 {
157 domain = domain.toUpperCase();
Wladimir Palant 2016/05/18 13:38:06 This operation is unnecessary, the domains are alr
kzar 2016/05/18 14:23:00 Done.
158 let filters = filtersByDomain[domain];
159 if (!filters)
160 filters = filtersByDomain[domain] = Object.create(null);
161 filters[filter.text] = domainMatches[domain] && filter;
Wladimir Palant 2016/05/18 13:38:05 I know, Sebastian loves such constructs. Still, I
kzar 2016/05/18 14:23:00 Done.
162 }
163 }
164
137 ElemHide.isDirty = true; 165 ElemHide.isDirty = true;
138 } 166 }
139 }, 167 },
140 168
141 /** 169 /**
142 * Removes an element hiding filter 170 * Removes an element hiding filter
143 * @param {ElemHideFilter} filter 171 * @param {ElemHideFilter} filter
144 */ 172 */
145 remove: function(filter) 173 remove: function(filter)
146 { 174 {
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 389
362 /** 390 /**
363 * Retrieves an element hiding filter by the corresponding protocol key 391 * Retrieves an element hiding filter by the corresponding protocol key
364 */ 392 */
365 getFilterByKey: function(/**String*/ key) /**Filter*/ 393 getFilterByKey: function(/**String*/ key) /**Filter*/
366 { 394 {
367 return (key in filterByKey ? filterByKey[key] : null); 395 return (key in filterByKey ? filterByKey[key] : null);
368 }, 396 },
369 397
370 /** 398 /**
371 * Returns a list of all selectors active on a particular domain (currently 399 * Returns a list of all selectors active on a particular domain, must not be
372 * used only in Chrome, Opera and Safari). 400 * used in Firefox (when usingFiltersByDomain is false).
373 */ 401 */
374 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 402 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly)
375 { 403 {
376 let result = []; 404 if (!usingFiltersByDomain)
377 let keys = Object.getOwnPropertyNames(filterByKey); 405 throw new Error("getSelectorsForDomain can not be used in Firefox!");
378 for (let key of keys) 406
407 let selectors = [];
408
409 let seenFilters = Object.create(null);
410 let currentDomain = domain ? domain.toUpperCase() : "";
411 while (true)
379 { 412 {
380 let filter = filterByKey[key]; 413 if (specificOnly && currentDomain == "")
381 if (specificOnly && (!filter.domains || filter.domains[""])) 414 break;
382 continue;
383 415
384 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) 416 let filters = filtersByDomain[currentDomain];
385 result.push(filter.selector); 417 if (filters)
418 {
419 for (let filterText in filters)
420 {
421 if (filterText in seenFilters)
422 continue;
423 seenFilters[filterText] = true;
424
425 let filter = filters[filterText];
426 if (filter && !this.getException(filter, domain))
427 selectors.push(filter.selector);
428 }
429 }
430
431 if (currentDomain == "")
432 break;
433
434 let nextDot = currentDomain.indexOf(".");
435 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
386 } 436 }
387 return result; 437
438 return selectors;
388 } 439 }
389 }; 440 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld