diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 17:50:59 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 17:50:59 -0600 |
| commit | fd782a60f3993d6ec302de669ceefbdacbf1f97a (patch) | |
| tree | 8111f5bf831e0a4df1be60980eaf32a00d3b45a9 | |
| parent | 0ebef9da03f1952e8d0189a48cd7b946a7369f4f (diff) | |
| download | zash-fd782a60f3993d6ec302de669ceefbdacbf1f97a.tar.gz | |
perf: run a lone ( cd dir ) without forking
| -rw-r--r-- | src/builtin.c | 45 | ||||
| -rw-r--r-- | src/exec.c | 19 | ||||
| -rw-r--r-- | src/exec.h | 2 |
3 files changed, 58 insertions, 8 deletions
diff --git a/src/builtin.c b/src/builtin.c index 8497a0d..5cca7f8 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -784,6 +784,47 @@ static int b_cd(int argc, char **argv) { } /** + * @brief Run `cd` in this process and put the shell back where it was. + * + * A subshell only needs the directory change to be invisible to its parent. + * Saving the open directory, `PWD`, `OLDPWD` and the logical directory cache + * lets `( cd dir )` skip the fork that z/OS makes expensive. + * + * @param argc Argument count. + * @param argv Arguments. + * @param status Where the `cd` status goes. + * @return false if the current directory could not be saved, in which case + * the caller must use a forked subshell instead. + */ +bool builtin_cd_isolated(int argc, char **argv, int *status) { + int cwd = open(".", O_RDONLY); + if (cwd < 0) + return false; + char *saved = xstrdup(get_curdir()); + bool had_pwd = var_get("PWD") != nullptr; + bool had_oldpwd = var_get("OLDPWD") != nullptr; + char *pwd = had_pwd ? xstrdup(var_get("PWD")) : nullptr; + char *oldpwd = had_oldpwd ? xstrdup(var_get("OLDPWD")) : nullptr; + *status = b_cd(argc, argv); + if (fchdir(cwd) < 0) + chdir(saved); + close(cwd); + if (had_pwd) + var_set("PWD", pwd, V_EXPORT); + else + var_unset("PWD"); + if (had_oldpwd) + var_set("OLDPWD", oldpwd, V_EXPORT); + else + var_unset("OLDPWD"); + free(curdir); + curdir = saved; + free(pwd); + free(oldpwd); + return true; +} + +/** * @brief The `pwd` builtin: `pwd [-L | -P]`. */ static int b_pwd(int argc, char **argv) { @@ -1085,8 +1126,8 @@ const builtin builtins[] = { {"bg", builtin_bg, 0}, {"bind", b_bind, 0}, {"break", b_break, BI_SPECIAL}, - {"cd", b_cd, 0}, - {"chdir", b_cd, 0}, + {"cd", b_cd, BI_CD}, + {"chdir", b_cd, BI_CD}, {"command", builtin_command, 0}, {"continue", b_continue, BI_SPECIAL}, {"echo", b_echo, BI_PURE}, @@ -1226,11 +1226,11 @@ static int run_pure(simple_cmd *sc, output *o) { * @brief Can a subshell run without a process of its own? * * Its body has to be one command whose name is written literally, that - * name has to be a builtin which only writes output, and neither the - * subshell nor the command may redirect or assign. Then nothing the - * parent can see can change. A `fork()` copies the address space on - * z/OS, 4.7 ms against 0.3 ms for a `spawn()`, and ksh88 does not pay it - * for `( : )` either. + * name has to be a builtin which only writes output or changes directory + * state that can be restored, and neither the subshell nor the command may + * redirect or assign. Then nothing the parent can see can change. A + * `fork()` copies the address space on z/OS, 4.7 ms against 0.3 ms for a + * `spawn()`, and ksh88 does not pay it for `( : )` either. */ static bool subshell_in_process(const node *n) { const node *b = n->u.body; @@ -1239,7 +1239,7 @@ 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); + return ci.kind == C_BUILTIN && (ci.bi->flags & (BI_PURE | BI_CD)); } /* ---- pipelines ------------------------------------------------------- */ @@ -1843,6 +1843,13 @@ static int eval_subshell(node *n, unsigned flags) { scope_pop(); return status; } + if (ok && (sc.cmd.bi->flags & BI_CD)) { + int status; + if (builtin_cd_isolated(sc.argc, sc.argv, &status)) { + scope_pop(); + return status; + } + } scope_pop(); if (!ok) return exitstatus; @@ -62,6 +62,7 @@ void exec_init(void); enum { BI_SPECIAL = 1u << 0, /**< POSIX special builtin */ BI_PURE = 1u << 1, + BI_CD = 1u << 2, /**< changes directory state; see builtin_cd_isolated() */ }; /** @@ -109,6 +110,7 @@ enum { }; void find_command(const char *name, cmdinfo *ci, unsigned flags); +bool builtin_cd_isolated(int argc, char **argv, int *status); void func_define(const char *name, node *body, arena *owner); bool func_unset(const char *name); void func_each(void (*fn)(const char *name, void *arg), void *arg); |
