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

Unified Diff: lib/matcher.js

Issue 29907586: Issue 6994 - Use shortcut matching for location only filters (Closed)
Patch Set: Created Oct. 21, 2018, 3:51 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 | « lib/filterClasses.js ('k') | test/filterListener.js » ('j') | 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
@@ -55,72 +55,93 @@
{
constructor()
{
/**
* Lookup table for filters by their associated keyword
* @type {Map.<string,(Filter|Set.<Filter>)>}
*/
this.filterByKeyword = new Map();
+
+ /**
+ * Lookup table for location only filters by their associated keyword
+ * for shortcut matching.
+ * @private
+ * @type {Map.<string,(Filter|Set.<Filter>)>}
+ */
+ this._fastFilterByKeyword = new Map();
}
/**
* Removes all known filters
*/
clear()
{
this.filterByKeyword.clear();
+ this._fastFilterByKeyword.clear();
}
/**
* Adds a filter to the matcher
* @param {RegExpFilter} filter
*/
add(filter)
{
// Look for a suitable keyword
let keyword = this.findKeyword(filter);
- let set = this.filterByKeyword.get(keyword);
+ let set = filter.isLocationOnly ? this._fastFilterByKeyword.get(keyword) :
+ this.filterByKeyword.get(keyword);
+
if (typeof set == "undefined")
{
- this.filterByKeyword.set(keyword, filter);
+ filter.isLocationOnly ?
+ this._fastFilterByKeyword.set(keyword, filter) :
+ this.filterByKeyword.set(keyword, filter);
}
else if (set.size == 1)
{
if (filter != set)
- this.filterByKeyword.set(keyword, new Set([set, filter]));
+ filter.isLocationOnly ?
+ this._fastFilterByKeyword.set(keyword, new Set([set, filter])) :
+ this.filterByKeyword.set(keyword, new Set([set, filter]));
}
else
{
set.add(filter);
}
}
/**
* Removes a filter from the matcher
* @param {RegExpFilter} filter
*/
remove(filter)
{
let keyword = this.findKeyword(filter);
- let set = this.filterByKeyword.get(keyword);
+ let set = filter.isLocationOnly ? this._fastFilterByKeyword.get(keyword) :
+ this.filterByKeyword.get(keyword);
+
if (typeof set == "undefined")
return;
if (set.size == 1)
{
if (filter == set)
- this.filterByKeyword.delete(keyword);
+ filter.isLocationOnly ?
+ this._fastFilterByKeyword.delete(keyword) :
+ this.filterByKeyword.delete(keyword);
}
else
{
set.delete(filter);
if (set.size == 1)
- this.filterByKeyword.set(keyword, [...set][0]);
+ filter.isLocationOnly ?
+ this._fastFilterByKeyword.set(keyword, [...set][0]) :
+ this.filterByKeyword.set(keyword, [...set][0]);
}
}
/**
* Chooses a keyword to be associated with the filter
* @param {Filter} filter
* @returns {string} keyword or an empty string if no keyword could be found
*/
@@ -130,17 +151,18 @@
let {pattern} = filter;
if (pattern == null)
return result;
let candidates = pattern.toLowerCase().match(allKeywordsRegExp);
if (!candidates)
return result;
- let hash = this.filterByKeyword;
+ let hash = filter.isLocationOnly ? this._fastFilterByKeyword :
+ this.filterByKeyword;
let resultCount = 0xFFFFFF;
let resultLength = 0;
for (let i = 0, l = candidates.length; i < l; i++)
{
let candidate = candidates[i].substr(1);
let filters = hash.get(candidate);
let count = typeof filters != "undefined" ? filters.size : 0;
if (count < resultCount ||
@@ -163,16 +185,32 @@
* @param {boolean} [thirdParty]
* @param {string} [sitekey]
* @param {boolean} [specificOnly]
* @returns {?Filter}
*/
_checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
specificOnly)
{
+ let fastSet = this._fastFilterByKeyword.get(keyword);
+ if (typeof fastSet != "undefined")
+ {
+ for (let filter of fastSet)
+ {
+ if (filter.contentType == 0 && typeMask == 0)
+ continue;
+
+ if (specificOnly && filter.isGeneric() &&
+ !(filter instanceof WhitelistFilter))
+ continue;
+
+ if (filter.matchesLocation(location))
+ return filter;
+ }
+ }
let set = this.filterByKeyword.get(keyword);
if (typeof set == "undefined")
return null;
for (let filter of set)
{
if (specificOnly && filter.isGeneric() &&
!(filter instanceof WhitelistFilter))
« no previous file with comments | « lib/filterClasses.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld