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.
This commit is contained in:
Maxime Coste 2019-08-31 13:14:46 +10:00
parent 95a8d21d72
commit b24be99fa3
2 changed files with 11 additions and 9 deletions

View File

@ -78,9 +78,9 @@ static ConstArrayView<Codepoint> get_extra_word_chars(const Buffer& buffer)
return buffer.options()["extra_word_chars"].get<Vector<Codepoint, MemoryDomain::Options>>(); return buffer.options()["extra_word_chars"].get<Vector<Codepoint, MemoryDomain::Options>>();
} }
void WordDB::add_words(StringView line) void WordDB::add_words(StringView line, ConstArrayView<Codepoint> 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); auto it = m_words.find(w);
if (it != m_words.end()) 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<Codepoint> 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); auto it = m_words.find(w);
kak_assert(it != m_words.end() and it->value.refcount > 0); kak_assert(it != m_words.end() and it->value.refcount > 0);
@ -138,10 +138,11 @@ void WordDB::rebuild_db()
m_words.clear(); m_words.clear();
m_lines.clear(); m_lines.clear();
m_lines.reserve((int)buffer.line_count()); 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) for (auto line = 0_line, end = buffer.line_count(); line < end; ++line)
{ {
m_lines.push_back(buffer.line_storage(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(); m_timestamp = buffer.timestamp();
} }
@ -159,6 +160,7 @@ void WordDB::update_db()
Lines new_lines; Lines new_lines;
new_lines.reserve((int)buffer.line_count()); new_lines.reserve((int)buffer.line_count());
auto extra_word_chars = get_extra_word_chars(buffer);
auto old_line = 0_line; auto old_line = 0_line;
for (auto& modif : modifs) for (auto& modif : modifs)
{ {
@ -173,13 +175,13 @@ void WordDB::update_db()
while (old_line < modif.old_line + modif.num_removed) while (old_line < modif.old_line + modif.num_removed)
{ {
kak_assert(old_line < m_lines.size()); 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) for (auto l = 0_line; l < modif.num_added; ++l)
{ {
new_lines.push_back(buffer.line_storage(modif.new_line + 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()) while (old_line != (int)m_lines.size())

View File

@ -28,8 +28,8 @@ public:
int get_word_occurences(StringView word) const; int get_word_occurences(StringView word) const;
private: private:
void update_db(); void update_db();
void add_words(StringView line); void add_words(StringView line, ConstArrayView<Codepoint> extra_word_chars);
void remove_words(StringView line); void remove_words(StringView line, ConstArrayView<Codepoint> extra_word_chars);
void rebuild_db(); void rebuild_db();