Skip to content

Instantly share code, notes, and snippets.

@ctechols
Last active March 6, 2024 08:37
Show Gist options
  • Save ctechols/ca1035271ad134841284 to your computer and use it in GitHub Desktop.
Save ctechols/ca1035271ad134841284 to your computer and use it in GitHub Desktop.
Speed up zsh compinit by only checking cache once a day.
# On slow systems, checking the cached .zcompdump file to see if it must be
# regenerated adds a noticable delay to zsh startup. This little hack restricts
# it to once a day. It should be pasted into your own completion file.
#
# The globbing is a little complicated here:
# - '#q' is an explicit glob qualifier that makes globbing work within zsh's [[ ]] construct.
# - 'N' makes the glob pattern evaluate to nothing when it doesn't match (rather than throw a globbing error)
# - '.' matches "regular files"
# - 'mh+24' matches files (or directories or whatever) that are older than 24 hours.
autoload -Uz compinit
if [[ -n ${ZDOTDIR}/.zcompdump(#qN.mh+24) ]]; then
compinit;
else
compinit -C;
fi;
@vincentbernat
Copy link

@thefotios my solution is already handling this case (but my message wasn't quite clear on that, so I understand you thought this was not the case). This is the purpose of the ln command (which is atomic, so no race condition).

@rafpaf
Copy link

rafpaf commented Dec 24, 2022

I commented out these lines in my .zshrc which sped it up a lot:

if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit
if

As this comment above pointed out, oh-my-zsh already runs compinit.

@cattokomo
Copy link

@aztack Thank you for your snippet, it helps alot :)

@acid-bong
Copy link

Fellas, if you're of that kind that checks their .zsh files and scripts with shellcheck (like I am), here's a more POSIX-compliant (as much as zsh allows) statement:

if [ "$(find ~/.zcompdump -mtime 1)" ] ; then
    compinit
fi
compinit -C

or oneliner, if you prefer that:

# negation, so that at least one exits on 0
[ ! "$(find ~/.zcompdump -mtime 1)" ] || compinit
compinit -C

@eeweegh
Copy link

eeweegh commented Sep 16, 2023

find's manpage is not clear on this, but I believe you want -mtime +1 to catch a file at least 24h old, rather than exactly 24h old.
On OSX, my .zcomdump was several days old, and the above would not trigger until I added the '+'.

@nimitagr
Copy link

nimitagr commented Oct 1, 2023

@aztack It helped. 🙇

@niqodea
Copy link

niqodea commented Jan 21, 2024

This is my take on the problem, it's a tradeoff between efficiency and simplicity:

autoload -Uz compinit; compinit -C  # Use cache to reduce startup time by ~0.1s
# Have another thread refresh the cache in the background (subshell to hide output)
(autoload -Uz compinit; compinit &)

Despite the obvious pitfall (having the shell start another thread at startup), I wonder if it's overall a good solution 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment