From 93bbcf2f9a75bb5090d96ec9908594e15aa5908b Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Wed, 25 Nov 2026 18:53:39 -0600 Subject: perf: restore cwd after a lone ( cd dir ) in-process A subshell whose body is a single cd now runs in the parent shell with the directory, PWD, OLDPWD and curdir saved and restored, instead of forking. On z/OS a fork costs ~0.4 ms there; the cd itself costs ~5 us. A readonly, integer or dynamic PWD/OLDPWD keeps the forked path. --- src/exec.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/exec.c') diff --git a/src/exec.c b/src/exec.c index 1967182..f09bea4 100644 --- a/src/exec.c +++ b/src/exec.c @@ -1239,7 +1239,11 @@ static bool subshell_in_process(const node *n) { return false; cmdinfo ci; find_command(b->u.simple.args->plain, &ci, 0); - return ci.kind == C_BUILTIN && (ci.bi->flags & (BI_PURE | BI_CD)); + if (ci.kind != C_BUILTIN) + return false; + if (ci.bi->flags & BI_CD) + return var_is_plain("PWD") && var_is_plain("OLDPWD"); + return (ci.bi->flags & BI_PURE) != 0; } /* ---- pipelines ------------------------------------------------------- */ @@ -1845,10 +1849,9 @@ static int eval_subshell(node *n, unsigned flags) { } if (ok && (sc.cmd.bi->flags & BI_CD)) { int status; - if (builtin_cd_isolated(sc.argc, sc.argv, &status)) { - scope_pop(); - return status; - } + builtin_cd_isolated(sc.argc, sc.argv, &status); + scope_pop(); + return status; } scope_pop(); if (!ok) -- cgit