Skip to content

perf(env): gate the process-env mirror on special names (#916) - #917

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/drop-process-env-mirror
Oct 20, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/drop-process-env-mirror

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem (#916)

Every export/unset/cd/tempassign ran an unconditional process-env write: glibc setenv/unsetenv take the global lock and linearly scan the environ, so 2000 exports cost 25x GNU (446 ms vs 34 ms best-of-3 on-device).

Analysis (full audit on the issue)

Children already receive the table via env_clear + explicit per-var env, and table reads see every write. The only direct process-env readers need exactly the special set (HOME/OLDPWD/PATH/PATHEXT/PWD/TEMP/TMP/TMPDIR/__RUBASH_*), which the codebase already centralizes in shell_assignment_needs_process_env — but export/unset/cd/tempassign bypassed it with ad-hoc mirrors.

Fix (8 files, +67/-38)

  • set_process_env gains the special-name gate; new gated unset_process_env counterpart. All mirror sites route through them (export/declare/readonly/unset/cd-PWD/tempassign-restore).
  • fc's editor spawn gets the standard explicit child env (apply_child_environment) instead of relying on process inheritance — the one implicit-inheritance spawn in the tree.
  • command -v/type -p deliberately untouched: process PATH stays current because every PATH write is special-gated, verified by GNU-parity probes (export FOO/unset FOO/printenv, array non-export [export] export 的数组变量泄漏进子进程环境:printenv A 输出 \x1d 编码数组(GNU 不导出数组)(P2) #534, PATH= switching).
  • set_shell_env (cd) keeps behavior byte-identical (PWD/OLDPWD are always special).

Verification

  • GNU parity probes byte-identical (incl. exit codes).
  • export+unset x2000: 446 ms -> 395 ms. Modest — the rest is the marker-string O(n) rebuild per fresh export (string_append), which needs the lazy-serialization redesign (structured flags already authoritative); tracked as the follow-up, deliberately out of this PR's blast radius.
  • Full suite: cli 466/466, lib 636/636, executor 11 pre-existing Android-env failures only (verified identical set vs pristine baseline; one flaky wait_n passes in isolation).

Every export/unset/cd/tempassign ran an unconditional locked O(n)
environ scan (glibc setenv/unsetenv), so 2000 exports cost 25x GNU.
Children already receive the table via env_clear + explicit env, and
table reads see every write — only direct process-env readers need
the mirror, and those read exactly the special set
(HOME/OLDPWD/PATH/.../TMPDIR/__RUBASH_*).

Route all mirror writes through one gate (set_process_env), add the
gated unset_process_env counterpart, and use them at every site
(export/declare/readonly/unset/cd-PWD/tempassign-restore/fc-editor
explicit env). tempassign-restore and fc-editor go through the
standard helpers instead of ad-hoc process writes/reads.

Measured (aarch64, best-of-3): export+unset x2000 446ms -> 395ms.
The remaining gap is the marker-string O(n) rebuild per fresh export
(string_append), tracked as the follow-up: structured flags are
already authoritative, the serialized lists need lazy rebuild.
Every export/unset/cd/tempassign ran an unconditional locked O(n)
environ scan (glibc setenv/unsetenv), so 2000 exports cost 25x GNU
(446ms vs 41ms best-of-3).

Meanwhile the tree already had the correct rule in one place:
local_helpers::sync_shell_assignment_process_env mirrors only
exported-or-special names, because children receive the table via
env_clear + explicit env and table reads see every write. This change
finishes that rule everywhere:

- set_process_env() itself gates on the special list
  (HOME/OLDPWD/PATH/PATHEXT/PWD/TEMP/TMP/TMPDIR/__RUBASH_*).
- New unset_process_env() gated counterpart; unset paths in
  export/declare/unset/tempassign-restore use it.
- set SHELL/PATH-style writers (declare/export/readonly/cd-PWD)
  route through the same gate instead of ad-hoc set_var calls.
- fc's editor spawn gets the explicit child env
  (apply_child_environment) — the only implicit-inheritance spawn.
- find_in_path stays on the process env: every PATH write still
  mirrors (special), verified unchanged by GNU-parity probes.

Measured: export+unset x2000 446ms -> ~395ms. The remaining gap is
the marker-string O(n) rebuild per fresh export (string_append);
tracked as the follow-up (structured flags are already authoritative,
needs lazy serialization + reader migration).

Verified: GNU parity probes byte-identical (export/printenv/unset,
declare -p, PATH switching, array non-export); full suite green
(cli 466, lib 636, executor 11 pre-existing Android-env failures).
@caomengxuan666

Copy link
Copy Markdown
Member Author

验证不通过:普通继承名 unset 后进程 env 残留,9 个形与 GNU/master 分歧(暂缓合并)

分支 head 7b11940c,对照 master 9f512fc5 与 WSL GNU bash 5.3.0(Ubuntu 24.04,同机实测,Windows 侧 rubash debug 构建)。CI 里的 parity 探针全部用新鲜名,本矩阵补的是父进程继承名维度——这一维度上回归成立。

分歧形与实测输出

探针(继承 FOO=parent/LANGC=xx 启动) GNU bash 5.3 master 9f512fc 本分支 7b11940
unset FOO; echo "[$FOO]" [] [] [parent]
set -u; unset FOO; echo "${FOO}" FOO: unbound variable rc=127 同 GNU 空行 + after,rc=0
unset LANGC; Y=$(printf hi); echo "[$LANGC][$Y]" [][hi] [][hi] [xx][hi]
unset FOO; echo "[$((FOO))]"(继承 FOO=7) [0] [0] [7]
unset FOO; export FOO; declare -p FOO declare -x FOO 同 GNU declare -x FOO="parent" + [parent]
unset FOO; ( echo bg=[$FOO] ) bg=[] bg=[] bg=[parent]
FOO=x; unset FOO; echo "[$FOO]" [] [] [parent]
unset FOO; FOO=new; unset FOO; echo "2=[$FOO]" 2=[] 2=[] 2=[parent]
unset LANGC; Y="$(dirname /d/x)"; echo "[$LANGC]" [] [] [xx](#182 复活类)

通过面(供对照)

根因

unset_process_env 以 shell_assignment_needs_process_env(特殊名表)门控 env::remove_var。master 对 unset 是无条件 env::remove_var——父进程继承的普通名在进程 env 里的拷贝因此被清掉;门控后这份拷贝永久残留。而 table-miss 的读取路径仍回退进程 env:裸 $NAME 链(exact_case_env_var:command_prepare.rs:1351/3710、embedded_parameters.rs:938、arithmetic_aliases.rs:397)、nounset 存在性检查(parameter_errors.rs:1909/1940/1991/2153/2196)、以及 fork+exec 后台子壳的 env 以 std::env::vars() 为基底(compound_exec.rs:502)。这正是 command_substitution.rs:1617-1627 注释里记录的 #182 复活类。

修复方向建议

unset_process_env 对"实际存在于进程 env 的名字"都应移除(存在性检查是廉价 getenv 查询;需要省的锁内 O(n) 扫描在写路径,不在删除路径),或 unset 对其 unbind 的名字一律清除继承拷贝。同时建议把 FOO=parent rubash -c 'unset FOO; echo "[$FOO]"' 这类继承名形补进 parity 探针——现有探针全是新鲜名,正好绕开本面。

修好重提后可直接进合并流程。

@caomengxuan666
caomengxuan666 merged commit a5f531f into master Oct 20, 2026
6 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/drop-process-env-mirror branch October 20, 2026 15:34
caomengxuan666 added a commit that referenced this pull request Oct 21, 2026
Verified against GNU bash 5.3 (WSL) on a 3-column probe matrix (base b934174 / branch e91495d / GNU): all 9 previously-divergent inherited-name unset forms flip to GNU-consistent ([], nounset rc=127, comsub, arithmetic, export+declare -p, bg subshell, reassign-unset, dirname comsub); original passing forms (printenv/cmd channels, fresh-name chain, array non-export, PATH, declare -p unbound, TMPDIR exemption, special-name write mirror, comsub/flat-subshell parent-env integrity, export -n) unchanged across all three columns. unset_process_env is existence-gated (non-locking var_os) — #916 write-path gate untouched; 2000x export+unset loop 790ms vs base 845ms. Env/unset/nounset + comsub/subshell/assign targeted suites green locally (1 pre-existing env-specific failure in issue705_error_branding reproduces identically on base). PR head CI green.
caomengxuan666 added a commit that referenced this pull request Oct 21, 2026
…est platform status, current architecture (#982)

* docs(readme): overhaul — Performance section with pinned numbers, honest platform status, current architecture

- New Performance section, every number pinned to its measuring issue/PR:
  - Android-matrix interpreter triage closed with numbers (#607/#608/#609
    -> #665/#671/#679, #666, #672/#678; net ~1.06x GNU scalar assign)
  - array store O(n^2)->O(1) amortized (#618/#616: assoc 445x->9.9x,
    indexed 736x->3.8x vs GNU at 5000 keys; docs/performance.md ledger)
  - literal fast-path wave with measured deltas: #967 (177->155 ms),
    #966 (1.41x->1.10x), #974 (~11%), #959 (3.65x->3.16x), #958 (-10%),
    #956/#973/#954 family
  - export/unset economics: #916 measured costs stated, write lane landed
    (#917/#918/#920, #943 correctness rework), issue open on residual
  - Windows process economics: #913 one hidden console per session
    (~22 ms/spawn -> <=1 per session; tests/console_window_gate.rs),
    #884 tput inline arm (1165 ms -> 47 ms, 269-row oracle matrix),
    WinuxCmd#1269 twin (~17 ms -> ~0.4 ms in-process), #800 face B open
- Platform status made verifiable: Linux CI lanes + upstream-corpus replay
  job named; 19/24 Linux pass dated and cited (#500) instead of 'and
  climbing'; macOS now runs lib tests in CI, not just compiles
- Architecture tree updated to the real src/ layout (bin/expand/fd/jobs/
  shell added); builtin count 40+ -> 62 engine builtins per the
  test-guarded docs/builtins.md + hidden fast-path builtins
- Testing section: regression suite, console gate, corpus replay commands
- Compatibility baseline line updated to vendored Bash-5.3 patch 20
  (#957) with local+WSL 5.3.20 oracles (#978)
- zh README rewritten to full parity with EN (was still describing the
  pre-dialect path model, missing the identity section, claiming 5.3.0)
- Last-updated stamp refreshed

* docs(readme): date the Linux corpus pass from the archive — 2026-09-27 wt4/linuxrun, 17/24 per its own ledger

The "(2026-09-26, #500)" tag was wrong on both counts: 09-26 is the
earlier "not-yet-usable" judgment (first smoke 8/10, gap-ledger C4) that
the 2026-09-27 wt4/linuxrun lane explicitly declared obsolete, and #500
only carried the figure forward — it is not the measuring record. No
archived record supports 19/24: the lane's own corpus-slice table
(docs/LINUX-RUN-STATUS.md, deleted in #478, archived in #477 section 4
and #581) tallies 17 suites at 0 diff + trap/redir/glob/extglob/new-exp/
read/ifs-posix with diffs = 17/24. README now reads "17/24 byte-identical
(2026-09-27 wt4/linuxrun lane, ledger archived in #477/#581)" in both
the platform-status paragraph and the compatibility table, EN + zh-CN
at parity. Addresses review comment 6319063190 on #982.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
caomengxuan666 added a commit that referenced this pull request Nov 5, 2026
…#2833) (#2844)

Re-count the retained-seam table (§2.2) against niubash master
413847b7 by receiver grep: claimed-live 21 -> true live 10.
Eleven rows are drift (migrations landed, rows never zeroed):
set_env, export_env, set_shopt_option, take_exit_jump_pending,
bind_registry_snapshot, bind_registry_generation, execute_command,
last_command_inverted, has_function, has_compspec,
call_function_with_env. Three rows corrected, not zeroed:
take_parse_error 4 -> 3, alias_expansion_enabled 4 -> 1,
find_script_on_path 1 -> 2. Same wave corrects §2.1
(CommandNode 14 -> 11, pretty_print_command 1 -> 0, the tracked
script-driver arm 1 -> 0, bytes_to_script_text 1 -> 3) and
records four Session faces consumed but nowhere in the ledger
(glob_filename, notify_terminal_size, expand_shell_words,
environment) plus the call_function_with_env §2.3 row.
Session::set_history_provider reads 1 now (GAP-10 host adoption
landed with niu #917). The Session-method census lands in the
tripwire (§5.5) with a self-test; it is what caught
Session::environment. The bind/readline assembly verbs enter
NOWHERE here, per the GAP-13 ruling Q-G.

Refs rubash#2833, niubash#863

Co-authored-by: 曹梦轩 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant