OLD | NEW |
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 | 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 }; |
| 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; |
36 | 73 |
(...skipping 204 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 } | 289 } |
253 exports.CombinedMatcher = CombinedMatcher; | 290 exports.CombinedMatcher = CombinedMatcher; |
254 | 291 |
255 /** | 292 /** |
256 * Maximal number of matching cache entries to be kept | 293 * Maximal number of matching cache entries to be kept |
257 * @type {number} | 294 * @type {number} |
258 */ | 295 */ |
259 CombinedMatcher.maxCacheEntries = 1000; | 296 CombinedMatcher.maxCacheEntries = 1000; |
260 | 297 |
261 CombinedMatcher.prototype = | 298 CombinedMatcher.prototype = |
262 { | 299 { |
263 /** | 300 /** |
264 * Matcher for blocking rules. | 301 * Matcher for blocking rules. |
265 * @type {Matcher} | 302 * @type {Matcher} |
266 */ | 303 */ |
267 blacklist: null, | 304 blacklist: null, |
268 | 305 |
269 /** | 306 /** |
270 * Matcher for exception rules. | 307 * Matcher for exception rules. |
271 * @type {Matcher} | 308 * @type {Matcher} |
272 */ | 309 */ |
273 whitelist: null, | 310 whitelist: null, |
274 | 311 |
275 /** | 312 /** |
276 * Lookup table of previous matchesAny results | 313 * Lookup table of previous matchesAny results |
277 * @type {Map.<string,Filter>} | 314 * @type {Cache.<string,Filter>} |
278 */ | 315 */ |
279 resultCache: null, | 316 resultCache: null, |
280 | 317 |
281 /** | 318 /** |
282 * @see Matcher#clear | 319 * @see Matcher#clear |
283 */ | 320 */ |
284 clear() | 321 clear() |
285 { | 322 { |
286 this.blacklist.clear(); | 323 this.blacklist.clear(); |
287 this.whitelist.clear(); | 324 this.whitelist.clear(); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 447 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
411 " " + sitekey + " " + specificOnly; | 448 " " + sitekey + " " + specificOnly; |
412 | 449 |
413 let result = this.resultCache.get(key); | 450 let result = this.resultCache.get(key); |
414 if (result !== undefined) | 451 if (result !== undefined) |
415 return result; | 452 return result; |
416 | 453 |
417 result = this.matchesAnyInternal(location, typeMask, docDomain, | 454 result = this.matchesAnyInternal(location, typeMask, docDomain, |
418 thirdParty, sitekey, specificOnly); | 455 thirdParty, sitekey, specificOnly); |
419 | 456 |
420 if (this.resultCache.size >= CombinedMatcher.maxCacheEntries) | |
421 this.resultCache.clear(); | |
422 | |
423 this.resultCache.set(key, result); | 457 this.resultCache.set(key, result); |
424 | 458 |
425 return result; | 459 return result; |
426 } | 460 } |
427 }; | 461 }; |
428 | 462 |
429 /** | 463 /** |
430 * Shared CombinedMatcher instance that should usually be used. | 464 * Shared CombinedMatcher instance that should usually be used. |
431 * @type {CombinedMatcher} | 465 * @type {CombinedMatcher} |
432 */ | 466 */ |
433 exports.defaultMatcher = new CombinedMatcher(); | 467 exports.defaultMatcher = new CombinedMatcher(); |
OLD | NEW |