neovim
neovim copied to clipboard
perf: avoid vim.validate in fast functions
For many small/simple functions (like those found in shared.lua), the runtime of vim.validate can far exceed the runtime of the function itself. For these simple functions, prefer a simple assertion instead of using vim.validate.
Ref: https://github.com/neovim/neovim/issues/28921
Using a modified version of the script in the linked issue, the runtime performance compared to vim.validate is as follows:
$ VIMRUNTIME=runtime build/bin/nvim --headless -l scratch/test.lua
vim.validate: 10000000 times took 1.24831 seconds
no validation: 10000000 times took 0.0033800000000002 seconds
assert: 10000000 times took 0.003174 seconds
The assert test case is this PR. We can see that this is just as fast as not using any validation at all (within measurement noise).
Are there other functions where this would make sense (possibly in a follow-up), e.g., in editor.lua, fs.lua (vim.fs.normalize instead of _fast = true), lsp/* (Client:request or resolve_bufnr) or treesitter/* (language.register for some reason is fairly slow in nvim-treesitter startup)?