From b24be99fa3e684201faed534c5988940d561df35 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 31 Aug 2019 13:14:46 +1000 Subject: [PATCH] Limit the amounts of calls to get_extra_word_chars This is a relatively expensive function as it needs to resolve an option, which uses dynamic_cast. --- src/word_db.cc | 16 +++++++++------- src/word_db.hh | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/word_db.cc b/src/word_db.cc index 9a7680a5..17db080b 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -78,9 +78,9 @@ static ConstArrayView get_extra_word_chars(const Buffer& buffer) return buffer.options()["extra_word_chars"].get>(); } -void WordDB::add_words(StringView line) +void WordDB::add_words(StringView line, ConstArrayView extra_word_chars) { - for (auto&& w : WordSplitter{line, get_extra_word_chars(*m_buffer)}) + for (auto&& w : WordSplitter{line, extra_word_chars}) { auto it = m_words.find(w); if (it != m_words.end()) @@ -94,9 +94,9 @@ void WordDB::add_words(StringView line) } } -void WordDB::remove_words(StringView line) +void WordDB::remove_words(StringView line, ConstArrayView extra_word_chars) { - for (auto&& w : WordSplitter{line, get_extra_word_chars(*m_buffer)}) + for (auto&& w : WordSplitter{line, extra_word_chars}) { auto it = m_words.find(w); kak_assert(it != m_words.end() and it->value.refcount > 0); @@ -138,10 +138,11 @@ void WordDB::rebuild_db() m_words.clear(); m_lines.clear(); m_lines.reserve((int)buffer.line_count()); + auto extra_word_chars = get_extra_word_chars(buffer); for (auto line = 0_line, end = buffer.line_count(); line < end; ++line) { m_lines.push_back(buffer.line_storage(line)); - add_words(m_lines.back()->strview()); + add_words(m_lines.back()->strview(), extra_word_chars); } m_timestamp = buffer.timestamp(); } @@ -159,6 +160,7 @@ void WordDB::update_db() Lines new_lines; new_lines.reserve((int)buffer.line_count()); + auto extra_word_chars = get_extra_word_chars(buffer); auto old_line = 0_line; for (auto& modif : modifs) { @@ -173,13 +175,13 @@ void WordDB::update_db() while (old_line < modif.old_line + modif.num_removed) { kak_assert(old_line < m_lines.size()); - remove_words(m_lines[(int)old_line++]->strview()); + remove_words(m_lines[(int)old_line++]->strview(), extra_word_chars); } for (auto l = 0_line; l < modif.num_added; ++l) { new_lines.push_back(buffer.line_storage(modif.new_line + l)); - add_words(new_lines.back()->strview()); + add_words(new_lines.back()->strview(), extra_word_chars); } } while (old_line != (int)m_lines.size()) diff --git a/src/word_db.hh b/src/word_db.hh index aef047d0..dab65d4c 100644 --- a/src/word_db.hh +++ b/src/word_db.hh @@ -28,8 +28,8 @@ public: int get_word_occurences(StringView word) const; private: void update_db(); - void add_words(StringView line); - void remove_words(StringView line); + void add_words(StringView line, ConstArrayView extra_word_chars); + void remove_words(StringView line, ConstArrayView extra_word_chars); void rebuild_db();