From 3efc406d571d651ed44b751eb798cc79a990c614 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 23 Aug 2017 08:42:00 +0700 Subject: [PATCH] Optimize DisplayBuffer::optimize() Previous implementation was erasing in the middle of the atoms vector each time two atoms were merged, leading to a move of all following atoms. --- src/display_buffer.cc | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index d9771327..3694839a 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -136,37 +136,31 @@ void DisplayLine::optimize() return; auto atom_it = m_atoms.begin(); - auto next_atom_it = atom_it + 1; - while (next_atom_it != m_atoms.end()) + for (auto next_it = atom_it + 1; next_it != m_atoms.end(); ++next_it) { auto& atom = *atom_it; - auto& next_atom = *next_atom_it; - bool merged = false; + auto& next = *next_it; - if (atom.face == next_atom.face and - atom.type() == next_atom.type()) + const auto type = atom.type(); + if (type == next.type() and atom.face == next.face) { - auto type = atom.type(); - if ((type == DisplayAtom::Range or - type == DisplayAtom::ReplacedRange) and - next_atom.begin() == atom.end()) - { - atom.m_range.end = next_atom.end(); - if (type == DisplayAtom::ReplacedRange) - atom.m_text += next_atom.m_text; - merged = true; - } if (type == DisplayAtom::Text) + atom.m_text += next.m_text; + else if ((type == DisplayAtom::Range or + type == DisplayAtom::ReplacedRange) and + next.begin() == atom.end()) { - atom.m_text += next_atom.m_text; - merged = true; + atom.m_range.end = next.end(); + if (type == DisplayAtom::ReplacedRange) + atom.m_text += next.m_text; } + else + *++atom_it = std::move(*next_it); } - if (merged) - next_atom_it = m_atoms.erase(next_atom_it); else - atom_it = next_atom_it++; + *++atom_it = std::move(*next_it); } + m_atoms.erase(atom_it+1, m_atoms.end()); } ColumnCount DisplayLine::length() const