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

Side by Side Diff: lib/elemHide.js

Issue 29773570: Issue 6652 - Implement fast selector lookups for unknown domains (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Limit size of generic-friendly domains set Created May 11, 2018, 10:08 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-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, active flag, by filter by domain. 28 * Lookup table, active flag, by filter by domain.
29 * (Only contains filters that aren't unconditionally matched for all domains.) 29 * (Only contains filters that aren't unconditionally matched for all domains.)
30 * @type {Map.<string,Map.<Filter,boolean>>} 30 * @type {Map.<string,?Map.<Filter,boolean>>}
31 */ 31 */
32 let filtersByDomain = new Map(); 32 let filtersByDomain = new Map();
33 33
34 /** 34 /**
35 * Lookup table, filter by selector. (Only used for selectors that are 35 * Lookup table, filter by selector. (Only used for selectors that are
36 * unconditionally matched for all domains.) 36 * unconditionally matched for all domains.)
37 * @type {Map.<string,Filter>} 37 * @type {Map.<string,Filter>}
38 */ 38 */
39 let filterBySelector = new Map(); 39 let filterBySelector = new Map();
40 40
(...skipping 13 matching lines...) Expand all
54 let defaultDomains = new Map([["", true]]); 54 let defaultDomains = new Map([["", true]]);
55 55
56 /** 56 /**
57 * Set containing known element hiding and exception filters 57 * Set containing known element hiding and exception filters
58 * @type {Set.<ElemHideBase>} 58 * @type {Set.<ElemHideBase>}
59 */ 59 */
60 let knownFilters = new Set(); 60 let knownFilters = new Set();
61 61
62 /** 62 /**
63 * Lookup table, lists of element hiding exceptions by selector 63 * Lookup table, lists of element hiding exceptions by selector
64 * @type {Map.<string,Filter>} 64 * @type {Map.<string,Filter[]>}
65 */ 65 */
66 let exceptions = new Map(); 66 let exceptions = new Map();
67 67
68 /** 68 /**
69 * Lookup table, lists of generic element hiding exceptions by selector
70 * @type {Map.<string,Filter[]>}
71 */
72 let genericExceptions = new Map();
73
74 /**
75 * List of selectors that apply on any unknown domain
76 * @type {?string[]}
77 */
78 let conditionalGenericSelectors = null;
79
80 /**
81 * Domains that are known not to be specifically excluded from any generic
82 * filters
83 * @type {Set.<string>}
84 */
85 let genericFriendlyDomains = new Set();
86
87 /**
88 * Returns a list of domain-specific filters matching a domain
89 * @param {string} [domain]
90 * @returns {Array.<?Map.<Filter,boolean>>}
91 */
92 function getSpecificFiltersForDomain(domain)
93 {
94 let filtersList = [];
95
96 if (domain)
97 domain = domain.toUpperCase();
98
99 while (domain)
100 {
101 // Note that we also push null values into the list, because
102 // ElemHide.getSelectorsForDomain still needs to know if there are any
103 // entries for the domain.
104 let filters = filtersByDomain.get(domain);
105 if (typeof filters != "undefined")
106 filtersList.push(filters);
107
108 let nextDot = domain.indexOf(".");
109 domain = nextDot == -1 ? null : domain.substring(nextDot + 1);
110 }
111
112 return filtersList;
113 }
114
115 /**
116 * Returns a list of selectors from a given list of filters that apply on a
117 * domain
118 * @param {string} [domain]
119 * @param {Array.<?Map.<Filter,boolean>>} filtersList
120 * @param {boolean} specificOnly
121 * @returns {string[]}
122 */
123 function getConditionalSelectorsForDomain(domain, filtersList, specificOnly)
124 {
125 let selectors = [];
126
127 let genericFilters = !specificOnly ? filtersList.pop() : null;
128 let excluded = new Set();
129
130 // This code is a performance hot-spot, which is why we've made certain
131 // micro-optimisations. Please be careful before making changes.
132 for (let i = 0; i < filtersList.length; i++)
133 {
134 if (!filtersList[i])
135 continue;
136
137 for (let [filter, isIncluded] of filtersList[i])
138 {
139 if (!isIncluded)
140 {
141 excluded.add(filter);
142 }
143 else if ((excluded.size == 0 || !excluded.has(filter)) &&
144 !ElemHide.getException(filter, domain))
145 {
146 selectors.push(filter.selector);
147 }
148 }
149 }
150
151 if (!genericFilters)
152 return selectors;
153
154 if (genericFriendlyDomains.has(domain))
155 return selectors.concat(getConditionalGenericSelectors());
156
157 let genericSelectors = [];
158
159 for (let filter of genericFilters.keys())
160 {
161 if ((excluded.size == 0 || !excluded.has(filter)) &&
162 !ElemHide.getException(filter, domain))
163 {
164 genericSelectors.push(filter.selector);
165 }
166 }
167
168 // If the number of conditional generic selectors that apply on this domain
169 // is the same as the total number of conditional generic selectors, the
170 // domain is "generic friendly". In that case, we mark it is as such for
171 // faster lookups.
172 if (conditionalGenericSelectors &&
173 genericSelectors.length == conditionalGenericSelectors.length)
174 {
175 if (genericFriendlyDomains.size >= 1000)
176 genericFriendlyDomains.clear();
177
178 genericFriendlyDomains.add(domain);
179 }
180
181 return selectors.concat(genericSelectors);
182 }
183
184 /**
185 * Returns a list of selectors that apply on any unknown domain
186 * @returns {string[]}
187 */
188 function getConditionalGenericSelectors()
189 {
190 if (conditionalGenericSelectors)
191 return conditionalGenericSelectors;
192
193 conditionalGenericSelectors = [];
194
195 let filters = filtersByDomain.get("");
196 if (!filters)
197 return conditionalGenericSelectors;
198
199 for (let {selector} of filters.keys())
200 {
201 if (genericExceptions.size == 0 || !genericExceptions.has(selector))
202 conditionalGenericSelectors.push(selector);
203 }
204
205 return conditionalGenericSelectors;
206 }
207
208 /**
69 * Returns a list of selectors that apply on each website unconditionally. 209 * Returns a list of selectors that apply on each website unconditionally.
70 * @returns {string[]} 210 * @returns {string[]}
71 */ 211 */
72 function getUnconditionalSelectors() 212 function getUnconditionalSelectors()
73 { 213 {
74 if (!unconditionalSelectors) 214 if (!unconditionalSelectors)
75 unconditionalSelectors = [...filterBySelector.keys()]; 215 unconditionalSelectors = [...filterBySelector.keys()];
76 216
77 return unconditionalSelectors; 217 return unconditionalSelectors;
78 } 218 }
79 219
80 /** 220 /**
81 * Container for element hiding filters 221 * Container for element hiding filters
82 * @class 222 * @class
83 */ 223 */
84 let ElemHide = exports.ElemHide = { 224 let ElemHide = exports.ElemHide = {
85 /** 225 /**
86 * Removes all known filters 226 * Removes all known filters
87 */ 227 */
88 clear() 228 clear()
89 { 229 {
90 for (let collection of [filtersByDomain, filterBySelector, 230 for (let collection of [filtersByDomain, filterBySelector,
91 knownFilters, exceptions]) 231 knownFilters, exceptions,
232 genericExceptions, genericFriendlyDomains])
92 { 233 {
93 collection.clear(); 234 collection.clear();
94 } 235 }
95 unconditionalSelectors = null; 236 unconditionalSelectors = null;
237 conditionalGenericSelectors = null;
96 FilterNotifier.emit("elemhideupdate"); 238 FilterNotifier.emit("elemhideupdate");
97 }, 239 },
98 240
99 _addToFiltersByDomain(filter) 241 _addToFiltersByDomain(filter)
100 { 242 {
101 let domains = filter.domains || defaultDomains; 243 let domains = filter.domains || defaultDomains;
102 for (let [domain, isIncluded] of domains) 244 if (filter instanceof ElemHideException)
103 { 245 {
104 // There's no need to note that a filter is generically disabled. 246 for (let domain of domains.keys())
105 if (!isIncluded && domain == "") 247 {
106 continue; 248 // Add an entry for each domain, but without any filters. This makes
249 // the domain "known" and helps us avoid certain optimizations that
250 // would otherwise yield incorrect results.
251 if (domain != "" && !filtersByDomain.has(domain))
252 filtersByDomain.set(domain, null);
253 }
254 }
255 else
256 {
257 for (let [domain, isIncluded] of domains)
258 {
259 // There's no need to note that a filter is generically disabled.
260 if (!isIncluded && domain == "")
261 continue;
107 262
108 let filters = filtersByDomain.get(domain); 263 let filters = filtersByDomain.get(domain);
109 if (!filters) 264 if (!filters)
110 filtersByDomain.set(domain, filters = new Map()); 265 filtersByDomain.set(domain, filters = new Map());
111 filters.set(filter, isIncluded); 266 filters.set(filter, isIncluded);
267 }
112 } 268 }
113 }, 269 },
114 270
115 /** 271 /**
116 * Add a new element hiding filter 272 * Add a new element hiding filter
117 * @param {ElemHideBase} filter 273 * @param {ElemHideBase} filter
118 */ 274 */
119 add(filter) 275 add(filter)
120 { 276 {
121 if (knownFilters.has(filter)) 277 if (knownFilters.has(filter))
122 return; 278 return;
123 279
280 conditionalGenericSelectors = null;
281 genericFriendlyDomains.clear();
282
124 if (filter instanceof ElemHideException) 283 if (filter instanceof ElemHideException)
125 { 284 {
126 let {selector} = filter; 285 let {selector, domains} = filter;
286
127 let list = exceptions.get(selector); 287 let list = exceptions.get(selector);
128 if (list) 288 if (list)
129 list.push(filter); 289 list.push(filter);
130 else 290 else
131 exceptions.set(selector, [filter]); 291 exceptions.set(selector, [filter]);
132 292
293 if (domains)
294 this._addToFiltersByDomain(filter);
295
296 if (filter.isGeneric())
297 {
298 list = genericExceptions.get(selector);
299 if (list)
300 list.push(filter);
301 else
302 genericExceptions.set(selector, [filter]);
303 }
304
133 // If this is the first exception for a previously unconditionally 305 // If this is the first exception for a previously unconditionally
134 // applied element hiding selector we need to take care to update the 306 // applied element hiding selector we need to take care to update the
135 // lookups. 307 // lookups.
136 let unconditionalFilterForSelector = filterBySelector.get(selector); 308 let unconditionalFilterForSelector = filterBySelector.get(selector);
137 if (unconditionalFilterForSelector) 309 if (unconditionalFilterForSelector)
138 { 310 {
139 this._addToFiltersByDomain(unconditionalFilterForSelector); 311 this._addToFiltersByDomain(unconditionalFilterForSelector);
140 filterBySelector.delete(selector); 312 filterBySelector.delete(selector);
141 unconditionalSelectors = null; 313 unconditionalSelectors = null;
142 } 314 }
(...skipping 16 matching lines...) Expand all
159 331
160 /** 332 /**
161 * Removes an element hiding filter 333 * Removes an element hiding filter
162 * @param {ElemHideBase} filter 334 * @param {ElemHideBase} filter
163 */ 335 */
164 remove(filter) 336 remove(filter)
165 { 337 {
166 if (!knownFilters.has(filter)) 338 if (!knownFilters.has(filter))
167 return; 339 return;
168 340
341 conditionalGenericSelectors = null;
342 genericFriendlyDomains.clear();
343
169 // Whitelisting filters 344 // Whitelisting filters
170 if (filter instanceof ElemHideException) 345 if (filter instanceof ElemHideException)
171 { 346 {
172 let list = exceptions.get(filter.selector); 347 let list = exceptions.get(filter.selector);
173 let index = list.indexOf(filter); 348 let index = list.indexOf(filter);
174 if (index >= 0) 349 if (index >= 0)
175 list.splice(index, 1); 350 list.splice(index, 1);
351
352 if (filter.isGeneric())
353 {
354 list = genericExceptions.get(filter.selector);
355 index = list.indexOf(filter);
356 if (index >= 0)
357 list.splice(index, 1);
358
359 if (list.length == 0)
360 genericExceptions.delete(filter.selector);
361 }
176 } 362 }
177 // Unconditially applied element hiding filters 363 // Unconditially applied element hiding filters
178 else if (filterBySelector.get(filter.selector) == filter) 364 else if (filterBySelector.get(filter.selector) == filter)
179 { 365 {
180 filterBySelector.delete(filter.selector); 366 filterBySelector.delete(filter.selector);
181 unconditionalSelectors = null; 367 unconditionalSelectors = null;
182 } 368 }
183 // Conditionally applied element hiding filters 369 // Conditionally applied element hiding filters
184 else 370 else
185 { 371 {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 * @param {number} [criteria] 435 * @param {number} [criteria]
250 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or 436 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
251 * ElemHide.SPECIFIC_ONLY. 437 * ElemHide.SPECIFIC_ONLY.
252 * @returns {string[]} 438 * @returns {string[]}
253 * List of selectors. 439 * List of selectors.
254 */ 440 */
255 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) 441 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING)
256 { 442 {
257 let selectors = []; 443 let selectors = [];
258 444
259 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 445 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY;
260 let excluded = new Set(); 446 let filtersList = getSpecificFiltersForDomain(domain);
261 let currentDomain = domain ? domain.toUpperCase() : "";
262 447
263 // This code is a performance hot-spot, which is why we've made certain 448 if (filtersList.length > 0)
264 // micro-optimisations. Please be careful before making changes.
265 while (true)
266 { 449 {
267 if (specificOnly && currentDomain == "") 450 if (!specificOnly)
268 break; 451 filtersList.push(filtersByDomain.get(""));
269 452
270 let filters = filtersByDomain.get(currentDomain); 453 selectors = getConditionalSelectorsForDomain(domain, filtersList,
271 if (filters) 454 specificOnly);
272 { 455 }
273 for (let [filter, isIncluded] of filters) 456 else if (!specificOnly)
274 { 457 {
275 if (!isIncluded) 458 selectors = getConditionalGenericSelectors();
276 {
277 excluded.add(filter);
278 }
279 else if ((excluded.size == 0 || !excluded.has(filter)) &&
280 !this.getException(filter, domain))
281 {
282 selectors.push(filter.selector);
283 }
284 }
285 }
286
287 if (currentDomain == "")
288 break;
289
290 let nextDot = currentDomain.indexOf(".");
291 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
292 } 459 }
293 460
294 if (criteria < ElemHide.NO_UNCONDITIONAL) 461 if (criteria < ElemHide.NO_UNCONDITIONAL)
295 selectors = getUnconditionalSelectors().concat(selectors); 462 selectors = getUnconditionalSelectors().concat(selectors);
296 463
464 // If the above logic leaves us with a reference to our internal cache of
465 // selectors, we make a copy here.
466 if (selectors == conditionalGenericSelectors)
467 selectors = selectors.slice();
468
297 return selectors; 469 return selectors;
298 } 470 }
299 }; 471 };
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