Highlighter engine returns cached spans by reference; drop unneeded mutexes

HighlighterEngine::GetLine copied the span vector on every call (twice
on a miss) and did a dynamic_cast per call; it now returns the cached
entry (valid until the next call) and dispatches on Stateful().

PieceTable and HighlighterEngine took a mutex in their read paths while
their writes took none, and PieceTable.h claimed concurrent reads were
safe; they are not (reads update caches). Every Buffer is used from the
main thread only (the swap writer thread receives copies; the regex
large-stack thread runs while its caller waits), so the mutexes are
removed and the headers say so. ASan and the threaded tests under TSan
are clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
Claude
2026-09-24 12:19:59 -07:00
committed by Kyle Isom
parent 7039c14507
commit d490245f30
8 changed files with 43 additions and 73 deletions
+6 -9
View File
@@ -1,11 +1,11 @@
// HighlighterEngine.h - caching layer for per-line highlights
// HighlighterEngine.h - caching layer for per-line highlights (main thread
// only, like the Buffer it reads)
#pragma once
#include <cstdint>
#include <map>
#include <memory>
#include <vector>
#include <mutex>
#include "../Highlight.h"
#include "LanguageHighlighter.h"
@@ -21,10 +21,10 @@ public:
void SetHighlighter(std::unique_ptr<LanguageHighlighter> hl);
// Retrieve highlights for a given line and buffer version.
// Returns a copy to avoid lifetime issues across threads/renderers.
// If cache is stale, recompute using the current highlighter.
LineHighlight GetLine(const Buffer &buf, int row, std::uint64_t buf_version) const;
// Highlights for a line at a buffer version, computed if not cached.
// The reference is valid until the next call on this engine (a later
// lookup may evict the entry); copy it to keep it longer.
const LineHighlight &GetLine(const Buffer &buf, int row, std::uint64_t buf_version) const;
// Invalidate cached lines from row (inclusive)
void InvalidateFrom(int row);
@@ -74,8 +74,5 @@ private:
static constexpr std::size_t kCacheMax = 16384;
void trim_cache_locked(int row) const;
// Guards the caches above.
mutable std::mutex mtx_;
};
} // namespace kte