summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGautham Warrier <gautham.r.w@gmail.com>2021-01-23 15:55:41 +0530
committerGitHub <noreply@github.com>2021-01-23 11:25:41 +0100
commit5eacd3abee879ad62dc8abdca6ad54f17d403658 (patch)
tree095ce23baf4904e046b987980b88fdbc4766ab1c
parentbcaf83532117c49e0f51864f59352630be1d0f90 (diff)
perf: use built-in EPOCHREALTIME env-var to compute cmd_duration for ZSH5+ (#1751)
We currently invoke `starship time` in a sub-shell to compute time, which is non-performant. By using $EPOCHREALTIME, which is an inbuilt env-var in ZSH5, we can get better performance. This commit only targets ZSH5+ and maintains the old behaviour.
-rw-r--r--src/init/starship.zsh23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/init/starship.zsh b/src/init/starship.zsh
index 07afe7cc1..f0140fa04 100644
--- a/src/init/starship.zsh
+++ b/src/init/starship.zsh
@@ -16,6 +16,20 @@ starship_render() {
PROMPT="$(::STARSHIP:: prompt --keymap="${KEYMAP-}" --status=$STARSHIP_CMD_STATUS --cmd-duration=${STARSHIP_DURATION-} --jobs="$NUM_JOBS")"
}
+# Defines a function `__starship_get_time` that sets the time since epoch in millis in STARSHIP_CAPTURED_TIME.
+if [[ $ZSH_VERSION == ([1-4]*) ]]; then
+ # ZSH <= 5; Does not have a built-in variable so we will rely on Starship's inbuilt time function.
+ __starship_get_time() {
+ STARSHIP_CAPTURED_TIME=$(::STARSHIP:: time)
+ }
+else
+ zmodload zsh/datetime
+ zmodload zsh/mathfunc
+ __starship_get_time() {
+ ((STARSHIP_CAPTURED_TIME = int(rint($EPOCHREALTIME * 1000))))
+ }
+fi
+
# Will be run before every prompt draw
starship_precmd() {
# Save the status, because commands in this pipeline will change $?
@@ -24,8 +38,8 @@ starship_precmd() {
# Compute cmd_duration, if we have a time to consume, otherwise clear the
# previous duration
if [[ -n "${STARSHIP_START_TIME+1}" ]]; then
- STARSHIP_END_TIME=$(::STARSHIP:: time)
- STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
+ __starship_get_time && STARSHIP_END_TIME=$STARSHIP_CAPTURED_TIME
+ ((STARSHIP_DURATION = STARSHIP_END_TIME - STARSHIP_START_TIME))
unset STARSHIP_START_TIME
else
unset STARSHIP_DURATION
@@ -35,7 +49,7 @@ starship_precmd() {
starship_render
}
starship_preexec() {
- STARSHIP_START_TIME=$(::STARSHIP:: time)
+ __starship_get_time && STARSHIP_START_TIME=$STARSHIP_CAPTURED_TIME
}
# If precmd/preexec arrays are not already set, set them. If we don't do this,
@@ -74,7 +88,8 @@ else
zle -N zle-keymap-select starship_zle-keymap-select-wrapped;
fi
-STARSHIP_START_TIME=$(::STARSHIP:: time)
+__starship_get_time && STARSHIP_START_TIME=$STARSHIP_CAPTURED_TIME
+
export STARSHIP_SHELL="zsh"
# Set up the session key that will be used to store logs