minor code review: do not cache hostname/domain pairs when parsing filters

This commit is contained in:
Raymond Hill 2018-11-24 12:09:27 -05:00
parent 2a91a685ce
commit 4504040344
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 30 additions and 29 deletions

View File

@ -323,7 +323,7 @@ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) {
continue; continue;
} }
if ( option === 'first-party' ) { if ( option === 'first-party' ) {
srcs.push(µburi.domainFromHostname(des) || des); srcs.push(µburi.domainFromHostnameNoCache(des) || des);
continue; continue;
} }
// One and only one type must be specified. // One and only one type must be specified.

View File

@ -392,7 +392,7 @@
domain: undefined, domain: undefined,
entity: undefined entity: undefined
}; };
request.domain = µb.URI.domainFromHostname(request.hostname); request.domain = µb.URI.domainFromHostnameNoCache(request.hostname);
request.entity = µb.URI.entityFromDomain(request.domain); request.entity = µb.URI.entityFromDomain(request.domain);
let scriptlets = µb.scriptletFilteringEngine.retrieve(request); let scriptlets = µb.scriptletFilteringEngine.retrieve(request);
if ( scriptlets === undefined ) { return; } if ( scriptlets === undefined ) { return; }

View File

@ -330,39 +330,40 @@ URI.pathFromURI = function(uri) {
// specific set of hostnames within a narrow time span -- in other words, I // specific set of hostnames within a narrow time span -- in other words, I
// believe probability of cache hit are high in uBlock. // believe probability of cache hit are high in uBlock.
var domainCache = new Map(); const domainCache = new Map();
var domainCacheCountLowWaterMark = 40; const domainCacheCountLowWaterMark = 40;
var domainCacheCountHighWaterMark = 60; const domainCacheCountHighWaterMark = 60;
var domainCacheEntryJunkyardMax = const domainCacheEntryJunkyardMax =
domainCacheCountHighWaterMark - domainCacheCountLowWaterMark; domainCacheCountHighWaterMark - domainCacheCountLowWaterMark;
var DomainCacheEntry = function(domain) { const DomainCacheEntry = function(domain) {
this.init(domain); this.init(domain);
}; };
DomainCacheEntry.prototype.init = function(domain) { DomainCacheEntry.prototype = {
init: function(domain) {
this.domain = domain; this.domain = domain;
this.tstamp = Date.now(); this.tstamp = Date.now();
return this; return this;
}; },
dispose: function() {
DomainCacheEntry.prototype.dispose = function() {
this.domain = ''; this.domain = '';
if ( domainCacheEntryJunkyard.length < domainCacheEntryJunkyardMax ) { if ( domainCacheEntryJunkyard.length < domainCacheEntryJunkyardMax ) {
domainCacheEntryJunkyard.push(this); domainCacheEntryJunkyard.push(this);
} }
},
}; };
var domainCacheEntryFactory = function(domain) { const domainCacheEntryFactory = function(domain) {
return domainCacheEntryJunkyard.length !== 0 ? return domainCacheEntryJunkyard.length !== 0 ?
domainCacheEntryJunkyard.pop().init(domain) : domainCacheEntryJunkyard.pop().init(domain) :
new DomainCacheEntry(domain); new DomainCacheEntry(domain);
}; };
var domainCacheEntryJunkyard = []; const domainCacheEntryJunkyard = [];
var domainCacheAdd = function(hostname, domain) { const domainCacheAdd = function(hostname, domain) {
var entry = domainCache.get(hostname); const entry = domainCache.get(hostname);
if ( entry !== undefined ) { if ( entry !== undefined ) {
entry.tstamp = Date.now(); entry.tstamp = Date.now();
} else { } else {
@ -374,17 +375,17 @@ var domainCacheAdd = function(hostname, domain) {
return domain; return domain;
}; };
var domainCacheEntrySort = function(a, b) { const domainCacheEntrySort = function(a, b) {
return domainCache.get(b).tstamp - domainCache.get(a).tstamp; return domainCache.get(b).tstamp - domainCache.get(a).tstamp;
}; };
var domainCachePrune = function() { const domainCachePrune = function() {
var hostnames = Array.from(domainCache.keys()) const hostnames = Array.from(domainCache.keys())
.sort(domainCacheEntrySort) .sort(domainCacheEntrySort)
.slice(domainCacheCountLowWaterMark); .slice(domainCacheCountLowWaterMark);
var i = hostnames.length; let i = hostnames.length;
while ( i-- ) { while ( i-- ) {
var hostname = hostnames[i]; const hostname = hostnames[i];
domainCache.get(hostname).dispose(); domainCache.get(hostname).dispose();
domainCache.delete(hostname); domainCache.delete(hostname);
} }