Skip to content
Myr

[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

3 files+114 added−4 removed

added:test/fast/Unit tests/nvm use skips re-resolving an exact installed version+31 added
1#!/bin/sh23cleanup () {4 rm -rf "$NVM_DIR"5 unset -f die cleanup nvm_version6}7die () { echo "$@" ; cleanup ; exit 1; }89: nvm.sh10\. ../../../nvm.sh1112NVM_DIR="$(mktemp -d)"13NODE_BIN="$NVM_DIR/versions/node/v12.0.0/bin"14mkdir -p "$NODE_BIN"15printf '#!/bin/sh\necho v12.0.0\n' > "$NODE_BIN/node"16chmod +x "$NODE_BIN/node"1718# the slow path resolves through nvm_version; make it fail so only the fast path can succeed19nvm_version() {20 return 4221}2223nvm use --silent v12.0.0 || die 'nvm use with an exact, installed version should not need nvm_version'24case ":${PATH}:" in25 *":${NODE_BIN}:"*) ;;26 *) die "nvm use did not put ${NODE_BIN} on PATH; got ${PATH}" ;;27esac2829! nvm use --silent v12 2>/dev/null || die 'nvm use with a partial version should still resolve it through nvm_version'3031cleanup
added:test/fast/Unit tests/nvm_is_installed_exact_version+58 added
1#!/bin/sh23cleanup () {4 rm -rf "$NVM_DIR"5 unset -f die cleanup6 unset NVM_DIR NODE_PATH VERSION7}8die () { echo "$@" ; cleanup ; exit 1; }910: nvm.sh11\. ../../../nvm.sh1213set -ex1415NVM_DIR=$(mktemp -d)16NODE_PATH="$NVM_DIR/versions/node"17mkdir -p "$NODE_PATH"18if [ -z "$NODE_PATH" ]; then19 die 'Unable to create temporary folder'20fi2122# nvm_is_installed_exact_version is available23type nvm_is_installed_exact_version > /dev/null 2>&1 || die 'nvm_is_installed_exact_version is not available'2425# nvm_is_installed_exact_version with no parameter fails26! nvm_is_installed_exact_version || die 'nvm_is_installed_exact_version without parameter should fail'2728# 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'3031# an exact version whose node binary is not executable fails32mkdir -p "$NODE_PATH/v12.0.0/bin"33touch "$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'3536# an exact, installed version succeeds37chmod +x "$NODE_PATH/v12.0.0/bin/node"38nvm_is_installed_exact_version 'v12.0.0' || die 'v12.0.0 is installed and should succeed'3940# something shaped almost like vX.Y.Z fails, even when a directory by that name is installed41for 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"46done4748# anything that is not an exact vX.Y.Z fails, even if it would resolve to an installed version49for 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"51done5253# an alias with the exact same name takes precedence, so the fast path must not apply54mkdir -p "$(nvm_alias_path)"55echo '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'5758cleanup
modified:nvm.sh+25 added-4 removed
210 unmodified lines
211nvm_has_system_iojs() {212 [ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ]213}214215nvm_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}231232nvm_is_version_installed() {233 if [ -z "${1-}" ]; then234 return 1235 fi
4316 unmodified lines
4552 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=045584559 while [ $# -ne 0 ]; do4560 case "$1" in4561 --silent)
32 unmodified lines
4594 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 lines
4638 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 fi46464647 local NVM_VERSION_DIR
613 unmodified lines
5261 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