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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29743730: Issue 6559 - Use maps and sets where appropriate (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created April 5, 2018, 5:38 p.m.
Right Patch Set: Revert logic in getUnconditionalFilterKeys Created April 6, 2018, 1:32 p.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/downloader.js ('k') | lib/elemHideEmulation.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-present eyeo GmbH 3 * Copyright (C) 2006-present 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Element hiding implementation. 21 * @fileOverview Element hiding implementation.
22 */ 22 */
23 23
24 const {ElemHideException} = require("./filterClasses"); 24 const {ElemHideException} = require("./filterClasses");
25 const {FilterNotifier} = require("./filterNotifier"); 25 const {FilterNotifier} = require("./filterNotifier");
26 26
27 /** 27 /**
28 * Lookup table, filters by their associated key 28 * Lookup table, filters by their associated key
29 * @type {Object} 29 * @type {Filter[]}
30 */ 30 */
31 let filterByKey = []; 31 let filterByKey = [];
32 32
33 /** 33 /**
34 * Lookup table, keys of the filters by filter 34 * Lookup table, keys of the filters by filter
35 * @type {Map.<Filter,string>} 35 * @type {Map.<Filter,number>}
36 */ 36 */
37 let keyByFilter = new Map(); 37 let keyByFilter = new Map();
38 38
39 /** 39 /**
40 * Nested lookup table, filter (or false if inactive) by filter key by domain. 40 * Nested lookup table, filter (or false if inactive) by filter key by domain.
41 * (Only contains filters that aren't unconditionally matched for all domains.) 41 * (Only contains filters that aren't unconditionally matched for all domains.)
42 * @type {Map.<string,Map.<string,(Filter|boolean)>>} 42 * @type {Map.<string,Map.<number,(Filter|boolean)>>}
43 */ 43 */
44 let filtersByDomain = new Map(); 44 let filtersByDomain = new Map();
45 45
46 /** 46 /**
47 * Lookup table, filter key by selector. (Only used for selectors that are 47 * Lookup table, filter key by selector. (Only used for selectors that are
48 * unconditionally matched for all domains.) 48 * unconditionally matched for all domains.)
49 * @type {Map.<string,number>} 49 * @type {Map.<string,number>}
50 */ 50 */
51 let filterKeyBySelector = new Map(); 51 let filterKeyBySelector = new Map();
52 52
(...skipping 10 matching lines...) Expand all
63 * cache needs to be rebuilt. 63 * cache needs to be rebuilt.
64 */ 64 */
65 let unconditionalFilterKeys = null; 65 let unconditionalFilterKeys = null;
66 66
67 /** 67 /**
68 * Object to be used instead when a filter has a blank domains property. 68 * Object to be used instead when a filter has a blank domains property.
69 */ 69 */
70 let defaultDomains = new Map([["", true]]); 70 let defaultDomains = new Map([["", true]]);
71 71
72 /** 72 /**
73 * Lookup table, keys are known element hiding exceptions 73 * Set containing known element hiding exceptions
kzar 2018/04/06 11:42:20 Nit: This comment reads a bit weirdly now that it'
Manish Jethani 2018/04/06 12:44:09 Done.
74 * @type {Set.<string>} 74 * @type {Set.<string>}
75 */ 75 */
76 let knownExceptions = new Set(); 76 let knownExceptions = new Set();
77 77
78 /** 78 /**
79 * Lookup table, lists of element hiding exceptions by selector 79 * Lookup table, lists of element hiding exceptions by selector
80 * @type {Map.<string,Filter>} 80 * @type {Map.<string,Filter>}
81 */ 81 */
82 let exceptions = new Map(); 82 let exceptions = new Map();
83 83
84 /** 84 /**
85 * Container for element hiding filters 85 * Container for element hiding filters
86 * @class 86 * @class
87 */ 87 */
88 let ElemHide = exports.ElemHide = { 88 let ElemHide = exports.ElemHide = {
89 /** 89 /**
90 * Removes all known filters 90 * Removes all known filters
91 */ 91 */
92 clear() 92 clear()
93 { 93 {
94 for (let collection of [keyByFilter, filtersByDomain, filterKeyBySelector, 94 for (let collection of [keyByFilter, filtersByDomain, filterKeyBySelector,
Manish Jethani 2018/04/06 05:36:16 Note that there's no need to create new objects fo
95 knownExceptions, exceptions]) 95 knownExceptions, exceptions])
96 { 96 {
97 collection.clear(); 97 collection.clear();
98 } 98 }
99 filterByKey = []; 99 filterByKey = [];
100 unconditionalSelectors = unconditionalFilterKeys = null; 100 unconditionalSelectors = unconditionalFilterKeys = null;
101 FilterNotifier.emit("elemhideupdate"); 101 FilterNotifier.emit("elemhideupdate");
102 }, 102 },
103 103
104 _addToFiltersByDomain(key, filter) 104 _addToFiltersByDomain(key, filter)
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 221
222 /** 222 /**
223 * Checks whether an exception rule is registered for a filter on a particular 223 * Checks whether an exception rule is registered for a filter on a particular
224 * domain. 224 * domain.
225 * @param {Filter} filter 225 * @param {Filter} filter
226 * @param {string} docDomain 226 * @param {string} docDomain
227 * @return {ElemHideException} 227 * @return {ElemHideException}
228 */ 228 */
229 getException(filter, docDomain) 229 getException(filter, docDomain)
230 { 230 {
231 let list = exceptions.get(filter.selector); 231 let list = exceptions.get(filter.selector);
Manish Jethani 2018/04/06 05:36:16 If list is falsy we know it's undefined, since we
kzar 2018/04/06 11:42:20 Acknowledged.
232 if (!list) 232 if (!list)
233 return null; 233 return null;
234 234
235 for (let i = list.length - 1; i >= 0; i--) 235 for (let i = list.length - 1; i >= 0; i--)
236 { 236 {
237 if (list[i].isActiveOnDomain(docDomain)) 237 if (list[i].isActiveOnDomain(docDomain))
238 return list[i]; 238 return list[i];
239 } 239 }
240 240
241 return null; 241 return null;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 return unconditionalSelectors.slice(); 288 return unconditionalSelectors.slice();
289 }, 289 },
290 290
291 /** 291 /**
292 * Returns a list of filter keys for selectors which apply to all websites 292 * Returns a list of filter keys for selectors which apply to all websites
293 * without exception. 293 * without exception.
294 * @returns {number[]} 294 * @returns {number[]}
295 */ 295 */
296 getUnconditionalFilterKeys() 296 getUnconditionalFilterKeys()
297 { 297 {
298 if (!unconditionalFilterKeys) 298 if (!unconditionalFilterKeys)
Manish Jethani 2018/04/06 05:36:16 Essentially we're getting the values of filterKeyB
kzar 2018/04/06 11:42:20 Previously we were caching though. But come to th
Manish Jethani 2018/04/06 12:44:09 So it looks like we could do away with the concept
kzar 2018/04/06 13:07:54 Cool, so looks like we could remove that parameter
Manish Jethani 2018/04/06 13:34:37 Sure, go ahead.
299 unconditionalFilterKeys = [...filterKeyBySelector.values()]; 299 {
300 let selectors = this.getUnconditionalSelectors();
301 unconditionalFilterKeys = [];
302 for (let selector of selectors)
303 unconditionalFilterKeys.push(filterKeyBySelector.get(selector));
304 }
300 return unconditionalFilterKeys.slice(); 305 return unconditionalFilterKeys.slice();
301 }, 306 },
302 307
303 308
304 /** 309 /**
305 * Constant used by getSelectorsForDomain to return all selectors applying to 310 * Constant used by getSelectorsForDomain to return all selectors applying to
306 * a particular hostname. 311 * a particular hostname.
307 */ 312 */
308 ALL_MATCHING: 0, 313 ALL_MATCHING: 0,
309 314
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 while (true) 359 while (true)
355 { 360 {
356 if (specificOnly && currentDomain == "") 361 if (specificOnly && currentDomain == "")
357 break; 362 break;
358 363
359 let filters = filtersByDomain.get(currentDomain); 364 let filters = filtersByDomain.get(currentDomain);
360 if (filters) 365 if (filters)
361 { 366 {
362 for (let [filterKey, filter] of filters) 367 for (let [filterKey, filter] of filters)
363 { 368 {
364 if (seenFilters.has(filterKey)) 369 if (seenFilters.has(filterKey))
kzar 2018/04/06 11:42:20 This is a hotspot, we spent quite some time gettin
Manish Jethani 2018/04/06 12:44:09 I ran this particular test and it's 2s vs 1.6s on
kzar 2018/04/06 13:07:54 Acknowledged.
Manish Jethani 2018/04/06 13:44:11 BTW I tried replacing `filterKey in seenFilters` w
365 continue; 370 continue;
366 seenFilters.add(filterKey); 371 seenFilters.add(filterKey);
367 372
368 if (filter && !this.getException(filter, domain)) 373 if (filter && !this.getException(filter, domain))
369 { 374 {
370 selectors.push(filter.selector); 375 selectors.push(filter.selector);
371 // It is faster to always push the key, even if not required. 376 // It is faster to always push the key, even if not required.
372 filterKeys.push(filterKey); 377 filterKeys.push(filterKey);
373 } 378 }
374 } 379 }
375 } 380 }
376 381
377 if (currentDomain == "") 382 if (currentDomain == "")
378 break; 383 break;
379 384
380 let nextDot = currentDomain.indexOf("."); 385 let nextDot = currentDomain.indexOf(".");
381 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 386 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
382 } 387 }
383 388
384 if (provideFilterKeys) 389 if (provideFilterKeys)
385 return [selectors, filterKeys]; 390 return [selectors, filterKeys];
386 return selectors; 391 return selectors;
387 } 392 }
388 }; 393 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld