From 4fc283f8b68a27cde934fa02a863f877e702ae1d Mon Sep 17 00:00:00 2001 From: Chester Liu Date: Tue, 29 Jun 2021 05:08:22 +0800 Subject: [PATCH] Throttle cursor redrawing in outputStream.cpp (#10394) Try to throttle the cursor redrawing in the conhost world. The motivation of this is the high CPU usage of `TriggerRedrawCursor` (#10393). This can be seen as the conhost version of #2960. This saves 5%~8% of the CPU time. Supports #10462. --- src/host/outputStream.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/host/outputStream.cpp b/src/host/outputStream.cpp index 3aa7fd5085b..cf6cdcca424 100644 --- a/src/host/outputStream.cpp +++ b/src/host/outputStream.cpp @@ -76,8 +76,15 @@ void WriteBuffer::_DefaultStringCase(const std::wstring_view string) { size_t dwNumBytes = string.size() * sizeof(wchar_t); - _io.GetActiveOutputBuffer().GetTextBuffer().GetCursor().SetIsOn(true); - + Cursor& cursor = _io.GetActiveOutputBuffer().GetTextBuffer().GetCursor(); + if (!cursor.IsOn()) + { + cursor.SetIsOn(true); + } + + // Defer the cursor drawing while we are iterating the string, for a better performance. + // We can not waste time displaying a cursor event when we know more text is coming right behind it. + cursor.StartDeferDrawing(); _ntstatus = WriteCharsLegacy(_io.GetActiveOutputBuffer(), string.data(), string.data(), @@ -87,6 +94,7 @@ void WriteBuffer::_DefaultStringCase(const std::wstring_view string) _io.GetActiveOutputBuffer().GetTextBuffer().GetCursor().GetPosition().X, WC_LIMIT_BACKSPACE | WC_DELAY_EOL_WRAP, nullptr); + cursor.EndDeferDrawing(); } ConhostInternalGetSet::ConhostInternalGetSet(_In_ IIoProvider& io) :