Hacker News new | ask | show | jobs
by bPspGiJT8Y 783 days ago
> much faster than zsh script completions

I'm curious where did you get that from.

Edit: I read the note in your dotfiles repo. Yes calling `compinit` on each shell invocation is going to be really slow. That's not how you're supposed to do it, you could at least add the `-C` flag to cache the completions. Ideally you'd also use `zcompile` to compile the cache to ZSH word code. This puts my completions initializing time at ~20ms on a lower/mid-end laptop. Additionally you can do the trick `fish` does and defer the initialization of completions until the first hit of Tab key, so the impact on shell startup time is exactly 0.

1 comments

I have had this snippet in my .zshrc for years,

    autoload -U compinit && compinit
    {
      # Compile the completion dump to increase startup speed. Run in background.
      zcompdump="${ZDOTDIR:-$HOME}/.zcompdump"
      if [[ -s "$zcompdump" && (! -s "${zcompdump}.zwc" || "$zcompdump" -nt "${zcompdump}.zwc") ]]; then
        # if zcompdump file exists, and we don't have a compiled version or the
        # dump file is newer than the compiled file
        zcompile "$zcompdump"
      fi
    } &!
Using `zmodload zsh/zprof`, I can see about 50% (16ms) of my start up is compinit (its about 28ms without the `zcompile`).

Do you have any pointers for the "load on tab" idea? I didn't turn up any good results in DDG and LLMs were just hallucinating.

BTW I believe `-C` will disable some cache checking, caching is enabled by default

> To speed up the running of compinit, it can be made to produce a dumped configuration that will be read in on future invocations; this is the default, but can be turned off by calling compinit with the option -D.

> ...

> ... The check performed to see if there are new functions can be omitted by giving the option -C. In this case the dump file will only be created if there isn't one already.

Unable to edit, but this is how I handled lazy loading the completions

    {
      # load compinit and rebind ^I (tab) to expand-or-complete, then compile
      # completions as bytecode if needed.
      lazyload-compinit() {
        autoload -Uz compinit
        # compinit will automatically cache completions to ~/.zcompdump
        compinit
        bindkey "^I" expand-or-complete
        {
          zcompdump="${ZDOTDIR:-$HOME}/.zcompdump"
          # if zcompdump file exists, and we don't have a compiled version or the
          # dump file is newer than the compiled file, update the bytecode.
          if [[ -s "$zcompdump" && (! -s "${zcompdump}.zwc" || "$zcompdump" -nt "${zcompdump}.zwc") ]]; then
            zcompile "$zcompdump"
          fi
        } &!
        # pretend we called this directly, instead of the lazy loader
        zle expand-or-complete
      }
      # mark the function as a zle widget
      zle -N lazyload-compinit
      bindkey "^I" lazyload-compinit
    }
> Do you have any pointers for the "load on tab" idea? I didn't turn up any good results in DDG and LLMs were just hallucinating.

This is bash, not zsh, but I have this working in my dotfiles by just telling bash where to look for my custom on-demand completions:

https://github.com/NateEag/dotfiles/blob/6862726ad2ecaa3a30e...

I imagine something similar works for zsh.

I just want to say - thank you! I've been using ZSH since it became the default on macOS and one thing that started annoying me recently is the slow startup time. Your snippet tangibly improved that.

Do you by chance have any good resources on optimising my config further?

Beyond zprof (https://www.bigbinary.com/blog/zsh-profiling) not really I'm afraid. I did the majority of my zsh-prompt hacking 10 years ago and haven't thought about it since. That snippet could be from anywhere.

You could peek at something like zprezto https://github.com/sorin-ionescu/prezto or pure https://github.com/sindresorhus/pure for tips.

Fetching git/hg/... info is always slow, so try and speed that up where you can (as to how to do that, uhh... I know my prompt has a dirty-state check nicked from pure for speed reasons). You can also cache any `asdf init zsh` or similar to a file and do the same "run in background" trick so the next shell will have any changes.

The biggest improvement I can remember was dropping zprezto for my own much smaller config, I really did not need much comparatively. Mostly some git info and "good default" options. I use zgenom for a plugin manager but only have 3 plugins, probably I should just dump it and inline the plugins to avoid getting owned one day.

You may be interested in this lazy completion loader https://news.ycombinator.com/item?id=40140873
> BTW I believe `-C` will disable some cache checking, caching is enabled by default

You're right, my memory has let me down.

> Do you have any pointers for the "load on tab" idea? I didn't turn up any good results in DDG and LLMs were just hallucinating.

The simplest implementation would be something like

    bindkey ^I init_completions

    init_completions () {
      # ... init logic here ...

      # rebind tab to complete
      bindkey ^I complete-word
      # actually do complete the initial request
      zle complete-word
    }
Edit: I see now you already figured it out, yeah that's exactly what I meant