colors.theme.bash: pre-compute colors

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
pull/699/head
Brian Malehorn 2016-04-09 19:41:13 -07:00
parent fd6c5e23c0
commit 2a3fde2b14
1 changed files with 76 additions and 76 deletions

View File

@ -182,89 +182,89 @@ function echo_color {
}
black="$(color reset black)"
red="$(color reset red)"
green="$(color reset green)"
yellow="$(color reset yellow)"
blue="$(color reset blue)"
purple="$(color reset magenta)"
cyan="$(color reset cyan)"
white="$(color reset white bold)"
orange="$(color reset red fg bright)"
black="\[\e[0;30m\]"
red="\[\e[0;31m\]"
green="\[\e[0;32m\]"
yellow="\[\e[0;33m\]"
blue="\[\e[0;34m\]"
purple="\[\e[0;35m\]"
cyan="\[\e[0;36m\]"
white="\[\e[0;37;1m\]"
orange="\[\e[0;91m\]"
bold_black="$(color black bold)"
bold_red="$(color red bold)"
bold_green="$(color green bold)"
bold_yellow="$(color yellow bold)"
bold_blue="$(color blue bold)"
bold_purple="$(color magenta bold)"
bold_cyan="$(color cyan bold)"
bold_white="$(color white bold)"
bold_orange="$(color red fg bright bold)"
bold_black="\[\e[30;1m\]"
bold_red="\[\e[31;1m\]"
bold_green="\[\e[32;1m\]"
bold_yellow="\[\e[33;1m\]"
bold_blue="\[\e[34;1m\]"
bold_purple="\[\e[35;1m\]"
bold_cyan="\[\e[36;1m\]"
bold_white="\[\e[37;1m\]"
bold_orange="\[\e[91;1m\]"
underline_black="$(color black underline)"
underline_red="$(color red underline)"
underline_green="$(color green underline)"
underline_yellow="$(color yellow underline)"
underline_blue="$(color blue underline)"
underline_purple="$(color magenta underline)"
underline_cyan="$(color cyan underline)"
underline_white="$(color white underline)"
underline_orange="$(color red fg bright underline)"
underline_black="\[\e[30;4m\]"
underline_red="\[\e[31;4m\]"
underline_green="\[\e[32;4m\]"
underline_yellow="\[\e[33;4m\]"
underline_blue="\[\e[34;4m\]"
underline_purple="\[\e[35;4m\]"
underline_cyan="\[\e[36;4m\]"
underline_white="\[\e[37;4m\]"
underline_orange="\[\e[91;4m\]"
background_black="$(color black bg)"
background_red="$(color red bg)"
background_green="$(color green bg)"
background_yellow="$(color yellow bg)"
background_blue="$(color blue bg)"
background_purple="$(color magenta bg)"
background_cyan="$(color cyan bg)"
background_white="$(color white bg bold)"
background_orange="$(color red bg bright)"
background_black="\[\e[40m\]"
background_red="\[\e[41m\]"
background_green="\[\e[42m\]"
background_yellow="\[\e[43m\]"
background_blue="\[\e[44m\]"
background_purple="\[\e[45m\]"
background_cyan="\[\e[46m\]"
background_white="\[\e[47;1m\]"
background_orange="\[\e[101m\]"
normal="$(color reset)"
reset_color="$(__make_ansi '' 39)"
normal="\[\e[0m\]"
reset_color="\[\e[39m\]"
# These colors are meant to be used with `echo -e`
echo_black="$(echo_color reset black)"
echo_red="$(echo_color reset red)"
echo_green="$(echo_color reset green)"
echo_yellow="$(echo_color reset yellow)"
echo_blue="$(echo_color reset blue)"
echo_purple="$(echo_color reset magenta)"
echo_cyan="$(echo_color reset cyan)"
echo_white="$(echo_color reset white bold)"
echo_orange="$(echo_color reset red fg bright)"
echo_black="\033[0;30m"
echo_red="\033[0;31m"
echo_green="\033[0;32m"
echo_yellow="\033[0;33m"
echo_blue="\033[0;34m"
echo_purple="\033[0;35m"
echo_cyan="\033[0;36m"
echo_white="\033[0;37;1m"
echo_orange="\033[0;91m"
echo_bold_black="$(echo_color black bold)"
echo_bold_red="$(echo_color red bold)"
echo_bold_green="$(echo_color green bold)"
echo_bold_yellow="$(echo_color yellow bold)"
echo_bold_blue="$(echo_color blue bold)"
echo_bold_purple="$(echo_color magenta bold)"
echo_bold_cyan="$(echo_color cyan bold)"
echo_bold_white="$(echo_color white bold)"
echo_bold_orange="$(echo_color red fg bright bold)"
echo_bold_black="\033[30;1m"
echo_bold_red="\033[31;1m"
echo_bold_green="\033[32;1m"
echo_bold_yellow="\033[33;1m"
echo_bold_blue="\033[34;1m"
echo_bold_purple="\033[35;1m"
echo_bold_cyan="\033[36;1m"
echo_bold_white="\033[37;1m"
echo_bold_orange="\033[91;1m"
echo_underline_black="$(echo_color black underline)"
echo_underline_red="$(echo_color red underline)"
echo_underline_green="$(echo_color green underline)"
echo_underline_yellow="$(echo_color yellow underline)"
echo_underline_blue="$(echo_color blue underline)"
echo_underline_purple="$(echo_color magenta underline)"
echo_underline_cyan="$(echo_color cyan underline)"
echo_underline_white="$(echo_color white underline)"
echo_underline_orange="$(echo_color red fg bright underline)"
echo_underline_black="\033[30;4m"
echo_underline_red="\033[31;4m"
echo_underline_green="\033[32;4m"
echo_underline_yellow="\033[33;4m"
echo_underline_blue="\033[34;4m"
echo_underline_purple="\033[35;4m"
echo_underline_cyan="\033[36;4m"
echo_underline_white="\033[37;4m"
echo_underline_orange="\033[91;4m"
echo_background_black="$(echo_color black bg)"
echo_background_red="$(echo_color red bg)"
echo_background_green="$(echo_color green bg)"
echo_background_yellow="$(echo_color yellow bg)"
echo_background_blue="$(echo_color blue bg)"
echo_background_purple="$(echo_color magenta bg)"
echo_background_cyan="$(echo_color cyan bg)"
echo_background_white="$(echo_color white bg bold)"
echo_background_orange="$(echo_color red bg bright)"
echo_background_black="\033[40m"
echo_background_red="\033[41m"
echo_background_green="\033[42m"
echo_background_yellow="\033[43m"
echo_background_blue="\033[44m"
echo_background_purple="\033[45m"
echo_background_cyan="\033[46m"
echo_background_white="\033[47;1m"
echo_background_orange="\033[101m"
echo_normal="$(echo_color reset)"
echo_reset_color="$(__make_echo '' 39)"
echo_normal="\033[0m"
echo_reset_color="\033[39m"