Solvedpowerlevel9k Caching to speed up prompt generation

tl;dr: I patched P9K to reduce my zsh prompt drawing latency by over 10x. Is this something you would be interested in adopting?

I recently started using Windows Subsystem for Linux and discovered that my standard Linux zsh environment was so slow, it was barely usable. It was taking over a second to draw a prompt after each command. Granted, it was never snappy even on Linux but it was at least manageable.

I've spent some time profiling and optimizing the code and the results look promising. On each prompt P9K performs a lot of computation and over 90% of it is the same as in the last prompt. I implemented simple caching to avoid recomputing things and now my prompts get rendered in less than 100ms on WSL -- over 10x reduction in latency. They are now also blazingly fast on Linux.

I'm not an expert in zsh, so I didn't want to open a PR right away. I also didn't write any tests. If there is interest in this type of improvements, the code is here: https://github.com/romkatv/powerlevel9k/tree/caching. The code adds a general caching mechanism for anything that prompt generators might want to cache between calls, and then applies it to left_prompt_segment and right_prompt_segment for nice latency reduction across the board. It also contains optimizations for all prompt generators that I personally use and that were showing up on my profile as being slow. Let me know what you want me to do. Open a PR, sign something so that you can pull the code, go away, something else?

P.S.

My .zshrc is here: https://github.com/romkatv/dotfiles-public/blob/master/.zshrc.

34 Answers

✔️Accepted Answer

By the way, for those of you who don't follow /r/zsh, Powerlevel10k now works on all major platforms (Mac, Linux, FreeBSD and WSL). It's backward-compatible with Powerlevel9k configuration and it's 50 times faster.

If you are currently using Powerlevel9k, type this and see how fast your shell will become while still looking the same:

git clone https://github.com/romkatv/powerlevel10k.git /tmp/powerlevel10k
source /tmp/powerlevel10k/powerlevel10k.zsh-theme

(When you are done playing, rm -rf /tmp/powerlevel10k and exit zsh.)

You can find other options for trying out the theme in the docs.

Other Answers:

In case anyone is also bothered by how slow the vcs/git prompt is, I've managed to speed it up by a large margin. On Linux my whole prompt (which includes vcs as well as all the prompts I listed in the previous comment) renders in 50ms when I'm in a git repo with 1k files. In a large repo such as Linux kernel, it renders in 300ms -- too slow for my taste. I added an option to disable scanning dirty files for repos above certain size and set it at 4k files in my .zshrc. With this option my prompt renders in 150ms when in Linux repo. The prompt also uses color-coding to signal that it didn't scan for dirty files. Finally vcs prompt is fast enough to be usable for me!

The trick was to write a custom C binary that prints everything we need to know about a git repo. It's much faster than calling git multiple times, which spawns a dozen processes with accompanying pipes and repeats many actions unnecessarily -- such as scanning parent folders in search of .git. And also caching, of course, to make rendering itself fast. The C code is in https://github.com/romkatv/gitstatus. The ZSH code is in the same branch as caching: https://github.com/romkatv/powerlevel9k/tree/caching.

