Skip to content

[perf] export/unset per-op cost ~100x: 2k dynamic export+unset 13x slower than bash #916

Closed

Description

@caomengxuan666

Numbers (aarch64 Termux, release, best-of-3)

workload GNU bash 5.3 rubash
export EV_\=\ x2000 33 ms 245 ms (7.5x)
+ unset EV_\ x2000 after ~1 ms ~200 ms (~200x on the unset leg)
export/unset/re-export fixed pair x2000 36 ms 126 ms (3.5x)
full bench-full export-declare 5k 86 ms 2555 ms (30x)

niubash tracks rubash here (no host delta) — pure engine cost.

Suspect (same family as #915)

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.

Activity

  1. caomengxuan666 commented on Oct 20, 2026

    @caomengxuan666
    MemberAuthor

    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.

  2. caomengxuan666 commented on Oct 20, 2026

    @caomengxuan666
    MemberAuthor

    Audit: full inventory for removing the per-op process-env mirror

    Writers to remove (6 sites, all same pattern):

    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.
    • export -p/declare -x listings, printenv (external, explicit env), comsub (in-process fork of table): unaffected.

    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).

  3. caomengxuan666 commented on Oct 20, 2026

    @caomengxuan666
    MemberAuthor

    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.

    Measured: export/unset x2000 446ms -> 79ms (linear scaling verified 2k/4k/8k); GNU parity probes byte-identical.

  4. caomengxuan666 commented on Oct 22, 2026

    @caomengxuan666
    MemberAuthor

    Verified fixed on current master lineage (6a38de0 build, Termux aarch64, release, best-of-3, same shapes as filed):

    • export-only x2000: bash 22ms vs rubash 45ms = 2.05x (was 7.5x)
    • export x2000 + unset x2000: bash 31ms vs rubash 71ms = 2.29x (the ~200x unset leg is gone)
    • export-declare 5k (bench-full): 2.01x (was 30x)

    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.

  5. added a commit that references this issue on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions