Repository navigation
perf: update the cursor mark in place and start the runtime after the first screen - #133
Merged
Merged
Conversation
The cursor provider sets its window mark on each cursor move. Each new
window revision made the renderer copy and sort the marks of all
providers again, rebuild the full mark layer, and compose every row.
With 1,000 search matches and 500 diagnostics, this work was most of the
cost of a scroll.
- The renderer keeps the sorted marks of each provider, keyed by the
mark arrays of the store. A change rebuilds only the marks of the
providers that changed.
- When those providers keep their mark count, the renderer writes the
new marks into the previous table and lists the changed indexes. The
line layer then computes the rows of those marks only, and rebuilds
only their old and new rows. The compose reuses the other rows.
- The incremental row rebuild now also works with compact search
matches, while the matches and the line count stay the same.
In a TUI benchmark (default config, 60 keys per second), the main-thread
cost of each scroll event, above a run without the plugin, went from
0.69 ms to 0.24 ms on a 10,000-line file, and from 1.89 ms to about 0 ms
on a 100,000-line file. With `providers = { cursor = false }`, the cost
is 0.19 ms and about 0 ms. The new benchmark cases
`render/line/cursor_mark` and `render/line/cursor_mark_search` go from
about 1.2 ms to 40 us.
`tests/test_multiwindow.lua` now registers its provider. Without the
registration, the scrollbar did not draw the marks of that provider, and
the test passed only because each window revision rebuilt the layer.
A `setup()` call in `init.lua` delayed the first screen by about 15 ms.
Most of the setup time went to the highlight groups (192 `nvim_set_hl`
calls), the scheduler, and the providers.
During start-up (before `VimEnter`), `setup()` now validates the options
and creates the commands at once. It starts the rest in a `vim.schedule`
callback: the highlight groups, the floats, the providers, and the
minimap.
- `show()`, `hide()`, `toggle()`, `refresh()`, `jump()`, and `glance()`,
and the four functions of `require("scrollbar.minimap")`, run a
pending start first. Thus a `hide()` in `init.lua` still keeps the
scrollbar hidden.
- A later `setup()` call replaces a pending start.
- After start-up, `setup()` starts everything at once, as before. This
includes a plugin manager that loads the plugin on an event or a key.
In a TUI in tmux (`--startuptime`, 10,000-line file, median of 20
starts), the first screen came after 24.6 ms instead of 29.6 ms. Without
the plugin, it comes after 14.2 ms. Most of the rest is
`require("scrollbar")`.
`:hi ScrollbarThumb` now shows no group directly after a `setup()` call
in `init.lua`. The new Start-up section of `docs/configuration.md`
describes the behavior.
The incremental row rebuild created a closure, `group_of`, for each rebuilt row. Each closure allocated a function and its upvalues, and LuaJIT does not compile the creation of a closure, so the rebuild ran in the interpreter. The Benchmarks job flagged `render/line/insert` at +25% (42.3 to 52.9 us) and 21.6 to 22.6 KB for each render. The helper is now a file-level function with explicit arguments. Over 2,000 insert renders, the time and the allocation equal main (74.5 us and 22.8 KB for each render).
`require("scrollbar")` loaded 37 modules (about 9.3 ms), although a
`setup()` call during start-up uses only the configuration modules
before the first screen. The renderer, the scheduler, the mouse module,
the providers, and their dependencies loaded with it.
`init.lua` now loads `mouse`, `providers`, `renderer`, `scheduler`, and
`text_tick` in the runtime start, or in the first call of `show()`,
`hide()`, `toggle()`, or `refresh()`. `require("scrollbar")` now loads
18 modules (about 3.8 ms). The configuration modules still load at once,
because `setup()` validates the options before it returns.
In a TUI in tmux (`--startuptime`, 10,000-line file, median of 20
starts), the first screen comes after 19.4 ms instead of 24.6 ms. Main
takes 28.5 ms, and Neovim without the plugin takes 13.8 ms.
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.
Fixes the two hot spots that a benchmark against satellite.nvim and nvim-scrollview found: each cursor move rebuilt and sorted the marks of all providers, and a
setup()call ininit.luadelayed the first screen by about 15 ms. Each change is one commit. A third commit fixes a slowerrender/line/insertthat the first commit caused. A fourth commit loads the runtime modules in the deferred start.Changes
Cursor moves (
perf(renderer))The cursor provider sets its window mark on each cursor move. Before this change, each new window revision made the renderer copy and sort the marks of all providers, rebuild the full mark layer, and compose every row.
perf(layout)): the first commit put a helper closure in the row rebuild. Each rebuilt row allocated it, and LuaJIT does not compile the creation of a closure. The Benchmarks job flaggedrender/line/insertat +25% (42.3 to 52.9 µs, 21.6 to 22.6 KB). The helper is now a file-level function. Over 2,000 insert renders, the time and the allocation equal main.Start-up (
perf(setup))VimEnter),setup()validates the options and creates the commands at once. It starts the rest in avim.schedulecallback: the highlight groups, the floats, the providers, and the minimap.show(),hide(),toggle(),refresh(),jump(), andglance(), and the four functions ofrequire("scrollbar.minimap"), run a pending start first. A latersetup()call replaces a pending start.setup()starts everything at once, as before. This includes a plugin manager that loads the plugin on an event or a key.perf(setup), fourth commit):require("scrollbar")loaded 37 modules (about 9.3 ms). Nowinit.lualoadsmouse,providers,renderer,scheduler, andtext_tickin the runtime start, or in the first call ofshow(),hide(),toggle(), orrefresh().require("scrollbar")now loads 18 modules (about 3.8 ms). The configuration modules still load at once, becausesetup()validates the options before it returns.Behavior change:
:hi ScrollbarThumbshows no group directly after asetup()call ininit.lua, and the bar comes one event loop iteration after the first screen. The new Start-up section describes this.Results
TUI benchmark in a private tmux server: 200x50, keys sent at 60 Hz, default config, 1,000 search matches and 500 diagnostics on the 10,000-line file. Main-thread CPU for each scroll event, above a run without the plugin, median of 3 runs:
providers = { cursor = false }An earlier run of the renderer commit gave 0.24 ms and about 0 ms. The runs vary by about 0.05 ms.
Time to the first screen (
--startuptimein the same TUI, 10,000-line file, median of 20 starts): 13.8 ms without the plugin, 28.5 ms with main, and 19.4 ms with this PR. The deferred start alone gave 24.6 ms, and the lazy modules take off 5.2 ms more. The plugin now adds 5.5 ms before the first screen, instead of 14.7 ms.In
make bench, the new caserender/line/cursor_mark(2,000 marks, one cursor mark that moves) goes from 1.19 ms to 37 µs and from 455 KB to 17.5 KB for each render.render/line/cursor_mark_searchadds 1,000 compact search matches and goes from 1.23 ms to 40 µs.Not in this PR
require("scrollbar"), andsetup()needs them to validate the options. They also loadhighlights,overlap, andnavigate.framefor that validation.Review Notes
{ from, indexes }for an in-place change. The line layer applies a patch only when its cache is atpatch.from, and else rebuilds. A cache hit also returns the patch, so a render that stops before the layer does not lose it.normalized_mark_rowsalways reports a patched index, also when its row stays the same, because a new line or type changes the content of the row.compact_search, so a search change still rebuilds.tests/test_multiwindow.lua: the test now registers its provider. Without the registration, the scrollbar did not consume the marks of that provider, and the test passed only because each window revision rebuilt the layer.vim_did_enter:nvim -lruns withvim_did_enter = 1, so the benchmarks and the test children keep the synchronous path. A-ccommand runs beforeVimEnter, so the new start-up tests start a child with-c.scrollbar.shared.deferred_start: this module keeps the pending start out of the public table ofrequire("scrollbar"), which a test checks. The minimap module also uses it, without a circular require.Tests
make cipasses: format, lint, typecheck, and the full suite of 1,212 cases on Neovim 0.12.6. The 1,212 cases also pass on Neovim 0.11.0.setup()during start-up,hide()during start-up, andsetup()after start-up. A change that makessetup()always synchronous fails the first test. A change that removes the pending start fromhide()fails the second. The first test also checks that no runtime module is loaded before the runtime start, and that all of them are loaded after it. It fails wheninit.lualoads them at once.make bench-compare REF=mainwith 3 runs, after the layout fix:render/line/insertis at +9% with equal allocation (21.6 to 21.7 KB). The run flaggedproviders/refresh/marks_2000_noopat +29%. This PR does not change that path (nvim -lkeepssetup()synchronous), and three isolated runs gave 99–108 µs on main and 102–106 µs on the branch. Earlier runs also flaggedminimap/squash/fixed_10konce at +52%, which isolated runs showed as noise (about 790 µs on both sides).rumdlreports no new warnings in the changed docs.