filesystem: skip hostname exec when HOSTNAME is already set#6053
Open
dand-oss wants to merge 1 commit intomsys2:masterfrom
Open
filesystem: skip hostname exec when HOSTNAME is already set#6053dand-oss wants to merge 1 commit intomsys2:masterfrom
dand-oss wants to merge 1 commit intomsys2:masterfrom
Conversation
Bash, zsh, and other shells set HOSTNAME as a builtin variable at
startup. The explicit `exec /usr/bin/hostname` in /etc/profile is
redundant and costs ~127ms per login on Windows due to Cygwin's
expensive fork/exec. Guard the assignment with `[ -z "${HOSTNAME}" ]`
so it only runs when HOSTNAME is not already set.
This is consistent with the existing guards in profile.d scripts
(lang.sh guards LC_ALL, tzset.sh guards TZ, 000-msys2.sh guards
__MSYS2_WINDOWS_VERSION_WARNING_DONE).
Member
|
Sounds reasonable, thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Guard the
HOSTNAME="$(exec /usr/bin/hostname)"assignments in/etc/profilewith[ -z "${HOSTNAME}" ]so the redundant fork/exec is skipped.Problem
Bash sets
$HOSTNAMEas a builtin shell variable at startup — before/etc/profileruns. The explicitexec /usr/bin/hostnamein/etc/profileunconditionally overwrites it with the same value, making the call 100% redundant for bash users.This matters because on MSYS2/Cygwin, every fork+exec costs ~50-130ms due to Windows lacking copy-on-write fork. The
hostnamecall alone adds ~127ms to every login shell startup — for a value bash already has.Demonstration that the builtin is identical to the external command:
Fix
Add
[ -z "${HOSTNAME}" ]guard before each HOSTNAME assignment. This is consistent with the existing pattern used by other profile.d scripts:profile.lang.shguards withtest -z "${LC_ALL}"profile.tzset.shguards withtest -z "$TZ"profile.000-msys2.shguards with__MSYS2_WINDOWS_VERSION_WARNING_DONEThe guard also benefits ksh/zsh/posh users who pre-set HOSTNAME via their environment, while preserving the fallback for shells that don't set it as a builtin.
Impact
Saves ~127ms per login shell startup on Windows — a ~25% reduction in
/etc/profileexecution time.