If anyone wants to give it a try with their own master-based setup (especially if it's sluggish), here's a non-committal way to do it.

# Enable caching of parts of the prompt to make rendering much faster.
POWERLEVEL9K_USE_CACHE=true
# Enable alternative implementation for the vcs prompt. It's much faster but it only supports git.
# Tell it to not scan for dirty files in repos with over 4k files.
POWERLEVEL9K_VCS_STATUS_COMMAND="/tmp/gitstatus --dirty-max-index-size=4096"
wget https://github.com/romkatv/gitstatus/releases/latest/download/gitstatus -P /tmp
chmod +x /tmp/gitstatus
# Adjust this path depending on where you normally source powerlevel9k.zsh-theme from.
POWERLEVEL9K_INSTALLATION_DIR=~/.oh-my-zsh/custom/themes/powerlevel9k
source <(curl -f https://raw.githubusercontent.com/romkatv/powerlevel9k/caching/powerlevel9k.zsh-theme)

Then see if your current shell feels more responsive. No permanent changes are done to your setup, so once you exit zsh you are back to business as usual.

I can vouch for the following prompts being fast in the patched version:

  • root_indicator
  • dir_writable
  • dir
  • status
  • command_execution_time
  • background_jobs
  • time
  • vcs

Related Issues:

28
powerlevel9k VSCode integrated terminal versus nerdfont-complete
Update At first it seemed like if I found a PowerLine font that was also enabled with nerd-fonts I c...
20
powerlevel9k Prompt with newline causes previous lines to be deleted when tab autocompleting
@bhilburn @jordanweaver I've got an update I've managed to get the issue to stop ...
11
powerlevel9k os_icon shows os as text
So POWERLEVEL9K_MODE='awesome-patched' stands before you source oh-my-zsh right? If so have you rest...
8
powerlevel9k Character duplicate when using tab completion as root
I have encountered the same problem on iTerm and I've solved it The reason for the problem is ROOT_I...
7
powerlevel9k Caching to speed up prompt generation
By the way for those of you who don't follow /r/zsh Powerlevel10k now works on all major platforms (...
29
vim devicons NERDTree: square brackets around icon on sourcing vimrc
Ah I think I know what this is Whenever I try to source ~/.vimrc file when NERDTree is open ...
574
terminal Typing inside of the default WSL terminal feels amazing, why is it better than every other app?
I really do not mind when someone comes by and decides to tell us that we're doing a good job at som...
294
ohmyzsh compinit:503: no such file or directory: /usr/local/share/zsh/site-functions/_brew
Per #9602 (comment) brew cleanup fixed it for me I am using Apple M1 When I added this line: export ...
264
tqdm Jupyterlab and tqdm_notebook
I confirm this problem on my environment but it is upstream so IMHO nothing can be done on tqdm Plea...
99
ohmyzsh how to enable username and hostname in the default theme?
In fact I finally found the solution for the problem I do want to stick with the default theme but I...
92
terminal Guide for build and installation
This information should be included in the Building the Code section of the README file. ...
87
ohmyzsh source ~/.zshrc error
@huangjihua @mcornella There are steps which helped me: zsh chsh -s $(which zsh) Logout from the sys...
86
alacritty Tmux austostart?
It should be like this: Which operating system does the issue occur on? MacOS Catalina I understood ...
85
terminal error 0x80070002 when launching `powershell.exe'
Just add the path of the power shell to the environment variable Thank you It works! %systemroot%\Sy...
84
ohmyzsh tab completion not working
For me there were no permission issues on my completion scripts so the above didn't fix my issue ...
78
alacritty Support for Alt+key (option as meta)
With latest alacritty and tmux I finally got forward and backward movement with the following: ...
76
termux app 403 forbidden while update or pkg Install
If someone facing the issue when you run pkg install ... Issue : when ever I try to update / upgrade...
75
gitsome Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7SrToZ/gitsome/
I had the same problem on OSX and Solve my issue (I have both python versions installed). ...
75
ohmyzsh Zsh is not installed! Please install zsh first!
Well zsh is not there so it must not be installed sudo apt install zsh will do. ...
71
bat Ubuntu install error when rg is installed also
As a workaround one can use sudo apt install -o Dpkg::Options::=--force-overwrite bat ripgrep (taken...
70
terminal Move the solution to VS2019 and v142 toolset
As mentioned in README (also related to #534) If you're running VS2019 ...
67
ohmyzsh rvm error after starting zsh
For me it is rm ~/.zcomp* I'm getting this error when opening new zshell: As suggested on other rela...
62
guake guake hangs when you put exit on the CLI and then choose No.
To whom hangs on Ubuntu 18.04 #1198 (comment) mentioned a solution: This works for me ...
58
ohmyzsh Warning: plugin cargo not found
Removing the comma at the end of git solved it for me thank you @mcornella ! Went from: to to fix it...
54
hyper Drag and drop does not work, in the terminal window anymore
guys I create a plugin for this: https://github.com/qweasd1/hyper-drop-file just hyper install hyper...
50
alacritty Alt-Left/Right and Home/End don't work and prevent alacritty from closing
@spacecowboy I added the following lines to support skipping words via Alt+Left and Alt+Right on mac...
47
tqdm How to update single progress bar in multiprocessing map()
from https://stackoverflow.com/questions/41920124/multiprocessing-use-tqdm-to-display-a-progress-bar...
44
guake Global shortcut does not work in gnome applications in ubuntu 17.10
@Woile @eN0Rm as a workaround you may disable the shortcut in the guake config and reassign it in th...
43
terminal MEGATHREAD: Breaking settings changes in version 0.11!
Out of curiosity BREAKING SETTINGS CHANGES IN 0.11 BREAKING CHANGE We've deleted a lot of legacy set...
42
ConEmu Arrow keys not working in Bash for Windows
@ErrantErinaceinae Are you kidding? Months??? The issue was created four days ago! This is absolutel...
42
terminal Windows Terminal keeps forgetting/can't find my default WSL Ubuntu profile
Happened to me (seemingly) after updating Docker for Windows Haven't had any issues with WSL2 for mo...
42
ohmyzsh Insecure completion-dependent directories detected on macOS Catalina
Put ZSH_DISABLE_COMPFIX=true in your ~/.zshrc Before source $ZSH/oh-my-zsh.sh ~/.zshrc ...
41
ohmyzsh _arguments:451: _vim_files: function definition file not found
I got it fixed by removing the .zcompdump file from my root directory and running exec zsh. ...
38
alacritty [Feature] Add setting to hide titlebar
PR #947 has been merged 2 months ago hiding the title-bar is now possible via: Like iTerm2 allows (P...
37
ConEmu Windows Subsystem For Linux Bash Specify Startup Directory
@NitroBAY Ops I made a mistake The Windows file system is mounted to Windows Subsystem For Linux(WSL...
37
terminal Terminal hangs on exit with 'process exited with code 127' message
@carragom Oh that explains it - closeOnExit is a Profile setting Hi! I've noticed that on newer rele...
34
termux app access from adb shell?
That's what I got so far (on my rooted phone): /data/data/com.termux/files/home/bin/termux-shell.sh ...
33
bat [fzf] Bat is awesome, but
It's so awesome but whyyyyy Whenever bat detects a non-interactive terminal it will fall back to pri...
31
kitty Error opening terminal: xterm-kitty
From stackoverflow: export TERM=xterm And it works. Any ideas how to fix this? This is a brand new M...
28
hyper Hyperterm fails to display symbols
termCSS: 'x-row {line-height: initial} .unicode-node {position: relative}' in config works well as t...
27
hyper Opening a new tab throws an uncaught exception
After taking some hints from the comments I removed the hypercwd plugin which seems to have solved t...
26
alacritty Font support for Oh-My-Zsh due to imperfect rendering
@kchibisov thank you for your patience and help It worked! After all it was nothing difficult ...
26
hyper Plugins not installing
I'm using Hyper 1.0.0 (64-bit) on Windows 10 I'm having issues getting plugins to install too Earlie...
24
terminal Alt arrow keys do nothing; key unbinding broken?
This behavior is due to #3585 Proper expected behavior is restored with the following: ...
24
ohmyzsh Got completions warning and command not found: compdef when tried to use omz on none root user
Set ZSH_DISABLE_COMPFIX=true in your zshrc file before oh-my-zsh.sh is sourced and update OMZ to the...
23
ConEmu WSL2 no longer working after windows update
Thank you This does indeed work You don't need to specify the user (the default one that corresponds...
23
hyper "Unable to start terminal process: CreateProcess failed" when opening new tab under WSL
Removing hypercwd plugin fixed this for me. EDIT: Noticed that this is a duplicate but still relevan...
22
alacritty w3mimgdisplay not working correctly
I guess that the only non-hacky way to make this working is to incorporate image preview functionali...
21
asciinema player asciinema-player with React
The player component is reagent component Hello Asciinema player has very strange behavior in my sma...
20
alacritty Auto copy
@unphased Can you try https://github.com/cathay4t/alacritty/tree/copy_selection on OSX? Just add sav...