Skip to content

Missing external command resolution costs ~60-85ms; consider caching negative lookups #71

Closed

Description

@caomengxuan666

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 total
  • niu -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

  1. caomengxuan666 commented on Sep 15, 2026

    @caomengxuan666
    MemberAuthor

    Fixed in commit(s) on master. Root-cause fix verified against WSL GNU Bash 5.3.0 baseline. See git log for details.

  2. added
    performance
    area:path
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:pathperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions