Browse Source

code review: this fix broken pruning code for `domainCache`

pull/2/head
gorhill 9 years ago
parent
commit
87e85fa0da
  1. 29
      src/js/uritools.js

29
src/js/uritools.js

@ -289,8 +289,8 @@ URI.hostnameFromURI = function(uri) {
URI.domainFromHostname = function(hostname) { URI.domainFromHostname = function(hostname) {
// Try to skip looking up the PSL database // 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(); entry.tstamp = Date.now();
return entry.domain; return entry.domain;
} }
@ -316,6 +316,13 @@ var psl = publicSuffixList;
// specific set of hostnames within a narrow time span -- in other words, I // specific set of hostnames within a narrow time span -- in other words, I
// believe probability of cache hit are high in uBlock. // 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) { var DomainCacheEntry = function(domain) {
this.init(domain); this.init(domain);
}; };
@ -328,7 +335,7 @@ DomainCacheEntry.prototype.init = function(domain) {
DomainCacheEntry.prototype.dispose = function() { DomainCacheEntry.prototype.dispose = function() {
this.domain = ''; this.domain = '';
if ( domainCacheEntryJunkyard.length < 25 ) {
if ( domainCacheEntryJunkyard.length < domainCacheEntryJunkyardMax ) {
domainCacheEntryJunkyard.push(this); domainCacheEntryJunkyard.push(this);
} }
}; };
@ -341,11 +348,10 @@ var domainCacheEntryFactory = function(domain) {
return new DomainCacheEntry(domain); return new DomainCacheEntry(domain);
}; };
var domainCacheEntryJunkyard = [];
var domainCacheAdd = function(hostname, domain) { 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 { } else {
domainCache[hostname] = domainCacheEntryFactory(domain); domainCache[hostname] = domainCacheEntryFactory(domain);
domainCacheCount += 1; domainCacheCount += 1;
@ -357,7 +363,7 @@ var domainCacheAdd = function(hostname, domain) {
}; };
var domainCacheEntrySort = function(a, b) { var domainCacheEntrySort = function(a, b) {
return b.tstamp - a.tstamp;
return domainCache[b].tstamp - domainCache[a].tstamp;
}; };
var domainCachePrune = function() { var domainCachePrune = function() {
@ -375,15 +381,10 @@ var domainCachePrune = function() {
}; };
var domainCacheReset = function() { var domainCacheReset = function() {
domainCache = {};
domainCache = Object.create(null);
domainCacheCount = 0; domainCacheCount = 0;
}; };
var domainCache = {};
var domainCacheCount = 0;
var domainCacheCountLowWaterMark = 75;
var domainCacheCountHighWaterMark = 100;
psl.onChanged.addListener(domainCacheReset); psl.onChanged.addListener(domainCacheReset);
/******************************************************************************/ /******************************************************************************/

Loading…
Cancel
Save