folke / which-key.nvim

💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type.
Apache License 2.0
5.43k stars 177 forks source link

bug: High CPU usage recording macros #864

Closed blarz closed 1 week ago

blarz commented 1 month ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.10.2

Operating system/version

Ubuntu 22.04.

Describe the bug

By accident I left a neovim instance open where there was a macro recording session still running (classic qq I guess). I noticed the CPU fan was spinning and then checked top confirming that nvim --embed was running at 100% CPU. Hitting q to stop the recording got the CPU usage back to normal.

Bisecting my neovim config I found out that this issue only occurs with the which-key plugin loaded.

Seeing that there was some work done in that are, e.g. #851, maybe there are some issues left there?

Steps To Reproduce

  1. Open any file nvim foo.txt
  2. Start a macro recording, e.g. qq
  3. Watch CPU usage of the nvim process go to 100%
  4. Stop recording with q
  5. CPU usage goes back to normal

Expected Behavior

Regular CPU usage

Health

No response

Log

No response

Repro

No response

theTastyWizard commented 1 month ago

I have the same bug, just noticed it last week before updating to 10.2. 100% cpu core usage when recording macro, but using a clean install of nvim it goes down to 5%

blarz commented 1 month ago

Had a look around in the code today and in the commit https://github.com/folke/which-key.nvim/commit/6b023b4c29ecc0aad06a51dd14bd2754b43bb0c8, fixing an issue with macros and which-key, it looks like an "infinite recursion", or busy waiting loop calling M.schedule() (wrapped in a timer though), until macro recording is finished: https://github.com/folke/which-key.nvim/blob/6b023b4c29ecc0aad06a51dd14bd2754b43bb0c8/lua/which-key/triggers.lua#L139-L149

Maybe I understand the code wrong and I am not sure if that was intended, or if there is a better way, but it would explain the CPU usage.

blarz commented 1 month ago

According to https://github.com/LazyVim/LazyVim/issues/4553 folke is on vacation until the end of the month :palm_tree:

blarz commented 1 week ago

Obligatory XKCD :sweat_smile:

But seriously, is this M.schedule() loop actually needed? I tried the plugin without that line, or rather just a return and I could record macros without high CPU usage and the issue with the double recorded characters also didn't happen.

But then again I don't know the codebase at all, so I am not sure what purpose that line serves.

blarz commented 1 week ago

Awesome, thank you! :rocket: