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: 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?


My .zshrc is here:

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 /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 The ZSH code is in the same branch as 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.
# 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 -P /tmp
chmod +x /tmp/gitstatus
# Adjust this path depending on where you normally source powerlevel9k.zsh-theme from.
source <(curl -f

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:

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...
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 ...
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...
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...
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 (...
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 ...
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...
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 ...
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...
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...
terminal Guide for build and installation
This information should be included in the Building the Code section of the README file. ...
ohmyzsh source ~/.zshrc error
@huangjihua @mcornella There are steps which helped me: zsh chsh -s $(which zsh) Logout from the sys...
alacritty Tmux austostart?
It should be like this: Which operating system does the issue occur on? MacOS Catalina I understood ...
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...
ohmyzsh tab completion not working
For me there were no permission issues on my completion scripts so the above didn't fix my issue ...
alacritty Support for Alt+key (option as meta)
With latest alacritty and tmux I finally got forward and backward movement with the following: ...
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...
gitsome Command "python 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). ...
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. ...
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...
terminal Move the solution to VS2019 and v142 toolset
As mentioned in README (also related to #534) If you're running VS2019 ...
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...
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 ...
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...
hyper Drag and drop does not work, in the terminal window anymore
guys I create a plugin for this: just hyper install hyper...
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...
tqdm How to update single progress bar in multiprocessing map()
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...
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...
ConEmu Arrow keys not working in Bash for Windows
@ErrantErinaceinae Are you kidding? Months??? The issue was created four days ago! This is absolutel...
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...
ohmyzsh Insecure completion-dependent directories detected on macOS Catalina
Put ZSH_DISABLE_COMPFIX=true in your ~/.zshrc Before source $ZSH/ ~/.zshrc ...
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. ...
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...
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...
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...
termux app access from adb shell?
That's what I got so far (on my rooted phone): /data/data/com.termux/files/home/bin/ ...
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...
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...
hyper Hyperterm fails to display symbols
termCSS: 'x-row {line-height: initial} .unicode-node {position: relative}' in config works well as t...
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...
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 ...
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...
terminal Alt arrow keys do nothing; key unbinding broken?
This behavior is due to #3585 Proper expected behavior is restored with the following: ...
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 is sourced and update OMZ to the...
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...
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...
alacritty w3mimgdisplay not working correctly
I guess that the only non-hacky way to make this working is to incorporate image preview functionali...
asciinema player asciinema-player with React
The player component is reagent component Hello Asciinema player has very strange behavior in my sma...
alacritty Auto copy
@unphased Can you try on OSX? Just add sav...