catppuccin / nvim

🍨 Soothing pastel theme for (Neo)vim
MIT License
5.39k stars 239 forks source link

An ability to disable compiler #348

Closed stasjok closed 1 year ago

stasjok commented 1 year ago

Is your feature request related to a problem? Please describe.

A compiled version is slow for me (around 7 ms). Regular highlights is faster (around 3 ms with impatient.nvim, without impatient it may be slow, I don't know).

Test case 1 (default)

plugin/colorscheme.lua:

local status, catppuccin = pcall(require, "catppuccin")

if status then
  catppuccin.setup({
    flavour = "macchiato",
    background = {
      light = "latte",
      dark = "macchiato",
    },
    styles = {
      conditionals = {},
      keywords = { "italic" },
    },
    integrations = {
      -- Disable default
      nvimtree = false,
      dashboard = false,
      indent_blankline = false,
      -- Enable optional
      mini = true,
    },
    custom_highlights = {
      TermCursor = { bg = "#179299" },
    },
  })
  catppuccin.load()
end

startuptime:

032.181  000.112  000.112: require('catppuccin')
038.271  000.017  000.017: sourcing /nix/store/7xzxpsa1d366501v259ywwghxj329bkj-vim-pack-dir/pack/myNeovimPackages/start/catppuccin-nvim/colors/catppuccin.vim
038.964  006.922  006.792: sourcing /home/stas/.config/nvim/plugin/colorscheme.lua

Notice that summary time for colorscheme.lua is 6,9 ms. Basically almost all that time is spend loading compiled file.

Test case 2 (no compiler)

plugin/colorscheme.lua:

local status, catppuccin = pcall(require, "catppuccin")

if status then
  catppuccin.setup({
    flavour = "macchiato",
    background = {
      light = "latte",
      dark = "macchiato",
    },
    styles = {
      conditionals = {},
      keywords = { "italic" },
    },
    integrations = {
      -- Disable default
      nvimtree = false,
      dashboard = false,
      indent_blankline = false,
      -- Enable optional
      mini = true,
    },
    custom_highlights = {
      TermCursor = { bg = "#179299" },
    },
  })
  local theme = require("catppuccin.lib.mapper").apply()
  vim.g.colors_name = "catppuccin"
  local tbl = vim.tbl_deep_extend(
    "keep",
    theme.custom_highlights,
    theme.integrations,
    theme.syntax,
    theme.editor
  )
  require("catppuccin.lib.highlighter").syntax(tbl)
end

startuptime:

031.276  000.110  000.110: require('catppuccin')
031.523  000.041  000.041: require('catppuccin.lib.mapper')
031.580  000.054  000.054: require('catppuccin.palettes')
031.645  000.062  000.062: require('catppuccin.palettes.macchiato')
031.781  000.066  000.066: require('catppuccin.lib.hsluv')
031.785  000.119  000.054: require('catppuccin.utils.colors')
031.849  000.049  000.049: require('catppuccin.groups.syntax')
031.942  000.052  000.052: require('catppuccin.groups.editor')
032.041  000.065  000.065: require('catppuccin.groups.integrations.mini')
032.171  000.048  000.048: require('catppuccin.groups.integrations.native_lsp')
032.286  000.067  000.067: require('catppuccin.groups.integrations.cmp')
032.356  000.044  000.044: require('catppuccin.groups.integrations.telescope')
032.426  000.052  000.052: require('catppuccin.groups.integrations.gitsigns')
032.531  000.071  000.071: require('catppuccin.groups.integrations.markdown')
032.678  000.076  000.076: require('catppuccin.groups.integrations.treesitter')
032.814  000.039  000.039: require('vim.treesitter.language')
032.825  000.099  000.060: require('vim.treesitter.query')
032.896  000.070  000.070: require('vim.treesitter.languagetree')
032.928  000.247  000.079: require('vim.treesitter')
032.985  000.055  000.055: require('vim.treesitter.highlighter')
033.097  000.043  000.043: require('catppuccin.groups.terminal')
033.214  000.033  000.033: require('catppuccin.lib.highlighter')
034.044  002.905  001.616: sourcing /home/stas/.config/nvim/plugin/colorscheme.lua

Notice that summary time for colorscheme.lua is 2,9 ms. Maybe it can even be optimized further.

I think that at least in my case luajit loop is faster than multiple commands in a compiled file.

Second problem I have is that I'm using Nix. A code for determining if the plugin is updated:

https://github.com/catppuccin/nvim/blob/8d4b9ed1f9cb5a575a1fa25c506409416d347241/lua/catppuccin/init.lua#L123

won't work, because plugin directory in nix doesn't have .git directory and all files have the same date:

ls -la /nix/store/7xzxpsa1d366501v259ywwghxj329bkj-vim-pack-dir/pack/myNeovimPackages/start/catppuccin-nvim/
total 56
dr-xr-xr-x 1 root root       220 Jan  1  1970 ./
drwxrwxr-t 1 root nixbld 4482910 Nov 20 11:40 ../
dr-xr-xr-x 1 root root        14 Jan  1  1970 after/
dr-xr-xr-x 1 root root        26 Jan  1  1970 autoload/
dr-xr-xr-x 1 root root       198 Jan  1  1970 colors/
dr-xr-xr-x 1 root root        36 Jan  1  1970 doc/
-r--r--r-- 4 root root       358 Jan  1  1970 .editorconfig
-r--r--r-- 3 root root        50 Jan  1  1970 .gitattributes
dr-xr-xr-x 1 root root        94 Jan  1  1970 .github/
-r--r--r-- 4 root root        28 Jan  1  1970 .gitignore
-r--r--r-- 5 root root      1067 Jan  1  1970 LICENSE.md
dr-xr-xr-x 1 root root        34 Jan  1  1970 lua/
-r--r--r-- 5 root root       509 Jan  1  1970 .luacheckrc
-r--r--r-- 2 root root     31135 Jan  1  1970 README.md
-r--r--r-- 5 root root       175 Jan  1  1970 stylua.toml

So it will result in a stale cache after plugin directory is updated.

Describe the solution you'd like An ability to disable auto compilation, or an optimization of compiled code.

Describe alternatives you've considered Do everything manually like in Test case 2, but it can be prone to errors after updates because it's not an official API.

nullchilly commented 1 year ago

The compiled cache is twice as fast than normal highlight for me

I will try to reproduce your issue on a nixos docker

nullchilly commented 1 year ago

@stasjok Can you run vim.cmd.colorscheme "catppuccin" instead? It is currently combined so I am unsure which part is slow

stasjok commented 1 year ago

I traced down a slowness to https://github.com/catppuccin/nvim/blob/8d4b9ed1f9cb5a575a1fa25c506409416d347241/lua/catppuccin/lib/compiler.lua#L26 Without extra vim.o.background = "dark" (which is default in neovim) the speed is normal:

028.927  000.113  000.113: require('catppuccin')
029.654  000.868  000.755: sourcing /home/stas/.config/nvim/plugin/colorscheme.lua

Also notice that there is no extra sourcing /nix/store/7xzxpsa1d366501v259ywwghxj329bkj-vim-pack-dir/pack/myNeovimPackages/start/catppuccin-nvim/colors/catppuccin.vim line. Probably changing background triggers loading of colorscheme again, and maybe some autocommands from plugins. So my 7 ms maybe from plugins triggered by colorscheme change.

So regarding "a compiled version is slow for me" I guess I was wrong.

But the stale cache for nix plugin stands. The only way I know to determine if plugin is updated is compare path. Hash part here /nix/store/7xzxpsa1d366501v259ywwghxj329bkj-vim-pack-dir is unique for every version of plugin (new versions are placed in new directories in store).

nullchilly commented 1 year ago

I unfortunately can't run your nix config

❯ nix registry add mypkgs github:stasjok/dotfiles
error: experimental Nix feature 'nix-command' is disabled; use '--extra-experimental-features nix-command' to override

Even though I have added the experiement flag to .config/nix.conf

stasjok commented 1 year ago

vim.cmd.colorscheme "catppuccin"

It doesn't make any difference. If I add vim.o.background = "dark" to my Test case 2, than it's 10 ms.

nullchilly commented 1 year ago

Without extra vim.o.background = "dark" (which is default in neovim) the speed is normal:

I literally have a lock to avoid colorscheme getting loaded twice https://github.com/catppuccin/nvim/blob/main/lua/catppuccin/init.lua#L75

stasjok commented 1 year ago

I unfortunately can't run your nix config

❯ nix registry add mypkgs github:stasjok/dotfiles
error: experimental Nix feature 'nix-command' is disabled; use '--extra-experimental-features nix-command' to override

Even though I have added the experiement flag to .config/nix.conf

I guess I was wrong in README. Home config is $XDG_CONFIG_HOME/nix/nix.conf. There is missing nix directory. I have config in /etc/. Also you can add command line option like it suggests. But I don't think it worth spending time trying my config. I also tried with -u NONE and it's also fast. So I think it is plugins or their autocommands triggered by colorsheme change.

nullchilly commented 1 year ago

cc @edeneast

I heard you also use nix, any idea on this? (The no .git/ folder issue)

EdenEast commented 1 year ago

I would suggest that if there is no git stat info then getting the fstat of a file in catppuccin ./lua/catppuccin/init.lua for example. Since every change is a new entry in the nix store it will also have different fstat values.

I do use nixos for my linux installs and nix and home-manger for my other non-linux systems. My nvim config changes so constantly that I just symlink my nvim config. I use packer with this pr to give me lockfile support so at least I have a lockfile for the plugins.

nullchilly commented 1 year ago

But the original poster's directory stat is Jan 1 1970

I am a lil confused

EdenEast commented 1 year ago

Ahhh I just checked my nix-store and you are right all the files are at the beginning of epoch.

Thinking about the reason that the git stat is there is to check if the plugin has been changed / updated. How about precomputing a hash of the contents of the repo.

How about instead of getting the fstat of the .git/ORIG_HEAD file there is a precomputed hash of all the contents of for example:

fd . -t f lua autoload plugin colors | sha1sum | awk '{print $1}'

This could be part of a precommit hook, or a workflow that would create a this file when there is a new commit into main.

Just an idea.

stasjok commented 1 year ago

But the original poster's directory stat is Jan 1 1970

I am a lil confused

All files in nix store have timestamp 1 second since epoch. It's by design. You don't have to fix it (I mean a special case like Nix), but if you want to, I think you can rely only on a combination of file path + mtime (e.g. some hash on both).

Regarding startup time, moving colorscheme loading to init.lua (essentially before any plugin) fixed speed.

EdenEast commented 1 year ago

I got around to implementing a solution to this issue for nightfox.