Intense lag when dragging window around

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Intense lag when dragging window around

Post by Tapsa »

Hi all,

For years I thought the lag in my app was caused due to sheer number of widgets I'm using and belief that the Windows API was extremely poor in performance. I used to develop using MinGW, but I recently moved to Visual Studio and used its performance profiler to check cause of the lag.

As you can see, one CPU core maxed out during the time I dragged the app around, but the app kept moving around for over ten seconds after I had released the mouse, however CPU was used very little during that time, so the lag cannot be drawing related I think. When one core was maxed out, about 78 % of the CPU time was spent in ::DefWindowProc(GetHwnd(), nMsg, wParam, lParam);
wxBug.png
wxBug.png (7.42 KiB) Viewed 1199 times
I have several wxPanels with lots of widgets in them added into wxNotebook. I disabled the code that creates all but one tab, and the lag was practically gone. Then I added back some tabs and the lag came back more with each tab. Remember, that only one tab is ever visible! I overrid and disabled ProcessEvent, Refresh, and Update on my very minimally customized wxPanel class that all the tabs use, and the lag was exactly the same, so at least it's not due to drawing anything invisible.

Does anybody have a clue what might be wrong here?
How can invisible tabs in wxNotebook cause extraordinary lag when dragging the whole app window around?
I think I need a way to stop whatever calls are made during window drag to propagate into invisible tabs and invisible widgets in visible tab.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Intense lag when dragging window around

Post by doublemax »

Just moving a window around doesn't cause any redraws, so it must be something else.

On what hardware (CPU, GPU etc) are you testing?

Can you estimate the total number of windows (that means every single control, panel, text etc) in your app?

Do you have wxIdle or wxUpdateUIEvent event handlers? Especially the latter are evil for performance.
Or any other event handlers that are called each time you move the mouse?
Use the source, Luke!
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: Intense lag when dragging window around

Post by Tapsa »

My CPU is Intel Core i7-7700 and GPU is nVidia GeForce GTX 1070.

My app has about 2500 instances of wxWidgets classes total, most of which are customized wxTextCtrl (>474), wxStaticText (>505), and wxComboCtrl (>96). There are also over 648 wxBoxSizers, over 117 wxGridSizers, for example. The sizers are not arranged very optimally.

I have wxIdleEvent handler bound to main custom wxFrame. Removing that appears to not affect the performance of moving the entire app window around. Most, if not all, other events are kill focus and mouse or keyboard click events. I have several wxAcceleratorEntry too.

I hope to find some solution that basically ignores invisible widgets from being touched when window is being moved (or resized, or scrolled), because omitting the creation of the hidden tabs eliminates the lag for tabs that have only a few dozens of widgets.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Intense lag when dragging window around

Post by doublemax »

With that many controls, i assume they are in a scrolled window? There is this very old unsolved ticket which could be related (although it strictly talks about scrolling): http://trac.wxwidgets.org/ticket/15766

Could you check if running the app in Vista compatibility mode changes anything?
Use the source, Luke!
Tapsa
Earned some good credits
Earned some good credits
Posts: 147
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: Intense lag when dragging window around

Post by Tapsa »

Correct, most tabs have a scrolled window.

Compatibility modes XP SP3, Vista and 7 have no effect on the lag.
I can try the detailed steps with custom databases etc, since I only used checkbox in executable properties.

Btw, I added logging into wxWindowMSW::MSWDefWindowProc, which to my surprise reduced the lag when moving window around (the app window would teleport large distances), but it made scrolling scrolled window much more laggy.

I think that bug you linked is definitely related. Reading the comments it almost feel like I wrote many of them.
What I can see in Visual Studio profiler is that wxWindowMSW::MSWDefWindowProc is called recursively. Time spent calling ::DispatchMessage(msg); is much lesser than what user32.dll decides to do with that later on.

EDIT:
I selected Windows Vista (Service Pack 2) and ScrollWindowExFlags in Compatibility Administrator and did a Test Run, but there was zero impact on window move lag. My scrolled windows are scrolling smooth enough already, due to custom code for that.

Code: Select all

// Smooth scrolling
Bind(wxEVT_MOUSEWHEEL, [this](wxMouseEvent &event)
{
    GetViewStart(&pos, &pos);
    Scroll(0, event.GetWheelRotation() < 0 ? pos + 3 : max(pos - 3, 0));
});
Maybe I can make another custom code for window move and resize and do something similar.
Post Reply