diff options
author | gorhill <rhill@raymondhill.net> | 2015-01-08 10:37:19 -0500 |
---|---|---|
committer | gorhill <rhill@raymondhill.net> | 2015-01-08 10:37:19 -0500 |
commit | 0c152b285939f08cebea3f61852fc6b35228ada6 (patch) | |
tree | 2c95d61822c7f4a094590cc80f2b16f5292f897e /src/js/uritools.js | |
parent | bdf770a1bb17b98a0cf5a84e7954bcb1656c6204 (diff) | |
download | uBlock-0c152b285939f08cebea3f61852fc6b35228ada6.zip uBlock-0c152b285939f08cebea3f61852fc6b35228ada6.tar.gz uBlock-0c152b285939f08cebea3f61852fc6b35228ada6.tar.bz2 |
fixed flawed 1st-/3rd-party test
Diffstat (limited to 'src/js/uritools.js')
-rw-r--r-- | src/js/uritools.js | 89 |
1 files changed, 83 insertions, 6 deletions
diff --git a/src/js/uritools.js b/src/js/uritools.js index 5e37bd5..45065fb 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -274,21 +274,98 @@ URI.hostnameFromURI = function(uri) { /******************************************************************************/ +URI.domainFromHostname = function(hostname) { + // Try to skip looking up the PSL database + if ( domainCache.hasOwnProperty(hostname) ) { + var entry = domainCache[hostname]; + entry.tstamp = Date.now(); + return entry.domain; + } + // Meh.. will have to search it + if ( reIPAddressNaive.test(hostname) === false ) { + return domainCacheAdd(hostname, psl.getDomain(hostname)); + } + return domainCacheAdd(hostname, hostname); +}; + +URI.domain = function() { + return this.domainFromHostname(this.hostname); +}; + // It is expected that there is higher-scoped `publicSuffixList` lingering // somewhere. Cache it. See <https://github.com/gorhill/publicsuffixlist.js>. var psl = publicSuffixList; -URI.domainFromHostname = function(hostname) { - if ( !reIPAddressNaive.test(hostname) ) { - return psl.getDomain(hostname); +/******************************************************************************/ + +// 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. + +var DomainCacheEntry = function(domain) { + this.init(domain); +}; + +DomainCacheEntry.prototype.init = function(domain) { + this.domain = domain; + this.tstamp = Date.now(); + return this; +}; + +DomainCacheEntry.prototype.dispose = function() { + this.domain = ''; + if ( domainCacheEntryJunkyard.length < 25 ) { + domainCacheEntryJunkyard.push(this); } - return hostname; }; -URI.domain = function() { - return this.domainFromHostname(this.hostname); +var domainCacheEntryFactory = function(domain) { + var entry = domainCacheEntryJunkyard.pop(); + if ( entry ) { + return entry.init(domain); + } + return new DomainCacheEntry(domain); +}; + +var domainCacheEntryJunkyard = []; + +var domainCacheAdd = function(hostname, domain) { + if ( domainCache.hasOwnProperty(hostname) ) { + domainCache[hostname].tstamp = Date.now(); + } else { + domainCache[hostname] = domainCacheEntryFactory(domain); + domainCacheCount += 1; + if ( domainCacheCount === domainCacheCountHighWaterMark ) { + domainCachePrune(); + } + } + return domain; +}; + +var domainCacheEntrySort = function(a, b) { + return b.tstamp - a.tstamp; }; +var domainCachePrune = function() { + var hostnames = Object.keys(domainCache) + .sort(domainCacheEntrySort) + .slice(domainCacheCountLowWaterMark); + var i = hostnames.length; + domainCacheCount -= i; + var hostname; + while ( i-- ) { + hostname = hostnames[i]; + domainCache[hostname].dispose(); + delete domainCache[hostname]; + } +}; + +var domainCache = {}; +var domainCacheCount = 0; +var domainCacheCountLowWaterMark = 75; +var domainCacheCountHighWaterMark = 100; + /******************************************************************************/ URI.domainFromURI = function(uri) { |