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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 29882558: Issue 6955 - Avoid making copies of common selector list (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Cache common selector list Created Sept. 17, 2018, 9:20 a.m.
Right Patch Set: Keep track of exceptions per domain Created Sept. 17, 2018, 12:15 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 | lib/elemHideExceptions.js » ('j') | test/elemHide.js » ('J')
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 29 matching lines...) Expand all
40 40
41 /** 41 /**
42 * This array caches the keys of filterBySelector table (selectors 42 * This array caches the keys of filterBySelector table (selectors
43 * which unconditionally apply on all domains). It will be null if the 43 * which unconditionally apply on all domains). It will be null if the
44 * cache needs to be rebuilt. 44 * cache needs to be rebuilt.
45 * @type {?string[]} 45 * @type {?string[]}
46 */ 46 */
47 let unconditionalSelectors = null; 47 let unconditionalSelectors = null;
48 48
49 /** 49 /**
50 * Cached list of selectors that apply on any domain that neither has any 50 * Cached list of selectors that apply on any domain that neither has any
Manish Jethani 2018/09/17 09:28:43 Here we are introducing the idea of "common" selec
51 * domain-specific filters or exceptions nor is specifically excluded from any 51 * domain-specific filters or exceptions nor is specifically excluded from any
52 * generic filters or exceptions. 52 * generic filters or exceptions.
53 * @type {?string[]} 53 * @type {?string[]}
54 */ 54 */
55 let commonSelectors = null; 55 let commonSelectors = null;
56 56
57 /** 57 /**
58 * Map to be used instead when a filter has a blank domains property. 58 * Map to be used instead when a filter has a blank domains property.
59 * @type {Map.<string,boolean>} 59 * @type {Map.<string,boolean>}
60 * @const 60 * @const
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 * @param {boolean} [specificOnly] true if generic filters should not apply. 209 * @param {boolean} [specificOnly] true if generic filters should not apply.
210 * @returns {string[]} List of selectors. 210 * @returns {string[]} List of selectors.
211 */ 211 */
212 getSelectorsForDomain(domain, specificOnly = false) 212 getSelectorsForDomain(domain, specificOnly = false)
213 { 213 {
214 let selectors = []; 214 let selectors = [];
215 215
216 let excluded = new Set(); 216 let excluded = new Set();
217 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : ""; 217 let currentDomain = domain ? domain.replace(/\.+$/, "").toLowerCase() : "";
218 218
219 let commonCase = true; 219 let commonCase = !specificOnly;
220 220
221 // This code is a performance hot-spot, which is why we've made certain 221 // This code is a performance hot-spot, which is why we've made certain
222 // micro-optimisations. Please be careful before making changes. 222 // micro-optimisations. Please be careful before making changes.
223 while (true) 223 while (true)
224 { 224 {
225 if (specificOnly && currentDomain == "") 225 if (specificOnly && currentDomain == "")
226 break; 226 break;
227
228 if (commonCase && currentDomain != "" &&
229 ElemHideExceptions.domainHasExceptions(currentDomain))
230 {
231 commonCase = false;
232 }
227 233
228 let filters = filtersByDomain.get(currentDomain); 234 let filters = filtersByDomain.get(currentDomain);
229 if (filters) 235 if (filters)
230 { 236 {
231 if (currentDomain != "") 237 if (currentDomain != "")
232 commonCase = false; 238 commonCase = false;
233 239
234 for (let [filter, isIncluded] of filters) 240 for (let [filter, isIncluded] of filters)
235 { 241 {
236 if (!isIncluded) 242 if (!isIncluded)
237 { 243 {
238 excluded.add(filter); 244 excluded.add(filter);
239 } 245 }
240 else 246 else
241 { 247 {
242 let {selector} = filter; 248 let {selector} = filter;
243 if (excluded.size == 0 || !excluded.has(filter)) 249 if ((excluded.size == 0 || !excluded.has(filter)) &&
250 !ElemHideExceptions.getException(selector, domain))
244 { 251 {
245 let exception = ElemHideExceptions.getException(selector, domain); 252 selectors.push(selector);
246 if (!exception)
247 selectors.push(selector);
248 else if (commonCase && exception.domains)
249 commonCase = false;
250 } 253 }
251 } 254 }
252 } 255 }
253 } 256 }
254 257
255 if (currentDomain == "") 258 if (currentDomain == "")
256 break; 259 break;
257 260
258 let nextDot = currentDomain.indexOf("."); 261 let nextDot = currentDomain.indexOf(".");
259 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); 262 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
260 } 263 }
261 264
262 if (!specificOnly) 265 if (!specificOnly)
263 { 266 {
264 // Use the cache of common selectors if there are no filters or 267 // Use the cache of common selectors if there are no filters or
265 // exceptions that apply or don't apply specifically to this domain. 268 // exceptions that apply or don't apply specifically to this domain.
266 if (commonCase) 269 if (commonCase)
267 { 270 {
268 if (!commonSelectors) 271 if (!commonSelectors)
269 { 272 {
270 let unconditional = getUnconditionalSelectors(); 273 commonSelectors =
271 commonSelectors = Object.freeze(unconditional.concat(selectors)); 274 Object.freeze(getUnconditionalSelectors().concat(selectors));
272 } 275 }
273 276
274 selectors = commonSelectors; 277 selectors = commonSelectors;
275 } 278 }
276 else 279 else
277 { 280 {
278 let unconditional = getUnconditionalSelectors(); 281 selectors = getUnconditionalSelectors().concat(selectors);
279 selectors = Object.freeze(unconditional.concat(selectors));
280 } 282 }
281 } 283 }
282 284
283 return selectors; 285 return selectors;
284 } 286 }
285 }; 287 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld