101

GitHub - lewis6991/impatient.nvim: Improve startup time for Neovim

 3 years ago
source link: https://github.com/lewis6991/impatient.nvim
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WIP

Speed up loading Lua modules in Neovim to improve startup time.

Optimisations include:

  • Cache for compiled lua modules.
  • Improved default package loader for uncached module loads
  • Restoring the preloader

The expectation is that a form of this plugin will eventually be merged into Neovim core via this PR. This plugin serves as a way for impatient users to speed up there Neovim 0.5 until the PR is merged and included in a following Neovim release at which point this plugin will be redundant.

Optimisations

This plugin does several things to speed up require in Lua.

Implements cache for all loaded Lua modules

This is done by using loadstring to compile the Lua modules to bytecode and stores them in a cache file. This also has the benefit of avoiding Neovim's expensive module loader which uses nvim_get_runtime_file(). The cache is invalidated using the modified time of each modules file path.

The cache file is located in $XDG_CACHE_HOME/nvim/luacache.

Reduces runtimepath during require

runtimepath contains directories for many things used by Neovim including Lua modules; the full list of what it is used for can be found using :help 'runtimepath'. When require is called, Neovim searches through every directory in runtimepath until it finds a match. This means it ends up searching in every plugin that doesn't have a Lua directory, which can be a lot and makes require much more expensive to run. To mitigate this, Impatient reduces runtimepath during require to only contain directories that have a Lua directory.

Restores the preloader

Neovim currently places its own loader for searching runtime files at the front of package.loaders. This prevents any preloaders in package.preload from being used. This plugin fixes that by moving the default package preloader to run before Neovim's loader. For example, LuaJIT provides preloaders for the built-in modules ffi and bit, so this optimisation will improve the loading of those.

Installation

packer.nvim:

-- Is using a standard Neovim install, i.e. built from source or using a
-- provided appimage.
use 'lewis6991/impatient.nvim'

Setup

impatient needs to be setup before any other lua plugin is loaded so it is recommended you add the following near the start of your init.vim.

lua require('impatient')

Commands

:LuaCacheClear:

Remove the loaded cache and delete the cache file. A new cache file will be created the next time you load Neovim.

:LuaCacheLog:

View log of impatient.

:LuaCacheProfile:

View profiling data. To enable, Impatient must be setup with:

lua require'impatient'.enable_profile()

Performance Example

Measured on a M1 MacBook Air.

Standard

Total resolve: 68.108365ms, total load: 56.954069ms

With reduced runtimepath

Total resolve: 39.302892ms, total load: 58.602757ms

With cache

Cache load: 3.414473ms

Total resolve: 2.122016ms, total load: 4.539859ms

Relevent Neovim PR's

libs: vendor libmpack and libmpack-lua [merged]

feat(lua): startup profiling

Credit

All credit goes to @bfredl who implemented the majority of this plugin in https://github.com/neovim/neovim/pull/15436.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK