adblocker: Extremely slow request decision

Hi, I noticed after update (0.10 I guess) the engine.match method became super-slow. Here’s the code I’m using in my Electron app, and it literally hangs up:

 webRequest.onBeforeRequest(
    { urls: ['<all_urls>'] },
    async (details: Electron.OnBeforeRequestDetails, callback: any) => {
      if (engine && settings.isShieldToggled) {
        console.time('engine.match');
        const { match, redirect } = engine.match(
          Request.fromRawDetails({
            type: details.resourceType as any,
            url: details.url,
          }),
        );
        console.timeEnd('engine.match');

        if (match || redirect) {
          appWindow.webContents.send(`blocked-ad-${details.webContentsId}`);

          if (redirect) {
            callback({ redirectURL: redirect });
          } else {
            callback({ cancel: true });
          }

          return;
        }
      }

      callback({ cancel: false });
    },
  );

image

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

To make optimal use of cosmetics (both in terms of performance and coverage), the handling needs to be slightly more involved. The way to do it optimally depends on the capabilities. If we have a way to inject “content scripts” in pages via Electron APIs, then you can implement the full capabilities like in WebExtensions. The ideal scenario with cosmetics is this one:

  1. as soon as a frame is created (either main frame of a page or sub-frames/iframes), inject generic cosmetics and scripts
  2. when DOM is ready, we can query the available node IDs, classes and hrefs present in the page, which allows the adblocker to return a very small subset of custom styles to inject in the page
  3. we can optionally monitor changes in the page to detect additions or modifications of elements which should be hidden (e.g.: new ads being loaded). This is done using MutationObserver in the web extension context and maybe there is a way to do the same for Electron.

In puppeteer we only do 1. and 2. at the moment. But probably we can do 3. for Electron.

Thank you for taking your time for investigating this issue. It would never come to my mind that it’s a particular list fault.

Also, I would really like the adblocker to offer a more robust support for Electron out of the box…

Sure, I will take a look at this.