From 8b5108db937472068ea36cb4c6311b6ae6cb1f5d Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 24 Jan 2016 16:03:08 -0500 Subject: [PATCH] code review: fixed broken sort in domainCachePrune(). A negative side-effect of not sorting properly the entries was to cause raw filter lists to linger in memory due to v8's sliced- string implementation, which caused the parent string (a whole filter list possibly) to be kept around forever even though it was no longer used by uBO (raw filter lists are compiled then discarded). --- src/js/uritools.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/js/uritools.js b/src/js/uritools.js index 9c4c74303..72390aeef 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -282,8 +282,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,12 @@ URI.pathFromURI = function(uri) { // 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 = 35; +var domainCacheCountHighWaterMark = 50; +var domainCacheEntryJunkyardMax = domainCacheCountHighWaterMark - domainCacheCountLowWaterMark; + var DomainCacheEntry = function(domain) { this.init(domain); }; @@ -328,7 +334,7 @@ DomainCacheEntry.prototype.init = function(domain) { DomainCacheEntry.prototype.dispose = function() { this.domain = ''; - if ( domainCacheEntryJunkyard.length < 25 ) { + if ( domainCacheEntryJunkyard.length < domainCacheEntryJunkyardMax ) { domainCacheEntryJunkyard.push(this); } }; @@ -344,8 +350,9 @@ var domainCacheEntryFactory = function(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 +364,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() { @@ -370,20 +377,15 @@ var domainCachePrune = function() { while ( i-- ) { hostname = hostnames[i]; domainCache[hostname].dispose(); - delete domainCache[hostname]; + delete domainCache[hostname]; } }; 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); /******************************************************************************/