Index: lib/matcher.js |
=================================================================== |
--- a/lib/matcher.js |
+++ b/lib/matcher.js |
@@ -244,24 +244,25 @@ |
* @constructor |
* @augments Matcher |
*/ |
function CombinedMatcher() |
{ |
this.blacklist = new Matcher(); |
this.whitelist = new Matcher(); |
this.resultCache = new Map(); |
+ this.frequentResultCache = new Map(); |
} |
exports.CombinedMatcher = CombinedMatcher; |
/** |
* Maximal number of matching cache entries to be kept |
* @type {number} |
*/ |
-CombinedMatcher.maxCacheEntries = 1000; |
+CombinedMatcher.maxCacheEntries = 500; |
CombinedMatcher.prototype = |
{ |
/** |
* Matcher for blocking rules. |
* @type {Matcher} |
*/ |
blacklist: null, |
@@ -274,51 +275,60 @@ |
/** |
* Lookup table of previous matchesAny results |
* @type {Map.<string,Filter>} |
*/ |
resultCache: null, |
/** |
+ * Lookup table of frequently accessed previous matchesAny results |
+ * @type {Map.<string,Filter>} |
+ */ |
+ frequentResultCache: null, |
+ |
+ /** |
* @see Matcher#clear |
*/ |
clear() |
{ |
this.blacklist.clear(); |
this.whitelist.clear(); |
this.resultCache.clear(); |
+ this.frequentResultCache.clear(); |
}, |
/** |
* @see Matcher#add |
* @param {Filter} filter |
*/ |
add(filter) |
{ |
if (filter instanceof WhitelistFilter) |
this.whitelist.add(filter); |
else |
this.blacklist.add(filter); |
this.resultCache.clear(); |
+ this.frequentResultCache.clear(); |
}, |
/** |
* @see Matcher#remove |
* @param {Filter} filter |
*/ |
remove(filter) |
{ |
if (filter instanceof WhitelistFilter) |
this.whitelist.remove(filter); |
else |
this.blacklist.remove(filter); |
this.resultCache.clear(); |
+ this.frequentResultCache.clear(); |
}, |
/** |
* @see Matcher#findKeyword |
* @param {Filter} filter |
* @return {string} keyword |
*/ |
findKeyword(filter) |
@@ -405,20 +415,31 @@ |
* @see Matcher#matchesAny |
* @inheritdoc |
*/ |
matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
{ |
let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
" " + sitekey + " " + specificOnly; |
- let result = this.resultCache.get(key); |
+ let result = this.frequentResultCache.get(key); |
if (result !== undefined) |
return result; |
+ result = this.resultCache.get(key); |
+ if (result !== undefined) |
+ { |
+ if (this.frequentResultCache.size >= CombinedMatcher.maxCacheEntries) |
+ this.frequentResultCache.clear(); |
+ |
+ this.frequentResultCache.set(key, result); |
+ |
+ return result; |
+ } |
+ |
result = this.matchesAnyInternal(location, typeMask, docDomain, |
thirdParty, sitekey, specificOnly); |
if (this.resultCache.size >= CombinedMatcher.maxCacheEntries) |
this.resultCache.clear(); |
this.resultCache.set(key, result); |