-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
colors.theme.bash: pre-compute colors, startup 3x faster #699
Conversation
bash-it takes rather a while to startup, around 0.5 seconds on my laptop. After a bit of timing it appears the majority of the time is spent in themes/colors.theme.bash. The reason for that is the fancy abstraction layers that use $(). For example, consider this code: red="$(color reset red)" It will go through 9 forkexecs to evaluate: red="$(color reset red)" "$(__color_parse make_ansi reset red)" "$(__make_ansi reset red)" "\[\e[$(__reset red)m\]" "\[\e[0;$(__red)m\]" "\[\e[0;$(__color red)m\]" "\[\e[0;$(__color_normal_fg $(__color_red))m\]" "\[\e[0;$(__color_normal_fg 1)m\]" "\[\e[0;31m\]" With all the variables in colors.theme.bash, this adds up to hundreds of forks: $ strace -f bash ./colors.theme.bash 2>&1 | grep clone | wc -l 649 The solution is to replace the function with its result: -red="$(color reset red)" +red='\[\e[0;31m\]' This is safe, since colors.theme.bash never calls external functions or takes any input. So, its result can be safely hard-coded. This improves startup time dramatically. Try adding "time" to your .bashrc: # Load Bash It time source $BASH_IT/bash_it.sh before: real 0m0.462s user 0m0.100s sys 0m0.399s after: real 0m0.150s user 0m0.091s sys 0m0.064s
This is great - thanks! Depending on the number of plugins that the user has enabled, the startup will still take a long time, but this is a great step to shave off a couple of seconds. |
No problem, I like making things faster. FWIW, most of the remaining time is split between
alias-completion.plugin.bash:57 is the culprit for me, causing a fork() per alias, and I have about 76 aliases. The real problem is that bash makes it difficult to get the output of bash builtins / functions without using |
Great! Thanks for this big improvement! |
If you have a suggestion for improving the alias completion plugin, feel free to provide a PR for it! I simply copied the code from a StackOverflow answer, and it worked fine. I must confess that I didn't look into timing or anything like that. The functionality that it provides easily makes up for the time it consumes during opening a new shell - at least for me... |
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts commit 2a3fde2 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer?
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts commit 2a3fde2 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer?
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts commit 2a3fde2 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer?
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts commit 2a3fde2 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer?
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts commit 2a3fde2 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer?
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts Bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in Bash-it#699; I'm just finishing the job.
This reverts commit 2a3fde2 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer?
bash-it takes rather a while to startup, around 0.5 seconds on my
laptop. After a bit of timing it appears the majority of the time is
spent in themes/colors.theme.bash. The reason for that is the fancy
abstraction layers that use $(). For example, consider this code:
It will go through 9 forkexecs to evaluate:
With all the variables in colors.theme.bash, this adds up to hundreds of
forks:
The solution is to replace the function with its result:
This is safe, since colors.theme.bash never calls external functions or
takes any input. So, its result can be safely hard-coded.
This improves startup time dramatically. Try adding "time" to your .bashrc:
before:
after: