From fefc6b4aff14de77e556afae5be2d0d13c108a64 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Wed, 10 Apr 2024 19:27:36 +0900 Subject: [PATCH] feat(bash/blesh): use _ble_exec_time_ata for duration even in bash < 5 Bash < 5.0 doesn't support the high-resolution clock EPOCHREALTIME, so ble.sh uses the builtin command `time` to measure the execution times in Bash < 5.0. This has a lower resolution of milliseconds but still more accurate than Atuin's measurent because Atuin's measurement includes the spawn cost of Atuin, which is typically larger than a millisecond. Reported-by: Georgios Vlassis Reference: https://github.com/atuinsh/atuin/pull/1484#issuecomment-2047068582 --- atuin/src/shell/atuin.bash | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/atuin/src/shell/atuin.bash b/atuin/src/shell/atuin.bash index 1266b8d8fab..885729b0200 100644 --- a/atuin/src/shell/atuin.bash +++ b/atuin/src/shell/atuin.bash @@ -56,29 +56,30 @@ __atuin_precmd() { fi local duration="" - if ((BASH_VERSINFO[0] >= 5)); then - # We use the high-resolution duration based on EPOCHREALTIME (bash >= - # 5.0) if available. - # shellcheck disable=SC2154,SC2309 - if [[ ${BLE_ATTACHED-} && ${_ble_exec_time_ata-} ]]; then - # With ble.sh, we utilize the shell variable `_ble_exec_time_ata` - # recorded by ble.sh. - duration=${_ble_exec_time_ata}000 + # shellcheck disable=SC2154,SC2309 + if [[ ${BLE_ATTACHED-} && ${_ble_exec_time_ata-} ]]; then + # With ble.sh, we utilize the shell variable `_ble_exec_time_ata` + # recorded by ble.sh. It is more accurate than the measurements by + # Atuin, which includes the spawn cost of Atuin. ble.sh uses the + # special shell variable `EPOCHREALTIME` in bash >= 5.0 with the + # microsecond resolution, or the builtin `time` in bash < 5.0 with the + # millisecond resolution. + duration=${_ble_exec_time_ata}000 + elif ((BASH_VERSINFO[0] >= 5)); then + # We calculate the high-resolution duration based on EPOCHREALTIME + # (bash >= 5.0) recorded by precmd/preexec, though it might not be as + # accurate as `_ble_exec_time_ata` provided by ble.sh because it + # includes the extra time of the precmd/preexec handling. Since Bash + # does not offer floating-point arithmetic, we remove the non-digit + # characters and perform the integral arithmetic. The fraction part of + # EPOCHREALTIME is fixed to have 6 digits in Bash. We remove all the + # non-digit characters because the decimal point is not necessarily a + # period depending on the locale. + duration=$((${__atuin_precmd_time//[!0-9]} - ${__atuin_preexec_time//[!0-9]})) + if ((duration >= 0)); then + duration=${duration}000 else - # With bash-preexec, we calculate the time duration here, though it - # might not be as accurate as `_ble_exec_time_ata` because it also - # includes the time for precmd/preexec handling. Bash does not - # allow floating-point arithmetic, so we remove the non-digit - # characters and perform the integral arithmetic. The fraction - # part of EPOCHREALTIME is fixed to have 6 digits in Bash. We - # remove all the non-digit characters because the decimal point is - # not necessarily a period depending on the locale. - duration=$((${__atuin_precmd_time//[!0-9]} - ${__atuin_preexec_time//[!0-9]})) - if ((duration >= 0)); then - duration=${duration}000 - else - duration="" # clear the result on overflow - fi + duration="" # clear the result on overflow fi fi