Skip to content

perf(shim): detach redirected children instead of giving them a hidden console - #751

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/shim-spawn-flags
Oct 18, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/shim-spawn-flags

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Follow-up to niubash#379, with an honest measurement.

What changes

suppress_child_console_window sets CREATE_NO_WINDOW when all three stdio streams are redirected, so a GUI parent spawning the shim does not make Windows allocate a visible console for the niu.exe child. That flag still allocates a console — a hidden one, with conhost.exe — on every invocation in exactly the case agents, CI and git hooks hit.

With all three streams redirected the child has no console to inherit and does not need one, so DETACHED_PROCESS gives it that for free, and it keeps the anti-flash property: a console-subsystem child started DETACHED_PROCESS gets no automatic console either. The gate is unchanged (an interactive session keeps the real console). Cancellation is unchanged too: neither flag lets the child receive the parent's Ctrl+C, because a CREATE_NO_WINDOW child has a console of its own rather than the parent's.

Measured

Median of 25, interleaved, all stdio redirected, NIU_SHELL pointed at hostname.exe so only the shim is timed:

build per call
installed (CREATE_NO_WINDOW) 22.9 ms
this change (DETACHED_PROCESS) 21.8 ms

The latency win is ~1 ms, not the ~22 ms an earlier report credited to this flag. That number did not reproduce here — conhost is evidently shared or already warm. Stating it plainly because the difference matters for where the real fix belongs.

Where the real fix is

A forwarding hop costs one extra process; the durable fix for bash/sh is a hardlink to the shell, which is unixwin/niubash#397 (merged). This shim stays the fallback for layouts without hardlink support (exFAT, network shares) — and this change means the fallback no longer allocates a console per invocation. That is the whole claim.

Verified

  • piped stdin/stdout round-trip (echo data | shim -c cat), exit-code passthrough (exit 7 → 7), -c arguments intact;
  • face selection unchanged: $0 = bash with POSIX off, sh.exe → POSIX on;
  • no conhost process for the duration of a 2 s child (only niu.exe), versus one hidden console before;
  • cargo test --bin bash → 4 passed.

…n console

`suppress_child_console_window` (niubash#379) chose CREATE_NO_WINDOW so a GUI
parent spawning the shim with piped stdio does not make Windows allocate a
*visible* console for the niu.exe child. That flag still allocates a console -
a hidden one, with conhost.exe - on every invocation in exactly the case
agents, CI and git hooks hit (all three streams redirected).

With all three streams redirected the child has no console to inherit and does
not need one, so DETACHED_PROCESS gives it that for free, and it keeps the
anti-flash property: a console-subsystem child started with DETACHED_PROCESS
gets no automatic console either. The gate is unchanged (all three streams
non-console; an interactive session keeps the real console, as before), and so
is cancellation: neither flag lets the child receive the parent's Ctrl+C, since
a CREATE_NO_WINDOW child has a console of its own rather than the parent's.

Measured here (median of 25, interleaved, all stdio redirected, NIU_SHELL
pointed at hostname.exe so only the shim is timed): 22.9 ms -> 21.8 ms. The
latency win is small - an earlier report credited this flag change with ~22 ms,
which did *not* reproduce on this machine; conhost is evidently shared or warm.
The real cost of a forwarding hop is the extra process, and the durable fix for
`bash`/`sh` is a hardlink to the shell (unixwin/niubash#397), which leaves this
shim as the fallback for layouts without hardlink support. What this change
buys is that the fallback no longer allocates a console per invocation.

Verified: piped stdin/stdout round-trip, exit-code passthrough (`exit 7` -> 7),
face selection unchanged (`$0` = bash, POSIX off; sh.exe -> POSIX on), and no
conhost process for the duration of a 2 s child (only niu.exe).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant