perf(highlighter): Cache decoration-provider hot paths#1148
Open
seflue wants to merge 3 commits into
Open
Conversation
In large folded org-files, foldtext.on_line is a hot path: two vim.fn calls (foldclosed, col) run per visible line per redraw, even on cache hits. Move them into on_win, building a per-window map of fold boundaries and line lengths for the visible range. on_line becomes an O(1) lookup. nvim_buf_get_lines replaces per-line col() queries; foldclosedend lets the scan skip over folded contents.
Replace the changedtick check that dropped the entire foldtext cache on every keystroke with an nvim_buf_attach listener: on_bytes drops cache entries inside the edited range and shifts later entries by the row delta, on_reload clears the cache for full reloads. Unedited closed folds keep their cached hl_group and skip the treesitter lookup on subsequent redraws.
Member
|
I just pushed a change to master to hide leading stars via tree-sitter queries (5ea6251), so we can rebase and simplify this a bit more. |
The on_line hook ran a tree query per visible line per redraw, making leading-star highlighting a hot path while typing. Cache end_col per buffer line. Shift/drop entries on buffer edits via nvim_buf_attach (on_bytes/on_reload), and rebuild the changed ranges from on_changedtree once the parser has produced a new tree. on_line is now a plain table lookup. Mirrors the cache pattern already used by foldtext.
921b11d to
48f02ee
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Typing in folded org-files is slow because
nvim_set_decoration_provider'son_linedoes per-line work on every redraw.Two commits add caching to foldtext: move
vim.fn.foldclosedandvim.fn.colfromon_linetoon_win, and replace thechangedtickwholesale-drop with annvim_buf_attachlistener that keeps the cache consistent under edits.A third commit applies the same pattern to stars. Each buffer keeps a per-line cache of the
(stars)node's end_col;on_bytesshifts and drops entries to stay consistent with edits, andon_changedtreere-queries the changed ranges after a reparse. Edits inside a headline that don't touch the leading stars keep their cache entry, since the stars themselves don't change.I used the snacks profiler to identify the hot methods; it's not a controlled benchmark, but
foldtext.on_lineandstars.on_lineno longer show up near the top after these changes.