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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29764555: Noissue - Use two sets for excluded and included filters by domain (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created April 28, 2018, 4:15 p.m.
Right Patch Set: Rebase and minor changes Created May 3, 2018, 12:44 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 | « no previous file | no next file » | 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 */ 45 */
46 let unconditionalSelectors = null; 46 let unconditionalSelectors = null;
47 47
48 /** 48 /**
49 * Map to be used instead when a filter has a blank domains property. 49 * Map to be used instead when a filter has a blank domains property.
50 */ 50 */
51 let defaultDomains = new Map([["", true]]); 51 let defaultDomains = new Map([["", true]]);
52 52
53 /** 53 /**
54 * Set containing known element hiding and exception filters 54 * Set containing known element hiding and exception filters
55 * @type {Set.<string>} 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 /** 65 /**
(...skipping 13 matching lines...) Expand all
79 } 79 }
80 unconditionalSelectors = null; 80 unconditionalSelectors = null;
81 FilterNotifier.emit("elemhideupdate"); 81 FilterNotifier.emit("elemhideupdate");
82 }, 82 },
83 83
84 _addToFiltersByDomain(filter) 84 _addToFiltersByDomain(filter)
85 { 85 {
86 let domains = filter.domains || defaultDomains; 86 let domains = filter.domains || defaultDomains;
87 for (let [domain, isIncluded] of domains) 87 for (let [domain, isIncluded] of domains)
88 { 88 {
89 // There's no need to note that a filter is generically disabled.
90 if (!isIncluded && domain == "")
91 continue;
92
89 let filters = filtersByDomain.get(domain); 93 let filters = filtersByDomain.get(domain);
90 if (!filters) 94 if (!filters)
91 filtersByDomain.set(domain, filters = []); 95 filtersByDomain.set(domain, filters = []);
92 96
93 let setIndex = +isIncluded; 97 let setIndex = +isIncluded;
Manish Jethani 2018/04/28 16:35:35 Conveniently boolean converted to number gives us
94 if (!filters[setIndex]) 98 if (!filters[setIndex])
Manish Jethani 2018/04/28 16:35:35 The number of entries is the same as before. If th
95 filters[setIndex] = new Set(); 99 filters[setIndex] = new Set();
96 100
97 filters[setIndex].add(filter); 101 filters[setIndex].add(filter);
98 } 102 }
99 }, 103 },
100 104
101 /** 105 /**
102 * Add a new element hiding filter 106 * Add a new element hiding filter
103 * @param {ElemHideFilter} filter 107 * @param {ElemHideBase} filter
104 */ 108 */
105 add(filter) 109 add(filter)
106 { 110 {
107 if (knownFilters.has(filter)) 111 if (knownFilters.has(filter))
108 return; 112 return;
109 113
110 if (filter instanceof ElemHideException) 114 if (filter instanceof ElemHideException)
111 { 115 {
112 let {selector} = filter; 116 let {selector} = filter;
113 let list = exceptions.get(selector); 117 let list = exceptions.get(selector);
(...skipping 24 matching lines...) Expand all
138 // The new filter's selector only applies to some domains 142 // The new filter's selector only applies to some domains
139 this._addToFiltersByDomain(filter); 143 this._addToFiltersByDomain(filter);
140 } 144 }
141 145
142 knownFilters.add(filter); 146 knownFilters.add(filter);
143 FilterNotifier.emit("elemhideupdate"); 147 FilterNotifier.emit("elemhideupdate");
144 }, 148 },
145 149
146 /** 150 /**
147 * Removes an element hiding filter 151 * Removes an element hiding filter
148 * @param {ElemHideFilter} filter 152 * @param {ElemHideBase} filter
149 */ 153 */
150 remove(filter) 154 remove(filter)
151 { 155 {
152 if (!knownFilters.has(filter)) 156 if (!knownFilters.has(filter))
153 return; 157 return;
154 158
155 // Whitelisting filters 159 // Whitelisting filters
156 if (filter instanceof ElemHideException) 160 if (filter instanceof ElemHideException)
157 { 161 {
158 let list = exceptions.get(filter.selector); 162 let list = exceptions.get(filter.selector);
159 let index = list.indexOf(filter); 163 let index = list.indexOf(filter);
160 if (index >= 0) 164 if (index >= 0)
161 list.splice(index, 1); 165 list.splice(index, 1);
162 } 166 }
163 // Unconditially applied element hiding filters 167 // Unconditially applied element hiding filters
164 else if (filterBySelector.get(filter.selector) === filter) 168 else if (filterBySelector.get(filter.selector) === filter)
165 { 169 {
166 filterBySelector.delete(filter.selector); 170 filterBySelector.delete(filter.selector);
167 unconditionalSelectors = null; 171 unconditionalSelectors = null;
168 } 172 }
169 // Conditionally applied element hiding filters 173 // Conditionally applied element hiding filters
170 else 174 else
171 { 175 {
172 let domains = filter.domains || defaultDomains; 176 let domains = filter.domains || defaultDomains;
173 for (let domain of domains.keys()) 177 for (let domain of domains.keys())
174 { 178 {
175 for (let set of filtersByDomain.get(domain) || []) 179 let filters = filtersByDomain.get(domain) || [];
180 for (let i = 0; i < filters.length; i++)
176 { 181 {
177 if (set) 182 if (!filters[i])
178 set.delete(filter); 183 continue;
184
185 filters[i].delete(filter);
186
187 if (filters[i].size == 0)
188 delete filters[i];
179 } 189 }
180 } 190 }
181 } 191 }
182 192
183 knownFilters.delete(filter); 193 knownFilters.delete(filter);
184 FilterNotifier.emit("elemhideupdate"); 194 FilterNotifier.emit("elemhideupdate");
185 }, 195 },
186 196
187 /** 197 /**
188 * Checks whether an exception rule is registered for a filter on a particular 198 * Checks whether an exception rule is registered for a filter on a particular
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 getSelectorsForDomain(domain, criteria) 258 getSelectorsForDomain(domain, criteria)
249 { 259 {
250 let selectors = []; 260 let selectors = [];
251 261
252 if (typeof criteria == "undefined") 262 if (typeof criteria == "undefined")
253 criteria = ElemHide.ALL_MATCHING; 263 criteria = ElemHide.ALL_MATCHING;
254 if (criteria < ElemHide.NO_UNCONDITIONAL) 264 if (criteria < ElemHide.NO_UNCONDITIONAL)
255 selectors = this.getUnconditionalSelectors(); 265 selectors = this.getUnconditionalSelectors();
256 266
257 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 267 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY);
258 let seenFilters = []; 268 let excludedFilters = [];
Manish Jethani 2018/04/28 16:35:35 This is now an array of sets. The maximum number o
269
259 let currentDomain = domain ? domain.toUpperCase() : ""; 270 let currentDomain = domain ? domain.toUpperCase() : "";
271 let currentDomainIsGeneric = currentDomain == "";
272
273 // This code is a performance hot-spot, which is why we've made certain
274 // micro-optimisations. Please be careful before making changes.
260 while (true) 275 while (true)
261 { 276 {
262 if (specificOnly && currentDomain == "") 277 if (specificOnly && currentDomainIsGeneric)
263 break; 278 break;
264 279
265 let [excluded, included] = filtersByDomain.get(currentDomain) || []; 280 let [excluded, included] = filtersByDomain.get(currentDomain) || [];
281
266 if (excluded) 282 if (excluded)
267 seenFilters.push(excluded); 283 excludedFilters.push(excluded);
268 284
269 for (let filter of included || []) 285 if (included)
270 { 286 {
271 for (let set of seenFilters) 287 for (let filter of included)
Manish Jethani 2018/04/28 18:47:06 By the way, in case it isn't clear, the reason thi
272 { 288 {
Manish Jethani 2018/04/28 18:47:06 Note that as a side effect of this change a filter
273 if (set.has(filter)) 289 for (let i = 0; i < excludedFilters.length; i++)
274 { 290 {
275 filter = null; 291 if (excludedFilters[i].has(filter))
276 break; 292 {
293 filter = null;
294 break;
295 }
277 } 296 }
297
298 if (filter && !this.getException(filter, domain))
299 selectors.push(filter.selector);
278 } 300 }
279
280 if (filter && !this.getException(filter, domain))
281 selectors.push(filter.selector);
282 } 301 }
283 302
284 if (currentDomain == "") 303 if (currentDomainIsGeneric)
285 break; 304 break;
286 305
287 let nextDot = currentDomain.indexOf("."); 306 let nextDot = currentDomain.indexOf(".");
288 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 307 if (nextDot == -1)
308 {
309 currentDomain = "";
310 currentDomainIsGeneric = true;
311 }
312 else
313 currentDomain = currentDomain.substr(nextDot + 1);
289 } 314 }
290 315
291 return selectors; 316 return selectors;
292 } 317 }
293 }; 318 };
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld