Bug 460621

Summary: kcalc: high energy use due to KCalculator::resizeEvent() storm
Product: [Applications] kcalc Reporter: Janne Grunau <janne-kde>
Component: generalAssignee: Evan Teran <evan.teran>
Status: REPORTED ---    
Severity: normal    
Priority: NOR    
Version First Reported In: 22.08.2   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: debug patch removing the resize call in resizeEvent

Description Janne Grunau 2022-10-17 19:55:31 UTC
Created attachment 152962 [details]
debug patch removing the resize call in resizeEvent

SUMMARY
Kcalc causes high energy use on Apple Silicon devices (M1 / M2) running Linux (https://asahilinux.org/). An idle Macbook with kcalc running uses ~9 Watt, the same system uses 3-4 Watt before starting kcalc and after closing it.

This seems to be caused an endless storm of KCalculator::resizeEvent()s and resulting redraws. There appears to be a problem with resizing to the minimal size. This is possibly caused by calling QWidget::resize() from within KCalculator::resizeEvent(). The Qt documentation warns explicitly about calling ::resize() inside of ::resizeEvent(): https://doc.qt.io/qt-5/qwidget.html#size-prop . I'm not sure if this is the endless recursion warned about in the documentation. If so the event handling prevents unlimited memory use one would expect from an endless recursion.

Commenting "KCalculator::resize(minSize);" in KCalculator::resizeEvent() prevents issue but results in window 1 pixel narrower than the minimal size.

STEPS TO REPRODUCE
1. start kcalc

OBSERVED RESULT
kcalc uses between 65% (kcalc window is on-screen) and 80% (kcalc window is behind) of a single CPU core 


EXPECTED RESULT
kcalc uses almost no CPU when idle

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: AsahiLinux (Qt/KDE packages are identical to ArchLinux ARM)
(available in About System)
KDE Plasma Version: 5.25.5, 5.26.0
KDE Frameworks Version: 5.98.0, 5.99.0
Qt Version: 5.15.6

ADDITIONAL INFORMATION
System is running Plasma (X11) with modeset driver, simpledrm and llvmpipe

Running kcalc with the attached kcalc_resize_debug.diff results in following output:
| resizeEvent: actual: 490x396 min: 490x396 old: 4294967295x4294967295
| resizeEvent: actual: 489x396 min: 490x396 old: 490x396
Comment 1 Janne Grunau 2022-10-17 20:38:19 UTC
This issue is not reproducible under Plasma (Wayland) on the same system with the same binaries.