Repository navigation
Description
Summary
Resolving a command name that does not exist costs roughly 60-85ms in
Executor::execute_ast on Windows. That is two orders of magnitude more than
resolving a real builtin, and it becomes the dominant fixed cost of a shell
process when the same missing name is looked up repeatedly.
Reproduction
Build niubash (which embeds rubash) in release mode, then:
time niu -c true
time niu -c 'definitely_not_a_real_command_xyz'On my machine (Windows 11, Ryzen 7 7700X, portable install with winuxcmd
command links on PATH):
| command | execute_ast wall time |
|---|---|
true |
~1.6 ms |
definitely_not_a_real_command_xyz |
~58 ms |
The extra ~56ms is entirely inside Executor::execute_ast; the niubash-side
tokenize / parse / normalize / host-synced-AST stages each measure
0.05-0.4 ms.
niu --version (no shell at all) is ~64 ms total, so the miss is comparable
to spawning the process itself.
Why this matters
niubash dispatches oh-my-niu framework hook runners by executing a script that
calls niubash_run_<hook>_hooks. There are six such dispatch sites per
interactive line (greeting, precmd, title, preexec, postcmd, zshaddhistory).
When the user rc never sourced the bundle — which is the default state for
niu -c, script files and piped stdin, and also what happens on my machine
because the generated rc's bundle-discovery list misses the app-installed
bundle directory — those names are never defined, so every dispatch paid
for a missing-command scan.
That was ~68ms per dispatch:
niu -c true: 68ms of the 98ms in-process totalniu -C true: ~480ms of the 659ms in-process total, with a completely
empty HOME and no rc at all
niubash now guards the dispatch with a one-time declare -F probe
(unixwin/niubash#80), which brings niu -c true from 260ms to 71ms wall and
niu -C true from ~1700ms to ~260ms wall. But the underlying lookup cost is
still there for every genuinely mistyped command, every command -v on a
missing name, and every other resolution miss.
Suggestion
Cache negative results. A miss for name N at path state S should be
remembered for the life of the process (invalidated if PATH or the
executable directories change), so the second and later misses are O(1)
instead of a full PATH × PATH_EXT scan.
declare -F <name> on a missing function returns in <0.5ms, so the function
miss path is already fine — it is specifically the external command
fallback scan that is expensive.
Happy to send a PR against rubash if that's the right shape.
Activity
caomengxuan666 commentedon Sep 15, 2026
Fixed in commit(s) on master. Root-cause fix verified against WSL GNU Bash 5.3.0 baseline. See git log for details.
test(path): add negative lookup cache regression tests for issue #71
fix(executor): confine fork/respawn state transport to the exec bound…
fix(executor): confine fork/respawn state transport to the exec bound…