1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:37:43 +00:00

LibVT: Let Terminal keep history in a circular buffer

This makes Terminal::scroll_up() O(1) instead of O(n) in the
size of the history. (It's still O(n) in the size of visible
lines.)

Reduces time to run `disasm /bin/id` with the default terminal
window size from 530ms to 409ms (min-of-5) on my system.
This commit is contained in:
Nico Weber 2020-09-09 19:06:57 -04:00 committed by Andreas Kling
parent 90d9c83067
commit 61060c0da8
2 changed files with 16 additions and 4 deletions

View file

@ -52,6 +52,8 @@ void Terminal::clear()
void Terminal::clear_including_history() void Terminal::clear_including_history()
{ {
m_history.clear(); m_history.clear();
m_history_start = 0;
clear(); clear();
m_client.terminal_history_changed(); m_client.terminal_history_changed();
@ -738,9 +740,7 @@ void Terminal::scroll_up()
invalidate_cursor(); invalidate_cursor();
if (m_scroll_region_top == 0) { if (m_scroll_region_top == 0) {
auto line = move(m_lines.ptr_at(m_scroll_region_top)); auto line = move(m_lines.ptr_at(m_scroll_region_top));
m_history.append(move(line)); add_line_to_history(move(line));
while (m_history.size() > max_history_size())
m_history.take_first();
m_client.terminal_history_changed(); m_client.terminal_history_changed();
} }
m_lines.remove(m_scroll_region_top); m_lines.remove(m_scroll_region_top);

View file

@ -76,7 +76,7 @@ public:
Line& line(size_t index) Line& line(size_t index)
{ {
if (index < m_history.size()) if (index < m_history.size())
return m_history[index]; return m_history[(m_history_start + index) % m_history.size()];
return m_lines[index - m_history.size()]; return m_lines[index - m_history.size()];
} }
const Line& line(size_t index) const const Line& line(size_t index) const
@ -154,7 +154,19 @@ private:
TerminalClient& m_client; TerminalClient& m_client;
size_t m_history_start = 0;
NonnullOwnPtrVector<Line> m_history; NonnullOwnPtrVector<Line> m_history;
void add_line_to_history(NonnullOwnPtr<Line>&& line)
{
if (m_history.size() < max_history_size()) {
ASSERT(m_history_start == 0);
m_history.append(move(line));
return;
}
m_history.ptr_at(m_history_start) = move(line);
m_history_start = (m_history_start + 1) % m_history.size();
}
NonnullOwnPtrVector<Line> m_lines; NonnullOwnPtrVector<Line> m_lines;
size_t m_scroll_region_top { 0 }; size_t m_scroll_region_top { 0 };