Skip to content

[perf][P3] shape-independent per-command floor (~2.8us) #682

Closed as not planned

Description

@caomengxuan666

Leftover of #607/#661: per-execute_command fixed cost is shape-independent (~2.8us; if = 3 traversals vs 2 for ;-sequence explains the ratio vs GNU's ~0.3us frame). Plus set_current_command render scaling with node size and the if…fi >/dev/null redirect path (+52us vs GNU +5.4us). All measured during #661 (see PR #673). This is cross-cutting command-dispatch machinery, not one construct — needs its own attribution pass before any slice. Do not start from a diff count.

Activity

  1. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    Attribution (frozen here — no implementation until the correctness queue is empty):

    1. dispatch (command_execute.rs:execute_command + ast_exec.rs:execute_ast_inner; GNU execute_cmd.c:624): floor base; slice = skip chain matchers when node kind known (risk: compound bypass).
    2. traversals if=3 vs ;-seq=2 (compound_exec.rs:execute_if_command; GNU :3862/:5200): structural +1 walk; slice = stored slices by reference (risk: inline-arith-for normalize).
    3. line_number save/restore (public_accessors.rs:set_current_line; GNU :629/:907): slice = stamp only on change (risk: stale LINENO).
    4. REAP/jobs+traps (ast_exec.rs:P_JOBS, job_builtins.rs:533; GNU :643/:2983): empty-table early return landed; slice = gate trap/notify on no-child flag.
    5. alias scan (landed perf(executor): if-condition dispatch residual (#661) #673; GNU parse-time parse.y): stateless empty-table slice kept; stateful cache rejected (needs generation invalidation).
    6. condition toggles (landed perf(executor): if-condition dispatch residual (#661) #673 shadow-gated; GNU :3862 CMD_IGNORE_RETURN).
    7. set_current_command render (command_text.rs:443; GNU :699 fork-path only): slice = lazy render for BASH_COMMAND/verbose/xtrace readers.
    8. redirect clone-and-apply (landed perf(1.4.1): if/case no-redirect pointer-walk fast path (if -53%, case -47%) #624 gate; GNU :827/:853): risk = new redirect field escaping the gate predicate.

    Landed slices already cover 5/6/8 partially; 1/2/3/4/7 await their own attribution passes. Do not start from a diff count.

  2. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Noting for the floor ledger (not a slice): prompt-identity probe during triage found uid_value() hardcoding root; fixed separately as #695.

  3. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    Correction: the uid fix PR is #698, not #695.

  4. added
    performance
    P3-low
    on Oct 17, 2026
  5. caomengxuan666 commented on Oct 22, 2026

    @caomengxuan666
    MemberAuthor

    Audit pass on current master (6a38de0 / rebased onto ce62721), release build, rotating-interleaved min-of-9/11, D-series Windows host. Method: loop-body marginal cost (body parsed once → exec-only) + flat-file exec−noexec slope. Raw samples: target/perf-682/results/*.json in the perf/682 worktree.

    Floor status: ~3.4-3.5µs per :/true (issue's ~2.8µs recorded during #661 — same ballpark; #967/#981 moved render but not the floor):

    shape per-unit cost notes
    : / true (loop body) 3.47 / 3.63µs shape-independent, confirmed
    : flat file, exec−noexec ~2.8µs execute_command alone at top level
    : flat file, all-in 12.7µs parse slope alone is ~10.1µs/cmd — top-level floor is parse-dominated, separate subsystem from this issue
    if :;then :;fi 11.7µs/stmt ≈3.9µs/slot × 3 traversals — ratio vs ;-seq holds
    if … >/dev/null 83.5µs/stmt +72µs vs plain if (issue recorded +52): splice >> into every leaf + each leaf re-opens + cmd/if_command clones — redirect architecture, not a slice
    : >/dev/null 17.5µs single open + expand + fd machinery
    f(){ :; }; f 12.2µs/call call-frame territory (#157/#186)

    RUBASH_EXEC_PROFILE split per : (flat-80k): dispatch 2.34µs · jobs 1.07 · chain 1.34 · linecmd 0.64 · scans 0.15. #981's literal fast render shows — linecmd is already small.

    One bounded slice landed → PR #989: take_pending_signals polled the signal-hint kernel event (WaitForSingleObject(,0), a real syscall 0.5-0.7µs) at every command boundary; GNU's catch_flag is a memory flag (trap.c:336). Auto-reset latches the pulse, so sampling every 8th boundary loses no delivery — jobs bucket 298→99ms/420k cmds, : marginal 3.43→3.2µs, loop-5colon −11.5%. Delivery batteries green (issue357 6/6 incl. busy-loop TERM trap, issue382 6/6, 343_345 9/9, 524 12/12, cli kill|signal|trap 21/21; issue526's two -1 asserts fail identically on baseline — Windows has no signaled ExitStatus).

    Remaining floor (post-PR ~3.2µs vs GNU ~0.3µs) is structural, no clean P3 slice: dispatch's ~2.3µs is ~20 sequential semantic checks (no single item); chain ~1.3µs is the alias/compound matcher family already gated by alias_chain_applicable; double set_current_line (ast_exec + execute_command) ~0.3µs needs a stamped-by-caller contract; the if…fi >/dev/null +72µs needs the leaf-splice redirect model replaced with entry-bound fd binding. Suggest keeping the issue open as the ledger for those; each wants its own attribution pass per the frozen plan.

  6. added a commit that references this issue on Oct 22, 2026
  7. caomengxuan666 commented on Oct 22, 2026

    @caomengxuan666
    MemberAuthor

    Status (master a42ec29): #989 landed the audit + the largest bounded slice (signal-hint event sampled every 8th boundary; loop-base -15.8%, marginal : 3.43->~3.2us/cmd). Floor is NOT closed — remaining ledger per #989: if-traversal ~3.9us/slot, compound-redirect model (+72us vs plain if), top-level parse ~10us/cmd, dispatch ~20 sequential checks ~2.3us. Keeping open as the tracking issue for those.

  8. caomengxuan666 commented on Oct 24, 2026

    @caomengxuan666
    MemberAuthor

    Floor re-measurement on master a3343c0 (measurement only — frozen item)

    Method: same as the audit pass on this issue — loop-body marginal cost, flat-file
    slope, and the RUBASH_EXEC_PROFILE split. Caveat: this host's rubash binary is a
    debug (unoptimized) build on a shared 2-core VM, so absolute µs are ~30–100× the
    release figures and cannot confirm/deny the release ~2.8–3.2µs floor; primary metric
    below is rusage user time. What transfers across build profiles is the shape:
    relative costs and the profile composition.

    shape rubash debug (user/unit) GNU 5.3.20 (user/unit)
    flat file, 20000× : (all-in slope) 65.7 µs/cmd 1.6 µs/cmd
    marginal 2nd : in a loop body ≈103 µs/cmd —
    true in a loop body 71.9 µs/cmd 2.8 µs/cmd
    x=1 in a loop body ≈32 µs/cmd —
    if :; then :; fi in a loop 169 µs/stmt ≈ 3 slots × ~56 µs 3.6 µs/stmt
    : >/dev/null in a loop 119 µs/cmd 5.1 µs/cmd
    if…fi >/dev/null in a loop 520 µs/stmt (+351 µs vs plain if) 5.9 µs/stmt
    f(){ :; }; f 195 µs/call 5.6 µs/call

    Exec-profile split, flat 20000× : (exec side, per command; buckets are nested
    timers and overlap): total 43.6 µs — dispatch 31.8 · jobs 22.5 · chain 13.8 ·
    matcmd 13.4 · linecmd 7.3 · scans 3.1 · expand 2.2. Same families, same order as
    this issue's ledger: dispatch dominates, linecmd is already small (#981's fast
    render shows), the compound-redirect splice is still the big adder on if…fi >/dev/null.

    Methodological trap found while measuring: an arithmetic-for whose body is a
    single : runs at ≈1 µs/iteration (user) even in debug — that is a trivial-body
    internal fast path, not an execute_command floor sample; do not quote it as one.

    Relation to the recent #914 fix batch (p2): that batch removed shape-dependent
    costs (per-command re-parsing that grows with array size). None of the buckets
    above shrank or vanished as a side effect of that batch: the shape-independent
    floor and p2's shape-dependent tax are separate ledgers, and the floor is
    unaffected by it.

  9. added
    deferred
    on Oct 29, 2026
  10. caomengxuan666 commented on Oct 30, 2026

    @caomengxuan666
    MemberAuthor

    Final attribution — Android/aarch64 release (master c31b357). Closing as architectural.

    Method: brace-expansion loop bodies (for i in {1..20000}; do …; done), N=20000, min of 5 runs, getrusage(RUSAGE_CHILDREN).ru_utime, release build, Termux aarch64 tablet.

    Methodology trap worth recording: for ((i=0;i<N;i++)); do :; done hits rubash's trivial-body fast path (0.75 µs/iter at N=100k — the body really runs, counter verified). A naive arith-for sample therefore reports "marginal 2nd : = 31.9 µs", which is an artifact of comparing a fast-pathed loop against a normal one. Brace loops give the real number; this matches the caveat already noted in the Oct 24 pass.

    shape (per iteration) rubash GNU 5.3.20 ratio
    : (loop base) 9.65 µs 2.00 µs 4.8x
    2nd : in the body (marginal) +5.8 µs +0.9 µs 6.4x
    x=1 9.77 µs 1.40 µs 7.0x
    : >/dev/null 16.89 µs 3.75 µs 4.5x
    if :; then :; fi 26.9 µs 3.08 µs 8.7x
    if :; then :; fi >/dev/null 57.3 µs 3.71 µs 15.4x
    f(){ :; }; f 24.2 µs 4.44 µs 5.4x
    flat file, 100k × : (all-in) 13.8 µs/cmd 1.3 µs/cmd 10.6x

    Reading:

    Decision: the shape-independent floor (~5.8 µs/cmd on ARM) is compositional and architectural; there is no bounded P3 slice left to land. Closing the ledger rather than leaving it open indefinitely. Reopen only when a real workload shows per-command dispatch as its bottleneck (then the reopen comment should include that workload, not a microbenchmark).

  11. removed
    deferred
    on Oct 31, 2026
  12. added this to the 1.5.0 milestone on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P3-lowperformance

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions