Repository navigation
fix(perf): skip framework hook dispatch when the runner is undefined - #80
Merged
Merged
Conversation
This was referenced Sep 12, 2026
niu -c paid ~68ms and niu -C paid ~480ms per invocation dispatching niubash_run_*_hooks entry points that do not exist. Without a sourced rc (niu -c, script files, piped stdin) or when bundle discovery misses, run_framework_hook_runner executed a script calling a name rubash could not resolve as a function, so it fell through to a full PATH/command-link scan of ~70ms per missing command. Probe each runner once with the declare -F builtin and memoize the answer per runner; the probe's exit code is saved and restored so it does not leak into $? / NIU_LAST_EXIT_CODE. Also add NIU_TRACE_STARTUP=1, the opt-in per-stage startup profiler requested in #79: cumulative and per-stage milliseconds on stderr, silent by default so niu -c and script runs stay deterministic and banner-free. Measured on Windows 11, release build: niu -c true 98ms -> 23ms in-process (260ms -> 71ms wall); niu -C true 588ms -> 131ms in-process (1700ms -> 260ms wall).
caomengxuan666
force-pushed
the
pr/fix-79-startup-overhead
branch
from
September 12, 2026 04:39
bef54eb to
6fbee9f
Compare
caomengxuan666
added a commit
that referenced
this pull request
Sep 12, 2026
Version bump 1.0.1 → 1.1.0 with CHANGELOG. Highlights since v1.0.1: - IDE-style completion menu with descriptions - Colorized plugin CLI (TTY-gated ANSI) - NIU_ENV/BASH_ENV opt-in env files for one-shot mode (#82) - Startup overhead fix: skip framework hook dispatch (#80) - Release binary size -35% (thin LTO + panic=abort) - Upstream rubash v1.1.0: CTLESC \x11 leak fix (NIU_BINDKEYS), heredoc warning diagnostics, warning suppression - Hook runners survive user set -eu - REPL heredoc-body scanning fix - $BASH set to running executable path - WinuxCmd portable first-run auto-activation Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.
What
run_framework_hook_runnerexecutes a script that callsniubash_run_<hook>_hooks. Those entry points are only defined after theuser rc has sourced the oh-my-niu bundle.
In non-rc modes that never happens —
niu -c, script files, piped stdin —yet niubash still dispatched them. When rubash cannot resolve the name as a
function it falls through to a full PATH / command-link scan, which costs
~55-70ms per missing command. There is one such dispatch on every exit trap,
so every
niu -cinvocation paid for it.Verified against the installed
v1.0.1binary, which is the reference:For contrast,
-Cdoes source the rc and the hooks really are defined there(
PC=[starship_precmd],starship_precmdresolvable), so-Cdoes notsuffer from this. See the note below about
-Ctiming.Fix
Probe each runner once with the
declare -Fbuiltin and memoize the answerper runner in a new
framework_hook_probesmap. Skip dispatch when therunner is not defined.
The probe's exit code is saved and restored so it does not leak into
$?/NIU_LAST_EXIT_CODE— the first attempt missed this and brokeniubashrc_framework_hooks_replace_host_source_lifecycle_hooks(NIU_FRAMEWORK_PRECMDcame back as
"1"instead of"0").Also: NIU_TRACE_STARTUP
This adds the diagnostic switch requested in #79.
NIU_TRACE_STARTUP=1prints cumulative and per-stage milliseconds to stderr for the
-c,-CandShellInvocationpaths plus everyShell::newstage. Silent by default,stderr only, so the deterministic no-banner contract for
niu -cand scriptruns is preserved.
This is what located the problem. It also ruled out the issue's guesses:
winuxcmd selection is 0.4ms, and zero plugins load under
-c.Numbers
Windows 11, release build, this machine.
Installed
v1.0.1binary (the honest baseline):niu -c trueniu -C true-Cis unchanged because ~1500ms there is legitimate work: the rc loads15 real plugin packs (prompt-core, git, starship, path-tools, extract,
zoxide, fzf, thefuck, command-not-found, direnv, dotenv, kubectl, npm,
keybindings, env-sync), and
starshiprunseval "$(starship init bash)",which spawns starship. That is not dead time and should not be optimized
away here.
Correction to my first revision: the earlier
1700ms -> 260msfigure for-Cwas measured against a stale developer checkout that sat two commitsbehind the shipped binary and therefore never loaded the bundle. It was dead
dispatch time in that broken state, not a defect in
-C. I also openedand have since closed #81, which incorrectly reported that the rc could not
find the app-installed bundle — niubash does export
NIU_APP_BUNDLE_PATH;my checkout simply did not yet contain that commit.
Tests
cargo fmt --check -p niubashclean.cargo test --workspace --lockedreports failures, but I verified they arepre-existing by stashing this branch's changes and re-running: identical
counts and identical test names.
niubash-runtimelib: 239 passed / 12 failed both before and afterhost_contract: 34 passed / 4 failed both before and after (includes theknown
\x1estdin-marker leak)Follow-up
underlying cost. Worth caching misses — see Missing external command resolution costs ~60-85ms; consider caching negative lookups rubash#71.
Lands in v1.1.0.
Closes #79