From f3201cb9569161151328fc54632c0861681bf5dd Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 15 Dec 2022 17:09:37 +1100 Subject: [PATCH] Speed up expand_unprintable by avoiding BufferIterator Using BufferIterator adds overhead, but we know that DisplayAtoms cannot span multiple buffer lines and hence we can directly iterate using char pointers. --- src/highlighters.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index fcd7650b..6d8eac7e 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1351,22 +1351,24 @@ void expand_unprintable(HighlightContext context, DisplayBuffer& display_buffer, if (atom_it->type() != DisplayAtom::Range) continue; - for (auto it = get_iterator(buffer, atom_it->begin()), - end = get_iterator(buffer, atom_it->end()); it < end;) + auto begin = atom_it->begin(); + auto line_data = buffer[begin.line].data(); + for (auto it = line_data + begin.column, end = line_data + atom_it->end().column; it < end;) { - auto coord = it.coord(); - Codepoint cp = utf8::read_codepoint(it, end); + auto next = it; + Codepoint cp = utf8::read_codepoint(next, end); if (cp != '\n' and (cp < ' ' or cp > '~') and not iswprint((wchar_t)cp)) { - if (coord != atom_it->begin()) - atom_it = ++line.split(atom_it, coord); - if (it.coord() < atom_it->end()) - atom_it = line.split(atom_it, it.coord()); + if (ByteCount pos(it - line_data); pos != begin.column) + atom_it = ++line.split(atom_it, {begin.line, pos}); + if (ByteCount pos(next - line_data); pos < atom_it->end().column) + atom_it = line.split(atom_it, {begin.line, pos}); atom_it->replace("�"); atom_it->face = error; break; } + it = next; } } }