diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 14:37:36 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-28 14:37:36 -0600 |
| commit | 3f24ca27250ade07c34a98fb792e6ca64e54c21b (patch) | |
| tree | 11e54f24ebb2d3f528ec31ac467fa864fe92595b | |
| parent | 95d31d2d7d5fd7de81d6d5e2caedcdc369ca0fcf (diff) | |
| parent | 78d73092a29cca7e3ce4ef6b5e2c6cd244f40202 (diff) | |
| download | zash-3f24ca27250ade07c34a98fb792e6ca64e54c21b.tar.gz | |
Merge pull request #31 from skurtz97/perf/cd-inproc
perf: run a lone ( cd dir ) without forking
| -rw-r--r-- | src/builtin.c | 64 | ||||
| -rw-r--r-- | src/exec.c | 34 | ||||
| -rw-r--r-- | src/exec.h | 3 | ||||
| -rw-r--r-- | src/var.c | 11 | ||||
| -rw-r--r-- | src/var.h | 1 | ||||
| -rw-r--r-- | tests/zash/cd-isolated.exp | 11 | ||||
| -rw-r--r-- | tests/zash/cd-isolated.sh | 16 |
7 files changed, 109 insertions, 31 deletions
diff --git a/src/builtin.c b/src/builtin.c index ac7aa45..e5ff809 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -672,11 +672,6 @@ static const char *get_curdir(void) { return curdir; } -void curdir_invalidate(void) { - free(curdir); - curdir = nullptr; -} - /** * @brief Remove `.`, `..` and repeated slashes from an absolute path, textually * (the logical view `cd -L` uses). @@ -819,6 +814,61 @@ 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. + */ +void builtin_cd_isolated(int argc, char **argv, int *status) { + int cwd = open(".", O_RDONLY); + char path[PATH_MAX]; + bool have_path = getcwd(path, sizeof path) != nullptr; + 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; + jmploc jl; + jmploc *prev = exc_handler; + int result; + + if (setjmp(jl.buf) == 0) { + exc_handler = &jl; + result = b_cd(argc, argv); + } else { + /* b_cd raised; the directory may still have moved. */ + result = exitstatus ? exitstatus : 2; + } + exc_handler = prev; + + bool restored = cwd >= 0 && fchdir(cwd) == 0; + if (cwd >= 0) + close(cwd); + if (!restored) + restored = chdir(saved) == 0; + if (!restored && have_path) + (void)chdir(path); + 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); + *status = result; +} + +/** * @brief The `pwd` builtin: `pwd [-L | -P]`. */ static int b_pwd(int argc, char **argv) { @@ -1120,8 +1170,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}, @@ -1235,11 +1235,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; @@ -1250,10 +1250,9 @@ static bool subshell_in_process(const node *n) { find_command(b->u.simple.args->plain, &ci, 0); if (ci.kind != C_BUILTIN) return false; - if (ci.bi->flags & BI_PURE) - return true; - /* cd / chdir: can run in-process if the cwd is saved and restored */ - return !strcmp(ci.bi->name, "cd") || !strcmp(ci.bi->name, "chdir"); + if (ci.bi->flags & BI_CD) + return var_is_plain("PWD") && var_is_plain("OLDPWD"); + return (ci.bi->flags & BI_PURE) != 0; } /* ---- pipelines ------------------------------------------------------- */ @@ -1920,21 +1919,10 @@ static int eval_subshell(node *n, unsigned flags) { scope_pop(); return status; } - /* cd / chdir: save cwd, run in-process, restore */ - if (ok && sc.cmd.kind == C_BUILTIN && - (!strcmp(sc.cmd.bi->name, "cd") || - !strcmp(sc.cmd.bi->name, "chdir"))) { - int cwd_fd = open(".", O_RDONLY | O_CLOEXEC); - if (cwd_fd < 0) { - scope_pop(); - return 1; - } - int status = sc.cmd.bi->fn(sc.argc, sc.argv); - if (fchdir(cwd_fd) < 0) - sh_error("cannot restore working directory"); - close(cwd_fd); + if (ok && (sc.cmd.bi->flags & BI_CD)) { + int status; + builtin_cd_isolated(sc.argc, sc.argv, &status); scope_pop(); - curdir_invalidate(); return status; } scope_pop(); @@ -63,6 +63,7 @@ int shell_exec_try(char **argv, char **envp); enum { BI_SPECIAL = 1u << 0, /**< POSIX special builtin */ BI_PURE = 1u << 1, + BI_CD = 1u << 2, /**< changes directory state; see builtin_cd_isolated() */ }; /** @@ -110,7 +111,7 @@ enum { }; void find_command(const char *name, cmdinfo *ci, unsigned flags); -void curdir_invalidate(void); +void 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); @@ -226,6 +226,17 @@ void var_set(const char *name, const char *value, unsigned flags) { } /** + * @brief Can var_set() replace this variable without a side effect or error? + * + * A readonly, integer or dynamic variable is not plain: setting it raises, + * converts the text, or calls a setter. + */ +bool var_is_plain(const char *name) { + var *v = var_find(name, strlen(name), false); + return !v || !(v->flags & (V_READONLY | V_INTEGER | V_DYNAMIC)); +} + +/** * @brief Assign a number to a variable. */ void var_set_long(const char *name, long value) { @@ -67,6 +67,7 @@ const char *var_get_n(const char *name, size_t nlen); var *var_find(const char *name, size_t nlen, bool create); void var_set(const char *name, const char *value, unsigned flags); +bool var_is_plain(const char *name); void var_set_n(const char *name, size_t nlen, const char *value, unsigned flags); void var_set_long(const char *name, long value); diff --git a/tests/zash/cd-isolated.exp b/tests/zash/cd-isolated.exp new file mode 100644 index 0000000..13467d5 --- /dev/null +++ b/tests/zash/cd-isolated.exp @@ -0,0 +1,11 @@ +after=0 same=yes +/ +/ +bad=nonzero same=yes +redir=0 same=yes +vars=pwd oldpwd +list=0 same=yes +yes +int=2 same=yes +ro=2 same=yes +[status 0] diff --git a/tests/zash/cd-isolated.sh b/tests/zash/cd-isolated.sh new file mode 100644 index 0000000..f740aa0 --- /dev/null +++ b/tests/zash/cd-isolated.sh @@ -0,0 +1,16 @@ +# A lone ( cd dir ) must put the shell back where it was. +start=$(pwd -P) +(cd /); echo "after=$? same=$([ "$(pwd -P)" = "$start" ] && echo yes || echo no)" +(cd /; pwd) +(cd / && pwd) +(cd /nonexistent) 2>/dev/null; echo "bad=$([ $? -ne 0 ] && echo nonzero) same=$([ "$(pwd -P)" = "$start" ] && echo yes || echo no)" +(cd /) 2>/dev/null; echo "redir=$? same=$([ "$(pwd -P)" = "$start" ] && echo yes || echo no)" +old=$OLDPWD +(cd /); echo "vars=$([ "$PWD" = "$start" ] && echo pwd) $([ "$OLDPWD" = "$old" ] && echo oldpwd)" +(cd /; :); echo "list=$? same=$([ "$(pwd -P)" = "$start" ] && echo yes || echo no)" +(cd /) || echo no +(cd /) && echo yes + +( typeset -i PWD; (cd /) 2>/dev/null; echo "int=$? same=$([ "$(pwd -P)" = "$start" ] && echo yes || echo no)" ) +readonly PWD +(cd /) 2>/dev/null; echo "ro=$? same=$([ "$(pwd -P)" = "$start" ] && echo yes || echo no)" |
