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).
This commit is contained in:
gorhill 2016-01-24 16:03:08 -05:00
parent 1f5b9b0e13
commit 8b5108db93
1 changed files with 15 additions and 13 deletions

View File

@ -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);
/******************************************************************************/