From 4902b8137b0b6135deffd6a8fd64d1df501de296 Mon Sep 17 00:00:00 2001 From: Ger Hobbelt Date: Sun, 23 Jan 2022 14:10:22 +0100 Subject: [PATCH] Improve startup time of the display sample under MSW Performance tweak for samples/display, resulting in a much faster application start, particularly when built in debug mode, when Windows messages are dumped to the system debug channel for inspection/diagnosis. Speed up the Append() loop below by foregoing the repeated resizing of the choice drop-down via repeated calls to GetBestSize() which happens deep inside the Append() call chain and executes another inner loop calling SendMessage() to get the control contents. (This exhibits 1/2*O(N^2) behaviour thanks to the linear growth of the length of the inner loop to the length of the outer loop (= number of items to add), while it is re-executed for every new added item.) With the 'display' sample, that's about 500+ rounds and about 500*500/2 SendMessage() calls less now on my dev/test rig, taking noticeable time to start the display application. --- Issue was found due to the barrage of '(winmsg)' Windows Message debug log lines zipping by in the monitor app when the sample was build in Debug Mode. Only significant difference with the Release Build is those debug lines being output, hence the performance gain is less, but still measurable, in a Release build. When the machine is otherwise severely loaded (UI render core maxing out), "measurable" becomes "obnoxiously noticeable" again on Win10/64. Closes #22049. --- samples/display/display.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/samples/display/display.cpp b/samples/display/display.cpp index ed1d196858..ac572af0cc 100644 --- a/samples/display/display.cpp +++ b/samples/display/display.cpp @@ -28,6 +28,7 @@ #include "wx/artprov.h" #include "wx/bookctrl.h" #include "wx/sysopt.h" +#include "wx/wupdlock.h" #include "wx/display.h" @@ -307,15 +308,28 @@ void MyFrame::PopuplateWithDisplayInfo() #if wxUSE_DISPLAY wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode); - const wxArrayVideoModes modes = display.GetModes(); - const size_t countModes = modes.GetCount(); - for ( size_t nMode = 0; nMode < countModes; nMode++ ) - { - const wxVideoMode& mode = modes[nMode]; - choiceModes->Append(VideoModeToText(mode), - new MyVideoModeClientData(mode)); - } + { + // Speed up the Append() loop below by foregoing the repeated resizing + // of the choice dropdown via repeated calls to GetBestSize() which + // happens deep inside the Append() call chain and executes another + // inner loop calling SendMessage() to get the control contents. + // + // As there can be a couple of hundreds of video modes, this saves + // many thousands of such calls and so has a very noticeable effect. + wxWindowUpdateLocker lockUpdates(choiceModes); + + const wxArrayVideoModes modes = display.GetModes(); + const size_t countModes = modes.GetCount(); + for ( size_t nMode = 0; nMode < countModes; nMode++ ) + { + const wxVideoMode& mode = modes[nMode]; + + choiceModes->Append(VideoModeToText(mode), + new MyVideoModeClientData(mode)); + } + } // Destroy wxWindowUpdateLocker to finally resize the window now. + const wxString currentMode = VideoModeToText(display.GetCurrentMode()); choiceModes->SetStringSelection(currentMode);