Win11 Hdf5TreeView: gui.hdf5.Hdf5Item: cache expensive qt.QApplication.style().standardIcon() lookups#4592
Conversation
t20100
left a comment
There was a problem hiding this comment.
Thanks for looking into this and for proposing a fix!
This is unfortunate qt does not cache standard icons.
Just one comment: the cache key could be simplified.
| devicePixelRatio = None | ||
| app = qt.QApplication.instance() | ||
| if app is not None: | ||
| screen = app.primaryScreen() | ||
| if screen is not None: | ||
| devicePixelRatio = screen.devicePixelRatio() | ||
| key = style.objectName(), devicePixelRatio, icon |
There was a problem hiding this comment.
devicePixelRatio shouldn't be needed in the cache key: the QIcon contains multiple-size pixmaps so there is no need to change it with devicePixelRatio.
| devicePixelRatio = None | |
| app = qt.QApplication.instance() | |
| if app is not None: | |
| screen = app.primaryScreen() | |
| if screen is not None: | |
| devicePixelRatio = screen.devicePixelRatio() | |
| key = style.objectName(), devicePixelRatio, icon | |
| key = style.objectName(), icon |
BTW, if devicePixelRatio is needed, best to use QWindow.devicePixelRatio (see https://doc.qt.io/qt-6/qscreen.html#devicePixelRatio-prop)
There was a problem hiding this comment.
You are correct, I have confirmed that QIcon returns differently sized icons. However, depending on QT_SCALE_FACTOR, the size is scaled for some icons, (used QIcon.availableSizes() to check it):
QT_SCALE_FACTOR = 1 : SP_DirIcon: QSize(100, 100) ... QSize(1600, 1600)
QT_SCALE_FACTOR = 2 : SP_DirIcon : QSize(200, 200) ... QSize(3200, 3200)
But the behavior is not really consistent. For example SP_DialogOkButton
does not get scaled (16, 16) -> (128, 128) independent of QT_SCALE_FACTOR.
Thus, devicePixelRatio might still be required. Though, QIcon has a built-in scaling feature. So it might not make a big difference.
I think we could remove it. Let me know.
style.objectName() is something like windowsvista, win11, ...
Do we expect this to change during runtime? - Could also be removed.
|
I reverted it back to a minimal working version. This version does not account for style changes. And also not for full scaling (High DPI) changes. I think this would still be fine, if we accept that:
https://doc.qt.io/qt-6/qicon.html#pixmap |
t20100
left a comment
There was a problem hiding this comment.
LGTM, thanks for the fix!
From what I tested on Linux with PySide6, icons behaves correctly when moving the window on different screens with different resolutions and changing screen display ratio.
Addresses #4485
PR summary
Fixes Hdf5TreeView slowness on Win 11 caused by slow
qt.QApplication.style().standardIcon(icon)calls ingui.hdf5.Hdf5Item.This PR just adds simple caching of the icons in a
dict.The scaling factor of the primary screen is added as key to the icon cache. This should account for multi-monitor setups with different DPI scaling. - Works on my two screens.
There is a related issue addressing silx icon caching, which causes Exceptions when leaving silx
#1771
But I haven't encountered this issue with the simple dict solution.
pytests fail on my machine with a probably unrelated issue:
AI Disclosure
AI tool Codex used for automated bug finding, benchmarks and code generation.