From 17270cd532cc21dd2c84aae387a30588e386f700 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 3 Nov 2026 14:42:50 -0600 Subject: [PATCH] [Perf] `nvm_print_implicit_alias`: classify implicit aliases in one awk pass To resolve `stable` and `unstable`, `nvm_print_implicit_alias` started an awk process per installed minor version, plus another per pre-1.0 one to check its parity. Classify the whole list in a single awk pass instead, and skip it when there is nothing to classify. The results are unchanged. Refs #3416. Refs #3811. --- nvm.sh | 45 +++++++++------ .../nvm_print_implicit_alias classification | 56 +++++++++++++++++++ 2 files changed, 85 insertions(+), 16 deletions(-) create mode 100755 test/fast/Unit tests/nvm_print_implicit_alias classification diff --git a/nvm.sh b/nvm.sh index 54a0d376..6af2eb1f 100755 --- a/nvm.sh +++ b/nvm.sh @@ -3292,28 +3292,41 @@ nvm_print_implicit_alias() { LAST_TWO=$($NVM_COMMAND | nvm_grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq) ;; esac - local MINOR local STABLE STABLE='' local UNSTABLE UNSTABLE='' - local MOD - local NORMALIZED_VERSION - - nvm_is_zsh && setopt local_options shwordsplit - for MINOR in $LAST_TWO; do - NORMALIZED_VERSION="$(nvm_normalize_version "$MINOR")" - if [ "_0${NORMALIZED_VERSION#?}" != "_$NORMALIZED_VERSION" ]; then - STABLE="$MINOR" + local NVM_CLASSIFIED + NVM_CLASSIFIED='' + if [ -n "${LAST_TWO}" ]; then + NVM_CLASSIFIED=$(command awk -v requested="$2" ' + NF { + split($0, parts, /\./); + normalized = sprintf("%d%06d%06d", parts[1], parts[2], parts[3]); + if (substr(normalized, 1, 1) != "0") { + stable = $0; + } else { + parity = int(normalized / 1000000) % 2; + if (parity == 0) { stable = $0; } + else if (parity == 1) { unstable = $0; } + } + } + END { + selected = requested == "stable" ? stable : unstable; + if (selected != "") { print "v" selected; } + } + ' <> "${COUNT_DIR}/calls"' 'exec "${REAL_AWK}" "$@"' > "${COUNT_DIR}/awk" +chmod +x "${COUNT_DIR}/awk" +VERSIONS='v1.0.1 +v1.1.1 +v1.2.1 +v1.3.1 +v1.4.1 +v1.5.1' +RESULT=$(PATH="${COUNT_DIR}:${PATH}" nvm_print_implicit_alias local stable) +[ "${RESULT}" = '1.5' ] || die 'latest minor classification' +CALLS=$(wc -l < "${COUNT_DIR}/calls") +[ "${CALLS}" -le 1 ] || die "classification launched ${CALLS} awk processes for six minors" + +VERSIONS='' +: > "${COUNT_DIR}/calls" +RESULT=$(PATH="${COUNT_DIR}:${PATH}" nvm_print_implicit_alias local unstable) +[ "${RESULT}" = 'N/A' ] || die 'empty unstable classification' +CALLS=$(wc -l < "${COUNT_DIR}/calls") +[ "${CALLS}" -eq 0 ] || die "empty classification launched ${CALLS} awk processes"