Skip to content

perf: hot-loop micro-op dispatch is ~6x slower than GNU bash across the board (1771-case bench, Linux) #3232

Open

Description

@caomengxuan666

Summary

Benchmarked niu 1.5.0 (rubash engine) vs GNU bash 5.3 on Ubuntu 26.04 with the new hotloop category of unixwin/shellbench — 1771 per-operation cases, each isolating one operation inside an identical for loop wrapper. Median ratio 5.79x slower; 1525/1569 comparable cases behind; only 37 ahead.

Raw data: results/linux-x86_64/20261110-083223-niu-linux-hotloop.json vs 20261109-132813-gnu-linux-hotloop.json in unixwin/shellbench.

Where the time goes (median ns/op ratio, niu/bash)

area cases median ratio notes
test / [ / [[ family 358 ~9x largest single cluster — every loop-condition eval pays it
control flow (if/case/nesting) 82 9.8x
readonly / shift / getopts 15 13–18x worst尖点: readonly pair ops hit 92x, typeset -i 80x
eval / declare / typeset ~60 6–9x
mapfile / readarray 15 7.4x single ops up to 6–7 ms/op
parameter expansion ${} 208 5.8x all operator families uniformly slow
help / churn / local / fn ~75 5.8–6.6x

What is NOT the problem

  • Loop machinery is fine: bare : floor case — niu is ~5x faster than bash.
  • Command substitution is a big win: $(...)/backquote cases run 5–25x faster than bash (in-process comsub, no fork). Worth keeping.
  • External spawn (/bin/true, /bin/echo) is roughly par, slightly slower.

So the gap is concentrated in per-operation dispatch: expansion, test/cond evaluation, variable ops (declare/readonly/shift/unset), builtin invocation overhead — not loop control or process creation.

Repro shape

# ~13x slower end-to-end (user-reported, confirmed):
i=1; time { while [ $i -le 10000 ]; do printf '%d\n' "$i"; ((i++)); done; } > /dev/null
# bash: 0.04s   niu: 0.67s
time { i=1; while [ $i -le 10000 ]; do :; ((i++)); done; }
# bash: 0.02s   niu: 0.10s

Suggested investigation order

  1. test/[/[[ eval path (358 cases @ ~9x — biggest cluster)
  2. variable-mutation builtins: readonly/shift/getopts/declare/typeset/unset
  3. ${parameter} expansion entry cost
  4. mapfile/readarray (ms-scale per op suggests allocation/copy churn)

Every case is a minimal reproducer — e.g. hotloop.cond_tint_* are test $i -lt 5 in a loop; hotloop.arith_* isolate each arithmetic form.

Generated with Devin

Activity

  1. added
    performance
    area:builtins
    area:expansion
    platform:linux
    P2-medium
    on Nov 10, 2026
  2. added
    deferred
    on Nov 10, 2026
  3. added this to the 1.5.1 milestone on Nov 10, 2026
  4. caomengxuan666 commented on Nov 10, 2026

    @caomengxuan666
    MemberAuthor

    归宿:性能专项 / 阶段 3(同 C1–C4 机制评估的归宿口径)。本单只做调查 + 修复排序方案,不派修复波、不改产品代码;调查结果回填本评论。

    立案卫生已补:标签 performance + area:builtins + area:expansion + platform:linux + P2-medium + deferred(阶段 3,不在本轮关闭),assignee caomengxuan666,milestone 1.5.1。

    调查车道已派,工作区 ~/workspace/fix-3232/,终报 REPORT.md。

  5. caomengxuan666 commented on Nov 10, 2026

    @caomengxuan666
    MemberAuthor

    调查完成,终报:~/workspace/fix-3232/REPORT.md(机制图 §3、盒上实测 §5、修复排序 §6)。

    盒上实测(Simular box,rubash release beab07dc vs 同机编译 GNU bash 5.3.0,66 操作 × 3 rep 中位;原始日志 fix-3232/data/m3232-*.txt):

    • 全部操作中位比 9.56×;单子 5.79×。本报偏高主因共享盒争用(rep 启动 load 0.98–2.11)+ 被测二进制 LTO 关——倍率取上限估计,簇排序与单子一致,不推翻 5.79×。
    • 簇中位:test/[/[[ 9.14×(单子 ~9× ✓)、${} 展开 9.36×(单子 5.8×)、declare 簇 13.57×、readonly 47.36×(assign 27×、-p/space 67×)、getopts 20×、shift 7–8×、mapfile/read 2.45×(形态与单子不同,待复测)。
    • 唯一胜项:$(true) 命令替换 RB 快 10×(单子 5–25× ✓)。

    最硬的一条证据(#815 profiler):test 重循环里 [ 内建本体仅占 2.2%(每调用 0.72µs),解释器自身(funnel + 展开 + 驱动)占 97.8%;展开重循环 99.1% vs 0.9%。test 簇的 ~9× 几乎全是脚手架税,不是 test 本体慢——修 test 本体最多买 2%。

    对单子一句的订正:“循环机械没问题”不成立。for ((…)) 驱动 8.9×、while ((…)) 条件驱动 27.1×、while : + […] 17.5×;而 : 与 true 作循环体几乎无差——: 本体便宜,贵的是驱动。单子“裸 : 快 5×”应是内建本体的孤立计时。

    修复排序(§6;结构性只给设计与量级估计):P0 每命令脚手架税(字面词快路径 + 内建分发短路 + VarTable 快速哈希;底座 9×→4–5×,全簇减半)→ P1 readonly / declare -i 本体税(67×/42×→10×)→ P2 getopts(20×→10×)→ P3 [[/cond(15–20×→10×)→ P4 算术条件驱动(27×→~9×)→ P5 ${} 各臂快路径 → P6 mapfile 先复测再定级。

    归宿仍为性能专项 / 阶段 3,本轮不关闭。另声明:本结论不构成启动全量 VM(#2826–#2828)的论据;与 FNPROTO 函数体编译(1.2–1.7×)正交——分发层税需 funnel/展开重构,不是 VM 能顺带解决的。

  6. caomengxuan666 commented on Nov 10, 2026

    @caomengxuan666
    MemberAuthor

    性能债登记(队长,2026-11-11,源 PR #2968 E4 终裁):

    PR #2968(declare 族三刀 + E4)以结构证据合入 d9555905,测量案关闭。其性能残差显式记入本单 P1 面(declare 族):

    • 归因代数闭合钉死成本落点在 declare 族成功路径,三刀恰削该落点每呼叫固定成本;守恒全过,全部测量读数与「残差 ≲1.5%」一致。
    • 未能以 ±1% 判词线结案的原因是仪器判别力不足(窗偏移 ±3.5pp、基底漂移 ≈1.2pp、ctrl_true 本身同走内建分发可能被污染),不是测得回归。
    • 后续以 shellbench 套件仪器在本面复测(declare / typeset / readonly -p / declare -i 各形),不再用 ad-hoc 计时窗。若套件复测坐实残差 >1.5%,按 P1 序在本单内续修。
  7. caomengxuan666 commented on Nov 20, 2026

    @caomengxuan666
    MemberAuthor

    1.5.1 队长分拣(2026-11-20):本条为 P2/P3,不阻塞 1.5.1,拟顺延至发版后处理;点火时以 owner 终裁为准。当前先行处理主干基线波与里程碑内队长修复候选。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P2-mediumarea:builtinsarea:expansiondeferredperformanceplatform:linux

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions