rcaloras / bash-preexec

⚡ preexec and precmd functions for Bash just like Zsh.
MIT License
862 stars 94 forks source link

Reimplement the string-manipulation functions to write to variables instead of stdout #112

Closed dimo414 closed 3 years ago

dimo414 commented 3 years ago

This change allows us to avoid unnecessary command substitutions which are (relatively speaking) quite expensive. Although __bp_sanitize_string is only used during installation __bp_trim_whitespace is invoked in a loop in the critical path as part of __bp_preexec_invoke_exec. Eliminating these subshells will reduce bash-preexec's overhead.

See https://github.com/dimo414/prompt.gem/commit/9764cdb3 for another example of a similar change.

Note: the printf -v syntax was added to Bash in v3.1: https://mywiki.wooledge.org/BashFAQ/061

rcaloras commented 3 years ago

Wow! Playing with a bit locally timing my prompt and its looking like ~2x faster. @dimo414 thank you for this change :pray: :tada: Much appreciated!

dimo414 commented 3 years ago

Nice! That's even better than I expected. Thanks for the quick review!