|
27 | 27 | const µb = µBlock,
|
28 | 28 | duplicates = new Set(),
|
29 | 29 | scriptletCache = new µb.MRUCache(32),
|
30 |
| - scriptletsRegister = new Map(), |
31 |
| - exceptionsRegister = new Set(), |
32 | 30 | reEscapeScriptArg = /[\\'"]/g;
|
33 | 31 |
|
34 | 32 | let acceptedCount = 0,
|
|
289 | 287 | // Only exception filters are allowed to be global.
|
290 | 288 | const normalized = normalizeRawFilter(parsed.suffix);
|
291 | 289 |
|
| 290 | + // Tokenless is meaningful only for exception filters. |
| 291 | + if ( normalized === '+js()' && parsed.exception === false ) { |
| 292 | + return; |
| 293 | + } |
| 294 | + |
292 | 295 | if ( parsed.hostnames.length === 0 ) {
|
293 | 296 | if ( parsed.exception ) {
|
294 | 297 | writer.push([ 32, '', 1, normalized ]);
|
|
358 | 361 | }
|
359 | 362 |
|
360 | 363 | const scriptlets = new Set();
|
361 |
| - const exceptions = exceptionsRegister; |
| 364 | + const exceptions = new Set(); |
362 | 365 |
|
363 | 366 | scriptletDB.retrieve(
|
364 | 367 | hostname,
|
|
370 | 373 | [ scriptlets, exceptions ]
|
371 | 374 | );
|
372 | 375 | }
|
| 376 | + if ( scriptlets.size === 0 ) { return; } |
373 | 377 |
|
374 |
| - for ( const rawToken of scriptlets ) { |
375 |
| - lookupScriptlet(rawToken, reng, scriptletsRegister); |
| 378 | + const loggerEnabled = µb.logger.enabled; |
| 379 | + |
| 380 | + // Wholly disable scriptlet injection? |
| 381 | + if ( exceptions.has('') ) { |
| 382 | + if ( loggerEnabled ) { |
| 383 | + logOne(true, '', request); |
| 384 | + } |
| 385 | + return; |
376 | 386 | }
|
377 | 387 |
|
378 |
| - if ( scriptletsRegister.size === 0 ) { return; } |
| 388 | + const scriptletToCodeMap = new Map(); |
| 389 | + for ( const rawToken of scriptlets ) { |
| 390 | + lookupScriptlet(rawToken, reng, scriptletToCodeMap); |
| 391 | + } |
| 392 | + if ( scriptletToCodeMap.size === 0 ) { return; } |
379 | 393 |
|
380 | 394 | // Return an array of scriptlets, and log results if needed.
|
381 | 395 | const out = [];
|
382 |
| - const loggerEnabled = µb.logger.enabled; |
383 |
| - for ( const [ rawToken, code ] of scriptletsRegister ) { |
| 396 | + for ( const [ rawToken, code ] of scriptletToCodeMap ) { |
384 | 397 | const isException = exceptions.has(rawToken);
|
385 | 398 | if ( isException === false ) {
|
386 | 399 | out.push(code);
|
|
390 | 403 | }
|
391 | 404 | }
|
392 | 405 |
|
393 |
| - scriptletsRegister.clear(); |
394 |
| - exceptionsRegister.clear(); |
395 |
| - |
396 | 406 | if ( out.length === 0 ) { return; }
|
397 | 407 |
|
398 | 408 | if ( µb.hiddenSettings.debugScriptlets ) {
|
|
454 | 464 | scriptletDB = new µb.staticExtFilteringEngine.HostnameBasedDB(1, selfie);
|
455 | 465 | };
|
456 | 466 |
|
| 467 | + api.benchmark = async function() { |
| 468 | + const requests = await µb.loadBenchmarkDataset(); |
| 469 | + if ( Array.isArray(requests) === false || requests.length === 0 ) { |
| 470 | + console.info('No requests found to benchmark'); |
| 471 | + return; |
| 472 | + } |
| 473 | + console.info('Benchmarking scriptletFilteringEngine.retrieve()...'); |
| 474 | + const details = { |
| 475 | + domain: '', |
| 476 | + entity: '', |
| 477 | + hostname: '', |
| 478 | + tabId: 0, |
| 479 | + url: '', |
| 480 | + }; |
| 481 | + let count = 0; |
| 482 | + const t0 = self.performance.now(); |
| 483 | + for ( let i = 0; i < requests.length; i++ ) { |
| 484 | + const request = requests[i]; |
| 485 | + if ( request.cpt !== 'document' ) { continue; } |
| 486 | + count += 1; |
| 487 | + details.url = request.url; |
| 488 | + details.hostname = µb.URI.hostnameFromURI(request.url); |
| 489 | + details.domain = µb.URI.domainFromHostname(details.hostname); |
| 490 | + details.entity = µb.URI.entityFromDomain(details.domain); |
| 491 | + void this.retrieve(details); |
| 492 | + } |
| 493 | + const t1 = self.performance.now(); |
| 494 | + const dur = t1 - t0; |
| 495 | + console.info(`Evaluated ${count} requests in ${dur.toFixed(0)} ms`); |
| 496 | + console.info(`\tAverage: ${(dur / count).toFixed(3)} ms per request`); |
| 497 | + }; |
| 498 | + |
457 | 499 | return api;
|
458 | 500 | })();
|
459 | 501 |
|
|
0 commit comments