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

Delta Between Two Patch Sets: lib/matcher.js

Issue 29892596: Issue 6992 - Remove keyword-by-filter map (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Keep isSlowFilter Created Sept. 27, 2018, 11:24 a.m.
Right Patch Set: Implement faster version of isSlowFilter Created Sept. 28, 2018, 9:22 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/filterClasses.js ('k') | test/filterListener.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Matcher class implementing matching addresses against 21 * @fileOverview Matcher class implementing matching addresses against
22 * a list of filters. 22 * a list of filters.
23 */ 23 */
24 24
25 const {WhitelistFilter} = require("./filterClasses"); 25 const {WhitelistFilter} = require("./filterClasses");
26 26
27 /** 27 /**
28 * Regular expression for matching a keyword in a filter.
29 * @type {RegExp}
30 */
31 const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/;
32
33 /**
34 * Regular expression for matching all keywords in a filter.
35 * @type {RegExp}
36 */
37 const allKeywordsRegExp = new RegExp(keywordRegExp, "g");
38
39 /**
40 * Checks whether a particular filter is slow.
41 * @param {RegExpFilter} filter
42 * @returns {boolean}
43 */
44 function isSlowFilter(filter)
45 {
46 return !filter.pattern || !keywordRegExp.test(filter.pattern);
47 }
48
49 exports.isSlowFilter = isSlowFilter;
50
51 /**
28 * Blacklist/whitelist filter matching 52 * Blacklist/whitelist filter matching
29 */ 53 */
30 class Matcher 54 class Matcher
31 { 55 {
32 constructor() 56 constructor()
33 { 57 {
34 /** 58 /**
35 * Lookup table for filters by their associated keyword 59 * Lookup table for filters by their associated keyword
36 * @type {Map.<string,(Filter|Set.<Filter>)>} 60 * @type {Map.<string,(Filter|Set.<Filter>)>}
37 */ 61 */
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 * @param {Filter} filter 124 * @param {Filter} filter
101 * @returns {string} keyword or an empty string if no keyword could be found 125 * @returns {string} keyword or an empty string if no keyword could be found
102 */ 126 */
103 findKeyword(filter) 127 findKeyword(filter)
104 { 128 {
105 let result = ""; 129 let result = "";
106 let {pattern} = filter; 130 let {pattern} = filter;
107 if (pattern == null) 131 if (pattern == null)
108 return result; 132 return result;
109 133
110 let candidates = pattern.toLowerCase().match( 134 let candidates = pattern.toLowerCase().match(allKeywordsRegExp);
111 /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g
112 );
113 if (!candidates) 135 if (!candidates)
114 return result; 136 return result;
115 137
116 let hash = this.filterByKeyword; 138 let hash = this.filterByKeyword;
117 let resultCount = 0xFFFFFF; 139 let resultCount = 0xFFFFFF;
118 let resultLength = 0; 140 let resultLength = 0;
119 for (let i = 0, l = candidates.length; i < l; i++) 141 for (let i = 0, l = candidates.length; i < l; i++)
120 { 142 {
121 let candidate = candidates[i].substr(1); 143 let candidate = candidates[i].substr(1);
122 let filters = hash.get(candidate); 144 let filters = hash.get(candidate);
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 * @returns {string} keyword 299 * @returns {string} keyword
278 */ 300 */
279 findKeyword(filter) 301 findKeyword(filter)
280 { 302 {
281 if (filter instanceof WhitelistFilter) 303 if (filter instanceof WhitelistFilter)
282 return this.whitelist.findKeyword(filter); 304 return this.whitelist.findKeyword(filter);
283 return this.blacklist.findKeyword(filter); 305 return this.blacklist.findKeyword(filter);
284 } 306 }
285 307
286 /** 308 /**
287 * Checks whether a particular filter is slow
288 * @param {RegExpFilter} filter
289 * @returns {boolean}
290 */
291 isSlowFilter(filter)
292 {
293 return !(filter instanceof WhitelistFilter ?
294 this.whitelist : this.blacklist).findKeyword(filter);
295 }
296
297 /**
298 * Optimized filter matching testing both whitelist and blacklist matchers 309 * Optimized filter matching testing both whitelist and blacklist matchers
299 * simultaneously. For parameters see 310 * simultaneously. For parameters see
300 {@link Matcher#matchesAny Matcher.matchesAny()}. 311 {@link Matcher#matchesAny Matcher.matchesAny()}.
301 * @see Matcher#matchesAny 312 * @see Matcher#matchesAny
302 * @inheritdoc 313 * @inheritdoc
303 */ 314 */
304 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, 315 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey,
305 specificOnly) 316 specificOnly)
306 { 317 {
307 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 318 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 367
357 exports.CombinedMatcher = CombinedMatcher; 368 exports.CombinedMatcher = CombinedMatcher;
358 369
359 /** 370 /**
360 * Shared {@link CombinedMatcher} instance that should usually be used. 371 * Shared {@link CombinedMatcher} instance that should usually be used.
361 * @type {CombinedMatcher} 372 * @type {CombinedMatcher}
362 */ 373 */
363 let defaultMatcher = new CombinedMatcher(); 374 let defaultMatcher = new CombinedMatcher();
364 375
365 exports.defaultMatcher = defaultMatcher; 376 exports.defaultMatcher = defaultMatcher;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld