From 97d645647668eb889a0e7b70ee210d97aaf9fff9 Mon Sep 17 00:00:00 2001 From: Chester Liu Date: Tue, 28 Apr 2020 23:41:44 +0800 Subject: [PATCH] Improve cluster construction performance (#5584) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary of the Pull Request A tiny performance fix in `renderer.cpp`. ## References ## PR Checklist * [ ] Closes #xxx * [X] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Requires documentation to be updated * [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx ## Detailed Description of the Pull Request / Additional comments The cluster construction code is intensively called during rendering. Even though a single `back()` is fast, but accumulated `back()`s still take a noticiable amount of CPU cycles. Before: ![perf1](https://user-images.githubusercontent.com/4710575/80323322-4342aa80-885d-11ea-92fb-06998dcef327.png) After: ![图片](https://user-images.githubusercontent.com/4710575/80323336-52c1f380-885d-11ea-8244-4d8d432f7c52.png) ## Validation Steps Performed Manually validated. --- src/renderer/base/renderer.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/renderer/base/renderer.cpp b/src/renderer/base/renderer.cpp index b974385020c..5a817b28bf1 100644 --- a/src/renderer/base/renderer.cpp +++ b/src/renderer/base/renderer.cpp @@ -730,6 +730,8 @@ void Renderer::_PaintBufferOutputHelper(_In_ IRenderEngine* const pEngine, } // Walk through the text data and turn it into rendering clusters. + // Keep the columnCount as we go to improve performance over digging it out of the vector at the end. + size_t columnCount = 0; // If we're on the first cluster to be added and it's marked as "trailing" // (a.k.a. the right half of a two column character), then we need some special handling. @@ -743,7 +745,8 @@ void Renderer::_PaintBufferOutputHelper(_In_ IRenderEngine* const pEngine, // And tell the next function to trim off the left half of it. trimLeft = true; // And add one to the number of columns we expect it to take as we insert it. - clusters.emplace_back(it->Chars(), it->Columns() + 1); + columnCount = it->Columns() + 1; + clusters.emplace_back(it->Chars(), columnCount); } else { @@ -755,11 +758,11 @@ void Renderer::_PaintBufferOutputHelper(_In_ IRenderEngine* const pEngine, // Otherwise if it's not a special case, just insert it as is. else { - clusters.emplace_back(it->Chars(), it->Columns()); + columnCount = it->Columns(); + clusters.emplace_back(it->Chars(), columnCount); } // Advance the cluster and column counts. - const auto columnCount = clusters.back().GetColumns(); it += columnCount > 0 ? columnCount : 1; // prevent infinite loop for no visible columns cols += columnCount;