0

I'm using the code snippet as shown below to "lazyload" some of my plugins for Neovim. I wrote the same function, once in LUA and once in vimscript. I would like to use the LUA implementation to try and get away from vimscript; however, in my tesing the lua version is significantly slower than the vimscript version.

It is worth noting, yes, the the autocommands are technically not exactly the same as the vimscript version is calling defer_fn to wait an additional millisecond before running the autocommand in this case. Regardless of this, both versions are using the same event VimEnter, so I do not understand why the lua version is so much slower.

lua version:

vim.api.nvim_create_autocmd({"VimEnter"}, {
    pattern = {"*"},
    callback = load_plugins,
})

Average Time of Nvim Startup: 33 ms

Vimscript version:

vim.cmd [[
      augroup user_cmds
          autocmd!
          autocmd VimEnter * lua vim.defer_fn(load_plugins, 1)
       augroup END
  ]]

Average Time of Nvim Startup: 23 ms

romainl
  • 186,200
  • 21
  • 280
  • 313
  • There must be really no difference. First code compiles "load_plugins" before VimEnter, but the second one, probably, after it. – Matt Jul 24 '22 at 03:33
  • For the record, a 10 ms difference on average doesn't qualify as "much slower". Additionally, the vimscript version is behind a layer of indirection provided by the language bindings. – r_31415 Jul 24 '22 at 05:47
  • @r_31415 10ms in general is not a significant amount, you're right. However, for this reason I specified that this is related to the startup time of Neovim. This, despite perhaps seeming arbitrary, does make a significant difference when you consider the average nvim startup is probably around 80-200ms. A 10 ms reduction to say a 80ms config is a 12.5% speed increase, far outside the margin of error, and rather significant at that. Regardless, why is "one of the fastest, best performance, scripting languages (lua) being outperformed by vimscipt if it is "behind a layer of indirection". – StarDrummer Jul 24 '22 at 18:21
  • 1
    I understand what you're trying to do, however, the difference is so small that it is even difficult to even measure it accurately. When you have an effect that is comparatively large to a very small quantity (in this case, the regular startup time), the conclusion is not necessarily that you found a significant effect. It could be that the uncertainty in your measurements is so large that you cannot draw any conclusions from your experiment (cont) – r_31415 Jul 24 '22 at 19:20
  • At any rate, I don't dispute that lua could be slower than vimscript in small tasks (after all, as a language binding, it needs to load more stuff). However, for larger and complex tasks, it should be faster than vimscript. Not sure why neovim, as a lightweight editor should have the need to perform complex tasks to the point that speed differences of that kind start to be important, but that's another topic. – r_31415 Jul 24 '22 at 19:25
  • Finally, just to provide additional perspective, Julia vs Python benchmarks often show the same effect. Julia is very fast for tasks with an extended execution time. When this is not true, Python is usually faster. – r_31415 Jul 24 '22 at 19:25
  • Hmm, alright then. An issue I see with this is that your comment makes perfect sense (loading of additional modules in the case of lua), if this was the first and only time lua was being loaded/used in my config. But it's not. The entire config is lua and that small snippet is the only vimscript. Therefore, I would think Neovim loads all the lua modules at startup, and from there executes the lua code (meaning the small lua version wouldn't be any slower than the vimscript at the very least). Maybe I don't understand lua runtime. Anyway, thanks for your comments and clarification. – StarDrummer Jul 24 '22 at 20:38

0 Answers0