Skip to content

perf(cmdline): reduce spurious cursor and char events #1953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

soifou
Copy link
Collaborator

@soifou soifou commented Jun 27, 2025

See discussion in #1952 (comment)

For Neovim 0.11, there’s no real change to the core logic, I added throttling for rapid cursor movements. Considering as "rapid", a cursor move below 60ms from the last "move" (char added included). This is not an ideal solution but I don't see a better option for now.

For Neovim 0.10, I simplified the logic. In both cases, the number of triggered events is reduced by at least 50%, should be easier on downstream listeners and, in my opinion, only includes the meaningful events.

Test case
-- abbreviation
vim.keymap.set('ca', 'mes', 'Messages')
vim.api.nvim_create_user_command('Messages', 'echo "It works!"', {})
User action Cmdline
:= :=
<SPACE> :=
<ALT+v> (paste) := vim.treesitter.
f := vim.treesitter.f
oldexpr (accept) := vim.treesitter.foldexpr
<C-w> := vim.treesitter.
<C-w> := vim.treesitter
<C-w> := vim.
<C-u> :
mes<CR> (abbrev) :mes -> : -> :Messages

Events triggered

Version Original Patched
0.11 22 11
0.10 20 11

Details

0.11 original

{ char = "=", event = "on_char_added" } 
{ char = " ", event = "on_char_added" }
{ char = ".", event = "on_char_added" } 
{ char = "f", event = "on_char_added" }
{ char = ".", event = "on_cursor_moved" } 
{ char = "r", event = "on_cursor_moved" } 
{ char = ".", event = "on_cursor_moved" } 
{ char = "", event = "on_cursor_moved" } 
{ char = "m", event = "on_char_added" } 
{ char = "e", event = "on_char_added" } 
{ char = "s", event = "on_char_added" } 
{ char = "e", event = "on_cursor_moved" } 
{ char = "m", event = "on_cursor_moved" } 
{ char = "", event = "on_cursor_moved" } 
{ char = "M", event = "on_cursor_moved" } 
{ char = "e", event = "on_cursor_moved" }
{ char = "s", event = "on_cursor_moved" } 
{ char = "s", event = "on_cursor_moved" } 
{ char = "a", event = "on_cursor_moved" } 
{ char = "g", event = "on_cursor_moved" } 
{ char = "e", event = "on_cursor_moved" } 
{ char = "s", event = "on_cursor_moved" }

0.11 patched

{ char = "=", event = "on_char_added" } 
{ char = " ", event = "on_char_added" }
{ char = ".", event = "on_char_added" } 
{ char = "f", event = "on_char_added" }
{ char = ".", event = "on_cursor_moved" } 
{ char = "r", event = "on_cursor_moved" } 
{ char = ".", event = "on_cursor_moved" } 
{ char = "", event = "on_cursor_moved" } 
{ char = "m", event = "on_char_added" } 
{ char = "e", event = "on_char_added" } 
{ char = "s", event = "on_char_added" } 

0.10 original

{ char = "", event = "on_cursor_moved" }
{ char = "=", event = "on_char_added" }
{ char = "=", event = "on_cursor_moved" }
{ char = " ", event = "on_char_added" }
{ char = " ", event = "on_cursor_moved" }
{ char = ".", event = "on_char_added" }
{ char = ".", event = "on_cursor_moved" }
{ char = "f", event = "on_char_added" }
{ char = "f", event = "on_cursor_moved" }
{ char = "r", event = "on_cursor_moved" }
{ char = ".", event = "on_cursor_moved" }
{ char = "r", event = "on_cursor_moved" }
{ char = ".", event = "on_cursor_moved" }
{ char = "", event = "on_cursor_moved" }
{ char = "m", event = "on_char_added" }
{ char = "m", event = "on_cursor_moved" }
{ char = "e", event = "on_char_added" }
{ char = "e", event = "on_cursor_moved" }
{ char = "s", event = "on_char_added" }
{ char = "s", event = "on_cursor_moved" }

0.10 patched

{ char = "=", event = "on_char_added" }
{ char = " ", event = "on_char_added" }
{ char = ".", event = "on_char_added" }
{ char = "f", event = "on_char_added" }
{ char = ".", event = "on_cursor_moved" }
{ char = "r", event = "on_cursor_moved" }
{ char = ".", event = "on_cursor_moved" }
{ char = "", event = "on_cursor_moved" }
{ char = "m", event = "on_char_added" }
{ char = "e", event = "on_char_added" }
{ char = "s", event = "on_char_added" }

@soifou soifou marked this pull request as draft June 27, 2025 18:43
@soifou soifou force-pushed the perf/cmdline-events branch from 34d487b to a380c22 Compare July 11, 2025 17:44
@soifou soifou force-pushed the perf/cmdline-events branch from 285f304 to f3e5ff7 Compare July 11, 2025 17:58
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