[Perf] `nvm use`: skip re-resolving an exact, installed version
Before adoption: this commit came in with the history the project adopted, so no landing records it.
When `nvm use` is given an exact `vX.Y.Z` that is already installed, and no alias with that exact name exists, use it directly instead of re-resolving it through `nvm_match_version` and `nvm_ensure_version_installed`, both of which call `nvm_version`. `nvm_auto` takes this path on every shell startup with an already-resolved version, so this removes ~95ms from each new shell (~330ms -> ~235ms in a clean environment on macOS/zsh). Fixes #3930
authored by Hridhin Varkey Joseph · committed by Jordan Harband · parent 077156e · Browse files · 3 files·+114 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+114 added−4 removed
- tests:+89 added−0 removed
- other:+25 added−4 removed
Provenance
- Commit
f1f3d46- Tree
f0c8937- Author
Hridhin Varkey Joseph <hridhinvarkeyjoseph@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
077156e
3 files changed+114 added−4 removed
added:test/fast/Unit tests/nvm use skips re-resolving an exact installed version+31 added
1+#!/bin/sh2+3+cleanup () {4+ rm -rf "$NVM_DIR"5+ unset -f die cleanup nvm_version6+}7+die () { echo "$@" ; cleanup ; exit 1; }8+9+: nvm.sh10+\. ../../../nvm.sh11+12+NVM_DIR="$(mktemp -d)"13+NODE_BIN="$NVM_DIR/versions/node/v12.0.0/bin"14+mkdir -p "$NODE_BIN"15+printf '#!/bin/sh\necho v12.0.0\n' > "$NODE_BIN/node"16+chmod +x "$NODE_BIN/node"17+18+# the slow path resolves through nvm_version; make it fail so only the fast path can succeed19+nvm_version() {20+ return 4221+}22+23+nvm use --silent v12.0.0 || die 'nvm use with an exact, installed version should not need nvm_version'24+case ":${PATH}:" in25+ *":${NODE_BIN}:"*) ;;26+ *) die "nvm use did not put ${NODE_BIN} on PATH; got ${PATH}" ;;27+esac28+29+! nvm use --silent v12 2>/dev/null || die 'nvm use with a partial version should still resolve it through nvm_version'30+31+cleanupadded:test/fast/Unit tests/nvm_is_installed_exact_version+58 added
1+#!/bin/sh2+3+cleanup () {4+ rm -rf "$NVM_DIR"5+ unset -f die cleanup6+ unset NVM_DIR NODE_PATH VERSION7+}8+die () { echo "$@" ; cleanup ; exit 1; }9+10+: nvm.sh11+\. ../../../nvm.sh12+13+set -ex14+15+NVM_DIR=$(mktemp -d)16+NODE_PATH="$NVM_DIR/versions/node"17+mkdir -p "$NODE_PATH"18+if [ -z "$NODE_PATH" ]; then19+ die 'Unable to create temporary folder'20+fi21+22+# nvm_is_installed_exact_version is available23+type nvm_is_installed_exact_version > /dev/null 2>&1 || die 'nvm_is_installed_exact_version is not available'24+25+# nvm_is_installed_exact_version with no parameter fails26+! nvm_is_installed_exact_version || die 'nvm_is_installed_exact_version without parameter should fail'27+28+# an exact version that is not installed fails29+! nvm_is_installed_exact_version 'v12.0.0' || die 'v12.0.0 is not installed and should fail'30+31+# an exact version whose node binary is not executable fails32+mkdir -p "$NODE_PATH/v12.0.0/bin"33+touch "$NODE_PATH/v12.0.0/bin/node"34+! nvm_is_installed_exact_version 'v12.0.0' || die 'v12.0.0 with a non-executable node should fail'35+36+# an exact, installed version succeeds37+chmod +x "$NODE_PATH/v12.0.0/bin/node"38+nvm_is_installed_exact_version 'v12.0.0' || die 'v12.0.0 is installed and should succeed'39+40+# something shaped almost like vX.Y.Z fails, even when a directory by that name is installed41+for VERSION in 'v12.0.0.0' 'v12.0.0.' 'v12..0' 'v.12.0' 'v12.0'; do42+ mkdir -p "$NODE_PATH/$VERSION/bin"43+ touch "$NODE_PATH/$VERSION/bin/node"44+ chmod +x "$NODE_PATH/$VERSION/bin/node"45+ ! nvm_is_installed_exact_version "$VERSION" || die "$VERSION is not an exact version and should fail, even when installed"46+done47+48+# anything that is not an exact vX.Y.Z fails, even if it would resolve to an installed version49+for VERSION in '12.0.0' '12' 'v12' 'v12.0' 'v12.0.0-rc.1' 'node' 'stable' 'lts/*' 'iojs-v1.0.0' 'system' 'default'; do50+ ! nvm_is_installed_exact_version "$VERSION" || die "$VERSION is not an exact version and should fail"51+done52+53+# an alias with the exact same name takes precedence, so the fast path must not apply54+mkdir -p "$(nvm_alias_path)"55+echo 'v12.0.0' > "$(nvm_alias_path)/v12.0.0"56+! nvm_is_installed_exact_version 'v12.0.0' || die 'an alias named v12.0.0 should take precedence'57+58+cleanupmodified:nvm.sh+25 added-4 removed
210 unmodified lines211 nvm_has_system_iojs() {212 [ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ]213 }214 215+nvm_is_installed_exact_version() {216+ case "${1-}" in217+ v*) ;;218+ *) return 1 ;;219+ esac220+ case "${1#v}" in221+ *[!0-9.]* | .* | *. | *..* | *.*.*.*) return 1 ;;222+ *.*.*) ;;223+ *) return 1 ;;224+ esac225+ # an alias with this exact name takes precedence, as it does in `nvm_version`226+ if [ -e "$(nvm_alias_path)/${1}" ]; then227+ return 1228+ fi229+ nvm_is_version_installed "${1}"230+}231+232 nvm_is_version_installed() {233 if [ -z "${1-}" ]; then234 return 1235 fi4316 unmodified lines4552 local IS_VERSION_FROM_NVMRC4553 IS_VERSION_FROM_NVMRC=04554 local NVM_WRITE_TO_NVMRC4555 NVM_WRITE_TO_NVMRC=04556+ local NVM_USE_EXACT_INSTALLED4557+ NVM_USE_EXACT_INSTALLED=04558 4559 while [ $# -ne 0 ]; do4560 case "$1" in4561 --silent)32 unmodified lines4594 if [ -z "${VERSION}" ]; then4595 nvm_err 'Please see `nvm --help` or https://github.com/nvm-sh/nvm#nvmrc for more information.'4596 return 1274597 fi4598+ elif nvm_is_installed_exact_version "${PROVIDED_VERSION}"; then4599+ VERSION="${PROVIDED_VERSION}"4600+ NVM_USE_EXACT_INSTALLED=14601 else4602 VERSION="$(nvm_match_version "${PROVIDED_VERSION}")"4603 fi4604 33 unmodified lines4638 if [ "${NVM_SILENT:-0}" -ne 1 ]; then4639 nvm_ensure_version_installed "${PROVIDED_VERSION}" "${IS_VERSION_FROM_NVMRC}"4640 fi4641 return 34620- # This nvm_ensure_version_installed call can be a performance bottleneck4621- # on shell startup. Perhaps we can optimize it away or make it faster.4622- elif ! nvm_ensure_version_installed "${VERSION}" "${IS_VERSION_FROM_NVMRC}"; then4642+ # an exact, already-installed version needs no further resolution or checks4643+ elif [ "${NVM_USE_EXACT_INSTALLED}" -ne 1 ] && ! nvm_ensure_version_installed "${VERSION}" "${IS_VERSION_FROM_NVMRC}"; then4644 return $?4645 fi4646 4647 local NVM_VERSION_DIR613 unmodified lines5261 nvm_auto nvm_supports_xz \5262 nvm_echo nvm_err nvm_grep nvm_cd \5263 nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \5264 nvm_has_solaris_binary nvm_is_merged_node_version \5244- nvm_is_natural_num nvm_is_version_installed nvm_validate_install \5265+ nvm_is_natural_num nvm_is_installed_exact_version nvm_is_version_installed nvm_validate_install \5266 nvm_install_lock_name nvm_acquire_install_lock nvm_release_install_lock \5267 nvm_list_aliases nvm_make_alias nvm_print_alias_file nvm_print_alias_path \5268 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \5269 nvm_sanitize_path nvm_has_colors nvm_has_italics nvm_process_parameters \183 unmodified lines