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

Side by Side Diff: lib/elemHide.js

Issue 29342830: Issue 4057 - Further speedup ElemHide.getSelectorsforDomain (Closed)
Patch Set: Addressed comments Created May 25, 2016, 5:09 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 | test/elemHide.js » ('j') | 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 = []; 34 var filterByKey = [];
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 43 * Indicates whether we are using the getSelectorsForDomain function and
44 * therefore mainting the required filtersByDomain, filtersBySelector and
45 * unconditionalSelectors lookups. (Will be false for Firefox)
46 * @type Boolean
47 */
48 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci);
49
50 /**
51 * Nested lookup table, filter (or false if inactive) by filter key by domain.
52 * (Only contains filters that aren't unconditionally matched for all domains.)
44 * @type Object 53 * @type Object
45 */ 54 */
46 var filtersByDomain = Object.create(null); 55 var filtersByDomain = Object.create(null);
47 56
48 /** 57 /**
49 * Indicates whether we are using (and maintaining) the filtersByDomain lookup. 58 * Lookup table, filters by selector. (Only contains filters that have a
50 * (Will be false for Firefox) 59 * selector that is unconditionally matched for all domains.)
51 * @type Boolean
52 */ 60 */
53 var usingFiltersByDomain = !("nsIStyleSheetService" in Ci); 61 var filtersBySelector = Object.create(null);
62
63 /**
64 * Array of selectors which unconditionally apply to all domains.
65 */
66 var unconditionalSelectors = [];
67
68 /**
69 * Indicates that the unconditionalSelectors Array needs to be regenerated.
70 */
71 var unconditionalSelectorsDirty = false;
54 72
55 /** 73 /**
56 * Object to be used instead when a filter has a blank domains property. 74 * Object to be used instead when a filter has a blank domains property.
57 */ 75 */
58 var defaultDomains = Object.create(null); 76 var defaultDomains = Object.create(null);
59 defaultDomains[""] = true; 77 defaultDomains[""] = true;
60 78
61 /** 79 /**
62 * Lookup table, keys are known element hiding exceptions 80 * Lookup table, keys are known element hiding exceptions
63 * @type Object 81 * @type Object
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 }, 130 },
113 131
114 /** 132 /**
115 * Removes all known filters 133 * Removes all known filters
116 */ 134 */
117 clear: function() 135 clear: function()
118 { 136 {
119 filterByKey = []; 137 filterByKey = [];
120 keyByFilter = Object.create(null); 138 keyByFilter = Object.create(null);
121 filtersByDomain = Object.create(null); 139 filtersByDomain = Object.create(null);
140 filtersBySelector = Object.create(null);
141 unconditionalSelectors = [];
142 unconditionalSelectorsDirty = false;
122 knownExceptions = Object.create(null); 143 knownExceptions = Object.create(null);
123 exceptions = Object.create(null); 144 exceptions = Object.create(null);
124 ElemHide.isDirty = false; 145 ElemHide.isDirty = false;
125 ElemHide.unapply(); 146 ElemHide.unapply();
126 }, 147 },
127 148
149 _addToFiltersByDomain: function(filter)
150 {
151 let key = keyByFilter[filter.text];
152 let domains = filter.domains || defaultDomains;
153 for (let domain in domains)
154 {
155 let filters = filtersByDomain[domain];
156 if (!filters)
157 filters = filtersByDomain[domain] = Object.create(null);
158
159 if (domains[domain])
160 filters[key] = filter;
161 else
162 filters[key] = false;
163 }
164 },
165
128 /** 166 /**
129 * Add a new element hiding filter 167 * Add a new element hiding filter
130 * @param {ElemHideFilter} filter 168 * @param {ElemHideFilter} filter
131 */ 169 */
132 add: function(filter) 170 add: function(filter)
133 { 171 {
134 if (filter instanceof ElemHideException) 172 if (filter instanceof ElemHideException)
135 { 173 {
136 if (filter.text in knownExceptions) 174 if (filter.text in knownExceptions)
137 return; 175 return;
138 176
139 let selector = filter.selector; 177 let selector = filter.selector;
140 if (!(selector in exceptions)) 178 if (!(selector in exceptions))
141 exceptions[selector] = []; 179 exceptions[selector] = [];
142 exceptions[selector].push(filter); 180 exceptions[selector].push(filter);
181
182 if (usingGetSelectorsForDomain)
183 {
184 // If this is the first exception for a previously unconditionally
185 // applied element hiding selector we need to take care to update the
186 // lookups.
187 let unconditionalFilters = filtersBySelector[selector];
188 if (unconditionalFilters)
189 {
190 for (let f of unconditionalFilters)
191 this._addToFiltersByDomain(f);
192 delete filtersBySelector[selector];
193 unconditionalSelectorsDirty = true;
194 }
195 }
196
143 knownExceptions[filter.text] = true; 197 knownExceptions[filter.text] = true;
144 } 198 }
145 else 199 else
146 { 200 {
147 if (filter.text in keyByFilter) 201 if (filter.text in keyByFilter)
148 return; 202 return;
149 203
150 let key = filterByKey.push(filter) - 1; 204 let key = filterByKey.push(filter) - 1;
151 keyByFilter[filter.text] = key; 205 keyByFilter[filter.text] = key;
152 206
153 if (usingFiltersByDomain) 207 if (usingGetSelectorsForDomain)
154 { 208 {
155 let domains = filter.domains || defaultDomains; 209 if (!(filter.domains || filter.selector in exceptions))
156 for (let domain in domains)
157 { 210 {
158 let filters = filtersByDomain[domain]; 211 // The new filter's selector is unconditionally applied to all domains
159 if (!filters) 212 let filters = filtersBySelector[filter.selector];
160 filters = filtersByDomain[domain] = Object.create(null); 213 if (filters)
161 214 {
162 if (domains[domain]) 215 filters.push(filter);
163 filters[filter.text] = filter; 216 }
164 else 217 else
165 filters[filter.text] = false; 218 {
219 filtersBySelector[filter.selector] = [filter];
220 unconditionalSelectorsDirty = true;
221 }
222 }
223 else
224 {
225 // The new filter's selector only applies to some domains
226 this._addToFiltersByDomain(filter);
166 } 227 }
167 } 228 }
168 229
169 ElemHide.isDirty = true; 230 ElemHide.isDirty = true;
170 } 231 }
171 }, 232 },
172 233
173 /** 234 /**
174 * Removes an element hiding filter 235 * Removes an element hiding filter
175 * @param {ElemHideFilter} filter 236 * @param {ElemHideFilter} filter
(...skipping 14 matching lines...) Expand all
190 else 251 else
191 { 252 {
192 if (!(filter.text in keyByFilter)) 253 if (!(filter.text in keyByFilter))
193 return; 254 return;
194 255
195 let key = keyByFilter[filter.text]; 256 let key = keyByFilter[filter.text];
196 delete filterByKey[key]; 257 delete filterByKey[key];
197 delete keyByFilter[filter.text]; 258 delete keyByFilter[filter.text];
198 ElemHide.isDirty = true; 259 ElemHide.isDirty = true;
199 260
200 if (usingFiltersByDomain) 261 if (usingGetSelectorsForDomain)
201 { 262 {
202 let domains = filter.domains || defaultDomains; 263 let filters = filtersBySelector[filter.selector];
203 for (let domain in domains) 264 if (filters)
204 { 265 {
205 let filters = filtersByDomain[domain]; 266 if (filters.length > 1)
206 if (filters) 267 {
207 delete filters[filter.text]; 268 let index = filters.indexOf(filter);
269 filters.splice(index, 1);
270 }
271 else
272 {
273 delete filtersBySelector[filter.selector];
274 unconditionalSelectorsDirty = true;
275 }
276 }
277 else
278 {
279 let domains = filter.domains || defaultDomains;
280 for (let domain in domains)
281 {
282 let filters = filtersByDomain[domain];
283 if (filters)
284 delete filters[key];
285 }
208 } 286 }
209 } 287 }
210 } 288 }
211 }, 289 },
212 290
213 /** 291 /**
214 * Checks whether an exception rule is registered for a filter on a particular 292 * Checks whether an exception rule is registered for a filter on a particular
215 * domain. 293 * domain.
216 */ 294 */
217 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 295 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 /** 487 /**
410 * Retrieves an element hiding filter by the corresponding protocol key 488 * Retrieves an element hiding filter by the corresponding protocol key
411 */ 489 */
412 getFilterByKey: function(/**String*/ key) /**Filter*/ 490 getFilterByKey: function(/**String*/ key) /**Filter*/
413 { 491 {
414 return (key in filterByKey ? filterByKey[key] : null); 492 return (key in filterByKey ? filterByKey[key] : null);
415 }, 493 },
416 494
417 /** 495 /**
418 * Returns a list of all selectors active on a particular domain, must not be 496 * Returns a list of all selectors active on a particular domain, must not be
419 * used in Firefox (when usingFiltersByDomain is false). 497 * used in Firefox (when usingGetSelectorsForDomain is false).
420 */ 498 */
421 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 499 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly)
422 { 500 {
423 if (!usingFiltersByDomain) 501 if (!usingGetSelectorsForDomain)
424 throw new Error("getSelectorsForDomain can not be used in Firefox!"); 502 throw new Error("getSelectorsForDomain can not be used in Firefox!");
425 503
426 let selectors = []; 504 if (unconditionalSelectorsDirty)
505 {
506 unconditionalSelectors = Object.keys(filtersBySelector);
507 unconditionalSelectorsDirty = false;
508 }
509 let selectors = specificOnly ? [] : unconditionalSelectors.slice();
Sebastian Noack 2016/05/25 07:30:49 You can skip the unconditionalSelectorsDirty logic
Sebastian Noack 2016/05/25 07:30:49 Also note that if you'd call Object.keys() uncondi
Sebastian Noack 2016/05/25 07:40:26 One more thing, if we need that is dirty logic, ho
kzar 2016/05/25 08:05:19 Done.
kzar 2016/05/25 08:05:19 I know I could skip restoring unconditionalSelecto
kzar 2016/05/25 08:05:19 Yep, just double checked and it's still quite a bi
Sebastian Noack 2016/05/25 08:11:56 Besides a speedup in specificOnly case which might
kzar 2016/05/25 08:29:04 I disagree on this one, but I see your point. I'll
Wladimir Palant 2016/05/25 10:43:03 Frankly, while I can see the arguments towards onl
427 510
428 let seenFilters = Object.create(null); 511 let seenFilters = Object.create(null);
429 let currentDomain = domain ? domain.toUpperCase() : ""; 512 let currentDomain = domain ? domain.toUpperCase() : "";
430 while (true) 513 while (true)
431 { 514 {
432 if (specificOnly && currentDomain == "") 515 if (specificOnly && currentDomain == "")
433 break; 516 break;
434 517
435 let filters = filtersByDomain[currentDomain]; 518 let filters = filtersByDomain[currentDomain];
436 if (filters) 519 if (filters)
437 { 520 {
438 for (let filterText in filters) 521 for (let filterKey in filters)
439 { 522 {
440 if (filterText in seenFilters) 523 if (filterKey in seenFilters)
441 continue; 524 continue;
442 seenFilters[filterText] = true; 525 seenFilters[filterKey] = true;
443 526
444 let filter = filters[filterText]; 527 let filter = filters[filterKey];
445 if (filter && !this.getException(filter, domain)) 528 if (filter && !this.getException(filter, domain))
446 selectors.push(filter.selector); 529 selectors.push(filter.selector);
447 } 530 }
448 } 531 }
449 532
450 if (currentDomain == "") 533 if (currentDomain == "")
451 break; 534 break;
452 535
453 let nextDot = currentDomain.indexOf("."); 536 let nextDot = currentDomain.indexOf(".");
454 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 537 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
455 } 538 }
456 539
457 return selectors; 540 return selectors;
458 } 541 }
459 }; 542 };
OLDNEW
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld