[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.
This commit is contained in:
2 files changed
+85
-16
No files matched your search
@@ -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; }
|
||||
}
|
||||
' <<EOF
|
||||
${LAST_TWO}
|
||||
EOF
|
||||
)
|
||||
fi
|
||||
if [ -n "${NVM_CLASSIFIED}" ]; then
|
||||
if [ "_$2" = '_stable' ]; then
|
||||
STABLE="${NVM_CLASSIFIED#v}"
|
||||
else
|
||||
MOD="$(awk 'BEGIN { print int(ARGV[1] / 1000000) % 2 ; exit(0) }' "${NORMALIZED_VERSION}")"
|
||||
if [ "${MOD}" -eq 0 ]; then
|
||||
STABLE="${MINOR}"
|
||||
elif [ "${MOD}" -eq 1 ]; then
|
||||
UNSTABLE="${MINOR}"
|
||||
fi
|
||||
UNSTABLE="${NVM_CLASSIFIED#v}"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
if [ "_$2" = '_stable' ]; then
|
||||
nvm_echo "${STABLE}"
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@"; exit 1; }
|
||||
\. ../../../nvm.sh --no-use
|
||||
|
||||
nvm_ls() { printf '%s\n' "${VERSIONS}"; }
|
||||
nvm_ls_remote() { printf '%s\n' "${VERSIONS}"; }
|
||||
|
||||
VERSIONS='v0.2.3
|
||||
v0.3.4
|
||||
v0.4.6
|
||||
v0.5.7
|
||||
v1.0.0
|
||||
v1.1.0
|
||||
v20.9.1
|
||||
v20.9.2'
|
||||
for MODE in local remote; do
|
||||
[ "$(nvm_print_implicit_alias "${MODE}" stable)" = '20.9' ] || die "${MODE} stable"
|
||||
[ "$(nvm_print_implicit_alias "${MODE}" unstable)" = '0.5' ] || die "${MODE} unstable"
|
||||
done
|
||||
VERSIONS='v0.1.0
|
||||
v0.3.1'
|
||||
[ -z "$(nvm_print_implicit_alias local stable)" ] || die 'odd-only stable'
|
||||
[ "$(nvm_print_implicit_alias local unstable)" = '0.3' ] || die 'odd-only unstable'
|
||||
VERSIONS='v0.2.0
|
||||
v0.4.1'
|
||||
[ "$(nvm_print_implicit_alias local stable)" = '0.4' ] || die 'even-only stable'
|
||||
[ "$(nvm_print_implicit_alias local unstable)" = 'N/A' ] || die 'even-only unstable'
|
||||
VERSIONS=''
|
||||
[ -z "$(nvm_print_implicit_alias local stable)" ] || die 'empty stable'
|
||||
[ "$(nvm_print_implicit_alias local unstable)" = 'N/A' ] || die 'empty unstable'
|
||||
|
||||
# Count actual awk launches, without timing-dependent assertions or provider calls.
|
||||
COUNT_DIR=$(mktemp -d) || die 'temporary directory'
|
||||
trap 'rm -f "${COUNT_DIR}/awk" "${COUNT_DIR}/calls"; rmdir "${COUNT_DIR}"' EXIT HUP INT TERM
|
||||
REAL_AWK=$(command -v awk)
|
||||
export REAL_AWK COUNT_DIR
|
||||
printf '%s\n' '#!/bin/sh' 'echo called >> "${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"
|
||||
Reference in new issue
Block a user