Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/matcher.js

Issue 29719572: Issue 6465 - Maintain separate long-term cache in CombinedMatcher (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created March 10, 2018, 11:09 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld