Skip to content

perf: update the cursor mark in place and start the runtime after the first screen - #133

Merged
petertriho merged 4 commits into
mainfrom
perf/flattened-marks-cache
Nov 21, 2026
Merged

petertriho merged 4 commits into
mainfrom
perf/flattened-marks-cache

Conversation

@petertriho

@petertriho petertriho commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

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 in init.lua delayed the first screen by about 15 ms. Each change is one commit. A third commit fixes a slower render/line/insert that 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.

  • Sorted marks for each provider: the renderer keys the sorted marks of each provider on the mark arrays of the store. A change rebuilds only the marks of the providers that changed.
  • In-place patch: when those providers keep their mark count, the renderer writes the new marks into the previous flattened table and lists the changed indexes. The line layer computes the rows of those marks only, and rebuilds only their old and new rows. The compose reuses the other rows, because the marks table keeps its identity.
  • Compact search: the incremental row rebuild now also works with the compact matches of the built-in search, while the matches and the line count stay the same. The full build records the entries of each row, and a row rebuild merges them.
  • No closure in the row rebuild (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 flagged render/line/insert at +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))

  • During start-up (before VimEnter), setup() 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. 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.
  • Lazy modules (perf(setup), fourth commit): require("scrollbar") loaded 37 modules (about 9.3 ms). Now init.lua 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.

Behavior change: :hi ScrollbarThumb shows no group directly after a setup() call in init.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:

File main this PR main with providers = { cursor = false }
10,000 lines 0.69 ms 0.28 ms 0.19 ms
100,000 lines 1.89 ms 0.03 ms about 0 ms

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 (--startuptime in 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 case render/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_search adds 1,000 compact search matches and goes from 1.23 ms to 40 µs.

Not in this PR

  • Lazy loading of the configuration modules. They take most of the remaining 3.8 ms of require("scrollbar"), and setup() needs them to validate the options. They also load highlights, overlap, and navigate.frame for that validation.
  • The CPU of the search worker while typing: 3.15 ms for each key, in a separate process.
  • Fewer highlight groups.

Review Notes

  • Generation and patch: the flattened cache has a generation that changes with each content change, and a patch { from, indexes } for an in-place change. The line layer applies a patch only when its cache is at patch.from, and else rebuilds. A cache hit also returns the patch, so a render that stops before the layer does not lose it.
  • Same-row changes: normalized_mark_rows always reports a patched index, also when its row stays the same, because a new line or type changes the content of the row.
  • Identity reuse: when no consumed mark changed, for example when only a minimap-only provider changed, the marks table keeps its identity and its generation. The line layer then also compares 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 -l runs with vim_did_enter = 1, so the benchmarks and the test children keep the synchronous path. A -c command runs before VimEnter, 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 of require("scrollbar"), which a test checks. The minimap module also uses it, without a circular require.

Tests

  • make ci passes: 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.
  • New renderer tests: a window mark move keeps the flattened marks of the other providers. In 300 random steps (mark moves, mark changes, count changes, compact search changes, and line inserts and deletes), the incremental render equals a full render in a second window. A change that skips the compact entries of a row fails the random test. A change that drops same-row changes fails it and two existing tests.
  • New core tests: setup() during start-up, hide() during start-up, and setup() after start-up. A change that makes setup() always synchronous fails the first test. A change that removes the pending start from hide() 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 when init.lua loads them at once.
  • make bench-compare REF=main with 3 runs, after the layout fix: render/line/insert is at +9% with equal allocation (21.6 to 21.7 KB). The run flagged providers/refresh/marks_2000_noop at +29%. This PR does not change that path (nvim -l keeps setup() synchronous), and three isolated runs gave 99–108 µs on main and 102–106 µs on the branch. Earlier runs also flagged minimap/squash/fixed_10k once at +52%, which isolated runs showed as noise (about 790 µs on both sides).
  • rumdl reports no new warnings in the changed docs.

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.
@petertriho
petertriho merged commit 376e2fa into main Nov 21, 2026
6 checks passed
@petertriho
petertriho deleted the perf/flattened-marks-cache branch November 21, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant