From 71e27c73af93c87bfd7f565350beeb5e35ae9be5 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 10 Oct 2018 21:56:03 +1100 Subject: [PATCH] Cleanup RegexHighlighter code and drop cache when it becomes too big The RegexHighlighter range cache can get pretty big in nested regions use cases, and maintaining it can become pretty costly, so if it hits a certain size, just drop it. Should improve performances in #2454 --- src/highlighters.cc | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index 7fa21abd..8ae36b79 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -280,7 +280,7 @@ public: return spec.second.empty() ? Face{} : faces[spec.second]; }) | gather>(); - auto& matches = get_matches(context.context.buffer(), display_buffer.range(), range); + const auto& matches = get_matches(context.context.buffer(), display_buffer.range(), range); kak_assert(matches.size() % m_faces.size() == 0); for (size_t m = 0; m < matches.size(); ++m) { @@ -352,8 +352,7 @@ private: m_faces.emplace(m_faces.begin(), 0, String{}); } - void add_matches(const Buffer& buffer, MatchList& matches, - BufferRange range) + void add_matches(const Buffer& buffer, MatchList& matches, BufferRange range) { kak_assert(matches.size() % m_faces.size() == 0); using RegexIt = RegexIterator; @@ -375,26 +374,27 @@ private: } } - MatchList& get_matches(const Buffer& buffer, BufferRange display_range, - BufferRange buffer_range) + const MatchList& get_matches(const Buffer& buffer, BufferRange display_range, BufferRange buffer_range) { Cache& cache = m_cache.get(buffer); auto& matches = cache.m_matches; if (cache.m_regex_version != m_regex_version or - cache.m_timestamp != buffer.timestamp()) + cache.m_timestamp != buffer.timestamp() or + matches.size() > 1000) { matches.clear(); cache.m_timestamp = buffer.timestamp(); cache.m_regex_version = m_regex_version; } + const LineCount line_offset = 3; BufferRange range{std::max(buffer_range.begin, display_range.begin.line - line_offset), std::min(buffer_range.end, display_range.end.line + line_offset)}; - auto it = std::upper_bound(matches.begin(), matches.end(), range, - [](const BufferRange& lhs, const Cache::RangeAndMatches& rhs) - { return lhs.begin < rhs.range.end; }); + auto it = std::upper_bound(matches.begin(), matches.end(), range.begin, + [](const BufferCoord& lhs, const Cache::RangeAndMatches& rhs) + { return lhs < rhs.range.end; }); if (it == matches.end() or it->range.begin > range.end) { @@ -423,14 +423,12 @@ private: // add regex matches from new begin to old first match end if (range.begin < old_range.begin) { - old_range.begin = range.begin; - MatchList new_matches; - add_matches(buffer, new_matches, {range.begin, first_end}); matches.erase(matches.begin(), matches.begin() + m_faces.size()); + size_t pivot = matches.size(); + old_range.begin = range.begin; + add_matches(buffer, matches, {range.begin, first_end}); - std::copy(std::make_move_iterator(new_matches.begin()), - std::make_move_iterator(new_matches.end()), - std::inserter(matches, matches.begin())); + std::rotate(matches.begin(), matches.begin() + pivot, matches.end()); } // add regex matches from old last match begin to new end if (old_range.end < range.end)