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 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 * Combines a matcher for blocking and exception rules, automatically sorts | 242 * Combines a matcher for blocking and exception rules, automatically sorts |
243 * rules into two Matcher instances. | 243 * rules into two Matcher instances. |
244 * @constructor | 244 * @constructor |
245 * @augments Matcher | 245 * @augments Matcher |
246 */ | 246 */ |
247 function CombinedMatcher() | 247 function CombinedMatcher() |
248 { | 248 { |
249 this.blacklist = new Matcher(); | 249 this.blacklist = new Matcher(); |
250 this.whitelist = new Matcher(); | 250 this.whitelist = new Matcher(); |
251 this.resultCache = new Map(); | 251 this.resultCache = new Map(); |
| 252 this.frequentResultCache = new Map(); |
252 } | 253 } |
253 exports.CombinedMatcher = CombinedMatcher; | 254 exports.CombinedMatcher = CombinedMatcher; |
254 | 255 |
255 /** | 256 /** |
256 * Maximal number of matching cache entries to be kept | 257 * Maximal number of matching cache entries to be kept |
257 * @type {number} | 258 * @type {number} |
258 */ | 259 */ |
259 CombinedMatcher.maxCacheEntries = 1000; | 260 CombinedMatcher.maxCacheEntries = 500; |
260 | 261 |
261 CombinedMatcher.prototype = | 262 CombinedMatcher.prototype = |
262 { | 263 { |
263 /** | 264 /** |
264 * Matcher for blocking rules. | 265 * Matcher for blocking rules. |
265 * @type {Matcher} | 266 * @type {Matcher} |
266 */ | 267 */ |
267 blacklist: null, | 268 blacklist: null, |
268 | 269 |
269 /** | 270 /** |
270 * Matcher for exception rules. | 271 * Matcher for exception rules. |
271 * @type {Matcher} | 272 * @type {Matcher} |
272 */ | 273 */ |
273 whitelist: null, | 274 whitelist: null, |
274 | 275 |
275 /** | 276 /** |
276 * Lookup table of previous matchesAny results | 277 * Lookup table of previous matchesAny results |
277 * @type {Map.<string,Filter>} | 278 * @type {Map.<string,Filter>} |
278 */ | 279 */ |
279 resultCache: null, | 280 resultCache: null, |
280 | 281 |
281 /** | 282 /** |
| 283 * Lookup table of frequently accessed previous matchesAny results |
| 284 * @type {Map.<string,Filter>} |
| 285 */ |
| 286 frequentResultCache: null, |
| 287 |
| 288 /** |
282 * @see Matcher#clear | 289 * @see Matcher#clear |
283 */ | 290 */ |
284 clear() | 291 clear() |
285 { | 292 { |
286 this.blacklist.clear(); | 293 this.blacklist.clear(); |
287 this.whitelist.clear(); | 294 this.whitelist.clear(); |
288 this.resultCache.clear(); | 295 this.resultCache.clear(); |
| 296 this.frequentResultCache.clear(); |
289 }, | 297 }, |
290 | 298 |
291 /** | 299 /** |
292 * @see Matcher#add | 300 * @see Matcher#add |
293 * @param {Filter} filter | 301 * @param {Filter} filter |
294 */ | 302 */ |
295 add(filter) | 303 add(filter) |
296 { | 304 { |
297 if (filter instanceof WhitelistFilter) | 305 if (filter instanceof WhitelistFilter) |
298 this.whitelist.add(filter); | 306 this.whitelist.add(filter); |
299 else | 307 else |
300 this.blacklist.add(filter); | 308 this.blacklist.add(filter); |
301 | 309 |
302 this.resultCache.clear(); | 310 this.resultCache.clear(); |
| 311 this.frequentResultCache.clear(); |
303 }, | 312 }, |
304 | 313 |
305 /** | 314 /** |
306 * @see Matcher#remove | 315 * @see Matcher#remove |
307 * @param {Filter} filter | 316 * @param {Filter} filter |
308 */ | 317 */ |
309 remove(filter) | 318 remove(filter) |
310 { | 319 { |
311 if (filter instanceof WhitelistFilter) | 320 if (filter instanceof WhitelistFilter) |
312 this.whitelist.remove(filter); | 321 this.whitelist.remove(filter); |
313 else | 322 else |
314 this.blacklist.remove(filter); | 323 this.blacklist.remove(filter); |
315 | 324 |
316 this.resultCache.clear(); | 325 this.resultCache.clear(); |
| 326 this.frequentResultCache.clear(); |
317 }, | 327 }, |
318 | 328 |
319 /** | 329 /** |
320 * @see Matcher#findKeyword | 330 * @see Matcher#findKeyword |
321 * @param {Filter} filter | 331 * @param {Filter} filter |
322 * @return {string} keyword | 332 * @return {string} keyword |
323 */ | 333 */ |
324 findKeyword(filter) | 334 findKeyword(filter) |
325 { | 335 { |
326 if (filter instanceof WhitelistFilter) | 336 if (filter instanceof WhitelistFilter) |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 | 413 |
404 /** | 414 /** |
405 * @see Matcher#matchesAny | 415 * @see Matcher#matchesAny |
406 * @inheritdoc | 416 * @inheritdoc |
407 */ | 417 */ |
408 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 418 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
409 { | 419 { |
410 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 420 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
411 " " + sitekey + " " + specificOnly; | 421 " " + sitekey + " " + specificOnly; |
412 | 422 |
413 let result = this.resultCache.get(key); | 423 let result = this.frequentResultCache.get(key); |
414 if (result !== undefined) | 424 if (result !== undefined) |
415 return result; | 425 return result; |
416 | 426 |
| 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 |
417 result = this.matchesAnyInternal(location, typeMask, docDomain, | 438 result = this.matchesAnyInternal(location, typeMask, docDomain, |
418 thirdParty, sitekey, specificOnly); | 439 thirdParty, sitekey, specificOnly); |
419 | 440 |
420 if (this.resultCache.size >= CombinedMatcher.maxCacheEntries) | 441 if (this.resultCache.size >= CombinedMatcher.maxCacheEntries) |
421 this.resultCache.clear(); | 442 this.resultCache.clear(); |
422 | 443 |
423 this.resultCache.set(key, result); | 444 this.resultCache.set(key, result); |
424 | 445 |
425 return result; | 446 return result; |
426 } | 447 } |
427 }; | 448 }; |
428 | 449 |
429 /** | 450 /** |
430 * Shared CombinedMatcher instance that should usually be used. | 451 * Shared CombinedMatcher instance that should usually be used. |
431 * @type {CombinedMatcher} | 452 * @type {CombinedMatcher} |
432 */ | 453 */ |
433 exports.defaultMatcher = new CombinedMatcher(); | 454 exports.defaultMatcher = new CombinedMatcher(); |
OLD | NEW |