Skip to content

CAnyRichEdit::AddStr() is very slow and should be optimized #79

@d0vgan

Description

@d0vgan

When NppExec performs a lot of actions within a script (for example, by running some loop) and NppExec's internal messages are enabled,
most of the time of the script execution is taken by not NppExec's ScriptEngine itself, but by CNppExecConsole::PrintMessage that callsCAnyRichEdit::AddStr that, in its turn, has the following implementation:

SendMessage(hRichEdit, EM_EXGETSEL, 0, (LPARAM) &cr);
SendMessage(hRichEdit, WM_SETREDRAW, FALSE, 0);
SendMessage(hRichEdit, EM_EXSETSEL, 0, (LPARAM) &CHARRANGE{-1, -1});
SendMessage(hRichEdit, EM_SETCHARFORMAT, (WPARAM) dwOptions, (LPARAM) &cf);
SendMessage(hRichEdit, EM_REPLACESEL, FALSE, (LPARAM) cszText);
SendMessage(hRichEdit, EM_EXSETSEL, 0, (LPARAM) &cr);
SendMessage(hRichEdit, EM_SCROLLCARET, 0, 0);
SendMessage(hRichEdit, WM_SETREDRAW, TRUE, 0);
if (InvalidateRect(hRichEdit, NULL, TRUE))
    UpdateWindow(hRichEdit);

And when I'm talking about most of the time of the script execution, here is what I mean:

  • a script with a silent loop of 10000 iterations takes around 1 second to complete when the internal messages are disabled.
  • the very same script takes around 2 minutes and 30 seconds when the internal messages are enabled (i.e. when each command prints something to NppExec's Console, thus calling CAnyRichEdit::AddStr).

This is the difference I'm talking about: 1 second vs. 2 minutes and 30 seconds.
Definitely, something needs to be improved here. But once I change something in the implementation of CAnyRichEdit::AddStr - for example, if I remove SendMessage(hRichEdit, WM_SETREDRAW, FALSE, 0), SendMessage(hRichEdit, WM_SETREDRAW, TRUE, 0), InvalidateRect(hRichEdit, NULL, TRUE) and UpdateWindow(hRichEdit) - it takes even more time than before (I got the results of 3 minutes and 30 seconds)!
If anyone has ideas/suggestions of how it can be improved, please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions