From 1c26afe874001c024113ad6a9adf99fb347612a8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 20 Feb 2019 08:51:14 -0500 Subject: [PATCH] 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. --- src/js/redirect-engine.js | 2 +- src/js/static-ext-filtering.js | 2 +- src/js/uritools.js | 84 ---------------------------------- 3 files changed, 2 insertions(+), 86 deletions(-) diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index 419361216..15982c437 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -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. diff --git a/src/js/static-ext-filtering.js b/src/js/static-ext-filtering.js index 8b2f533ef..598d6bb01 100644 --- a/src/js/static-ext-filtering.js +++ b/src/js/static-ext-filtering.js @@ -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); }; diff --git a/src/js/uritools.js b/src/js/uritools.js index 0bb59c8d0..dbc8c8590 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -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 '';