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

Unified Diff: lib/matcher.js

Issue 29719569: Issue 6467 - Use Map object for caching filter matches (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Use typeof Created March 12, 2018, 6:58 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
@@ -243,17 +243,17 @@
* rules into two Matcher instances.
* @constructor
* @augments Matcher
*/
function CombinedMatcher()
{
this.blacklist = new Matcher();
this.whitelist = new Matcher();
- this.resultCache = Object.create(null);
+ this.resultCache = new Map();
}
exports.CombinedMatcher = CombinedMatcher;
/**
* Maximal number of matching cache entries to be kept
* @type {number}
*/
CombinedMatcher.maxCacheEntries = 1000;
@@ -269,71 +269,56 @@
/**
* Matcher for exception rules.
* @type {Matcher}
*/
whitelist: null,
/**
* Lookup table of previous matchesAny results
- * @type {Object}
+ * @type {Map.<string,Filter>}
*/
resultCache: null,
/**
- * Number of entries in resultCache
- * @type {number}
- */
- cacheEntries: 0,
-
- /**
* @see Matcher#clear
*/
clear()
{
this.blacklist.clear();
this.whitelist.clear();
- this.resultCache = Object.create(null);
- this.cacheEntries = 0;
+ this.resultCache.clear();
},
/**
* @see Matcher#add
* @param {Filter} filter
*/
add(filter)
{
if (filter instanceof WhitelistFilter)
this.whitelist.add(filter);
else
this.blacklist.add(filter);
- if (this.cacheEntries > 0)
- {
- this.resultCache = Object.create(null);
- this.cacheEntries = 0;
- }
+ this.resultCache.clear();
},
/**
* @see Matcher#remove
* @param {Filter} filter
*/
remove(filter)
{
if (filter instanceof WhitelistFilter)
this.whitelist.remove(filter);
else
this.blacklist.remove(filter);
- if (this.cacheEntries > 0)
- {
- this.resultCache = Object.create(null);
- this.cacheEntries = 0;
- }
+ this.resultCache.clear();
},
/**
* @see Matcher#findKeyword
* @param {Filter} filter
* @return {string} keyword
*/
findKeyword(filter)
@@ -419,30 +404,28 @@
/**
* @see Matcher#matchesAny
* @inheritdoc
*/
matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
{
let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
" " + sitekey + " " + specificOnly;
- if (key in this.resultCache)
- return this.resultCache[key];
- let result = this.matchesAnyInternal(location, typeMask, docDomain,
- thirdParty, sitekey, specificOnly);
+ let result = this.resultCache.get(key);
+ if (typeof result != "undefined")
+ return result;
- if (this.cacheEntries >= CombinedMatcher.maxCacheEntries)
- {
- this.resultCache = Object.create(null);
- this.cacheEntries = 0;
- }
+ result = this.matchesAnyInternal(location, typeMask, docDomain,
+ thirdParty, sitekey, specificOnly);
- this.resultCache[key] = result;
- this.cacheEntries++;
+ if (this.resultCache.size >= CombinedMatcher.maxCacheEntries)
+ this.resultCache.clear();
+
+ this.resultCache.set(key, result);
return result;
}
};
/**
* Shared CombinedMatcher instance that should usually be used.
* @type {CombinedMatcher}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld