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: Use separate Cache class Created March 11, 2018, 12:13 a.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
@@ -19,16 +19,53 @@
/**
* @fileOverview Matcher class implementing matching addresses against
* a list of filters.
*/
const {Filter, WhitelistFilter} = require("./filterClasses");
+function Cache(capacity)
+{
+ this.capacity = capacity;
+
+ this.fresh = new Map();
+ this.hot = new Map();
+}
+
+Cache.prototype = {
+ get(key)
+ {
+ let value = this.hot.get(key);
+ if (value !== undefined)
+ return value;
+
+ value = this.fresh.get(key);
+ if (value !== undefined)
+ this.set(key, value, this.hot);
+
+ return value;
+ },
+
+ set(key, value, map = this.fresh)
+ {
+ if (map.size >= this.capacity / 2)
+ map.clear();
+
+ map.set(key, value);
+ },
+
+ clear()
+ {
+ this.fresh.clear();
+ this.hot.clear();
+ }
+};
+
/**
* Blacklist/whitelist filter matching
* @constructor
*/
function Matcher()
{
this.clear();
}
@@ -243,17 +280,17 @@
* rules into two Matcher instances.
* @constructor
* @augments Matcher
*/
function CombinedMatcher()
{
this.blacklist = new Matcher();
this.whitelist = new Matcher();
- this.resultCache = new Map();
+ this.resultCache = new Cache(CombinedMatcher.maxCacheEntries);
}
exports.CombinedMatcher = CombinedMatcher;
/**
* Maximal number of matching cache entries to be kept
* @type {number}
*/
CombinedMatcher.maxCacheEntries = 1000;
@@ -269,17 +306,17 @@
/**
* Matcher for exception rules.
* @type {Matcher}
*/
whitelist: null,
/**
* Lookup table of previous matchesAny results
- * @type {Map.<string,Filter>}
+ * @type {Cache.<string,Filter>}
*/
resultCache: null,
/**
* @see Matcher#clear
*/
clear()
{
@@ -412,19 +449,16 @@
let result = this.resultCache.get(key);
if (result !== undefined)
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);
return result;
}
};
/**
* Shared CombinedMatcher instance that should usually be used.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld