8000 Fix cycle in _update_draw/_set_highlight for Points and Shapes (high CPU background usage) by aganders3 · Pull Request #6425 · napari/napari · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix cycle in _update_draw/_set_highlight for Points and Shapes (high CPU background usage) #6425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2023

Conversation

aganders3
Copy link
Contributor

I agree something like #6234 is a better long-term fix, but we should do something in the meantime because the high CPU is pretty severe.

References and relevant issues

Closes #6223

Description

This breaks a cycle when updating the highlight on Points and Shapes layers. The problem was _set_highlight being called with force=True in _update_draw. This caused a cycle as redrawing the highlights would again call _update_draw. This was added to fix the appearance of the highlights during interaction - see #6223 for more discussion. I'm hoping @brisvag can confirm this PR maintains the correct behavior.

This PR tracks scale_factor the same way selected_data and other properties are stored in order to conditionally update the highlight instead of forcing it.

@aganders3 aganders3 requested a review from brisvag November 8, 2023 15:13
@@ -1610,7 +1615,7 @@ def _update_draw(
super()._update_draw(
scale_factor, corner_pixels_displayed, shape_threshold
)
self._set_highlight(force=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Is this change not enough? There is a reason why someone use force here. Has this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is enough to break the cycle, but the update (with force=True) was added in #5737 and #5802 to fix the highlights when zooming. So this should be the same - forces an update if the scale factor is changed but not otherwise.

Copy l 8000 ink
Collaborator
@Czaki Czaki Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just

        prev_scale = self.scale_factor
        super()._update_draw(
            scale_factor, corner_pixels_displayed, shape_threshold
        )
        self._set_highlight(force=(prev_scale != scale_factor))

To avoid additional parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha - this was my initial proposal in #6223 (comment).

I like it better as well, though 188438f perhaps matches more closely how other parameters are handled in _set_highlight so I could go either way. I'll update the PR and see if anyone else has feedback.

Copy link
Contributor
@brisvag brisvag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either would work IMO. Good for me!

@psobolewskiPhD
Copy link
Member

I can confirm this fixes the issue on my end.
Also the issue is not present in 0.4.19rc, just main, because the PR that triggered the issue is 0.5.0 but I think that's because it was a time crunch into 0.4.18.
#5802 is a bugfix, so I wonder if it should be tagged 0.4.19 along with this PR and cherry picked to v0.4.19?

@psobolewskiPhD psobolewskiPhD added performance Relates to performance bugfix PR with bugfix labels Nov 8, 2023
@brisvag
Copy link
Contributor
brisvag commented Nov 9, 2023

#5802 is a bugfix, so I wonder if it should be tagged 0.4.19 along with this PR and cherry picked to v0.4.19?

Agree in principle, but it might be a nasty one to cherry pick since ti touches many little spots... @Czaki ?

@Czaki Czaki added this to the 0.4.19 milestone Nov 9, 2023
@Czaki Czaki added the ready to merge Last chance for comments! Will be merged in ~24h label Nov 9, 2023
Copy link
codecov bot commented Nov 9, 2023

Codecov Report

Merging #6425 (3545e9e) into main (a59cdac) will decrease coverage by 0.07%.
Report is 1 commits behind head on main.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #6425      +/-   ##
==========================================
- Coverage   92.16%   92.09%   -0.07%     
==========================================
  Files         597      597              
  Lines       52896    52898       +2     
==========================================
- Hits        48749    48716      -33     
- Misses       4147     4182      +35     
Files Coverage Δ
napari/layers/points/points.py 97.79% <100.00%> (+<0.01%) ⬆️
napari/layers/shapes/shapes.py 92.44% <100.00%> (+<0.01%) ⬆️

... and 11 files with indirect coverage changes

@jni jni merged commit 1779bd5 into napari:main Nov 11, 2023
@jni
Copy link
Member
jni commented Nov 11, 2023

I propose not cherry-picking. Let's get 0.4.19 out and focus our efforts on getting 0.5.0 out soon too.

@jni jni removed the ready to merge Last chance for comments! Will be merged in ~24h label Nov 11, 2023
@jni
Copy link
Member
jni commented Nov 11, 2023

(But I see @Czaki has already set the milestone — as he is leading the cherry-picking I'll let him change this or not as he chooses.)

@aganders3 aganders3 deleted the fix-6223-high-cpu-points-shapes branch November 11, 2023 11:18
Czaki pushed a commit that referenced this pull request Nov 13, 2023
…CPU background usage) (#6425)

I agree something like #6234 is a
better long-term fix, but we should do something in the meantime because
the high CPU is pretty severe.

# References and relevant issues
Closes #6223

# Description
This breaks a cycle when updating the highlight on Points and Shapes
layers. The problem was `_set_highlight` being called with `force=True`
in `_update_draw`. This caused a cycle as redrawing the highlights would
again call `_update_draw`. This was added to fix the appearance of the
highlights during interaction - see #6223 for more discussion. I'm
hoping @brisvag can confirm this PR maintains the correct behavior.

This PR tracks `scale_factor` the same way `selected_data` and other
properties are stored in order to conditionally update the highlight
instead of forcing it.
Czaki pushed a commit that referenced this pull request Nov 13, 2023
…CPU background usage) (#6425)

I agree something like #6234 is a
better long-term fix, but we should do something in the meantime because
the high CPU is pretty severe.

# References and relevant issues
Closes #6223

# Description
This breaks a cycle when updating the highlight on Points and Shapes
layers. The problem was `_set_highlight` being called with `force=True`
in `_update_draw`. This caused a cycle as redrawing the highlights would
again call `_update_draw`. This was added to fix the appearance of the
highlights during interaction - see #6223 for more discussion. I'm
hoping @brisvag can confirm this PR maintains the correct behavior.

This PR tracks `scale_factor` the same way `selected_data` and other
properties are stored in order to conditionally update the highlight
instead of forcing it.
Czaki added a commit that referenced this pull request Aug 8, 2024
# References and relevant issues
closes #7071


# Description

Currently, on main, we omit a highlight event every time when
`_set_highlight` method is called. However, it is called every time the
zoom or canvas size is changed to fix some problems with highlights (see
#6425)

I think that it should be emitted only if selection is changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR with bugfix performance Relates to performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Performance, main] High CPU usage with Shapes/Points when napari is in the background
5 participants
0