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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29773570: Issue 6652 - Implement fast selector lookups for unknown domains (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Move selector matching into its own function Created May 13, 2018, 12:12 p.m.
Right Patch Set: Avoid unnecessary Array.concat Created May 23, 2018, 12:22 a.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 | « no previous file | test/elemHide.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
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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[]>}
kzar 2018/05/15 13:26:31 I guess you need to rebase again.
Manish Jethani 2018/05/15 16:00:08 Done.
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 69 * Lookup table, lists of generic element hiding exceptions by selector
70 * @type {Map.<string,Filter[]>} 70 * @type {Map.<string,Filter[]>}
71 */ 71 */
72 let genericExceptions = new Map(); 72 let genericExceptions = new Map();
73 73
74 /** 74 /**
75 * List of selectors that apply on any unknown domain 75 * List of selectors that apply on any unknown domain
76 * @type {?string[]} 76 * @type {?string[]}
77 */ 77 */
78 let conditionalGenericSelectors = null; 78 let conditionalGenericSelectors = null;
kzar 2018/05/15 13:26:31 I think this cache is probably a good idea. I figu
Manish Jethani 2018/05/15 16:00:07 It also applies to known domains (see my other com
79 79
80 /** 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 * Adds a filter to the lookup table of filters by domain. 81 * Adds a filter to the lookup table of filters by domain.
89 * @param {Filter} filter 82 * @param {Filter} filter
90 */ 83 */
91 function addToFiltersByDomain(filter) 84 function addToFiltersByDomain(filter)
92 { 85 {
93 let domains = filter.domains || defaultDomains; 86 let domains = filter.domains || defaultDomains;
94 if (filter instanceof ElemHideException) 87 if (filter instanceof ElemHideException)
95 { 88 {
96 for (let domain of domains.keys()) 89 for (let domain of domains.keys())
97 { 90 {
98 // Add an entry for each domain, but without any filters. This makes 91 // Add an entry for each domain, but without any filters. This makes
99 // the domain "known" and helps us avoid certain optimizations that 92 // the domain "known" and helps us avoid certain optimizations that
100 // would otherwise yield incorrect results. 93 // would otherwise yield incorrect results.
101 if (domain != "" && !filtersByDomain.has(domain)) 94 if (domain != "" && !filtersByDomain.has(domain))
102 filtersByDomain.set(domain, null); 95 filtersByDomain.set(domain, null);
kzar 2018/05/15 13:26:30 Am I missing something or do we never remove the d
Manish Jethani 2018/05/15 16:00:08 You're right, we never remove a domain like that f
kzar 2018/05/18 14:34:53 Acknowledged.
103 } 96 }
104 } 97 }
105 else 98 else
106 { 99 {
107 for (let [domain, isIncluded] of domains) 100 for (let [domain, isIncluded] of domains)
108 { 101 {
109 // There's no need to note that a filter is generically disabled. 102 // There's no need to note that a filter is generically disabled.
110 if (!isIncluded && domain == "") 103 if (!isIncluded && domain == "")
111 continue; 104 continue;
112 105
113 let filters = filtersByDomain.get(domain); 106 let filters = filtersByDomain.get(domain);
114 if (!filters) 107 if (!filters)
115 filtersByDomain.set(domain, filters = new Map()); 108 filtersByDomain.set(domain, filters = new Map());
116 filters.set(filter, isIncluded); 109 filters.set(filter, isIncluded);
117 } 110 }
118 } 111 }
119 } 112 }
120 113
121 /** 114 /**
122 * Checks whether a filter applies on a domain
123 * @param {Filter} filter
124 * @param {string} [domain]
125 * @param {Set.<Filter>} excludeSet
126 * @returns {boolean}
127 */
128 function doesFilterApply(filter, domain, excludeSet)
129 {
130 return (excludeSet.size == 0 || !excludeSet.has(filter)) &&
131 !ElemHide.getException(filter, domain);
132 }
133
134 /**
135 * Returns a list of domain-specific filters matching a domain 115 * Returns a list of domain-specific filters matching a domain
136 * @param {string} [domain] 116 * @param {string} [domain]
137 * @returns {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} 117 * @returns {Array.<?Map.<Filter,boolean>>}
138 */ 118 */
139 function getSpecificFiltersForDomain(domain) 119 function getSpecificFiltersForDomain(domain)
Manish Jethani 2018/05/15 16:20:59 Note: getSpecificFiltersForDomain could be reused
kzar 2018/05/18 14:34:53 Acknowledged.
140 { 120 {
141 let filtersList = []; 121 let filtersList = [];
142 122
143 if (domain) 123 if (domain)
144 domain = domain.toUpperCase(); 124 domain = domain.toUpperCase();
145 125
146 while (domain) 126 while (domain)
147 { 127 {
148 let filters = filtersByDomain.get(domain); 128 let filters = filtersByDomain.get(domain);
149 if (typeof filters != "undefined") 129 if (typeof filters != "undefined")
150 filtersList.push({domain, filters}); 130 filtersList.push(filters);
151 131
152 let nextDot = domain.indexOf("."); 132 let nextDot = domain.indexOf(".");
153 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); 133 domain = nextDot == -1 ? null : domain.substring(nextDot + 1);
154 } 134 }
155 135
156 return filtersList; 136 return filtersList;
157 } 137 }
158 138
159 /** 139 /**
160 * Returns a list of selectors that apply on a domain from a given list of 140 * Returns a list of selectors that apply on a domain from a given list of
161 * filters 141 * filters
162 * @param {string} [domain] 142 * @param {string} [domain]
163 * @param {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} 143 * @param {Array.<?Map.<Filter,boolean>>} filtersList
164 * @param {Set.<Filter>} excludeSet 144 * @param {Set.<Filter>} excludeSet
165 * @returns {string[]} 145 * @returns {string[]}
166 */ 146 */
167 function matchSelectors(domain, filtersList, excludeSet) 147 function matchSelectors(domain, filtersList, excludeSet)
Manish Jethani 2018/05/15 16:20:59 Note: matchSelectors could be reused by ElemHideEm
168 { 148 {
169 let matches = []; 149 let matches = [];
170 150
171 // This code is a performance hot-spot, which is why we've made certain 151 // This code is a performance hot-spot, which is why we've made certain
172 // micro-optimisations. Please be careful before making changes. 152 // micro-optimisations. Please be careful before making changes.
173 for (let i = 0; i < filtersList.length; i++) 153 for (let i = 0; i < filtersList.length; i++)
174 { 154 {
175 let filters = filtersList[i].filters; 155 let filters = filtersList[i];
176 if (filters) 156 if (filters)
177 { 157 {
178 for (let [filter, isIncluded] of filters) 158 for (let [filter, isIncluded] of filters)
179 { 159 {
180 if (!isIncluded) 160 if (!isIncluded)
161 {
181 excludeSet.add(filter); 162 excludeSet.add(filter);
182 else if (doesFilterApply(filter, domain, excludeSet)) 163 }
164 else if ((excludeSet.size == 0 || !excludeSet.has(filter)) &&
165 !exports.ElemHide.getException(filter, domain))
166 {
183 matches.push(filter.selector); 167 matches.push(filter.selector);
168 }
184 } 169 }
185 } 170 }
186 } 171 }
187 172
188 return matches; 173 return matches;
189 }
190
191 /**
192 * Returns a list of selectors that apply on a domain
193 * @param {string} [domain]
194 * @param {boolean} specificOnly
195 * @returns {string[]}
196 */
197 function getConditionalSelectorsForDomain(domain, specificOnly)
198 {
199 let specificFilters = getSpecificFiltersForDomain(domain);
200
201 // If there are no specific filters (nor any specific exceptions), we can
202 // just return the selectors from all the generic filters modulo any generic
203 // exceptions.
204 if (specificFilters.length == 0)
205 return specificOnly ? [] : getConditionalGenericSelectors();
kzar 2018/05/18 14:34:53 Nit: Seems a waste to create a new empty Array whe
206
207 let excludeSet = new Set();
208 let specificSelectors = matchSelectors(domain, specificFilters, excludeSet);
209
210 if (specificOnly)
211 return specificSelectors;
212
213 // We use the longest subdomain of this domain found in our data structures
214 // as the key to check if the domain is "generic friendly." For example,
215 // given foo.example.com, there may be an entry for example.com in our data
216 // structures (e.g. "example.com###foo"), so we use that subdomain as the
217 // key. This way we make only one entry and it works for all subdomains of
218 // example.com, except those that have specific entries
219 // (e.g. "~bar.example.com##.no-bar").
220 let domainKey = specificFilters[0].domain;
221
222 if (genericFriendlyDomains.has(domainKey))
223 return specificSelectors.concat(getConditionalGenericSelectors());
224
225 let genericFilters = [{filters: filtersByDomain.get("")}];
226 let genericSelectors = matchSelectors(domain, genericFilters, excludeSet);
227
228 // If the number of conditional generic selectors that apply on this domain
229 // is the same as the total number of conditional generic selectors, the
230 // domain is "generic friendly" (i.e. all generic filters apply, except those
231 // with generic exceptions). In that case, we mark it is as such for faster
232 // lookups.
233 if (genericSelectors.length == (conditionalGenericSelectors || {}).length)
234 genericFriendlyDomains.add(domainKey);
235
236 return specificSelectors.concat(genericSelectors);
237 } 174 }
238 175
239 /** 176 /**
240 * Returns a list of selectors that apply on any unknown domain 177 * Returns a list of selectors that apply on any unknown domain
241 * @returns {string[]} 178 * @returns {string[]}
242 */ 179 */
243 function getConditionalGenericSelectors() 180 function getConditionalGenericSelectors()
244 { 181 {
245 if (conditionalGenericSelectors) 182 if (conditionalGenericSelectors)
246 return conditionalGenericSelectors; 183 return conditionalGenericSelectors;
(...skipping 22 matching lines...) Expand all
269 if (!unconditionalSelectors) 206 if (!unconditionalSelectors)
270 unconditionalSelectors = [...filterBySelector.keys()]; 207 unconditionalSelectors = [...filterBySelector.keys()];
271 208
272 return unconditionalSelectors; 209 return unconditionalSelectors;
273 } 210 }
274 211
275 /** 212 /**
276 * Container for element hiding filters 213 * Container for element hiding filters
277 * @class 214 * @class
278 */ 215 */
279 let ElemHide = exports.ElemHide = { 216 exports.ElemHide = {
280 /** 217 /**
281 * Removes all known filters 218 * Removes all known filters
282 */ 219 */
283 clear() 220 clear()
284 { 221 {
285 for (let collection of [filtersByDomain, filterBySelector, 222 for (let collection of [filtersByDomain, filterBySelector,
286 knownFilters, exceptions, 223 knownFilters, exceptions,
287 genericExceptions, genericFriendlyDomains]) 224 genericExceptions])
288 { 225 {
289 collection.clear(); 226 collection.clear();
290 } 227 }
291 unconditionalSelectors = null; 228 unconditionalSelectors = null;
292 conditionalGenericSelectors = null; 229 conditionalGenericSelectors = null;
293 FilterNotifier.emit("elemhideupdate"); 230 FilterNotifier.emit("elemhideupdate");
294 }, 231 },
295 232
296 /** 233 /**
297 * Add a new element hiding filter 234 * Add a new element hiding filter
298 * @param {ElemHideBase} filter 235 * @param {ElemHideBase} filter
299 */ 236 */
300 add(filter) 237 add(filter)
301 { 238 {
302 if (knownFilters.has(filter)) 239 if (knownFilters.has(filter))
303 return; 240 return;
304 241
305 conditionalGenericSelectors = null; 242 conditionalGenericSelectors = null;
306 genericFriendlyDomains.clear();
307 243
308 if (filter instanceof ElemHideException) 244 if (filter instanceof ElemHideException)
309 { 245 {
310 let {selector, domains} = filter; 246 let {selector, domains} = filter;
311 247
312 let list = exceptions.get(selector); 248 let list = exceptions.get(selector);
313 if (list) 249 if (list)
314 list.push(filter); 250 list.push(filter);
315 else 251 else
316 exceptions.set(selector, [filter]); 252 exceptions.set(selector, [filter]);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 /** 293 /**
358 * Removes an element hiding filter 294 * Removes an element hiding filter
359 * @param {ElemHideBase} filter 295 * @param {ElemHideBase} filter
360 */ 296 */
361 remove(filter) 297 remove(filter)
362 { 298 {
363 if (!knownFilters.has(filter)) 299 if (!knownFilters.has(filter))
364 return; 300 return;
365 301
366 conditionalGenericSelectors = null; 302 conditionalGenericSelectors = null;
367 genericFriendlyDomains.clear();
368 303
369 // Whitelisting filters 304 // Whitelisting filters
370 if (filter instanceof ElemHideException) 305 if (filter instanceof ElemHideException)
371 { 306 {
372 let list = exceptions.get(filter.selector); 307 let list = exceptions.get(filter.selector);
373 let index = list.indexOf(filter); 308 let index = list.indexOf(filter);
374 if (index >= 0) 309 if (index >= 0)
375 list.splice(index, 1); 310 list.splice(index, 1);
376 311
377 if (filter.isGeneric()) 312 if (filter.isGeneric())
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 for (let i = list.length - 1; i >= 0; i--) 360 for (let i = list.length - 1; i >= 0; i--)
426 { 361 {
427 if (list[i].isActiveOnDomain(docDomain)) 362 if (list[i].isActiveOnDomain(docDomain))
428 return list[i]; 363 return list[i];
429 } 364 }
430 365
431 return null; 366 return null;
432 }, 367 },
433 368
434 /** 369 /**
435 * Constant used by getSelectorsForDomain to return all selectors applying to
436 * a particular hostname.
437 * @type {number}
438 * @const
439 */
440 ALL_MATCHING: 0,
441
442 /**
443 * Constant used by getSelectorsForDomain to exclude selectors which apply to
444 * all websites without exception.
445 * @type {number}
446 * @const
447 */
448 NO_UNCONDITIONAL: 1,
449
450 /**
451 * Constant used by getSelectorsForDomain to return only selectors for filters
452 * which specifically match the given host name.
453 * @type {number}
454 * @const
455 */
456 SPECIFIC_ONLY: 2,
457
458 /**
459 * Determines from the current filter list which selectors should be applied 370 * Determines from the current filter list which selectors should be applied
460 * on a particular host name. 371 * on a particular host name.
461 * @param {string} domain 372 * @param {string} domain
462 * @param {number} [criteria] 373 * @param {boolean} [specificOnly] true if generic filters should not apply.
463 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or 374 * @returns {string[]} List of selectors.
464 * ElemHide.SPECIFIC_ONLY.
465 * @returns {string[]}
466 * List of selectors.
467 */ 375 */
468 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) 376 getSelectorsForDomain(domain, specificOnly = false)
469 { 377 {
470 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY; 378 let specificFilters = getSpecificFiltersForDomain(domain);
471 let selectors = getConditionalSelectorsForDomain(domain, specificOnly); 379
kzar 2018/05/15 13:26:31 I feel like some of these changes (like this one,
Manish Jethani 2018/05/15 16:00:08 Yeah this patch has been updated a lot. Now the fu
Manish Jethani 2018/05/15 16:16:15 Actually there are three return statements (out of
Manish Jethani 2018/05/15 16:20:59 I meant getConditionalSelectorsForDomain of course
472 380 // If there are no specific filters (nor any specific exceptions), we can
473 if (criteria < ElemHide.NO_UNCONDITIONAL) 381 // just return the selectors from all the generic filters modulo any
474 selectors = getUnconditionalSelectors().concat(selectors); 382 // generic exceptions.
475 else if (criteria == ElemHide.NO_UNCONDITIONAL) 383 if (specificFilters.length == 0)
476 selectors = selectors.slice(); 384 {
477 385 return specificOnly ? [] :
478 return selectors; 386 getUnconditionalSelectors()
387 .concat(getConditionalGenericSelectors());
388 }
389
390 let excluded = new Set();
391 let selectors = matchSelectors(domain, specificFilters, excluded);
392
393 if (specificOnly)
394 return selectors;
395
396 return getUnconditionalSelectors()
397 .concat(selectors,
398 matchSelectors(domain, [filtersByDomain.get("")],
399 excluded));
479 } 400 }
480 }; 401 };
LEFTRIGHT
« no previous file | test/elemHide.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld