Left: | ||
Right: |
OLD | NEW |
---|---|
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 14 matching lines...) Expand all Loading... | |
55 * @type {Set.<ElemHideBase>} | 55 * @type {Set.<ElemHideBase>} |
56 */ | 56 */ |
57 let knownFilters = new Set(); | 57 let knownFilters = new Set(); |
58 | 58 |
59 /** | 59 /** |
60 * Lookup table, lists of element hiding exceptions by selector | 60 * Lookup table, lists of element hiding exceptions by selector |
61 * @type {Map.<string,Filter>} | 61 * @type {Map.<string,Filter>} |
62 */ | 62 */ |
63 let exceptions = new Map(); | 63 let exceptions = new Map(); |
64 | 64 |
65 /* | |
66 * Set containing selectors with generic exceptions | |
67 * @type {Set.<string>} | |
68 */ | |
69 let genericExceptionSelectors = new Set(); | |
70 | |
71 /* | |
72 * Checks if a domain is known | |
73 * @param {string} domain | |
74 * @returns {boolean} | |
75 */ | |
76 function isDomainKnown(domain) | |
77 { | |
78 while (domain) | |
79 { | |
80 // A domain is "known" if we have seen any filters that would apply to it. | |
81 // For example, given the filters "##foo" and "example.com#@#foo", | |
82 // example.com is a known domain, as is mail.example.com and any other | |
83 // subdomains of example.com. | |
84 if (filtersByDomain.has(domain)) | |
85 return true; | |
86 | |
87 let nextDot = domain.indexOf("."); | |
88 domain = nextDot == -1 ? null : domain.substring(nextDot + 1); | |
89 } | |
90 | |
91 return false; | |
92 } | |
93 | |
94 /* | |
95 * Returns a list of selectors that apply on any unknown domain | |
96 * @returns {string[]} | |
97 */ | |
98 function getConditionalGenericSelectors() | |
99 { | |
100 let selectors = []; | |
101 | |
102 let filters = filtersByDomain.get(""); | |
103 if (!filters) | |
104 return selectors; | |
105 | |
106 for (let {selector} of filters.keys()) | |
107 { | |
108 if (genericExceptionSelectors.size == 0 || | |
Manish Jethani
2018/05/07 16:15:08
Right now some selectors are disabled on all domai
| |
109 !genericExceptionSelectors.has(selector)) | |
110 { | |
111 selectors.push(selector); | |
112 } | |
113 } | |
114 | |
115 return selectors; | |
116 } | |
117 | |
65 /** | 118 /** |
66 * Container for element hiding filters | 119 * Container for element hiding filters |
67 * @class | 120 * @class |
68 */ | 121 */ |
69 let ElemHide = exports.ElemHide = { | 122 let ElemHide = exports.ElemHide = { |
70 /** | 123 /** |
71 * Removes all known filters | 124 * Removes all known filters |
72 */ | 125 */ |
73 clear() | 126 clear() |
74 { | 127 { |
75 for (let collection of [filtersByDomain, filterBySelector, | 128 for (let collection of [filtersByDomain, filterBySelector, |
76 knownFilters, exceptions]) | 129 knownFilters, exceptions, |
130 genericExceptionSelectors]) | |
77 { | 131 { |
78 collection.clear(); | 132 collection.clear(); |
79 } | 133 } |
80 unconditionalSelectors = null; | 134 unconditionalSelectors = null; |
81 FilterNotifier.emit("elemhideupdate"); | 135 FilterNotifier.emit("elemhideupdate"); |
82 }, | 136 }, |
83 | 137 |
84 _addToFiltersByDomain(filter) | 138 _addToFiltersByDomain(filter) |
85 { | 139 { |
86 let domains = filter.domains || defaultDomains; | 140 let domains = filter.domains || defaultDomains; |
87 for (let [domain, isIncluded] of domains) | 141 if (filter instanceof ElemHideException) |
88 { | 142 { |
89 // There's no need to note that a filter is generically disabled. | 143 for (let domain of domains.keys()) |
90 if (!isIncluded && domain == "") | 144 { |
91 continue; | 145 // Add an entry for each domain, but without any filters. This makes |
146 // the domain "known" and helps us avoid the optimized path (which | |
147 // would give incorrect results). | |
148 if (domain != "" && !filtersByDomain.has(domain)) | |
149 filtersByDomain.set(domain, null); | |
150 } | |
151 } | |
152 else | |
153 { | |
154 for (let [domain, isIncluded] of domains) | |
155 { | |
156 // There's no need to note that a filter is generically disabled. | |
157 if (!isIncluded && domain == "") | |
158 continue; | |
92 | 159 |
93 let filters = filtersByDomain.get(domain); | 160 let filters = filtersByDomain.get(domain); |
94 if (!filters) | 161 if (!filters) |
95 filtersByDomain.set(domain, filters = new Map()); | 162 filtersByDomain.set(domain, filters = new Map()); |
96 filters.set(filter, isIncluded); | 163 filters.set(filter, isIncluded); |
164 } | |
97 } | 165 } |
98 }, | 166 }, |
99 | 167 |
100 /** | 168 /** |
101 * Add a new element hiding filter | 169 * Add a new element hiding filter |
102 * @param {ElemHideBase} filter | 170 * @param {ElemHideBase} filter |
103 */ | 171 */ |
104 add(filter) | 172 add(filter) |
105 { | 173 { |
106 if (knownFilters.has(filter)) | 174 if (knownFilters.has(filter)) |
107 return; | 175 return; |
108 | 176 |
109 if (filter instanceof ElemHideException) | 177 if (filter instanceof ElemHideException) |
110 { | 178 { |
111 let {selector} = filter; | 179 let {selector, domains} = filter; |
180 | |
112 let list = exceptions.get(selector); | 181 let list = exceptions.get(selector); |
113 if (list) | 182 if (list) |
114 list.push(filter); | 183 list.push(filter); |
115 else | 184 else |
116 exceptions.set(selector, [filter]); | 185 exceptions.set(selector, [filter]); |
117 | 186 |
187 if (domains) | |
188 this._addToFiltersByDomain(filter); | |
189 | |
190 if (filter.isGeneric()) | |
191 genericExceptionSelectors.add(filter.selector); | |
192 | |
118 // If this is the first exception for a previously unconditionally | 193 // If this is the first exception for a previously unconditionally |
119 // applied element hiding selector we need to take care to update the | 194 // applied element hiding selector we need to take care to update the |
120 // lookups. | 195 // lookups. |
121 let unconditionalFilterForSelector = filterBySelector.get(selector); | 196 let unconditionalFilterForSelector = filterBySelector.get(selector); |
122 if (unconditionalFilterForSelector) | 197 if (unconditionalFilterForSelector) |
123 { | 198 { |
124 this._addToFiltersByDomain(unconditionalFilterForSelector); | 199 this._addToFiltersByDomain(unconditionalFilterForSelector); |
125 filterBySelector.delete(selector); | 200 filterBySelector.delete(selector); |
126 unconditionalSelectors = null; | 201 unconditionalSelectors = null; |
127 } | 202 } |
(...skipping 23 matching lines...) Expand all Loading... | |
151 if (!knownFilters.has(filter)) | 226 if (!knownFilters.has(filter)) |
152 return; | 227 return; |
153 | 228 |
154 // Whitelisting filters | 229 // Whitelisting filters |
155 if (filter instanceof ElemHideException) | 230 if (filter instanceof ElemHideException) |
156 { | 231 { |
157 let list = exceptions.get(filter.selector); | 232 let list = exceptions.get(filter.selector); |
158 let index = list.indexOf(filter); | 233 let index = list.indexOf(filter); |
159 if (index >= 0) | 234 if (index >= 0) |
160 list.splice(index, 1); | 235 list.splice(index, 1); |
236 | |
237 if (filter.isGeneric()) | |
238 genericExceptionSelectors.delete(filter.selector); | |
161 } | 239 } |
162 // Unconditially applied element hiding filters | 240 // Unconditially applied element hiding filters |
163 else if (filterBySelector.get(filter.selector) == filter) | 241 else if (filterBySelector.get(filter.selector) == filter) |
164 { | 242 { |
165 filterBySelector.delete(filter.selector); | 243 filterBySelector.delete(filter.selector); |
166 unconditionalSelectors = null; | 244 unconditionalSelectors = null; |
167 } | 245 } |
168 // Conditionally applied element hiding filters | 246 // Conditionally applied element hiding filters |
169 else | 247 else |
170 { | 248 { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 getSelectorsForDomain(domain, criteria) | 323 getSelectorsForDomain(domain, criteria) |
246 { | 324 { |
247 let selectors = []; | 325 let selectors = []; |
248 | 326 |
249 if (typeof criteria == "undefined") | 327 if (typeof criteria == "undefined") |
250 criteria = ElemHide.ALL_MATCHING; | 328 criteria = ElemHide.ALL_MATCHING; |
251 if (criteria < ElemHide.NO_UNCONDITIONAL) | 329 if (criteria < ElemHide.NO_UNCONDITIONAL) |
252 selectors = this.getUnconditionalSelectors(); | 330 selectors = this.getUnconditionalSelectors(); |
253 | 331 |
254 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 332 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); |
255 let excluded = new Set(); | |
256 let currentDomain = domain ? domain.toUpperCase() : ""; | 333 let currentDomain = domain ? domain.toUpperCase() : ""; |
257 | 334 |
258 // This code is a performance hot-spot, which is why we've made certain | 335 if (isDomainKnown(currentDomain)) |
kzar
2018/05/08 13:46:47
I don't quite understand how your change speeds th
Manish Jethani
2018/05/08 14:20:43
Let me come back with an answer after investigatin
Manish Jethani
2018/05/08 15:49:00
Alright, I checked.
Fifty percent of the speedup
kzar
2018/05/08 17:13:43
Interesting, I wonder why that is. Is something in
Manish Jethani
2018/05/08 17:37:13
Yes, I found out:
https://codereview.adblockplus.
Manish Jethani
2018/05/08 17:49:30
getException should really be called hasException
| |
259 // micro-optimisations. Please be careful before making changes. | |
260 while (true) | |
261 { | 336 { |
262 if (specificOnly && currentDomain == "") | 337 let excluded = new Set(); |
263 break; | |
264 | 338 |
265 let filters = filtersByDomain.get(currentDomain); | 339 // This code is a performance hot-spot, which is why we've made certain |
266 if (filters) | 340 // micro-optimisations. Please be careful before making changes. |
341 while (true) | |
267 { | 342 { |
268 for (let [filter, isIncluded] of filters) | 343 if (specificOnly && currentDomain == "") |
344 break; | |
345 | |
346 let filters = filtersByDomain.get(currentDomain); | |
347 if (filters) | |
269 { | 348 { |
270 if (!isIncluded) | 349 for (let [filter, isIncluded] of filters) |
271 { | 350 { |
272 excluded.add(filter); | 351 if (!isIncluded) |
273 } | 352 { |
274 else if ((excluded.size == 0 || !excluded.has(filter)) && | 353 excluded.add(filter); |
275 !this.getException(filter, domain)) | 354 } |
276 { | 355 else if ((excluded.size == 0 || !excluded.has(filter)) && |
277 selectors.push(filter.selector); | 356 !this.getException(filter, domain)) |
357 { | |
358 selectors.push(filter.selector); | |
359 } | |
278 } | 360 } |
279 } | 361 } |
362 | |
363 if (currentDomain == "") | |
364 break; | |
365 | |
366 let nextDot = currentDomain.indexOf("."); | |
367 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | |
280 } | 368 } |
281 | 369 } |
282 if (currentDomain == "") | 370 else if (!specificOnly) |
283 break; | 371 { |
284 | 372 selectors = selectors.concat(getConditionalGenericSelectors()); |
kzar
2018/05/08 13:46:47
Won't selectors always be [] here? I wonder why we
Manish Jethani
2018/05/08 14:20:43
No, the selectors array will contain the unconditi
kzar
2018/05/08 17:13:44
Ah yea, gotya.
| |
285 let nextDot = currentDomain.indexOf("."); | |
286 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | |
287 } | 373 } |
288 | 374 |
289 return selectors; | 375 return selectors; |
290 } | 376 } |
291 }; | 377 }; |
OLD | NEW |