by mananaysiempre · 4y ago · 0 comments
Question: How the hell do you take 130ms to print out the commands to set up shell completion? Is there something meaningful happening in that time, or is this simply the cost for the kernel to load a 43 MB do-everything kubectl binary? (43 MB is a lot, admittedly, but 130ms when most of that code stays untouched still sounds high to me.)
No it does not, the article is wrong or phrased incorrectly.
Zsh completion functions, when properly configured, are autoloaded. The function is only read from disk when called. Say with _kubectl, if you do it correctly with a static _kubectl instead of following the stupid advice of `source <(kubectl completion zsh)`, on shell startup your _kubectl should be uninitialized:
$ which _kubectl
_kubectl () {
# undefined
builtin autoload -XUz
}
After you actually try to complete kubectl once, run that again and this time you can see the actual function body. $ which _kubectl
_kubectl () {
local shellCompDirectiveError=1
# ...
}
That function is read from disk on use, not cached.What zsh does cache, with compinit[1], is association of commands with completion functions. compinit reads the #compdef line of functions on $fpath (e.g. `#compdef _kubectl kubectl`), and generates a ~/.zcompdump (or some other path, mine is ~/.local/share/zsh/compdump for instance), which is basically a list of command and completion function pairs, plus a huge autoload invocation registering all the completion functions. This way zsh knows which function to load when a command needs completion.
[1] https://zsh.sourceforge.io/Doc/Release/Completion-System.htm...
What exactly happens with kubectl is hard to answer and need someone to profile it if that's true. May be it checks for updates or something like that.
The whole experience caused me to try different shells, I ended up on ksh for simplicity and comparative leanness, works well for my needs.
It was that experience which led me to look try other shells, in the end I found ksh's simpler feature set and manpage more to my liking.