perf(highlighter): Cache decoration-provider hot paths#1148
Open
seflue wants to merge 2 commits into
Open
Conversation
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. |
921b11d to
48f02ee
Compare
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.
48f02ee to
a2978c8
Compare
Contributor
Author
|
I removed the commit regarding the stars from my PR and force-pushed. You said, you have some simplifications in mind. Is it regarding the window map, holding a cache for each window? I was a bit undecided ... it's covering an edge case, when the user has the buffer open in two windows in parallel. You could argue, that this is in indeed a bit "edgy". On the other hand - especially when working with large files it is not completely uncommon to have the same buffer at different positions open in parallel. |
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.