You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
set_shell_option (builtins/set/options.rs) rebinds SHELLOPTS from scratch on every call, and export/unset flip per-variable flags through paths that may re-walk option/variable tables per op. #915 just fixed the shopt twin of this (redundant materialization flipping all lookups to the slow path); the set -o / export-attr side deserves the same audit: skip redundant writes when the value already matches, and check what unset re-renders per call (100 us/unset smells like a full-table walk, not a hash removal).
Found via the niubash#352 comprehensive matrix re-runs.
Mechanism (code-read, needs a maintainer nod before implementing)
export EV_\=\ → declare/attrs.rs → env::set_var(name, value)per call (process-global lock + O(n) environ scan in glibc).
unset → unset.rs → env::remove_var per call, plus 10x unmark_variable (those are cheap — VarTable already no-ops unchanged bits).
With N exported vars live, each op scans ~N environ entries → the observed O(n²) (2000 exports: 245ms; +2000 unsets: +200ms).
Meanwhile child spawning (external_inner.rs apply_env_command_environment) does process.env_clear() + explicit per-var process.env() from the engine table — it never relies on process-env inheritance. So the per-op process-env mirror looks redundant for children; its only possible consumers are direct std::env::var reads in Rust code (HOME/PATH/TMPDIR style), which need an audit before removal.
Suggested fix shape (for approval): stop mirroring on every export/unset; if any Rust-side reader truly needs it, sync lazily at those read sites (or once per external spawn for the marked subset). Same family as #915 (redundant write poisoning a hot path) — happy to implement.
setattr/apply.rs: 1x set_var (~113, same array exclusion) + Unset arm remove_var
set/unset.rs: 1x remove_var (~236)
cd/paths.rs set_shell_env: 1x set_var (PWD/OLDPWD per cd)
Kept as-is:kill.rs BACKGROUND_WRAPPER set/remove (coordination nonce, not shell vars); pwd.rs set/remove (test fixtures under #[cfg(test)] only).
Readers — why removal is safe:
External children: apply_external_environment (env_clear + explicit per-var from table) and coproc spawn (explicit per-var). Never inherit process env. Verified all Command::new sites except one:
fc editor spawn (job_builtins.rs): inherits process env implicitly — needs explicit table env added alongside (same helper), else the editor loses exported vars. Only behavioral fix required besides below.
command -v/type -p (command.rs find_in_path): reads process env today — must switch to the table. Bonus correctness fix: plain PATH=x (never mirrored) is currently ignored by lookup; table fixes that.
Fallback readers (env::var(name).ok() after table miss in export/declare/cd paths, SHOPT_STATE-style raw gets): only fire for non-table vars — unaffected, since the mirror only ever added table vars.
Accepted behavior delta:/proc/PID/environ introspection will no longer show shell-exported vars (same tradeoff as the explicit-env spawn design, which already bypasses process env for children). GNU parity surface (printenv FOO, command -v, child env) unchanged — will prove with probes.
Test plan: full suite (export/unset/command/command-v/declare/cd suites green), export/unset bench 25x -> ~1x, plus GNU-diff probes for export FOO=1; printenv FOO, PATH=x; command -v, fc-editor env, and array-export exclusion (#534: printenv A stays empty).
Audit delivered above; implementation is up as PR #918 (branch fix/lazy-marker-strings, stacked on master).
What changed vs the audit: instead of gating string writes, mark/unmark no longer maintain the serialized strings at all for the ten attribute keys (structured flags are the store of record); marked_names renders on demand (sorted); all raw string readers migrated to flag reads (unset -v checks, pushd/cd readonly guards now take &VarTable, command -v/type faces, export -p/declare -p, coproc env, fc editor env, tempenv save materializes on write-behind). Non-attribute keys keep the legacy string path. Two pre-existing duplicate helpers (declare/marks.rs, exec.rs) now delegate to the single implementation.
Also fixed along the way: is_marked_array_var (and coproc fd tracking) read a stale hardcoded string copy — same bug class, which is why 20 cli tests (coproc suite + the new unset-index-zero test) failed mid-migration. Full suite green after migration: cli 466, lib 636, executor 11 pre-existing env failures only.
Activity
caomengxuan666 commentedon Oct 20, 2026
Mechanism (code-read, needs a maintainer nod before implementing)
export EV_\=\→declare/attrs.rs→env::set_var(name, value)per call (process-global lock + O(n) environ scan in glibc).unset→unset.rs→env::remove_varper call, plus 10xunmark_variable(those are cheap — VarTable already no-ops unchanged bits).Meanwhile child spawning (
external_inner.rs apply_env_command_environment) doesprocess.env_clear()+ explicit per-varprocess.env()from the engine table — it never relies on process-env inheritance. So the per-op process-env mirror looks redundant for children; its only possible consumers are directstd::env::varreads in Rust code (HOME/PATH/TMPDIR style), which need an audit before removal.Suggested fix shape (for approval): stop mirroring on every export/unset; if any Rust-side reader truly needs it, sync lazily at those read sites (or once per external spawn for the marked subset). Same family as #915 (redundant write poisoning a hot path) — happy to implement.
caomengxuan666 commentedon Oct 20, 2026
Audit: full inventory for removing the per-op process-env mirror
Writers to remove (6 sites, all same pattern):
declare/attrs.rs: 3xenv::set_var(case-mod declare ~362, export-with-value ~380 skipping arrays per [export] export 的数组变量泄漏进子进程环境:printenv A 输出 \x1d 编码数组(GNU 不导出数组)(P2) #534, export-empty ~385)setattr/apply.rs: 1xset_var(~113, same array exclusion) +Unsetarmremove_varset/unset.rs: 1xremove_var(~236)cd/paths.rs set_shell_env: 1xset_var(PWD/OLDPWD per cd)Kept as-is:
kill.rsBACKGROUND_WRAPPER set/remove (coordination nonce, not shell vars);pwd.rsset/remove (test fixtures under #[cfg(test)] only).Readers — why removal is safe:
apply_external_environment(env_clear+ explicit per-var from table) and coproc spawn (explicit per-var). Never inherit process env. Verified allCommand::newsites except one:fceditor spawn (job_builtins.rs): inherits process env implicitly — needs explicit table env added alongside (same helper), else the editor loses exported vars. Only behavioral fix required besides below.command -v/type -p(command.rs find_in_path): reads process env today — must switch to the table. Bonus correctness fix: plainPATH=x(never mirrored) is currently ignored by lookup; table fixes that.env::var(name).ok()after table miss in export/declare/cd paths,SHOPT_STATE-style raw gets): only fire for non-table vars — unaffected, since the mirror only ever added table vars.export -p/declare -xlistings,printenv(external, explicit env), comsub (in-process fork of table): unaffected.Accepted behavior delta:
/proc/PID/environintrospection will no longer show shell-exported vars (same tradeoff as the explicit-env spawn design, which already bypasses process env for children). GNU parity surface (printenv FOO,command -v, child env) unchanged — will prove with probes.Test plan: full suite (export/unset/command/command-v/declare/cd suites green), export/unset bench 25x -> ~1x, plus GNU-diff probes for
export FOO=1; printenv FOO,PATH=x; command -v,fc-editor env, and array-export exclusion (#534:printenv Astays empty).perf(env): gate the process-env mirror on special names (#916)
caomengxuan666 commentedon Oct 20, 2026
Audit delivered above; implementation is up as PR #918 (branch fix/lazy-marker-strings, stacked on master).
What changed vs the audit: instead of gating string writes, mark/unmark no longer maintain the serialized strings at all for the ten attribute keys (structured flags are the store of record);
marked_namesrenders on demand (sorted); all raw string readers migrated to flag reads (unset -v checks, pushd/cd readonly guards now take&VarTable, command -v/type faces, export -p/declare -p, coproc env, fc editor env, tempenv save materializes on write-behind). Non-attribute keys keep the legacy string path. Two pre-existing duplicate helpers (declare/marks.rs, exec.rs) now delegate to the single implementation.Also fixed along the way:
is_marked_array_var(and coproc fd tracking) read a stale hardcoded string copy — same bug class, which is why 20 cli tests (coproc suite + the new unset-index-zero test) failed mid-migration. Full suite green after migration: cli 466, lib 636, executor 11 pre-existing env failures only.Measured: export/unset x2000 446ms -> 79ms (linear scaling verified 2k/4k/8k); GNU parity probes byte-identical.
Merge pull request #917 from unixwin/fix/drop-process-env-mirror
perf(env): write-behind marker strings, read from structured flags (#916
Merge pull request #920 from unixwin/fix/export-unset-cost
test(executor): retire ASCII payload expectations after sentinel move…
fix(executor): E400-aware sentinel decode family for marker output bo…
fix(env): unbind drops inherited process-env copies (#917 rework) (#943)
docs(readme): overhaul — Performance section with pinned numbers, hon…
caomengxuan666 commentedon Oct 22, 2026
Verified fixed on current master lineage (6a38de0 build, Termux aarch64, release, best-of-3, same shapes as filed):
Fixed by the export/unset family: #917 (process-env gating), #918 (lazy markers), #920 (single-probe + env-fallback removal), #984 (silent-builtin short-circuit), #986 (IFS-marking skip), plus #992 pending (another ~15%, 2.22x->1.89x on the interleave loop). Residual ~2x is per-op expansion/dispatch floor (allocator + walker passes), tracked separately — no longer the full-table-walk class this issue filed. Closing.
fix(env): wait -p / coproc unbinds drop inherited process-env copies (#…
fix(env): wait -p / coproc unbinds drop inherited process-env copies (#…
perf(assign): widen the marks-free scalar bind lane by attribute shap…
fix(#2486): respawned children must inherit variable attributes