Skip to content

fix(perf): skip framework hook dispatch when the runner is undefined - #80

Merged
caomengxuan666 merged 1 commit into
masterfrom
pr/fix-79-startup-overhead
Sep 12, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
pr/fix-79-startup-overhead

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

What

run_framework_hook_runner executes a script that calls
niubash_run_<hook>_hooks. Those entry points are only defined after the
user 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 -c invocation paid for it.

Verified against the installed v1.0.1 binary, which is the reference:

$ niu -c 'declare -F niubash_run_zshexit_hooks >/dev/null 2>&1; echo $?'
1                # undefined — no rc sourced under -c

$ niu -c true          -> ~197 ms
$ niu -c <bogus name>  -> ~258 ms     # +55ms, the dead PATH scan

For contrast, -C does source the rc and the hooks really are defined there
(PC=[starship_precmd], starship_precmd resolvable), so -C does not
suffer from this. See the note below about -C timing.

Fix

Probe each runner once with the declare -F builtin and memoize the answer
per runner in a new framework_hook_probes map. Skip dispatch when the
runner 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 broke
niubashrc_framework_hooks_replace_host_source_lifecycle_hooks (NIU_FRAMEWORK_PRECMD
came back as "1" instead of "0").

Also: NIU_TRACE_STARTUP

This adds the diagnostic switch requested in #79. NIU_TRACE_STARTUP=1
prints cumulative and per-stage milliseconds to stderr for the -c, -C and
ShellInvocation paths plus every Shell::new stage. Silent by default,
stderr only, so the deterministic no-banner contract for niu -c and script
runs 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.1 binary (the honest baseline):

before after
niu -c true ~197 ms ~67 ms
niu -C true ~1500 ms ~1500 ms (unchanged)

-C is unchanged because ~1500ms there is legitimate work: the rc loads
15 real plugin packs (prompt-core, git, starship, path-tools, extract,
zoxide, fzf, thefuck, command-not-found, direnv, dotenv, kubectl, npm,
keybindings, env-sync), and starship runs eval "$(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 -> 260ms figure for
-C was measured against a stale developer checkout that sat two commits
behind 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 opened
and 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 niubash clean.

cargo test --workspace --locked reports failures, but I verified they are
pre-existing by stashing this branch's changes and re-running: identical
counts and identical test names.

  • niubash-runtime lib: 239 passed / 12 failed both before and after
  • host_contract: 34 passed / 4 failed both before and after (includes the
    known \x1e stdin-marker leak)

Follow-up

Lands in v1.1.0.

Closes #79

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
caomengxuan666 force-pushed the pr/fix-79-startup-overhead branch from bef54eb to 6fbee9f Compare September 12, 2026 04:39
@caomengxuan666
caomengxuan666 merged commit 0b37bec into master Sep 12, 2026
1 check failed
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>
@caomengxuan666
caomengxuan666 deleted the pr/fix-79-startup-overhead branch September 16, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant