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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29909555: Issue 7046 - Defer caching of domain maps (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Add _domainsAccessCacheThreshold property Created Oct. 13, 2018, 11:37 p.m.
Right Patch Set: Change shouldCacheDomain function to cacheDomain property Created Oct. 16, 2018, 6 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 | « lib/elemHide.js ('k') | test/filterClasses.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 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 domainSource: null, 440 domainSource: null,
441 441
442 /** 442 /**
443 * Separator character used in domainSource property, must be 443 * Separator character used in domainSource property, must be
444 * overridden by subclasses 444 * overridden by subclasses
445 * @type {string} 445 * @type {string}
446 */ 446 */
447 domainSeparator: null, 447 domainSeparator: null,
448 448
449 /** 449 /**
450 * Internal counter to keep track of the number of times the
451 * {@link ActiveFilter#domains} property is accessed.
452 * @type {number}
453 * @private
454 */
455 _domainsAccessCount: 0,
456
457 /**
458 * Maximum number of times the {@link ActiveFilter#domains} property can be
459 * accessed before the value is cached.
460 * @type {number}
461 * @private
462 */
463 _domainsAccessCacheThreshold: 3,
464
465 /**
466 * Map containing domains that this filter should match on/not match 450 * Map containing domains that this filter should match on/not match
467 * on or null if the filter should match on all domains 451 * on or null if the filter should match on all domains
468 * @type {?Map.<string,boolean>} 452 * @type {?Map.<string,boolean>}
469 */ 453 */
470 get domains() 454 get domains()
471 { 455 {
472 // For some filter types this property is accessed only rarely, especially
473 // when the subscriptions are initially loaded. We defer any caching for
474 // such filters.
475 let cacheValue = ++this._domainsAccessCount >
476 this._domainsAccessCacheThreshold;
477
478 let domains = null; 456 let domains = null;
479 457
480 if (this.domainSource) 458 if (this.domainSource)
481 { 459 {
460 // For some filter types this property is accessed only rarely,
461 // especially when the subscriptions are initially loaded. We defer any
462 // caching for such filters.
463 let {cacheDomains} = this;
464
482 let source = this.domainSource.toLowerCase(); 465 let source = this.domainSource.toLowerCase();
483 466
484 let knownMap = knownDomainMaps.get(source); 467 let knownMap = knownDomainMaps.get(source);
485 if (knownMap) 468 if (knownMap)
486 { 469 {
487 domains = knownMap; 470 domains = knownMap;
488 } 471 }
489 else 472 else
490 { 473 {
491 let list = source.split(this.domainSeparator); 474 let list = source.split(this.domainSeparator);
(...skipping 26 matching lines...) Expand all
518 if (!domains) 501 if (!domains)
519 domains = new Map(); 502 domains = new Map();
520 503
521 domains.set(domain, include); 504 domains.set(domain, include);
522 } 505 }
523 506
524 if (domains) 507 if (domains)
525 domains.set("", !hasIncludes); 508 domains.set("", !hasIncludes);
526 } 509 }
527 510
528 if (!domains || cacheValue) 511 if (!domains || cacheDomains)
529 knownDomainMaps.set(source, domains); 512 knownDomainMaps.set(source, domains);
530 } 513 }
531 514
532 if (!domains || cacheValue) 515 if (!domains || cacheDomains)
516 {
533 this.domainSource = null; 517 this.domainSource = null;
534 } 518 Object.defineProperty(this, "domains", {value: domains});
535 519 }
536 if (!domains || cacheValue) 520 }
537 Object.defineProperty(this, "domains", {value: domains});
538 521
539 return domains; 522 return domains;
540 }, 523 },
524
525 /**
526 * Whether the value of {@link ActiveFilter#domains} should be cached.
527 * Defaults to <code>true</code>, but may be overridden by subclasses that
528 * don't want the value to be cached (for better memory usage).
529 * @type {boolean}
530 * @protected
531 */
532 cacheDomains: true,
541 533
542 /** 534 /**
543 * Array containing public keys of websites that this filter should apply to 535 * Array containing public keys of websites that this filter should apply to
544 * @type {?string[]} 536 * @type {?string[]}
545 */ 537 */
546 sitekeys: null, 538 sitekeys: null,
547 539
548 /** 540 /**
549 * Checks whether this filter is active on a domain. 541 * Checks whether this filter is active on a domain.
550 * @param {string} [docDomain] domain name of the document that loads the URL 542 * @param {string} [docDomain] domain name of the document that loads the URL
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 if (contentType == null) 852 if (contentType == null)
861 ({contentType} = RegExpFilter.prototype); 853 ({contentType} = RegExpFilter.prototype);
862 contentType &= ~type; 854 contentType &= ~type;
863 } 855 }
864 else 856 else
865 { 857 {
866 contentType |= type; 858 contentType |= type;
867 859
868 if (type == RegExpFilter.typeMap.CSP) 860 if (type == RegExpFilter.typeMap.CSP)
869 { 861 {
870 if (!value) 862 if (blocking && !value)
871 return new InvalidFilter(origText, "filter_invalid_csp"); 863 return new InvalidFilter(origText, "filter_invalid_csp");
872 csp = value; 864 csp = value;
873 } 865 }
874 } 866 }
875 } 867 }
876 else 868 else
877 { 869 {
878 switch (option.toLowerCase()) 870 switch (option.toLowerCase())
879 { 871 {
880 case "match-case": 872 case "match-case":
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 * @augments ContentFilter 1164 * @augments ContentFilter
1173 */ 1165 */
1174 function ElemHideBase(text, domains, selector) 1166 function ElemHideBase(text, domains, selector)
1175 { 1167 {
1176 ContentFilter.call(this, text, domains, selector); 1168 ContentFilter.call(this, text, domains, selector);
1177 } 1169 }
1178 exports.ElemHideBase = ElemHideBase; 1170 exports.ElemHideBase = ElemHideBase;
1179 1171
1180 ElemHideBase.prototype = extend(ContentFilter, { 1172 ElemHideBase.prototype = extend(ContentFilter, {
1181 /** 1173 /**
1174 * @see ActiveFilter#domains
1175 * @type {?Map.<string,boolean>}
1176 */
1177 get domains()
1178 {
1179 let {get} = Object.getOwnPropertyDescriptor(ActiveFilter.prototype,
1180 "domains");
1181 let value = get.call(this);
1182 this.cacheDomains = true;
1183 return value;
1184 },
1185
1186 /**
1187 * Initially <code>false</code>, but set to <code>true</code> after
1188 * {@link ActiveFilter#domains} has been accessed once.
1189 * @see ActiveFilter#cacheDomains
1190 * @type {boolean}
1191 * @protected
1192 */
1193 cacheDomains: false,
1194
1195 /**
1182 * CSS selector for the HTML elements that should be hidden 1196 * CSS selector for the HTML elements that should be hidden
1183 * @type {string} 1197 * @type {string}
1184 */ 1198 */
1185 get selector() 1199 get selector()
1186 { 1200 {
1187 // Braces are being escaped to prevent CSS rule injection. 1201 // Braces are being escaped to prevent CSS rule injection.
1188 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); 1202 return this.body.replace("{", "\\7B ").replace("}", "\\7D ");
1189 } 1203 }
1190 }); 1204 });
1191 1205
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1262 1276
1263 /** 1277 /**
1264 * Script that should be executed 1278 * Script that should be executed
1265 * @type {string} 1279 * @type {string}
1266 */ 1280 */
1267 get script() 1281 get script()
1268 { 1282 {
1269 return this.body; 1283 return this.body;
1270 } 1284 }
1271 }); 1285 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld