summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2018-10-10 21:56:03 +1100
committerMaxime Coste <mawww@kakoune.org>2018-10-14 09:48:39 +1100
commit71e27c73af93c87bfd7f565350beeb5e35ae9be5 (patch)
tree28b2a6210f1a3ec8e0651f09fa07ec775905a353 /src
parent194a5db5d636a343fe4edb07a727cab93aa8315e (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/highlighters.cc28
1 files 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<Vector<Face>>();
- 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<BufferIterator>;
@@ -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<BufferCoord>(buffer_range.begin, display_range.begin.line - line_offset),
std::min<BufferCoord>(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)