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

Side by Side Diff: lib/matcher.js

Issue 29907586: Issue 6994 - Use shortcut matching for location only filters (Closed)
Patch Set: Reduce ternary use Created Oct. 21, 2018, 4:26 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 */ 53 */
54 class Matcher 54 class Matcher
55 { 55 {
56 constructor() 56 constructor()
57 { 57 {
58 /** 58 /**
59 * Lookup table for filters by their associated keyword 59 * Lookup table for filters by their associated keyword
60 * @type {Map.<string,(Filter|Set.<Filter>)>} 60 * @type {Map.<string,(Filter|Set.<Filter>)>}
61 */ 61 */
62 this.filterByKeyword = new Map(); 62 this.filterByKeyword = new Map();
63
64 /**
65 * Lookup table for location only filters by their associated keyword
66 * for shortcut matching.
67 * @private
68 * @type {Map.<string,(Filter|Set.<Filter>)>}
69 */
70 this._fastFilterByKeyword = new Map();
63 } 71 }
64 72
65 /** 73 /**
66 * Removes all known filters 74 * Removes all known filters
67 */ 75 */
68 clear() 76 clear()
69 { 77 {
70 this.filterByKeyword.clear(); 78 this.filterByKeyword.clear();
79 this._fastFilterByKeyword.clear();
71 } 80 }
72 81
73 /** 82 /**
74 * Adds a filter to the matcher 83 * Adds a filter to the matcher
75 * @param {RegExpFilter} filter 84 * @param {RegExpFilter} filter
76 */ 85 */
77 add(filter) 86 add(filter)
78 { 87 {
79 // Look for a suitable keyword 88 // Look for a suitable keyword
80 let keyword = this.findKeyword(filter); 89 let keyword = this.findKeyword(filter);
81 let set = this.filterByKeyword.get(keyword); 90 let filterMap = filter.isLocationOnly ? this._fastFilterByKeyword :
91 this.filterByKeyword;
92 let set = filterMap.get(keyword);
93
82 if (typeof set == "undefined") 94 if (typeof set == "undefined")
83 { 95 {
84 this.filterByKeyword.set(keyword, filter); 96 filterMap.set(keyword, filter);
85 } 97 }
86 else if (set.size == 1) 98 else if (set.size == 1)
87 { 99 {
88 if (filter != set) 100 if (filter != set)
89 this.filterByKeyword.set(keyword, new Set([set, filter])); 101 filterMap.set(keyword, new Set([set, filter]));
90 } 102 }
91 else 103 else
92 { 104 {
93 set.add(filter); 105 set.add(filter);
94 } 106 }
95 } 107 }
96 108
97 /** 109 /**
98 * Removes a filter from the matcher 110 * Removes a filter from the matcher
99 * @param {RegExpFilter} filter 111 * @param {RegExpFilter} filter
100 */ 112 */
101 remove(filter) 113 remove(filter)
102 { 114 {
103 let keyword = this.findKeyword(filter); 115 let keyword = this.findKeyword(filter);
104 let set = this.filterByKeyword.get(keyword); 116 let filterMap = filter.isLocationOnly ? this._fastFilterByKeyword :
117 this.filterByKeyword;
118 let set = filterMap.get(keyword);
119
105 if (typeof set == "undefined") 120 if (typeof set == "undefined")
106 return; 121 return;
107 122
108 if (set.size == 1) 123 if (set.size == 1)
109 { 124 {
110 if (filter == set) 125 if (filter == set)
111 this.filterByKeyword.delete(keyword); 126 filterMap.delete(keyword);
112 } 127 }
113 else 128 else
114 { 129 {
115 set.delete(filter); 130 set.delete(filter);
116 131
117 if (set.size == 1) 132 if (set.size == 1)
118 this.filterByKeyword.set(keyword, [...set][0]); 133 filterMap.set(keyword, [...set][0]);
119 } 134 }
120 } 135 }
121 136
122 /** 137 /**
123 * Chooses a keyword to be associated with the filter 138 * Chooses a keyword to be associated with the filter
124 * @param {Filter} filter 139 * @param {Filter} filter
125 * @returns {string} keyword or an empty string if no keyword could be found 140 * @returns {string} keyword or an empty string if no keyword could be found
126 */ 141 */
127 findKeyword(filter) 142 findKeyword(filter)
128 { 143 {
129 let result = ""; 144 let result = "";
130 let {pattern} = filter; 145 let {pattern} = filter;
131 if (pattern == null) 146 if (pattern == null)
132 return result; 147 return result;
133 148
134 let candidates = pattern.toLowerCase().match(allKeywordsRegExp); 149 let candidates = pattern.toLowerCase().match(allKeywordsRegExp);
135 if (!candidates) 150 if (!candidates)
136 return result; 151 return result;
137 152
138 let hash = this.filterByKeyword; 153 let hash = filter.isLocationOnly ? this._fastFilterByKeyword :
154 this.filterByKeyword;
Jon Sonesen 2018/10/21 17:29:51 In the tests this fails on the last test of extrac
139 let resultCount = 0xFFFFFF; 155 let resultCount = 0xFFFFFF;
140 let resultLength = 0; 156 let resultLength = 0;
141 for (let i = 0, l = candidates.length; i < l; i++) 157 for (let i = 0, l = candidates.length; i < l; i++)
142 { 158 {
143 let candidate = candidates[i].substr(1); 159 let candidate = candidates[i].substr(1);
144 let filters = hash.get(candidate); 160 let filters = hash.get(candidate);
145 let count = typeof filters != "undefined" ? filters.size : 0; 161 let count = typeof filters != "undefined" ? filters.size : 0;
146 if (count < resultCount || 162 if (count < resultCount ||
147 (count == resultCount && candidate.length > resultLength)) 163 (count == resultCount && candidate.length > resultLength))
148 { 164 {
(...skipping 14 matching lines...) Expand all
163 * @param {boolean} [thirdParty] 179 * @param {boolean} [thirdParty]
164 * @param {string} [sitekey] 180 * @param {string} [sitekey]
165 * @param {boolean} [specificOnly] 181 * @param {boolean} [specificOnly]
166 * @returns {?Filter} 182 * @returns {?Filter}
167 */ 183 */
168 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, 184 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
169 specificOnly) 185 specificOnly)
170 { 186 {
171 let set = this.filterByKeyword.get(keyword); 187 let set = this.filterByKeyword.get(keyword);
172 if (typeof set == "undefined") 188 if (typeof set == "undefined")
189 {
190 let fastSet = this._fastFilterByKeyword.get(keyword);
191 if (typeof fastSet == "undefined")
192 return null;
193
194 for (let filter of fastSet)
195 {
196 if (specificOnly && filter.isGeneric() &&
197 !(filter instanceof WhitelistFilter))
198 continue;
199
200 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
201 return filter;
202 }
173 return null; 203 return null;
204 }
174 205
175 for (let filter of set) 206 for (let filter of set)
176 { 207 {
177 if (specificOnly && filter.isGeneric() && 208 if (specificOnly && filter.isGeneric() &&
178 !(filter instanceof WhitelistFilter)) 209 !(filter instanceof WhitelistFilter))
179 continue; 210 continue;
180 211
181 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) 212 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
182 return filter; 213 return filter;
183 } 214 }
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 398
368 exports.CombinedMatcher = CombinedMatcher; 399 exports.CombinedMatcher = CombinedMatcher;
369 400
370 /** 401 /**
371 * Shared {@link CombinedMatcher} instance that should usually be used. 402 * Shared {@link CombinedMatcher} instance that should usually be used.
372 * @type {CombinedMatcher} 403 * @type {CombinedMatcher}
373 */ 404 */
374 let defaultMatcher = new CombinedMatcher(); 405 let defaultMatcher = new CombinedMatcher();
375 406
376 exports.defaultMatcher = defaultMatcher; 407 exports.defaultMatcher = defaultMatcher;
OLDNEW
« lib/filterClasses.js ('K') | « lib/filterClasses.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld