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: Created March 10, 2018, 3:30 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
Manish Jethani 2018/03/10 15:54:37 There's no need for cacheEntries anymore.
- * @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();
Manish Jethani 2018/03/10 15:54:37 We don't have to check the size here because we're
sergei 2018/03/12 12:03:00 I'm pretty sure that now it's even faster, tough i
},
/**
* @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 (result !== undefined)
Manish Jethani 2018/03/10 15:54:37 Assumption: an entry will never have a value of un
sergei 2018/03/12 12:03:01 AFAIK our coding style says to rather use `typeof
Manish Jethani 2018/03/12 13:42:42 This is really not about coding style. The cache c
sergei 2018/03/12 17:48:31 Please take a look around in this file, whenever o
Manish Jethani 2018/03/12 18:59:21 Done.
sergei 2018/03/13 13:56:41 I still think we should discuss whether we use typ
Manish Jethani 2018/03/13 14:32:49 Yes, let's have that discussion.
+ 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)
Manish Jethani 2018/03/10 15:54:37 Again, we don't have to create a new object every
+ 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