Skip to content

perf(assoc): cut per-op fixed costs on the k[i] interleave tail - #997

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/assoc-tail2
Oct 22, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/assoc-tail2

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Assoc interleave remainder was per-op fixed costs (data structures already O(1), verified by counters: subscript lane 100% hit, typed reads 100%, no linear scans). 8 files: dynamic-name class gate before the UNSET_DYNAMIC_VARS hash, fused attr_of reads for shell_variable_value + element read/write paths (one probe instead of 4-5 SipHashes, same verdicts), plain-literal subscript fast lane, literal+$name value splice for the assignment funnel with synthetic-word shortcut, all whitelist (doubt falls through). Tests: lib 689 + fuzz + assoc/subscript/array cli suites green; GNU + base parity probes byte-identical (2 pre-existing divergences unchanged). Interleave 8k, my A/B: base 148ms (2.55x) -> branch 129ms (2.22x) vs bash 58ms (~-13%). Residual ~2.2x is per-command dispatch + allocator + SipHash floor.

assoc交错 `m[k$i]=v$i; t=${m[k$i]}` 仍 ~2.4-3.5x (release best-of-3,
4k/8k/16k)。读写分离 + 30x gdb 采样 + 临时原子计数确认数据结构
侧已是 O(1)(下标快道 100% 命中、typed cell 100% 命中、
assoc_value_at 零调用、dedupe/写 buffer 均摊),剩余是每次
assoc 操作周围的通用展开/执行固定开销:每次读写 ~7-9 次
SipHash 查表 + 若干小分配 + TLS 帧。

GNU owner: assoc.c assoc_insert / assoc_reference (O(1) 单哈希);
subst.c:11063 expand_subscript_string (W_NOSPLIT2 | W_NOPROCSUB,
walker 逐字节拷贝); variables.c SHELL_VAR flag word。

最小刀(白名单/融合/摊销,语义存疑一律回慢路):

- K1 literal-subscript fast lane (expand_subscript_string_inner):
  闭集合纯字节下标直接返回,跳 mask 分配 + walker + marker 链。
  GNU subst.c:11063,无 introducer/引号/转义则恒等。
- K3/K4 写/读 attr_of 一次抓取 (readonly/assoc/integer/nameref),
  替代 3-4 次 is_marked_var;无 nameref 标记时跳过 resolution
  及其 NotNameref String 分配(borrow 替代),marked 走原路径
  并对 resolved 名重取 flags。
- K5 dynamic_parameter_value 前置 match:非动态名零哈希直接
  None;四种组合(动态与否 x 标记与否)结论与原来完全一致。
- K6 shell_variable_value 融合:nameref/array/declared-unset/
  assoc/array 五处查表合一,is_marked_array_var 分支折叠进
  fallthrough(原分支与 fallthrough 逐字节同构)。
- K7 `name=literal+$name` splice (fast_assignment_plain_splice):
  赋值 funnel 内联展开在该形状下恒等于拼接(同一
  dynamic -> shell -> env 链 + exact_case_env_var 回退 +
  marker 全拒绝 + nounset/动态拒收),跳过 hoist/walker/tilde;
  prepare 对字面 key + 可 splice value 直接合成 synthetic 词,
  省掉整词 roundtrip。

数字(release best-of-3,Termux aarch64):
- interleave 16k: 357ms -> 292ms (3.19x -> 2.65x)
- write-only 16k: 232ms -> 201ms (2.86x -> 2.64x)
- read-only 16k: 445ms -> 371ms (2.95x -> 2.77x)
- 8k 微分解:wfull assoc -20%,wval -17%,rlit -30%

验证:cargo fmt clean;cargo test --lib 688 通过;fuzz
typed_cell_coherence 2 通过;assoc 相关集成 143 通过;
parity 探针与 master 二进制逐字节一致(两处差异经 master
二进制确认为既有行为)。剩余 ~2.6x 为通用逐命令/逐词脚手架
+ 分配器 + SipHash,需更大手术,不属本刀范围。
@caomengxuan666
caomengxuan666 merged commit 992e577 into master Oct 22, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/assoc-tail2 branch October 22, 2026 06:00
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