bug: High CPU usage recording macros
Did you check docs and existing issues?
- [X] I have read all the which-key.nvim docs
- [X] I have updated the plugin to the latest version before submitting this issue
- [X] I have searched the existing issues of which-key.nvim
- [X] I have searched the existing issues of plugins related to this issue
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
- Open any file
nvim foo.txt - Start a macro recording, e.g.
qq - Watch CPU usage of the nvim process go to 100%
- Stop recording with
q - CPU usage goes back to normal
Expected Behavior
Regular CPU usage
Health
No response
Log
No response
Repro
No response
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%
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.
According to https://github.com/LazyVim/LazyVim/issues/4553 folke is on vacation until the end of the month :palm_tree:
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.
Awesome, thank you! :rocket: