From 87e85fa0daa6fb678da12f72773de021aabbfe06 Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 25 Jan 2016 10:20:50 -0500 Subject: [PATCH] code review: this fix broken pruning code for `domainCache` --- src/js/uritools.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/js/uritools.js b/src/js/uritools.js index 8f4d082..cff07c0 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -289,8 +289,8 @@ URI.hostnameFromURI = function(uri) { URI.domainFromHostname = function(hostname) { // Try to skip looking up the PSL database - if ( domainCache.hasOwnProperty(hostname) ) { - var entry = domainCache[hostname]; + var entry = domainCache[hostname]; + if ( entry !== undefined ) { entry.tstamp = Date.now(); return entry.domain; } @@ -316,6 +316,13 @@ var psl = publicSuffixList; // specific set of hostnames within a narrow time span -- in other words, I // believe probability of cache hit are high in uBlock. +var domainCache = Object.create(null); +var domainCacheCount = 0; +var domainCacheCountLowWaterMark = 75; +var domainCacheCountHighWaterMark = 100; +var domainCacheEntryJunkyard = []; +var domainCacheEntryJunkyardMax = domainCacheCountHighWaterMark - domainCacheCountLowWaterMark; + var DomainCacheEntry = function(domain) { this.init(domain); }; @@ -328,7 +335,7 @@ DomainCacheEntry.prototype.init = function(domain) { DomainCacheEntry.prototype.dispose = function() { this.domain = ''; - if ( domainCacheEntryJunkyard.length < 25 ) { + if ( domainCacheEntryJunkyard.length < domainCacheEntryJunkyardMax ) { domainCacheEntryJunkyard.push(this); } }; @@ -341,11 +348,10 @@ var domainCacheEntryFactory = function(domain) { return new DomainCacheEntry(domain); }; -var domainCacheEntryJunkyard = []; - var domainCacheAdd = function(hostname, domain) { - if ( domainCache.hasOwnProperty(hostname) ) { - domainCache[hostname].tstamp = Date.now(); + var entry = domainCache[hostname]; + if ( entry !== undefined ) { + entry.tstamp = Date.now(); } else { domainCache[hostname] = domainCacheEntryFactory(domain); domainCacheCount += 1; @@ -357,7 +363,7 @@ var domainCacheAdd = function(hostname, domain) { }; var domainCacheEntrySort = function(a, b) { - return b.tstamp - a.tstamp; + return domainCache[b].tstamp - domainCache[a].tstamp; }; var domainCachePrune = function() { @@ -375,15 +381,10 @@ var domainCachePrune = function() { }; var domainCacheReset = function() { - domainCache = {}; + domainCache = Object.create(null); domainCacheCount = 0; }; -var domainCache = {}; -var domainCacheCount = 0; -var domainCacheCountLowWaterMark = 75; -var domainCacheCountHighWaterMark = 100; - psl.onChanged.addListener(domainCacheReset); /******************************************************************************/