LEFT | RIGHT |
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 {Filter, WhitelistFilter} = require("./filterClasses"); | 25 const {Filter, WhitelistFilter} = require("./filterClasses"); |
| 26 |
| 27 function Cache(capacity) |
| 28 { |
| 29 this.capacity = capacity; |
| 30 |
| 31 this.fresh = new Map(); |
| 32 this.hot = new Map(); |
| 33 } |
| 34 |
| 35 Cache.prototype = { |
| 36 get(key) |
| 37 { |
| 38 let value = this.hot.get(key); |
| 39 if (value !== undefined) |
| 40 return value; |
| 41 |
| 42 value = this.fresh.get(key); |
| 43 if (value !== undefined) |
| 44 this.set(key, value, this.hot); |
| 45 |
| 46 return value; |
| 47 }, |
| 48 |
| 49 set(key, value, map = this.fresh) |
| 50 { |
| 51 if (map.size >= this.capacity / 2) |
| 52 map.clear(); |
| 53 |
| 54 map.set(key, value); |
| 55 }, |
| 56 |
| 57 clear() |
| 58 { |
| 59 this.fresh.clear(); |
| 60 this.hot.clear(); |
| 61 } |
| 62 }; |
26 | 63 |
27 /** | 64 /** |
28 * Blacklist/whitelist filter matching | 65 * Blacklist/whitelist filter matching |
29 * @constructor | 66 * @constructor |
30 */ | 67 */ |
31 function Matcher() | 68 function Matcher() |
32 { | 69 { |
33 this.clear(); | 70 this.clear(); |
34 } | 71 } |
35 exports.Matcher = Matcher; | 72 exports.Matcher = Matcher; |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 /** | 278 /** |
242 * Combines a matcher for blocking and exception rules, automatically sorts | 279 * Combines a matcher for blocking and exception rules, automatically sorts |
243 * rules into two Matcher instances. | 280 * rules into two Matcher instances. |
244 * @constructor | 281 * @constructor |
245 * @augments Matcher | 282 * @augments Matcher |
246 */ | 283 */ |
247 function CombinedMatcher() | 284 function CombinedMatcher() |
248 { | 285 { |
249 this.blacklist = new Matcher(); | 286 this.blacklist = new Matcher(); |
250 this.whitelist = new Matcher(); | 287 this.whitelist = new Matcher(); |
251 this.resultCache = new Map(); | 288 this.resultCache = new Cache(CombinedMatcher.maxCacheEntries); |
252 this.frequentResultCache = new Map(); | |
253 } | 289 } |
254 exports.CombinedMatcher = CombinedMatcher; | 290 exports.CombinedMatcher = CombinedMatcher; |
255 | 291 |
256 /** | 292 /** |
257 * Maximal number of matching cache entries to be kept | 293 * Maximal number of matching cache entries to be kept |
258 * @type {number} | 294 * @type {number} |
259 */ | 295 */ |
260 CombinedMatcher.maxCacheEntries = 500; | 296 CombinedMatcher.maxCacheEntries = 1000; |
261 | 297 |
262 CombinedMatcher.prototype = | 298 CombinedMatcher.prototype = |
263 { | 299 { |
264 /** | 300 /** |
265 * Matcher for blocking rules. | 301 * Matcher for blocking rules. |
266 * @type {Matcher} | 302 * @type {Matcher} |
267 */ | 303 */ |
268 blacklist: null, | 304 blacklist: null, |
269 | 305 |
270 /** | 306 /** |
271 * Matcher for exception rules. | 307 * Matcher for exception rules. |
272 * @type {Matcher} | 308 * @type {Matcher} |
273 */ | 309 */ |
274 whitelist: null, | 310 whitelist: null, |
275 | 311 |
276 /** | 312 /** |
277 * Lookup table of previous matchesAny results | 313 * Lookup table of previous matchesAny results |
278 * @type {Map.<string,Filter>} | 314 * @type {Cache.<string,Filter>} |
279 */ | 315 */ |
280 resultCache: null, | 316 resultCache: null, |
281 | |
282 /** | |
283 * Lookup table of frequently accessed previous matchesAny results | |
284 * @type {Map.<string,Filter>} | |
285 */ | |
286 frequentResultCache: null, | |
287 | 317 |
288 /** | 318 /** |
289 * @see Matcher#clear | 319 * @see Matcher#clear |
290 */ | 320 */ |
291 clear() | 321 clear() |
292 { | 322 { |
293 this.blacklist.clear(); | 323 this.blacklist.clear(); |
294 this.whitelist.clear(); | 324 this.whitelist.clear(); |
295 this.resultCache.clear(); | 325 this.resultCache.clear(); |
296 this.frequentResultCache.clear(); | |
297 }, | 326 }, |
298 | 327 |
299 /** | 328 /** |
300 * @see Matcher#add | 329 * @see Matcher#add |
301 * @param {Filter} filter | 330 * @param {Filter} filter |
302 */ | 331 */ |
303 add(filter) | 332 add(filter) |
304 { | 333 { |
305 if (filter instanceof WhitelistFilter) | 334 if (filter instanceof WhitelistFilter) |
306 this.whitelist.add(filter); | 335 this.whitelist.add(filter); |
307 else | 336 else |
308 this.blacklist.add(filter); | 337 this.blacklist.add(filter); |
309 | 338 |
310 this.resultCache.clear(); | 339 this.resultCache.clear(); |
311 this.frequentResultCache.clear(); | |
312 }, | 340 }, |
313 | 341 |
314 /** | 342 /** |
315 * @see Matcher#remove | 343 * @see Matcher#remove |
316 * @param {Filter} filter | 344 * @param {Filter} filter |
317 */ | 345 */ |
318 remove(filter) | 346 remove(filter) |
319 { | 347 { |
320 if (filter instanceof WhitelistFilter) | 348 if (filter instanceof WhitelistFilter) |
321 this.whitelist.remove(filter); | 349 this.whitelist.remove(filter); |
322 else | 350 else |
323 this.blacklist.remove(filter); | 351 this.blacklist.remove(filter); |
324 | 352 |
325 this.resultCache.clear(); | 353 this.resultCache.clear(); |
326 this.frequentResultCache.clear(); | |
327 }, | 354 }, |
328 | 355 |
329 /** | 356 /** |
330 * @see Matcher#findKeyword | 357 * @see Matcher#findKeyword |
331 * @param {Filter} filter | 358 * @param {Filter} filter |
332 * @return {string} keyword | 359 * @return {string} keyword |
333 */ | 360 */ |
334 findKeyword(filter) | 361 findKeyword(filter) |
335 { | 362 { |
336 if (filter instanceof WhitelistFilter) | 363 if (filter instanceof WhitelistFilter) |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 | 440 |
414 /** | 441 /** |
415 * @see Matcher#matchesAny | 442 * @see Matcher#matchesAny |
416 * @inheritdoc | 443 * @inheritdoc |
417 */ | 444 */ |
418 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 445 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
419 { | 446 { |
420 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 447 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
421 " " + sitekey + " " + specificOnly; | 448 " " + sitekey + " " + specificOnly; |
422 | 449 |
423 let result = this.frequentResultCache.get(key); | 450 let result = this.resultCache.get(key); |
424 if (result !== undefined) | 451 if (result !== undefined) |
425 return result; | 452 return result; |
426 | 453 |
427 result = this.resultCache.get(key); | |
428 if (result !== undefined) | |
429 { | |
430 if (this.frequentResultCache.size >= CombinedMatcher.maxCacheEntries) | |
431 this.frequentResultCache.clear(); | |
432 | |
433 this.frequentResultCache.set(key, result); | |
434 | |
435 return result; | |
436 } | |
437 | |
438 result = this.matchesAnyInternal(location, typeMask, docDomain, | 454 result = this.matchesAnyInternal(location, typeMask, docDomain, |
439 thirdParty, sitekey, specificOnly); | 455 thirdParty, sitekey, specificOnly); |
440 | |
441 if (this.resultCache.size >= CombinedMatcher.maxCacheEntries) | |
442 this.resultCache.clear(); | |
443 | 456 |
444 this.resultCache.set(key, result); | 457 this.resultCache.set(key, result); |
445 | 458 |
446 return result; | 459 return result; |
447 } | 460 } |
448 }; | 461 }; |
449 | 462 |
450 /** | 463 /** |
451 * Shared CombinedMatcher instance that should usually be used. | 464 * Shared CombinedMatcher instance that should usually be used. |
452 * @type {CombinedMatcher} | 465 * @type {CombinedMatcher} |
453 */ | 466 */ |
454 exports.defaultMatcher = new CombinedMatcher(); | 467 exports.defaultMatcher = new CombinedMatcher(); |
LEFT | RIGHT |