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

Delta Between Two Patch Sets: lib/matcher.js

Issue 29926557: Issue 6994 - Use shortcut matching for location-only filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Oct. 25, 2018, 8:43 p.m.
Right Patch Set: Fix comment Created Oct. 25, 2018, 9:27 p.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
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 60
61 /** 61 /**
62 * Blacklist/whitelist filter matching 62 * Blacklist/whitelist filter matching
63 */ 63 */
64 class Matcher 64 class Matcher
65 { 65 {
66 constructor() 66 constructor()
67 { 67 {
68 /** 68 /**
69 * Lookup table for simple filters by their associated keyword 69 * Lookup table for simple filters by their associated keyword
70 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} 70 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>}
Manish Jethani 2018/10/25 21:24:10 Unrelated documentation update, but these can only
71 * @private 71 * @private
72 */ 72 */
73 this._simpleFiltersByKeyword = new Map(); 73 this._simpleFiltersByKeyword = new Map();
74 74
75 /** 75 /**
76 * Lookup table for complex filters by their associated keyword 76 * Lookup table for complex filters by their associated keyword
77 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} 77 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>}
78 * @private 78 * @private
79 */ 79 */
80 this._complexFiltersByKeyword = new Map(); 80 this._complexFiltersByKeyword = new Map();
81 } 81 }
82 82
83 /** 83 /**
84 * Removes all known filters 84 * Removes all known filters
85 */ 85 */
86 clear() 86 clear()
87 { 87 {
88 this._simpleFiltersByKeyword.clear(); 88 this._simpleFiltersByKeyword.clear();
89 this._complexFiltersByKeyword.clear(); 89 this._complexFiltersByKeyword.clear();
90 } 90 }
91 91
92 /** 92 /**
93 * Adds a filter to the matcher 93 * Adds a filter to the matcher
94 * @param {RegExpFilter} filter 94 * @param {RegExpFilter} filter
95 */ 95 */
96 add(filter) 96 add(filter)
97 { 97 {
98 let filtersByKeyword = filter.isLocationOnly() ? 98 let filtersByKeyword = filter.isLocationOnly() ?
Manish Jethani 2018/10/25 21:24:10 The filter goes into one of the two maps, dependin
99 this._simpleFiltersByKeyword : 99 this._simpleFiltersByKeyword :
100 this._complexFiltersByKeyword; 100 this._complexFiltersByKeyword;
101 // Look for a suitable keyword 101 // Look for a suitable keyword
102 let keyword = this.findKeyword(filter); 102 let keyword = this.findKeyword(filter);
103 let set = filtersByKeyword.get(keyword); 103 let set = filtersByKeyword.get(keyword);
104 if (typeof set == "undefined") 104 if (typeof set == "undefined")
105 { 105 {
106 filtersByKeyword.set(keyword, filter); 106 filtersByKeyword.set(keyword, filter);
107 } 107 }
108 else if (set.size == 1) 108 else if (set.size == 1)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 if (!candidates) 161 if (!candidates)
162 return result; 162 return result;
163 163
164 let resultCount = 0xFFFFFF; 164 let resultCount = 0xFFFFFF;
165 let resultLength = 0; 165 let resultLength = 0;
166 for (let i = 0, l = candidates.length; i < l; i++) 166 for (let i = 0, l = candidates.length; i < l; i++)
167 { 167 {
168 let candidate = candidates[i].substr(1); 168 let candidate = candidates[i].substr(1);
169 let simpleFilters = this._simpleFiltersByKeyword.get(candidate); 169 let simpleFilters = this._simpleFiltersByKeyword.get(candidate);
170 let complexFilters = this._complexFiltersByKeyword.get(candidate); 170 let complexFilters = this._complexFiltersByKeyword.get(candidate);
171 let count = (typeof simpleFilters != "undefined" ? 171 let count = (typeof simpleFilters != "undefined" ?
Manish Jethani 2018/10/25 21:24:10 Now count needs to be the sum of the sizes of the
172 simpleFilters.size : 0) + 172 simpleFilters.size : 0) +
173 (typeof complexFilters != "undefined" ? 173 (typeof complexFilters != "undefined" ?
174 complexFilters.size : 0); 174 complexFilters.size : 0);
175 if (count < resultCount || 175 if (count < resultCount ||
176 (count == resultCount && candidate.length > resultLength)) 176 (count == resultCount && candidate.length > resultLength))
177 { 177 {
178 result = candidate; 178 result = candidate;
179 resultCount = count; 179 resultCount = count;
180 resultLength = candidate.length; 180 resultLength = candidate.length;
181 } 181 }
(...skipping 10 matching lines...) Expand all
192 * @param {boolean} [thirdParty] 192 * @param {boolean} [thirdParty]
193 * @param {string} [sitekey] 193 * @param {string} [sitekey]
194 * @param {boolean} [specificOnly] 194 * @param {boolean} [specificOnly]
195 * @returns {?Filter} 195 * @returns {?Filter}
196 * @protected 196 * @protected
197 */ 197 */
198 checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, 198 checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
199 specificOnly) 199 specificOnly)
200 { 200 {
201 // We need to skip the simple (location-only) filters if the type mask does 201 // We need to skip the simple (location-only) filters if the type mask does
202 // not contain any non-default content types like $document, $elemhide, 202 // not contain any default content types.
Manish Jethani 2018/10/25 21:28:40 Fixed the comment, it's actually the opposite.
203 // $csp, and so on.
204 if ((typeMask & RegExpFilter.prototype.contentType) != 0) 203 if ((typeMask & RegExpFilter.prototype.contentType) != 0)
205 { 204 {
206 let simpleSet = this._simpleFiltersByKeyword.get(keyword); 205 let simpleSet = this._simpleFiltersByKeyword.get(keyword);
207 if (simpleSet) 206 if (simpleSet)
208 { 207 {
209 for (let filter of simpleSet) 208 for (let filter of simpleSet)
210 { 209 {
211 if ((!specificOnly || filter instanceof WhitelistFilter) && 210 if (specificOnly && !(filter instanceof WhitelistFilter))
212 filter.matchesLocation(location)) 211 continue;
Manish Jethani 2018/10/25 21:24:10 For these filters, we only match the location. We
213 { 212
213 if (filter.matchesLocation(location))
214 return filter; 214 return filter;
215 }
216 } 215 }
217 } 216 }
218 } 217 }
219 218
220 let complexSet = this._complexFiltersByKeyword.get(keyword); 219 let complexSet = this._complexFiltersByKeyword.get(keyword);
221 if (complexSet) 220 if (complexSet)
222 { 221 {
223 for (let filter of complexSet) 222 for (let filter of complexSet)
224 { 223 {
225 if (specificOnly && filter.isGeneric() && 224 if (specificOnly && filter.isGeneric() &&
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 432
434 exports.CombinedMatcher = CombinedMatcher; 433 exports.CombinedMatcher = CombinedMatcher;
435 434
436 /** 435 /**
437 * Shared {@link CombinedMatcher} instance that should usually be used. 436 * Shared {@link CombinedMatcher} instance that should usually be used.
438 * @type {CombinedMatcher} 437 * @type {CombinedMatcher}
439 */ 438 */
440 let defaultMatcher = new CombinedMatcher(); 439 let defaultMatcher = new CombinedMatcher();
441 440
442 exports.defaultMatcher = defaultMatcher; 441 exports.defaultMatcher = defaultMatcher;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld