From 69b0c9e53e523ac43a303964fc9c5bc0da7d5d61 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Fri, 13 Aug 2021 00:11:52 +0200 Subject: [PATCH] Optimize performance. (#189) Screen::ApplyShader accounted for 60% of the computation. This patch optimize it. Performance on a 80x80 frame improved from 1400 draw/s to 7000 draw/s. --- src/ftxui/screen/screen.cpp | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/ftxui/screen/screen.cpp b/src/ftxui/screen/screen.cpp index db682ba..6da31a1 100644 --- a/src/ftxui/screen/screen.cpp +++ b/src/ftxui/screen/screen.cpp @@ -227,21 +227,28 @@ void Screen::ApplyShader() { // Merge box characters togethers. for (int y = 1; y < dimy_; ++y) { for (int x = 1; x < dimx_; ++x) { - std::string& left = at(x - 1, y); - std::string& top = at(x, y - 1); - std::string& cur = at(x, y); + // Box drawing character uses exactly 3 byte. + std::string& cur = pixels_[y][x].character; + if (cur.size() != 3u) + continue; - // Left vs current - if (cur == "│" && left == "─") cur = "┤"; - if (cur == "─" && left == "│") left = "├"; - if (cur == "├" && left == "─") cur = "┼"; - if (cur == "─" && left == "┤") left = "┼"; + // Left vs current. + std::string& left = pixels_[y][x-1].character; + if (left.size() == 3u) { + if (cur == "│" && left == "─") cur = "┤"; + if (cur == "├" && left == "─") cur = "┼"; + if (cur == "─" && left == "│") left = "├"; + if (cur == "─" && left == "┤") left = "┼"; + } - // Top vs current - if (cur == "─" && top == "│") cur = "┴"; - if (cur == "│" && top == "─") top = "┬"; - if (cur == "┬" && top == "│") cur = "┼"; - if (cur == "│" && top == "┴") top = "┼"; + // Top vs current. + std::string& top = pixels_[y-1][x].character; + if (top.size() == 3u) { + if (cur == "─" && top == "│") cur = "┴"; + if (cur == "┬" && top == "│") cur = "┼"; + if (cur == "│" && top == "─") top = "┬"; + if (cur == "│" && top == "┴") top = "┼"; + } } } }