| 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. |