extesy / hoverzoom

Google Chrome extension for zooming images on mouse hover
https://chrome.google.com/webstore/detail/hover-zoom%20/pccckmaobkjjboncdfnnofkonhgpceea
MIT License
1.12k stars 177 forks source link

Global plugins cause significant CPU usage #1275

Open williamvds opened 8 months ago

williamvds commented 8 months ago

Describe the bug

Global plugins cause significant CPU usage when loading pages with lots of matched elements, particularly <a> links.

This appears to be due to matching elements overzealously with loose patterns.

To Reproduce

  1. Enable hoverzoom with all global plugins

  2. Visit a site with a lot of elements, e.g. https://nix-community.github.io/home-manager/options.xhtml

  3. Observe slowdowns, and probably a warning from your browser about hoverzoom slowing down the page

Expected behavior

Loading large pages with hoverzoom enabled does not slow down the browser.

Screenshots

I profiled the page linked above on Firefox, with all the hoverzoom plugins enabled:

Profiling results with all plugins enabled ![image](https://github.com/extesy/hoverzoom/assets/26379999/f26dd06c-d832-4417-a84b-f898938eb7d0)

I then disabled most of the global plugins I could find, particularly the most expensive ones in the above image. Profiling again, I see lower CPU usage, but the enabled plugins now appear to run twice?

Profiling results with some global plugins disabled ![image](https://github.com/extesy/hoverzoom/assets/26379999/015c6e76-62ae-4a73-9e5a-cbc06a8a2b19)

Desktop (please complete the following information):

Additional context

The profiling suggests the JQuery selectors are the most expensive part of processing. E.g. the most expensive matcher here

https://github.com/extesy/hoverzoom/blob/acccb87b2841624b6f5b4fd228152d795d3adbe1/plugins/niconico_a.js#L24

Is performing fuzzy matching on every link and image on every page.

image

Could these be prefix matchers instead, e.g. img[src^="https://lohas.nicoseiga.jp/thumb/"]? I couldn't quite work out what the URL should be for niconico, comments in the plugin suggest you need an account to view some pages.

Would document.querySelectorAll be faster than JQuery?

extesy commented 8 months ago

@GrosPoulet You added this plugin ("niconico_a.js") - does it have to be a global plugin, can it be scoped to a smaller subset of sites?

GrosPoulet commented 8 months ago

@williamvds @extesy #1276 should improve things.

extesy commented 6 months ago

@williamvds Can you please confirm if this is still a problem in version 1.0.214.2?

williamvds commented 6 months ago

image 1.0.214.2 shows significant improvement with the niconico plugin specifically. Hoverzoom still locks up on the page in question, because all the global plugins add up.

For now I'll keep global plugins disabled, there isn't really any other workaround.

thunfischbrot commented 4 months ago

Is there a way to identify global plugins from the hoverzoom+ UI, in order to deactivate them in one go?

williamvds commented 4 months ago

@thunfischbrot The plugins with the _a suffix are the global ones.

I had to disable them with some javascript in the console while on the settings page (for me that's moz-extension://effe954f-ff0e-4b40-bc2c-4dda6eff4348/html/options.html)

Array.from(document.querySelectorAll('input[type="checkbox"][id*="_a"]')).forEach(e => $(e).trigger('gumby.uncheck'))