1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

Remove caching the result of domain extraction from hostname

With the new PSL implementation, benchmarks do not show benefit
from caching the domain extracted from a hostname for later
reuse -- the caching seems to even add an overhead instead with
the new publicSuffixList implementation.
This commit is contained in:
Raymond Hill 2019-02-20 08:51:14 -05:00
parent dc1d3c19b3
commit 1c26afe874
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 2 additions and 86 deletions

View File

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

View File

@ -615,7 +615,7 @@
const pos = hostname.lastIndexOf('.', hostname.length - 3);
domain = pos !== -1 ? hostname.slice(pos + 1) : hostname;
} else {
domain = µb.URI.domainFromHostnameNoCache(hostname);
domain = µb.URI.domainFromHostname(hostname);
}
return api.makeHash(domain);
};

View File

@ -286,18 +286,6 @@ URI.hostnameFromURI = function(uri) {
/******************************************************************************/
URI.domainFromHostname = function(hostname) {
let entry = domainCache.get(hostname);
if ( entry !== undefined ) {
entry.tstamp = Date.now();
return entry.domain;
}
if ( reIPAddressNaive.test(hostname) === false ) {
return domainCacheAdd(hostname, psl.getDomain(hostname));
}
return domainCacheAdd(hostname, hostname);
};
URI.domainFromHostnameNoCache = function(hostname) {
return reIPAddressNaive.test(hostname) ? hostname : psl.getDomain(hostname);
};
@ -325,78 +313,6 @@ URI.pathFromURI = function(uri) {
/******************************************************************************/
// Trying to alleviate the worries of looking up too often the domain name from
// a hostname. With a cache, uBlock benefits given that it deals with a
// specific set of hostnames within a narrow time span -- in other words, I
// believe probability of cache hit are high in uBlock.
const domainCache = new Map();
const domainCacheCountLowWaterMark = 40;
const domainCacheCountHighWaterMark = 60;
const domainCacheEntryJunkyardMax =
domainCacheCountHighWaterMark - domainCacheCountLowWaterMark;
const DomainCacheEntry = function(domain) {
this.init(domain);
};
DomainCacheEntry.prototype = {
init: function(domain) {
this.domain = domain;
this.tstamp = Date.now();
return this;
},
dispose: function() {
this.domain = '';
if ( domainCacheEntryJunkyard.length < domainCacheEntryJunkyardMax ) {
domainCacheEntryJunkyard.push(this);
}
},
};
const domainCacheEntryFactory = function(domain) {
return domainCacheEntryJunkyard.length !== 0 ?
domainCacheEntryJunkyard.pop().init(domain) :
new DomainCacheEntry(domain);
};
const domainCacheEntryJunkyard = [];
const domainCacheAdd = function(hostname, domain) {
const entry = domainCache.get(hostname);
if ( entry !== undefined ) {
entry.tstamp = Date.now();
} else {
domainCache.set(hostname, domainCacheEntryFactory(domain));
if ( domainCache.size === domainCacheCountHighWaterMark ) {
domainCachePrune();
}
}
return domain;
};
const domainCacheEntrySort = function(a, b) {
return domainCache.get(b).tstamp - domainCache.get(a).tstamp;
};
const domainCachePrune = function() {
const hostnames = Array.from(domainCache.keys())
.sort(domainCacheEntrySort)
.slice(domainCacheCountLowWaterMark);
let i = hostnames.length;
while ( i-- ) {
const hostname = hostnames[i];
domainCache.get(hostname).dispose();
domainCache.delete(hostname);
}
};
window.addEventListener('publicSuffixListChanged', function() {
domainCache.clear();
});
/******************************************************************************/
URI.domainFromURI = function(uri) {
if ( !uri ) {
return '';