Avoid unnecessary allocations in NCursesUI::Window::draw calls

Pass an ArrayView<DisplayAtom> instead of a DisplayLine& so that
the newly common case of passing a single atom does not require
constructing a Vector.
This commit is contained in:
Maxime Coste 2019-09-17 19:06:49 +10:00
parent f855deaf8b
commit 2eccbbbe6f
3 changed files with 26 additions and 26 deletions

View File

@ -30,6 +30,9 @@ public:
DisplayAtom(String str, Face face) DisplayAtom(String str, Face face)
: face(face), m_type(Text), m_text(std::move(str)) {} : face(face), m_type(Text), m_text(std::move(str)) {}
explicit DisplayAtom(String str)
: DisplayAtom(std::move(str), Face{}) {}
StringView content() const; StringView content() const;
ColumnCount length() const; ColumnCount length() const;

View File

@ -66,10 +66,8 @@ void NCursesUI::Window::move_cursor(DisplayCoord coord)
wmove(win, (int)coord.line, (int)coord.column); wmove(win, (int)coord.line, (int)coord.column);
} }
void NCursesUI::Window::draw_line(Palette& palette, void NCursesUI::Window::draw(Palette& palette, ConstArrayView<DisplayAtom> atoms,
const DisplayLine& line, ColumnCount max_width, const Face& default_face)
ColumnCount width,
const Face& default_face)
{ {
auto add_str = [&](StringView str) { waddnstr(win, str.begin(), (int)str.length()); }; auto add_str = [&](StringView str) { waddnstr(win, str.begin(), (int)str.length()); };
@ -101,7 +99,7 @@ void NCursesUI::Window::draw_line(Palette& palette,
wbkgdset(win, COLOR_PAIR(palette.get_color_pair(default_face))); wbkgdset(win, COLOR_PAIR(palette.get_color_pair(default_face)));
ColumnCount column = 0; ColumnCount column = 0;
for (const DisplayAtom& atom : line) for (const DisplayAtom& atom : atoms)
{ {
set_face(atom.face); set_face(atom.face);
@ -109,7 +107,7 @@ void NCursesUI::Window::draw_line(Palette& palette,
if (content.empty()) if (content.empty())
continue; continue;
const auto remaining_columns = width - column; const auto remaining_columns = max_width - column;
if (content.back() == '\n' and if (content.back() == '\n' and
content.column_length() - 1 < remaining_columns) content.column_length() - 1 < remaining_columns)
{ {
@ -123,7 +121,7 @@ void NCursesUI::Window::draw_line(Palette& palette,
column += content.column_length(); column += content.column_length();
} }
} }
if (column < width) if (column < max_width)
wclrtoeol(win); wclrtoeol(win);
} }
@ -465,16 +463,15 @@ void NCursesUI::draw(const DisplayBuffer& display_buffer,
for (const DisplayLine& line : display_buffer.lines()) for (const DisplayLine& line : display_buffer.lines())
{ {
m_window.move_cursor(line_index); m_window.move_cursor(line_index);
m_window.draw_line(m_palette, line, dim.column, default_face); m_window.draw(m_palette, line.atoms(), dim.column, default_face);
++line_index; ++line_index;
} }
auto face = merge_faces(default_face, padding_face); auto face = merge_faces(default_face, padding_face);
DisplayLine line("~", {});
while (line_index < dim.line + line_offset) while (line_index < dim.line + line_offset)
{ {
m_window.move_cursor(line_index++); m_window.move_cursor(line_index++);
m_window.draw_line(m_palette, line, dim.column, face); m_window.draw(m_palette, DisplayAtom("~"), dim.column, face);
} }
m_dirty = true; m_dirty = true;
@ -487,7 +484,7 @@ void NCursesUI::draw_status(const DisplayLine& status_line,
const LineCount status_line_pos = m_status_on_top ? 0 : m_dimensions.line; const LineCount status_line_pos = m_status_on_top ? 0 : m_dimensions.line;
m_window.move_cursor(status_line_pos); m_window.move_cursor(status_line_pos);
m_window.draw_line(m_palette, status_line, m_dimensions.column, default_face); m_window.draw(m_palette, status_line.atoms(), m_dimensions.column, default_face);
const auto mode_len = mode_line.length(); const auto mode_len = mode_line.length();
m_status_len = status_line.length(); m_status_len = status_line.length();
@ -496,7 +493,7 @@ void NCursesUI::draw_status(const DisplayLine& status_line,
{ {
ColumnCount col = m_dimensions.column - mode_len; ColumnCount col = m_dimensions.column - mode_len;
m_window.move_cursor({status_line_pos, col}); m_window.move_cursor({status_line_pos, col});
m_window.draw_line(m_palette, mode_line, m_dimensions.column - col, default_face); m_window.draw(m_palette, mode_line.atoms(), m_dimensions.column - col, default_face);
} }
else if (remaining > 2) else if (remaining > 2)
{ {
@ -507,7 +504,7 @@ void NCursesUI::draw_status(const DisplayLine& status_line,
ColumnCount col = m_dimensions.column - remaining + 1; ColumnCount col = m_dimensions.column - remaining + 1;
m_window.move_cursor({status_line_pos, col}); m_window.move_cursor({status_line_pos, col});
m_window.draw_line(m_palette, trimmed_mode_line, m_dimensions.column - col, default_face); m_window.draw(m_palette, trimmed_mode_line.atoms(), m_dimensions.column - col, default_face);
} }
if (m_set_title) if (m_set_title)
@ -803,8 +800,8 @@ void NCursesUI::draw_menu()
ColumnCount pos = 0; ColumnCount pos = 0;
m_menu.move_cursor({0, 0}); m_menu.move_cursor({0, 0});
m_menu.draw_line(m_palette, DisplayLine(m_menu.first_item > 0 ? "< " : " ", {}), m_menu.draw(m_palette, DisplayAtom(m_menu.first_item > 0 ? "< " : " "),
m_menu.size.column, m_menu.bg); m_menu.size.column, m_menu.bg);
int i = m_menu.first_item; int i = m_menu.first_item;
for (; i < item_count and pos < win_width; ++i) for (; i < item_count and pos < win_width; ++i)
@ -812,14 +809,14 @@ void NCursesUI::draw_menu()
const DisplayLine& item = m_menu.items[i]; const DisplayLine& item = m_menu.items[i];
const ColumnCount item_width = item.length(); const ColumnCount item_width = item.length();
auto& face = i == m_menu.selected_item ? m_menu.fg : m_menu.bg; auto& face = i == m_menu.selected_item ? m_menu.fg : m_menu.bg;
m_menu.draw_line(m_palette, item, win_width - pos, face); m_menu.draw(m_palette, item.atoms(), win_width - pos, face);
m_menu.draw_line(m_palette, DisplayLine(item_width > win_width - pos ? "" : " ", {}), m_menu.draw(m_palette, DisplayAtom(item_width > win_width - pos ? "" : " "),
win_width - pos - item_width, m_menu.bg); win_width - pos - item_width, m_menu.bg);
pos += item_width + 1; pos += item_width + 1;
} }
auto end_str = String{' ', pos > win_width ? 0 : win_width - pos + 1} + (i == item_count ? " " : ">"); auto end_str = String{' ', pos > win_width ? 0 : win_width - pos + 1} + (i == item_count ? " " : ">");
m_menu.draw_line(m_palette, DisplayLine(end_str, {}), m_menu.size.column - pos, m_menu.bg); m_menu.draw(m_palette, DisplayAtom(end_str), m_menu.size.column - pos, m_menu.bg);
m_dirty = true; m_dirty = true;
return; return;
@ -850,14 +847,14 @@ void NCursesUI::draw_menu()
if (item_idx < item_count) if (item_idx < item_count)
{ {
const DisplayLine& item = m_menu.items[item_idx]; const DisplayLine& item = m_menu.items[item_idx];
m_menu.draw_line(m_palette, item, column_width, face); m_menu.draw(m_palette, item.atoms(), column_width, face);
column = item.length(); column = item.length();
} }
m_menu.draw_line(m_palette, DisplayLine(String{' ', column_width - column}, {}), column_width - column, face); m_menu.draw(m_palette, DisplayAtom(String{' ', column_width - column}), column_width - column, face);
} }
const bool is_mark = line >= mark_line and line < mark_line + mark_height; const bool is_mark = line >= mark_line and line < mark_line + mark_height;
m_menu.move_cursor({line, m_menu.size.column - 1}); m_menu.move_cursor({line, m_menu.size.column - 1});
m_menu.draw_line(m_palette, DisplayLine(is_mark ? "" : "", {}), 1, m_menu.bg); m_menu.draw(m_palette, DisplayAtom(is_mark ? "" : ""), 1, m_menu.bg);
} }
m_dirty = true; m_dirty = true;
} }
@ -1185,7 +1182,7 @@ void NCursesUI::info_show(StringView title, StringView content,
for (auto line = 0_line; line < info_box.size.line; ++line) for (auto line = 0_line; line < info_box.size.line; ++line)
{ {
m_info.move_cursor(line); m_info.move_cursor(line);
m_info.draw_line(m_palette, DisplayLine(info_box.contents[(int)line], {}), info_box.size.column, face); m_info.draw(m_palette, DisplayAtom(info_box.contents[(int)line]), info_box.size.column, face);
} }
m_dirty = true; m_dirty = true;
} }

View File

@ -15,6 +15,8 @@
namespace Kakoune namespace Kakoune
{ {
class DisplayAtom;
struct NCursesWin; struct NCursesWin;
class NCursesUI : public UserInterface, public Singleton<NCursesUI> class NCursesUI : public UserInterface, public Singleton<NCursesUI>
@ -101,10 +103,8 @@ private:
void refresh(bool force); void refresh(bool force);
void move_cursor(DisplayCoord coord); void move_cursor(DisplayCoord coord);
void mark_dirty(LineCount pos, LineCount count); void mark_dirty(LineCount pos, LineCount count);
void draw_line(Palette& palette, void draw(Palette& palette, ConstArrayView<DisplayAtom> atoms,
const DisplayLine& line, ColumnCount width, const Face& default_face);
ColumnCount width,
const Face& default_face);
explicit operator bool() const { return win; } explicit operator bool() const { return win; }