summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-25 18:53:39 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-25 18:53:39 -0600
commit93bbcf2f9a75bb5090d96ec9908594e15aa5908b (patch)
tree5069e98a1caee726174588d3522d4e85e5cbc353
parentfd782a60f3993d6ec302de669ceefbdacbf1f97a (diff)
downloadzash-perf/cd-inproc.tar.gz
perf: restore cwd after a lone ( cd dir ) in-processperf/cd-inproc
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.
-rw-r--r--src/builtin.c34
-rw-r--r--src/exec.c13
-rw-r--r--src/exec.h2
-rw-r--r--src/var.c11
-rw-r--r--src/var.h1
-rw-r--r--tests/zash/cd-isolated.exp11
-rw-r--r--tests/zash/cd-isolated.sh16
7 files changed, 72 insertions, 16 deletions
diff --git a/src/builtin.c b/src/builtin.c
index 5cca7f8..887d7ce 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -793,22 +793,36 @@ static int b_cd(int argc, char **argv) {
* @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) {
+void builtin_cd_isolated(int argc, char **argv, int *status) {
int cwd = open(".", O_RDONLY);
- if (cwd < 0)
- return false;
+ 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;
- *status = b_cd(argc, argv);
- if (fchdir(cwd) < 0)
- chdir(saved);
- close(cwd);
+ 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
@@ -821,7 +835,7 @@ bool builtin_cd_isolated(int argc, char **argv, int *status) {
curdir = saved;
free(pwd);
free(oldpwd);
- return true;
+ *status = result;
}
/**
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)
diff --git a/src/exec.h b/src/exec.h
index 432a32c..43af437 100644
--- a/src/exec.h
+++ b/src/exec.h
@@ -110,7 +110,7 @@ enum {
};
void find_command(const char *name, cmdinfo *ci, unsigned flags);
-bool builtin_cd_isolated(int argc, char **argv, int *status);
+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);
diff --git a/src/var.c b/src/var.c
index def6978..d4251cf 100644
--- a/src/var.c
+++ b/src/var.c
@@ -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) {
diff --git a/src/var.h b/src/var.h
index 7be4fd8..bffa745 100644
--- a/src/var.h
+++ b/src/var.h
@@ -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)"