wardite 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -16
  3. data/Rakefile +0 -177
  4. data/benchmark/README.md +11 -37
  5. data/benchmark/call_cost_worker.rb +3 -2
  6. data/benchmark/compare.rb +1 -2
  7. data/benchmark/compare_linear_memory_loads.py +144 -0
  8. data/benchmark/i32_memory.wat +5 -1
  9. data/benchmark/investigate_calls.rb +3 -3
  10. data/benchmark/linear_memory_loads.wat +52 -0
  11. data/benchmark/optimization_worker.rb +3 -2
  12. data/benchmark/patches/adr0017-spinel-signed64.patch +190 -0
  13. data/benchmark/patches/adr0017-wardite-split-load64.patch +70 -0
  14. data/benchmark/results/adr0019-startup.json +1687 -0
  15. data/benchmark/results/adr0020-i64.json +2034 -0
  16. data/benchmark/results/adr0021-optimizer.json +5798 -0
  17. data/benchmark/results/adr0022-lazy-mir.json +3057 -0
  18. data/benchmark/results/crc8-stream-repeat.csv +89 -0
  19. data/benchmark/results/crc8-stream.csv +89 -0
  20. data/benchmark/results/linear-memory-loads-a2aa05227.csv +793 -0
  21. data/benchmark/results/linear-memory-loads-stage-b.csv +353 -0
  22. data/benchmark/results/linear-memory-loads.csv +1057 -0
  23. data/benchmark/results/real-workloads-baseline-2026-11-18.json +1741 -0
  24. data/benchmark/results/stack-vm-removal.json +334 -0
  25. data/benchmark/validate_m4.rb +0 -2
  26. data/docs/adr/0017-efflorescence-linear-memory-loads.md +90 -0
  27. data/docs/adr/0018-efflorescence-real-workload-measurement.md +126 -0
  28. data/docs/adr/0019-efflorescence-startup-quick-wins.md +85 -0
  29. data/docs/adr/0020-efflorescence-inline-i64-bit-operations.md +89 -0
  30. data/docs/adr/0021-efflorescence-optimize-real-functions.md +133 -0
  31. data/docs/adr/0022-efflorescence-lazy-function-build.md +111 -0
  32. data/docs/adr/0023-efflorescence-byte-sequences-and-copy-coalescing.md +56 -0
  33. data/docs/efflorescence-ja.md +6 -9
  34. data/docs/efflorescence-linear-memory-loads.md +134 -0
  35. data/docs/efflorescence-m6.md +1 -1
  36. data/docs/efflorescence-mir2-cache.md +7 -0
  37. data/docs/efflorescence-real-workloads.md +312 -0
  38. data/docs/efflorescence-spinel-linear-memory.md +2 -2
  39. data/docs/efflorescence.md +7 -8
  40. data/docs/stack-vm-removal.md +49 -0
  41. data/lib/wardite/cli/cli.rb +24 -21
  42. data/lib/wardite/efflorescence/byte_sequences.rb +80 -0
  43. data/lib/wardite/efflorescence/function_preparer.rb +64 -0
  44. data/lib/wardite/efflorescence/i64_arithmetic.rb +31 -0
  45. data/lib/wardite/efflorescence/inspection.rb +44 -12
  46. data/lib/wardite/efflorescence/ir.rb +33 -10
  47. data/lib/wardite/efflorescence/mir_cache.rb +10 -1
  48. data/lib/wardite/efflorescence/optimizer.rb +151 -42
  49. data/lib/wardite/efflorescence/store_access.rb +17 -0
  50. data/lib/wardite/efflorescence/translation_source.rb +50 -0
  51. data/lib/wardite/efflorescence/translator.rb +43 -13
  52. data/lib/wardite/efflorescence/vm.rb +116 -48
  53. data/lib/wardite/efflorescence.rb +3 -0
  54. data/lib/wardite/load.rb +47 -63
  55. data/lib/wardite/version.rb +1 -1
  56. data/lib/wardite.rb +21 -757
  57. data/scripts/build_spinel_zig.rb +1 -1
  58. data/sig/generated/wardite/cli/cli.rbs +4 -2
  59. data/sig/generated/wardite/efflorescence/byte_sequences.rbs +33 -0
  60. data/sig/generated/wardite/efflorescence/function_preparer.rbs +25 -0
  61. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +19 -0
  62. data/sig/generated/wardite/efflorescence/inspection.rbs +8 -0
  63. data/sig/generated/wardite/efflorescence/ir.rbs +18 -2
  64. data/sig/generated/wardite/efflorescence/optimizer.rbs +41 -3
  65. data/sig/generated/wardite/efflorescence/store_access.rbs +5 -0
  66. data/sig/generated/wardite/efflorescence/translation_source.rbs +34 -0
  67. data/sig/generated/wardite/efflorescence/translator.rbs +11 -1
  68. data/sig/generated/wardite/efflorescence/vm.rbs +19 -9
  69. data/sig/generated/wardite/load.rbs +18 -18
  70. data/sig/generated/wardite.rbs +4 -155
  71. metadata +32 -17
  72. data/benchmark/compare_engines.rb +0 -119
  73. data/benchmark/engine_phases.rb +0 -30
  74. data/lib/wardite/alu_f32.generated.rb +0 -261
  75. data/lib/wardite/alu_f64.generated.rb +0 -261
  76. data/lib/wardite/alu_i32.generated.rb +0 -510
  77. data/lib/wardite/alu_i64.generated.rb +0 -572
  78. data/lib/wardite/convert.generated.rb +0 -340
  79. data/scripts/gen_alu.rb +0 -864
  80. data/scripts/gen_conv.rb +0 -86
  81. data/scripts/templates/alu_module.rb.tmpl +0 -20
  82. data/scripts/templates/conv_module.rb.tmpl +0 -20
  83. data/sig/generated/wardite/alu_f32.generated.rbs +0 -12
  84. data/sig/generated/wardite/alu_f64.generated.rbs +0 -12
  85. data/sig/generated/wardite/alu_i32.generated.rbs +0 -12
  86. data/sig/generated/wardite/alu_i64.generated.rbs +0 -12
  87. data/sig/generated/wardite/convert.generated.rbs +0 -12
@@ -0,0 +1,312 @@
1
+ # ADR 0018の実装とベースライン
2
+
3
+ [ADR 0018](adr/0018-efflorescence-real-workload-measurement.md) の計測手順を実装し、ADR 0019〜0022の比較に使うベースラインを取った。ベースラインの計測では、wardite-benchesの全15ケースで、出力がWasmtimeとiwasmに一致した。
4
+
5
+ ## 実装した変更
6
+
7
+ Wardite:
8
+
9
+ - WASIのコマンドでも、`--mir-profile` を指定すると `Metrics#report` を標準エラーに出す。`_start` が戻ったときと、`proc_exit` で終わったときの両方で出す。Spinel版の `exit` も `ensure` を通ることを確かめた。
10
+ - `Metrics#opcode_report` を足した。実行したopcodeごとに `opcode 名前 回数` の行を、opcodeの番号順に出す。CLIは、`--invoke` とWASIのどちらでも `report` の次にこれを出す。`report` の行は変えていない。
11
+
12
+ wardite-benches:
13
+
14
+ - `script/compare` は、`script/run` を経由せずに `build/wardite --new-engine` を直接起動する。`--bench` の計測から、シェルの起動時間が除かれる。
15
+ - `script/fixtures` は、データを16倍にした `data-4m.bin` と `data-4m.b64`、画像の一辺を4倍にした `rgb-1024.png`、空の `empty` も作る。
16
+ - `script/build` は、Warditeをビルドしたときの `git describe` と `spinel --version` を `build/wardite.version` に書く。
17
+ - `script/baseline.sh` は、各ケースで結果を照合してから3つのエンジンをhyperfineで測る。WASIのケースでは、`WARDITE_TRACE` によるロード時間と、`--mir-profile` によるMIR命令の実行回数も残す。
18
+
19
+ ## 計測の条件
20
+
21
+ ADR 0018を書いた後に、mainに [PR #53](https://github.com/udzura/wardite/pull/53) が入り、CIのSpinelの固定版が `58ee636` から `a2aa052` に上がった。ベースラインは、この時点のmainで取り直した。
22
+
23
+ - Wardite:`v1.2.2-10-g5c96407-dirty`。mainの `5c96407` に、この変更(`--mir-profile` の出力だけ)を加えた作業ツリーでビルドした
24
+ - Spinel:`spinel 2026.10.25+6092 (a2aa05227) [apple clang 21.0.0 (cc)]`、`-O2`、既定の整数overflow検査
25
+ - wardite-benches:`1155333-dirty`。`1155333` に、この変更を加えた作業ツリー
26
+ - マシン:Apple M3 Pro、macOS 26.6.2
27
+ - 比較の相手:iwasm 2.4.5 の `--interp`、wasmtime 28.0.0 (2e584e841 2024-12-20)
28
+ - 計測:hyperfine 2.0.0
29
+ - WasmのSHA-256は生値のJSONに記録した
30
+
31
+ 時間は、各エンジンを2回ウォームアップした後の9回の中央値である。Warditeのロードは、`WARDITE_TRACE` の `load_from_buffer` を9回取った中央値である。RSSは、hyperfineが測った9回の最大値である。
32
+
33
+ ## ベースライン
34
+
35
+ | ケース | Wardite | うちロード | WarditeのRSS | Wasmtime | iwasm | iwasmとの比 | MIR命令の実行回数 |
36
+ |---|---:|---:|---:|---:|---:|---:|---:|
37
+ | `xorshift`(WAT、1億回) | 2.386 s | — | 6.1 MiB | 268.1 ms | 915.4 ms | 2.6 | — |
38
+ | `xorshift`(Rust、1億回) | 1.597 s | — | 6.2 MiB | 446.6 ms | 1.285 s | 1.2 | — |
39
+ | `splitmix64`(1,000万回) | 1.561 s | — | 6.1 MiB | 22.2 ms | 123.1 ms | 12.7 | — |
40
+ | `mandelbrot_f32`(200×150、100回) | 2.321 s | — | 7.8 MiB | 15.6 ms | 23.9 ms | 97.1 | — |
41
+ | `mandelbrot_f64`(同上) | 2.367 s | — | 7.9 MiB | 14.6 ms | 22.3 ms | 106.3 | — |
42
+ | `monte_carlo_pi`(100万点) | 2.458 s | — | 7.6 MiB | 14.6 ms | 55.4 ms | 44.3 | — |
43
+ | `crc8_stream`(16 MiB) | 531.7 ms | — | 6.0 MiB | 48.8 ms | 82.9 ms | 6.4 | — |
44
+ | `crc8_stream`(16 MiB、grow後) | 657.3 ms | — | 8.2 MiB | 48.4 ms | 83.7 ms | 7.8 | — |
45
+ | Base64 encode、空 | 41.3 ms | 33.4 ms | 52.6 MiB | 14.4 ms | 12.3 ms | 3.3 | 2,731 |
46
+ | Base64 encode、256 KiB | 93.8 ms | 29.9 ms | 60.8 MiB | 15.6 ms | 16.6 ms | 5.7 | 4,322,082 |
47
+ | Base64 encode、4 MiB | 838.3 ms | 28.8 ms | 117.0 MiB | 44.6 ms | 75.1 ms | 11.2 | 69,041,163 |
48
+ | Base64 decode、256 KiB | 90.3 ms | 31.0 ms | 58.7 MiB | 18.5 ms | 16.9 ms | 5.3 | 4,934,288 |
49
+ | Base64 decode、4 MiB | 759.4 ms | 28.5 ms | 95.2 MiB | 52.8 ms | 78.2 ms | 9.7 | 78,828,412 |
50
+ | PNG、256×256 | 98.9 ms | 61.5 ms | 106.4 MiB | 15.4 ms | 18.2 ms | 5.4 | 3,779,456 |
51
+ | PNG、1024×1024 | 417.2 ms | 57.5 ms | 114.6 MiB | 19.2 ms | 62.9 ms | 6.6 | 54,437,427 |
52
+
53
+ i32だけのループ(`xorshift`)はiwasmの1.2〜2.6倍で、i64を使う `splitmix64` は12.7倍、浮動小数点を使うものは44〜106倍だった。WASIのサンプルは5.3〜11.2倍、バイト単位の読み出しが中心の `crc8_stream` は6.4〜7.8倍だった。
54
+
55
+ 既定サイズの入力では、ロードとMIR準備がBase64で約30 ms、PNGで約62 msかかり、プロセス全体の3分の1(Base64)から6割(PNG)にあたる。16倍の入力では、ロードとMIR準備以外がBase64で96〜97%、PNGで86%を占める。
56
+
57
+ WarditeのRSSは、空の入力のBase64で52.6 MiB、PNGで106〜115 MiBだった。ADR 0018の予備計測(Wardite `5baaf38`、Spinel `58ee636`)より、空の入力で6.1 MiB、4 MiBのencodeで32.2 MiB大きい。予備計測とは、Wardite、Spinel、encodeのゲスト(末尾のLFの追加)が違う。どれが原因かは調べていない。
58
+
59
+ 既定サイズの入力で実行したMIR命令の内訳は次のとおりである。
60
+
61
+ | 入力 | 実行回数の多い命令(全命令に対する割合) |
62
+ |---|---|
63
+ | Base64 encode、256 KiB | `I32_ADD` 21.0%、`COPY` 10.7%、`I64_SHR_U` 10.1%、`I32_LOAD8_U` 8.1%、`I32_STORE8` 8.1%、`I32_AND` 7.1% |
64
+ | Base64 decode、256 KiB | `COPY` 25.1%、`I32_ADD` 16.4%、`I64_OR` 9.7%、`I64_SHL` 8.9%、`BR_IF` 7.5%、`I32_LOAD8_U` 7.1% |
65
+ | PNG、256×256 | `I32_ADD` 30.3%、`COPY` 23.0%、`I32_LOAD8_U` 10.7%、`I32_STORE8` 7.3%、`BR_IF` 6.1%、`I32_MUL` 5.2% |
66
+
67
+ 生値は [real-workloads-baseline-2026-11-18.json](../benchmark/results/real-workloads-baseline-2026-11-18.json) にある。hyperfineの各回の時間とRSS、ロード時間の各回の値、WASIのケースのopcodeごとの実行回数を含む。
68
+
69
+ ## 再計測の手順
70
+
71
+ wardite-benchesのルートで、CIの `SPINEL_REF` と同じSpinelを `SPINEL` に指定する。
72
+
73
+ ```sh
74
+ SPINEL=/path/to/spinel script/build
75
+ script/fixtures
76
+ script/baseline.sh build/results/after
77
+ ```
78
+
79
+ 前後を比べるときは、`build/wardite` を別名でコピーしてから、もう一方をビルドする。そのうえで、2つのバイナリを1回のhyperfineに並べ、順序を入れ替えてもう1回測る([ADR 0018](adr/0018-efflorescence-real-workload-measurement.md))。
80
+
81
+ ## ADR 0019 の再計測(2026-11-18)
82
+
83
+ Wardite `c48e371` と最終候補 `0710c91` を、Apple M3 Pro、macOS 27.2.1、Spinel `a2aa05227` の `-O2` と既定のoverflow検査で比較した。wardite-benches は `f07c454`。上のベースラインと同じ15ケースを使った。Rust製Wasm 7本は上の表とSHA-256が異なるため、今回の比較では変更前後で同一のWasmと入力を使い、変更前の値も取り直した。
84
+
85
+ ### 変更を分けた計測
86
+
87
+ 同じmainから、(1) store命令の判定だけをHashにしたもの、(2) semantic IRの複製を初回の検査まで遅らせたもの、(3) 両方を組み合わせたものをビルドした。表は各実験の順方向での中央値。RSSはプロセスごとの最大RSSの中央値。逆順の値と各回の生値も [計測JSON](../benchmark/results/adr0019-startup.json) に残した。
88
+
89
+ | 変更 | Base64 encode 256 KiB ロード | 同 RSS | PNG 256×256 ロード | 同 RSS |
90
+ |---|---:|---:|---:|---:|
91
+ | 表引きのみ | 21.70 → 20.67 ms | 58.75 → 58.95 MiB | 46.59 → 44.12 ms | 103.78 → 103.81 MiB |
92
+ | IR複製の遅延のみ(試作) | 23.01 → 20.78 ms | 58.75 → 54.33 MiB | 45.76 → 44.35 ms | 103.81 → 95.00 MiB |
93
+ | 両方(試作) | 21.52 → 19.88 ms | 58.75 → 54.42 MiB | 44.53 → 41.05 ms | 103.78 → 95.08 MiB |
94
+
95
+ 表引きは `MEMORY_WIDTHS.merge(...)` でstore系の全opcodeを含むHashを作り、`STORE_CODES.include?(code)` を `key?(code)` に置き換えて測った。Base64とPNGでRSSの改善条件を満たさず、逆順ではロード時間の短縮も変更前の標準偏差の2倍を下回ったため見送った。
96
+
97
+ ### Spinelビルドで見つかった数値ベンチの回帰
98
+
99
+ 初版 `4561231` は、両順で `splitmix64` のプロセス全体が6.0〜6.6%遅くなった。正順のハードウェア命令数の中央値は287.50億 → 287.68億、CPUサイクルは43.50億 → 46.15億だった。arm64の逆アセンブルでは `execute_flat` が9,200命令から9,920命令に増え、変更前にあった `FramePool#acquire` への直接呼び出しが消えていた。
100
+
101
+ 保存する宣言を定義する `Translator::ModuleSource` を専用ファイルに分け、VM定義の後で読み込んだ。Rubyの処理は同じだが、このSpinelビルドでは `execute_flat` が9,200命令に戻り、`FramePool#acquire` の直接呼び出しも戻った。この候補について全15ケースを再計測した。JSONの `before_file_split` が初版、`final` が分割後である。
102
+
103
+ ### 最終版の比較方法
104
+
105
+ 検査用IRの遅延再生成を実装した。cold loadでは、元のコードセクションと翻訳に必要な宣言を保存し、semantic IRをその場で最適化する。`semantic_ir`、`optimized_ir`、`dump_mir` の初回呼び出しでは、この入力から検査用IRを再構成する。ロード後にホストがStoreや関数本体を変更してもロード時のMIRを表示し、start関数を再実行しない。MIR2ヒットでは従来の関数単位の復元を使う。
106
+
107
+ 全15ケースで、2つのバイナリをhyperfine 2.0.0の同じ呼び出しに並べ、各2回ウォームアップ後に9回計測した。順序を逆にして同じ手順を繰り返した。WASIのロードは別プロセスで `WARDITE_TRACE=1` の `load_from_buffer` を同じ順序・回数で測った。数値のケースではプロセス全体の時間、WASIでは全体の時間・ロード・RSSを比較する。以下の表の矢印は変更前から変更後への変化を表す。
108
+
109
+ たとえば、Base64 encode の比較は wardite-benches のルートで次のように再現できる。`BEFORE` と `AFTER` には各チェックアウトを同じSpinelでビルドしたバイナリの絶対パスを指定する。
110
+
111
+ ```sh
112
+ guest=build/rust/wasm32-wasip1/release/examples/base64_codec.wasm
113
+ for order in forward reverse; do
114
+ first=$BEFORE second=$AFTER
115
+ if [ "$order" = reverse ]; then first=$AFTER second=$BEFORE; fi
116
+ hyperfine -N --warmup 2 --runs 9 --input build/fixtures/data.bin \
117
+ --export-json "encode-$order.json" \
118
+ "$first --new-engine -- $guest encode" "$second --new-engine -- $guest encode"
119
+ done
120
+ ```
121
+
122
+ | WASIケース | ロード 正順 (ms) | ロード 逆順 (ms) | RSS 正順 (MiB) | RSS 逆順 (MiB) |
123
+ |---|---:|---:|---:|---:|
124
+ | base64-encode-empty | 20.73 → 19.66 | 20.63 → 19.80 | 50.52 → 46.05 | 50.50 → 46.05 |
125
+ | base64-encode | 20.61 → 19.55 | 20.47 → 19.87 | 58.75 → 54.42 | 58.75 → 54.44 |
126
+ | base64-encode-4m | 20.53 → 19.64 | 20.48 → 19.71 | 114.88 → 109.38 | 114.89 → 109.38 |
127
+ | base64-decode | 20.21 → 19.57 | 20.27 → 19.47 | 56.20 → 51.98 | 56.22 → 51.97 |
128
+ | base64-decode-4m | 20.48 → 19.92 | 20.65 → 19.56 | 93.09 → 88.73 | 93.08 → 88.73 |
129
+ | png | 42.32 → 40.04 | 42.56 → 40.12 | 103.80 → 95.11 | 103.78 → 95.11 |
130
+ | png-1024 | 42.91 → 40.57 | 42.74 → 40.63 | 112.05 → 103.34 | 112.03 → 103.34 |
131
+
132
+ | ケース | プロセス全体 正順 (ms) | プロセス全体 逆順 (ms) |
133
+ |---|---:|---:|
134
+ | xorshift-wat | 1776.69 → 1773.93 | 1774.05 → 1774.76 |
135
+ | xorshift | 1181.62 → 1183.44 | 1181.25 → 1183.76 |
136
+ | splitmix64 | 1153.41 → 1150.77 | 1159.77 → 1146.68 |
137
+ | mandelbrot-f32 | 1642.62 → 1668.09 | 1643.04 → 1668.18 |
138
+ | mandelbrot-f64 | 1663.57 → 1665.91 | 1661.49 → 1662.35 |
139
+ | monte-carlo-pi | 1756.38 → 1744.17 | 1759.31 → 1743.40 |
140
+ | crc8-stream | 389.26 → 389.75 | 388.38 → 388.89 |
141
+ | crc8-stream-grown | 485.17 → 485.04 | 488.22 → 490.12 |
142
+ | base64-encode-empty | 24.41 → 23.55 | 24.47 → 23.51 |
143
+ | base64-encode | 63.57 → 61.55 | 63.67 → 61.46 |
144
+ | base64-encode-4m | 635.21 → 631.43 | 634.88 → 630.39 |
145
+ | base64-decode | 58.83 → 56.75 | 58.51 → 57.16 |
146
+ | base64-decode-4m | 558.82 → 558.69 | 559.19 → 558.83 |
147
+ | png | 65.39 → 62.73 | 64.99 → 62.46 |
148
+ | png-1024 | 306.26 → 305.56 | 305.81 → 304.49 |
149
+
150
+ ### 判定
151
+
152
+ WASI全7ケースで両順ともロード時間の中央値が短くなり、RSSの差は全ケースで変更前の標準偏差の2倍を超えた。ロード時間の短縮幅が2標準偏差に届かなかったのは既定サイズのBase64 encodeの逆順だけだった。
153
+
154
+ 全体時間が回帰閾値を超えたのは `mandelbrot-f32` で、正順は1.6%、逆順は1.5%遅くなった。今回はWASIのロード時間とRSSの改善を優先し、この回帰を許容してレビューに進める。初版にあった `splitmix64` の回帰は解消した。
155
+
156
+ ### 正しさの検証
157
+
158
+ - wardite-benchesの全15ケースで `script/compare --iwasm` が一致した。WASI全7ケースの `--mir-profile` は合計と全opcodeの内訳が同じWasmを使った変更前と一致した。
159
+ - Base64、PNG、Ruby 3.4.2の `ruby.wasm`(SHA-256 `1ac460e944a54c17529aa05e6cad4575aab6d3d2466745f935237f7a46351e98`)で、既存の `mir_generation_digest.rb` による変更前後のダイジェストが一致した。プロファイル有効・無効の両方でsemantic IR、検査用optimized IR、実行用IRのbytecodeと副表を照合した。
160
+ - `bundle exec rake test` は316件、うちネイティブ用87件は `make spinel-test RUBY="bundle exec ruby"` で別途全件成功した。`bundle exec rake check` は型エラーなし。`WARDITE_VERIFY_MIR_CACHE_PARITY=1 bundle exec rake parity` はCRuby・Spinelともに436 fixtureの実行とMIR2復元を確認した。
161
+
162
+ ## ADR 0020 の再計測(2026-11-18)
163
+
164
+ [ADR 0020](adr/0020-efflorescence-inline-i64-bit-operations.md) の対象は、i64 のビット演算・比較・シフト・回転と整数幅の変換の計25命令。Spinel の生成Cで、シフト・回転の補助関数の引数と戻り値はすべて `sp_int` になった。検証済みのレジスタは `fetch` で読み、シフト量は下位6ビットを使う。
165
+
166
+ Wardite は main `5f461c4` と実装 `b949062`、wardite-benches は `f07c454`。Apple M3 Pro、macOS 27.2.1、Spinel `a2aa05227` の `-O2` と既定の整数overflow検査で比較した。ADR 0018 の初回記録とはRust製Wasmのハッシュが異なるため、両バイナリで同じWasmと入力を使ってベースラインを取り直した。
167
+
168
+ 表はプロセス全体の実行時間の中央値。hyperfine 2.0.0 の `-N` で各バイナリを2回ウォームアップして9回測り、逆順でも繰り返した。各セルは main → 実装の順。入力、WasmのSHA-256、実行引数、順序別の全サンプルと最大RSSは[計測JSON](../benchmark/results/adr0020-i64.json)に記録した。
169
+
170
+ | ケース | mainを先に測定 (ms) | 実装を先に測定 (ms) |
171
+ |---|---:|---:|
172
+ | xorshift-wat | 1789.46 → 1798.84 (+0.5%) | 1811.69 → 1806.17 (-0.3%) |
173
+ | xorshift | 1210.92 → 1202.06 (-0.7%) | 1219.34 → 1209.56 (-0.8%) |
174
+ | splitmix64 | 1165.36 → 633.45 (-45.6%) | 1165.87 → 642.64 (-44.9%) |
175
+ | mandelbrot-f32 | 1716.20 → 1700.52 (-0.9%) | 1703.66 → 1696.44 (-0.4%) |
176
+ | mandelbrot-f64 | 1698.65 → 1706.60 (+0.5%) | 1711.40 → 1727.36 (+0.9%) |
177
+ | monte-carlo-pi | 1790.82 → 1679.52 (-6.2%) | 1827.34 → 1683.66 (-7.9%) |
178
+ | crc8-stream | 397.67 → 401.55 (+1.0%) | 394.95 → 397.75 (+0.7%) |
179
+ | crc8-stream-grown | 493.78 → 477.97 (-3.2%) | 495.60 → 475.87 (-4.0%) |
180
+ | base64-encode-empty | 25.84 → 25.73 (-0.4%) | 25.35 → 25.50 (+0.6%) |
181
+ | base64-encode | 64.55 → 52.76 (-18.3%) | 67.18 → 53.23 (-20.8%) |
182
+ | base64-encode-4m | 649.03 → 419.62 (-35.3%) | 638.38 → 414.07 (-35.1%) |
183
+ | base64-decode | 63.88 → 46.77 (-26.8%) | 60.78 → 48.19 (-20.7%) |
184
+ | base64-decode-4m | 569.03 → 360.47 (-36.7%) | 563.53 → 360.29 (-36.1%) |
185
+ | png | 66.48 → 66.35 (-0.2%) | 66.59 → 66.35 (-0.4%) |
186
+ | png-1024 | 315.35 → 309.87 (-1.7%) | 314.98 → 311.10 (-1.2%) |
187
+
188
+ 16倍入力のBase64は、encode・decodeともに両順で35%以上短縮し、中央値の差は変更前の標準偏差の10倍を超えた。ADR 0018初回の中央値はencode 838.3 ms、decode 759.4 msだった。今回の改善率は同一ゲストを使った直前のmainとの比較から算出した。
189
+
190
+ 既定サイズのBase64もencodeで18〜21%、decodeで21〜27%短縮した。数値ケースでは `splitmix64` が45%前後、`monte-carlo-pi` が6〜8%短縮した。全15ケースでWasmtime・iwasmと出力が一致した。
191
+
192
+ 初回に回帰閾値を超えたのは `crc8-stream` の正順だけで、3.88 ms増(+1.0%、変更前の2.04標準偏差)だった。同じバイナリで各31回の追加計測を行うと、正順は395.25 → 399.91 ms(+1.2%、1.35標準偏差)、逆順は415.06 → 403.11 ms(−2.9%)となり、両順とも閾値内だった。初回と追加計測は別々にJSONに収録した。
193
+
194
+ ## ADR 0021 の段階別比較(2026-11-18)
195
+
196
+ [ADR 0021](adr/0021-efflorescence-optimize-real-functions.md) の準備段階1・2は、生成コードの一致とテストの成功で判定した。副表に残るCOPYを保つ修正前には4つの回帰テストがすべて失敗し、修正後はすべて成功した。Base64、PNG、i32ベンチ、Ruby 3.4.2のWasmでデコード結果・semantic MIR・実行用MIRのダイジェストがmainと一致した。段階2の変更はparityランナーだけである。
197
+
198
+ | 段階 | PR | ビルド内容 |
199
+ |---|---|---|
200
+ | 基準 | [#57](https://github.com/udzura/wardite/pull/57) マージ後のmain | `cbb0664` |
201
+ | 1 | [#58](https://github.com/udzura/wardite/pull/58) | 副表の読み出しを生存解析に含める |
202
+ | 2 | [#59](https://github.com/udzura/wardite/pull/59) | 全無効・既定の両構成でparityを実行する |
203
+ | 3 | [#60](https://github.com/udzura/wardite/pull/60) | `3f1172a`、128命令までのstore関数を対象にする |
204
+ | 4 | [#61](https://github.com/udzura/wardite/pull/61) | `a458739`、同じ上限で動的呼び出しを持つ関数を含める |
205
+ | 5 | [#62](https://github.com/udzura/wardite/pull/62) | `db5a180`、i64を含め、準備コストに応じて対象関数とcopyパスの範囲を調整する |
206
+ | 6 | [#63](https://github.com/udzura/wardite/pull/63) | `13410da`、段階5の条件でfloat関数を含める |
207
+
208
+ 段階5以降、従来から対象だったi32関数は全パスを使う。新しく対象となった関数は16命令以下なら全パス、17〜128命令なら呼び出しを含まないループがある場合、129〜1,536命令ならそのループが64命令以上の場合に最適化する。16命令を超える対象でcopyパスが行うのは、結果を直後のCOPYの宛先へ直接書く変換である。これには副表の読み出しを含む全関数の使用回数と分岐先の検査を使う。EH関数とインライン展開のcallee側は従来の除外を維持した。生成コードが変わる段階3〜6でMIR2のコード版を1ずつ上げた。
209
+
210
+ ### 実行したMIR命令数
211
+
212
+ Spinelの `--mir-profile` で取得した動的な命令数。速度計測は計装を外して別に行った。全15ケースのopcode別カウントと公開時点で測定済みの実行時間は[計測JSON](../benchmark/results/adr0021-optimizer.json)に収録した。
213
+
214
+ | WASIケース | main | 段階3 | 段階4 | 段階5 | 段階6 |
215
+ |---|---:|---:|---:|---:|---:|
216
+ | base64-encode-empty | 2,740 | 2,631 | 2,626 | 2,638 | 2,638 |
217
+ | base64-encode | 4,322,095 | 4,321,849 | 4,321,829 | 3,885,040 | 3,885,040 |
218
+ | base64-encode-4m | 69,041,176 | 69,040,890 | 69,040,866 | 62,050,498 | 62,050,498 |
219
+ | base64-decode | 4,934,301 | 4,933,919 | 4,933,898 | 3,972,806 | 3,972,806 |
220
+ | base64-decode-4m | 78,828,425 | 78,828,006 | 78,827,981 | 63,448,963 | 63,448,963 |
221
+ | png | 3,777,248 | 3,546,710 | 3,546,681 | 3,119,683 | 3,119,683 |
222
+ | png-1024 | 54,413,537 | 51,130,112 | 51,130,081 | 44,309,090 | 44,309,090 |
223
+
224
+ 16倍のBase64 encodeで `COPY` は7,342,460 → 351,789回、`I32_ADD` は14,506,254 → 6,641,832回になった。decodeではそれぞれ19,750,763 → 4,371,311回、12,933,462 → 5,942,794回だった。PNG 1024では12,563,900 → 2,461,607回、17,541,431 → 9,580,270回に減った。
225
+
226
+ WASIの2本のゲストはfloat関数を使わず、段階5・6でWASI全7ケースのopcode別回数は一致した。段階6で変化したのは数値ベンチで、mandelbrot-f32は14,017,517 → 10,150,898命令、mandelbrot-f64は14,084,730 → 10,177,721命令、monte-carlo-piは41,000,011 → 31,000,009命令となった。
227
+
228
+ ### プロセス全体の速度
229
+
230
+ Apple M3 Pro、macOS 27.2.1、Spinel `a2aa05227`、`-O2` と既定の整数overflow検査、wardite-benches `f07c454`。全段階で同じゲスト・入力を使用した。ケースごとにhyperfine 2.0.0の `-N` でmain → 3 → 4 → 5 → 6の順と逆順を測った。各バイナリを2回ウォームアップし、9回の中央値を比較した。表の各セルはmain → 段階6の時間。JSONには測定済みの各段階の時間・最大RSS、入力のハッシュ、引数も含めた。
231
+
232
+ | ケース | 正順 (ms) | 逆順 (ms) |
233
+ |---|---:|---:|
234
+ | splitmix64 | 901.65 → 693.16 (-23.1%) | 897.86 → 697.21 (-22.3%) |
235
+ | mandelbrot-f32 | 2479.27 → 2347.13 (-5.3%) | 2437.69 → 2337.11 (-4.1%) |
236
+ | mandelbrot-f64 | 2460.07 → 2365.24 (-3.9%) | 2491.27 → 2367.97 (-4.9%) |
237
+ | base64-encode-empty | 38.84 → 42.47 (+9.3%) | 40.96 → 44.65 (+9.0%) |
238
+ | base64-encode | 74.92 → 78.11 (+4.2%) | 75.75 → 78.58 (+3.7%) |
239
+ | base64-encode-4m | 604.06 → 574.17 (-4.9%) | 597.80 → 608.75 (+1.8%) |
240
+ | base64-decode | 70.36 → 70.85 (+0.7%) | 70.35 → 70.66 (+0.4%) |
241
+ | base64-decode-4m | 517.93 → 463.40 (-10.5%) | 520.05 → 456.20 (-12.3%) |
242
+ | png | 102.43 → 107.17 (+4.6%) | 106.09 → 109.08 (+2.8%) |
243
+ | png-1024 | 441.37 → 408.09 (-7.5%) | 447.01 → 407.30 (-8.9%) |
244
+
245
+ ### 公開時点の判定
246
+
247
+ 全15ケースで正しさと命令数を確認した。上表は公開時点で正順・逆順とも完了した10ケース。残るケースの速度判定は保留した。Base64 decodeの16倍入力は両順で約12%、PNG 1024も両順で短縮した。Base64 encodeの16倍入力は正順で速く、逆順で遅くなった。既定サイズのencodeとPNG、空入力では準備コストを含む全体時間が増えた。ADR 0021単体では全受け入れ条件の合格に至っていない。
248
+
249
+ ADR 0022の初回呼び出し時のMIR準備が入れば、実行しない関数の最適化にかかる時間を省ける可能性がある。併用時の実測は今後の比較対象とする。
250
+
251
+ ### 動作確認
252
+
253
+ - store・動的呼び出し・i64・floatの各段階で、wardite-benches全15ケースの結果をWasmtimeおよびiwasmと照合した。WASIは出力バイト列、数値はビット列が一致した。
254
+ - 段階1〜6のCRuby・Spinelで構成マトリクスを通した。段階2〜6の `rake parity` では、両ランタイムで436 fixtureを既定・全無効の両構成で実行し、各構成でMIR2の作成・復元も検証した。
255
+ - Ruby 4.0.9 Wasm(SHA-256 `cb740da111ff6ef951f51fac3296466777cc7b9b48d327f8d8089edeae71a7a4`)で `-e`、`require_relative`、`exit 7` の標準出力と終了コードがWasmtimeと一致した。各段階3〜6のSpinelと最終版のCRubyを既定・全無効の両方で比較した。
256
+ - 最終版は `bundle exec rake test` 323件(ネイティブ用87件は別実行)、`bundle exec rake check` 型エラー0件、`make spinel-test RUBY="bundle exec ruby"` 87件成功。全無効で生成する実行用MIRのダイジェストはBase64・PNG・mandelbrot・splitmix64・Ruby 3.4.2の5入力でmainと一致した。
257
+
258
+ ## ADR 0022とADR 0021を併用した結果
259
+
260
+ 同じWasm・入力を使い、0021導入前のmain(`cbb0664`)、0021単体(`13410da`、#63の先端 `3990251` と実装は同一)、0021+0022段階A(`e79000f`)を比較した。表の矢印はこの順序。Apple M3 Pro、macOS 27.2.1、Spinel `a2aa05227` の `-O2`、wardite-benches `f07c454` を使用した。
261
+
262
+ MIR2キャッシュとプロファイラは無効。hyperfine 2.0.0から各バイナリを直接起動し、正順と逆順のそれぞれでウォームアップ2回・計測9回を実行した。表は両順の中央値の平均。全体時間には初回呼び出しでの準備も含む。WASIのロード時間は `WARDITE_TRACE=1` の別プロセスで同じ回数測った。[実行順ごとの生値、入力SHA-256、出力照合](../benchmark/results/adr0022-lazy-mir.json)
263
+
264
+ | ケース | 全体 ms(main → 0021 → 併用) | 対main | 対0021 | ロード ms | ピークRSS MiB |
265
+ |---|---:|---:|---:|---:|---:|
266
+ | base64-encode-empty | 44.30 → 49.78 → 37.73 | -14.8% | -24.2% | 38.15 → 40.84 → 24.53 | 46.02 → 48.48 → 42.27 |
267
+ | base64-encode | 78.16 → 80.77 → 68.13 | -12.8% | -15.7% | 34.17 → 36.79 → 23.93 | 54.41 → 56.85 → 51.22 |
268
+ | base64-encode-4m | 600.66 → 588.68 → 565.11 | -5.9% | -4.0% | 29.97 → 32.74 → 21.09 | 109.35 → 113.02 → 106.26 |
269
+ | base64-decode | 68.20 → 69.78 → 60.98 | -10.6% | -12.6% | 32.99 → 36.13 → 23.64 | 51.95 → 54.35 → 49.42 |
270
+ | base64-decode-4m | 518.83 → 455.13 → 448.69 | -13.5% | -1.4% | 29.64 → 33.23 → 20.52 | 88.70 → 91.22 → 84.55 |
271
+ | png | 99.58 → 103.78 → 90.68 | -8.9% | -12.6% | 66.17 → 73.69 → 42.06 | 95.11 → 99.38 → 93.72 |
272
+ | png-1024 | 429.64 → 390.22 → 379.04 | -11.8% | -2.9% | 57.19 → 62.99 → 36.68 | 103.32 → 106.41 → 100.88 |
273
+ | xorshift-wat | 2545.23 → 2549.97 → 2555.06 | +0.4% | +0.2% | — | 6.33 → 6.38 → 6.31 |
274
+ | xorshift | 1701.92 → 1698.99 → 1698.58 | -0.2% | -0.0% | — | 6.42 → 6.49 → 6.44 |
275
+ | splitmix64 | 892.43 → 685.27 → 685.07 | -23.2% | -0.0% | — | 6.37 → 6.45 → 6.41 |
276
+ | mandelbrot-f32 | 2395.37 → 2306.19 → 2332.76 | -2.6% | +1.2% | — | 8.05 → 8.12 → 8.10 |
277
+ | mandelbrot-f64 | 2405.04 → 2317.46 → 2362.70 | -1.8% | +2.0% | — | 8.06 → 8.12 → 8.09 |
278
+ | monte-carlo-pi | 2349.91 → 2214.37 → 2285.22 | -2.8% | +3.2% | — | 7.90 → 7.95 → 7.95 |
279
+ | crc8-stream | 563.13 → 516.93 → 512.89 | -8.9% | -0.8% | — | 6.36 → 6.44 → 6.38 |
280
+ | crc8-stream-grown | 674.05 → 627.81 → 627.18 | -7.0% | -0.1% | — | 8.47 → 8.56 → 8.50 |
281
+
282
+ 0021で起動時間が増えていた既定入力のBase64 encode・PNGは、併用すると0021導入前のmainよりそれぞれ12.8%、8.9%短い。0021単体との比較でも、既定サイズのBase64 encode・decode・PNGはロード時間とRSSが両順で旧標準偏差の2倍を超えて減った。全体時間の中央値も両順で減ったが、encodeの逆順ではこの差が2倍に届かなかった。16倍の3入力は全体時間・RSSとも両順で低下した。
283
+
284
+ 数値ベンチでは、0021単体比でmandelbrot-f32が1.2%、f64が2.0%、monte-carlo-piが3.2%遅くなった。monte-carlo-piは両順で0021の中央値+2標準偏差を超えた。main比ではこの3件とも速い。xorshift-watの全体時間はmain比0.4%増で両順の退行閾値を超えた。小規模な数値ゲストの一部ではmain比のRSSにも数十KiBの増加がある。WASIの速度とメモリは改善したが、全ケースの無退行条件は満たしていない。
285
+
286
+ 翻訳時間は同じ段階AのSpinelバイナリに計時だけを足し、既定入力で別途測った。Base64ではロード23.52 msのうち翻訳15.59 ms、PNGでは41.87 msのうち29.54 ms(各9回の中央値)。段階Bの判断には、ロード時の型検査だけにかかる時間と、実行対象を再翻訳するコストを合わせて測る必要がある。今回の実装は段階Aまでとした。
287
+
288
+ 全15ケースをwardite-benchesの `script/compare --iwasm` で照合した(WASIは出力バイト列、iwasmの浮動小数点表示は有効数字7桁)。Base64・PNG・Ruby 3.4.2 Wasmではプロファイル有効・無効の両方で、全関数のbytecode・レジスタ・副表を含むダイジェストがeager版と一致した。キャッシュの形式・バージョンは従来と同じ。
289
+
290
+ ### CLIでのキャッシュの選び方
291
+
292
+ CLI変更を含む `5f9ca01` でBase64 encode(256 KiB)のプロセス全体を別途計測した。各組で空の一時ディレクトリを作り、表の順に実行した。2組のウォームアップ後に9組を採取し、各回で標準出力のSHA-256を照合した。時間は標準出力のパイプ読み取りを含む中央値。
293
+
294
+ | オプション | キャッシュ状態 | 全体時間 |
295
+ |---|---|---:|
296
+ | 指定なし | ディスクキャッシュなし・遅延仕上げ | 63.1 ms |
297
+ | `--mir-eager` | ディスクキャッシュなし・全関数仕上げ | 72.8 ms |
298
+ | `--mir-cache DIR` | ミス・全関数仕上げと保存 | 81.3 ms |
299
+ | `--mir-cache DIR` | ヒット・呼んだ関数を復元 | 43.4 ms |
300
+
301
+ ### 手元での再計測
302
+
303
+ wardite-benchesのルートから、3つのリビジョンを同じSpinelでビルドした実行ファイルの絶対パスを `MAIN`、`ADR21`、`STACK` に設定して実行する。下記はBase64 encodeの既定入力。逆順でも実行すると起動順の影響を確認できる。
304
+
305
+ ```sh
306
+ guest=build/rust/wasm32-wasip1/release/examples/base64_codec.wasm
307
+ hyperfine -N --warmup 2 --runs 9 --input build/fixtures/data.bin \
308
+ "$MAIN --new-engine -- $guest encode" \
309
+ "$ADR21 --new-engine -- $guest encode" \
310
+ "$STACK --new-engine -- $guest encode"
311
+ WARDITE_TRACE=1 "$STACK" --new-engine -- "$guest" encode < build/fixtures/data.bin > /dev/null
312
+ ```
@@ -16,13 +16,13 @@ CRubyのホストAPIは `Memory#data` のStringを引き続き使える。両ホ
16
16
 
17
17
  ## ビルド
18
18
 
19
- WarditeのCIは [Spinel upstreamの `58ee6361`](https://github.com/matz/spinel/commit/58ee6361642befd3770516137c41a6553f0262d1) に固定する。このリビジョンは `IO::Buffer.new` の領域割り当て失敗を `IO::Buffer::AllocationError` として伝える。CIはコミットとビルド環境でツールチェーンを識別し、Actionsキャッシュ、`main` の成功済み実行のArtifact、ソースからのビルドの順で取得する。インストール一式の `spinel.tar.gz` は、保存元のActions実行ページからダウンロードできる。Artifactにはリポジトリの既定の保持期間を適用する。
19
+ WarditeのCIは [Spinel upstreamの `a2aa05227`](https://github.com/matz/spinel/commit/a2aa0522761482cd83c09fc289eb1e91609e433a) に固定する。このリビジョンは `IO::Buffer.new` の領域割り当て失敗を `IO::Buffer::AllocationError` として伝える。CIはコミットとビルド環境でツールチェーンを識別し、Actionsキャッシュ、`main` の成功済み実行のArtifact、ソースからのビルドの順で取得する。インストール一式の `spinel.tar.gz` は、保存元のActions実行ページからダウンロードできる。Artifactにはリポジトリの既定の保持期間を適用する。
20
20
 
21
21
  同じリビジョンをローカルでビルドするには、クローン先のパスを `spinel_src` に指定し、Warditeのルートから実行する。
22
22
 
23
23
  ```sh
24
24
  git clone https://github.com/matz/spinel.git "$spinel_src"
25
- git -C "$spinel_src" switch --detach 58ee6361642befd3770516137c41a6553f0262d1
25
+ git -C "$spinel_src" switch --detach a2aa0522761482cd83c09fc289eb1e91609e433a
26
26
  make -C "$spinel_src" deps
27
27
  make -C "$spinel_src" all
28
28
  make spinel SPINEL="$spinel_src/bin/spinel"
@@ -10,16 +10,14 @@ The Phase 1 limitations below are historical.
10
10
 
11
11
  This document records Phase 1 (`67fbfae`). Current Efflorescence enables the
12
12
  [Phase 2 optimizations](efflorescence-phase2.md) by default; `--mir-disable all`
13
- selects unoptimized MIR lowered to the same flat bytecode executor. Wardite's default is still the stack VM.
13
+ selects unoptimized MIR lowered to the same flat bytecode executor.
14
14
 
15
- Efflorescence is Wardite's opt-in, pure Ruby register interpreter. The default
16
- remains `Wardite::Runtime`, the stack interpreter. Both engines share the binary
17
- loader, decoded Wasm instructions, store and exports; execution is independent.
15
+ Efflorescence is Wardite’s pure Ruby register interpreter.
18
16
 
19
17
  ```sh
20
18
  wasm-tools parse benchmark/i32_mix.wat -o build/i32_mix.wasm
21
- ruby -Ilib exe/wardite --new-engine --no-wasi --invoke inline_mix build/i32_mix.wasm 100000 123
22
- ruby -Ilib exe/wardite --new-engine --no-wasi --invoke call_mix build/i32_mix.wasm 100000 123
19
+ ruby -Ilib exe/wardite --no-wasi --invoke inline_mix build/i32_mix.wasm 100000 123
20
+ ruby -Ilib exe/wardite --no-wasi --invoke call_mix build/i32_mix.wasm 100000 123
23
21
  # Both: return value: I32(-424175775)
24
22
  ```
25
23
 
@@ -27,7 +25,7 @@ The equivalent Ruby API is:
27
25
 
28
26
  ```ruby
29
27
  require "wardite"
30
- instance = Wardite.new(path: "build/i32_mix.wasm", enable_wasi: false, engine: :efflorescence)
28
+ instance = Wardite.new(path: "build/i32_mix.wasm", enable_wasi: false)
31
29
  instance.runtime.call("call_mix", [100000, 123]) # Wardite::I32, same as Runtime
32
30
  instance.runtime.call_unboxed("call_mix", [100000, 123]) # 3870791521 (Integer)
33
31
  ```
@@ -107,10 +105,11 @@ decoding and validation limitations still apply.
107
105
  bundle exec rake test
108
106
  bundle exec rake check
109
107
  make spinel-test RUBY="bundle exec ruby"
108
+ # Historical comparison, run from commit 0c806bc
110
109
  ruby benchmark/compare_engines.rb 100000 123
111
110
  ```
112
111
 
113
- Tests compile the **entire unchanged** `benchmark/i32_mix.wat` using the existing
112
+ The Phase 1 tests described below compiled the **entire unchanged** `benchmark/i32_mix.wat` using the existing
114
113
  `wasm-tools parse` flow. No prebuilt fixture or reduced workload is substituted.
115
114
  All eight specified input pairs run against both exports, both engines and an
116
115
  independent modulo-based reference on CRuby and Spinel. Additional tests cover
@@ -0,0 +1,49 @@
1
+ # 旧スタックVM削除の計測
2
+
3
+ 2026年11月19日。比較元は `0c806bc`。削除前後とも既定のEfflorescenceで同じWasmを実行した。
4
+
5
+ ## 計測条件
6
+
7
+ - macOS 27.2.1 arm64、CRuby 4.0.9(YJIT無効)、Spinel `2026.10.25+6092`(`a2aa05227`、Apple clang 21.0.0)。
8
+ - ビルドは同じマシンの別ディレクトリで、出力バイナリを毎回削除して `make spinel` を各4回実行した。プロジェクト既定の `--strict-require -O2` を使用し、Spinel本体の構築は含めていない。
9
+ - 実行時間は各条件で1回ウォームアップしてから各9回計測した。各ペア内の実行順は交互に入れ替えた。外部からの計測はプロセス起動から終了まで。MIRディスクキャッシュ、プロファイラは使用していない。
10
+ - 整数演算には `benchmark/i32_mix.wat` の `call_mix`、メモリ読み取りには `benchmark/i32_memory.wat` の `array_scan` を使った。どちらもseedは123。各回、i32のラップを考慮した独立の計算式と戻り値を照合した。
11
+ - ロード済みVMの計測では各サンプルで新しいプロセスとVMを作り、同じ関数を1回呼んでMIRを準備した。続けて3回呼び、単調時計で測った所要時間を3で割った。Spinelでは両リビジョンに同じRubyドライバをコンパイルした。
12
+
13
+ ## 結果
14
+
15
+ 中央値。差は削除前に対する増減で、時間のマイナスは短縮を表す。CRubyの起動計測には `ruby --disable-gems --disable-yjit -Ilib -rwardite -e ''` を使った。
16
+
17
+ | 計測対象 | 削除前 | 削除後 | 差 |
18
+ | --- | ---: | ---: | ---: |
19
+ | Spinelフルビルド | 23.071 s | 19.787 s | -14.2% |
20
+ | Spinel実行ファイル | 5,241,928 B | 4,991,320 B | −4.8% |
21
+ | CRuby起動+ `require "wardite"` | 24.847 ms | 21.902 ms | -11.9% |
22
+ | Spinel CLI · `call_mix` × 0 | 4.465 ms | 4.417 ms | -1.1% |
23
+ | Spinel CLI · `call_mix` × 100,000 | 6.234 ms | 6.115 ms | -1.9% |
24
+ | Spinel CLI · `array_scan` × 1,000 | 9.838 ms | 9.912 ms | +0.8% |
25
+ | CRuby CLI · `call_mix` × 100,000 | 117.440 ms | 114.806 ms | -2.2% |
26
+ | CRuby CLI · `array_scan` × 1,000 | 301.837 ms | 298.209 ms | -1.2% |
27
+ | Spinel ロード済み · `call_mix` × 30,000 | 0.640 ms | 0.647 ms | +1.0% |
28
+ | Spinel ロード済み · `array_scan` × 300 | 1.750 ms | 1.839 ms | +5.0% |
29
+ | CRuby ロード済み · `call_mix` × 30,000 | 19.756 ms | 20.017 ms | +1.3% |
30
+ | CRuby ロード済み · `array_scan` × 300 | 74.809 ms | 75.044 ms | +0.3% |
31
+
32
+ Spinelのビルドは全4組で短縮し、中央値で3.28秒短くなった。CRubyではライブラリ読み込みまでが約2.95 ms短縮した。ロード済みVMでは、整数演算・メモリ走査のいずれも高速化を確認できなかった。Spinelのメモリ走査は9組すべてで削除後のほうが遅かった。
33
+
34
+ ### Spinelのメモリ走査の再測定
35
+
36
+ 同じ `array_scan` を20,000周に延ばし、各1回ウォームアップ後に順序を交互にして15組を測った。CLIはプロセス全体、ロード済みVMは上記と同じく3回の平均を1サンプルとした。
37
+
38
+ | 計測対象 | 削除前 | 削除後 | 差 |
39
+ | --- | ---: | ---: | ---: |
40
+ | 配布用CLI | 126.267 ms | 125.838 ms | -0.3% |
41
+ | ロード済みVM(計測用実行ファイル) | 117.455 ms | 122.826 ms | +4.6% |
42
+
43
+ 長い走査でも計測用バイナリでは4.6%の遅延が再現した。配布用CLIでの差は0.3%だった。Spinelはエントリポイントごとにネイティブコードを生成するため、この結果から配布用CLIでも同じ遅延が起きるとは言えない。遅延の原因は特定していない。
44
+
45
+ [全サンプルの秒数](../benchmark/results/stack-vm-removal.json)
46
+
47
+ ## 動作確認
48
+
49
+ Spinelネイティブ版を指定した全334テスト(37,298 assertions)が成功し、省略は0件。`bundle exec rake check` と `bundle exec rake parity` も成功した。命令検証では436件をWasmtimeと照合し、CRuby・Spinelの両方で既定の最適化と全最適化無効の条件を確認した。
@@ -22,7 +22,6 @@ module Wardite
22
22
  attr_reader :memsize #: Integer
23
23
  attr_reader :wasi #: bool
24
24
  attr_reader :yjit #: bool
25
- attr_reader :new_engine #: bool
26
25
  attr_reader :disabled_optimizations #: Array[Symbol]
27
26
  attr_reader :mir_dump #: bool
28
27
  attr_reader :mir_profile #: bool
@@ -37,13 +36,12 @@ module Wardite
37
36
  @invoke = @mapdir = nil
38
37
  @wasi = true
39
38
  @yjit = false
40
- @new_engine = false
41
39
  @disabled_optimizations = []
42
40
  @mir_dump = false
43
41
  @mir_profile = false
44
42
  @mir_cache = nil
45
43
  @mir_cache_verify = :checksum
46
- @mir_cache_eager = false
44
+ @mir_eager = false
47
45
  @snapshot = nil
48
46
  @memsize = 1
49
47
  parser = RUBY_ENGINE == "spinel" ? SpinelOptions : OptionParser
@@ -61,16 +59,14 @@ module Wardite
61
59
  opts.on("--no-wasi", "Disable WASI feature") {|_v|
62
60
  @wasi = false
63
61
  }
64
- opts.on("--new-engine", "Use the Efflorescence register VM") {|_v|
65
- @new_engine = true
66
- }
62
+ opts.on("--new-engine", "Use Efflorescence (kept for compatibility)") { }
67
63
  opts.on("--mir-disable NAME", "Disable copy, immediate, branch, superinstruction, inline, frames, or all (repeatable)") {|v|
68
64
  @disabled_optimizations << v.to_sym
69
65
  }
70
66
  opts.on("--mir-dump", "Print semantic and optimized MIR") { @mir_dump = true }
71
67
  opts.on("--mir-profile", "Instrument MIR execution (do not use for timing)") { @mir_profile = true }
72
- opts.on("--mir-cache DIR", "Reuse MIR2 in a local directory") { |dir| @mir_cache = dir }
73
- opts.on("--mir-cache-eager", "Restore and check every cached MIR2 function at load time") { @mir_cache_eager = true }
68
+ opts.on("--mir-cache DIR", "Build all functions on a miss; restore on first call on a hit") { |dir| @mir_cache = dir }
69
+ opts.on("--mir-eager", "--mir-cache-eager", "Prepare all MIR at load time (default: on first call)") { @mir_eager = true }
74
70
  opts.on("--mir-cache-verify MODE", "Verify the MIR2 cache with checksum (default) or sha256") do |mode|
75
71
  unless mode == "checksum" || mode == "sha256"
76
72
  raise Wardite::ArgumentError, "--mir-cache-verify expects checksum or sha256"
@@ -87,11 +83,8 @@ module Wardite
87
83
  opts.on("FILE.wasm") { }
88
84
  end
89
85
  options.parse!(args)
90
- if !new_engine && (!disabled_optimizations.empty? || mir_dump || mir_profile || mir_cache)
91
- raise "MIR options require --new-engine"
92
- end
93
- if snapshot && (!new_engine || !mir_cache || mir_dump || mir_profile)
94
- raise "--snapshot requires --new-engine and --mir-cache, without --mir-dump or --mir-profile"
86
+ if snapshot && (!mir_cache || mir_dump || mir_profile)
87
+ raise "--snapshot requires --mir-cache, without --mir-dump or --mir-profile"
95
88
  end
96
89
  @file = args[0] || raise("require file argument")
97
90
  @args = (args[1..-1] || [])
@@ -124,9 +117,9 @@ module Wardite
124
117
  # @rbs return: ::Wardite::Instance
125
118
  def new_instance
126
119
  ins = File.open(file, "rb") do |f|
127
- Wardite::BinaryLoader.load_from_buffer(f, enable_wasi: wasi, engine: new_engine ? :efflorescence : :stack,
120
+ Wardite::BinaryLoader.load_from_buffer(f, enable_wasi: wasi,
128
121
  disabled_optimizations: disabled_optimizations, mir_profile: mir_profile,
129
- mir_cache: mir_dump ? nil : mir_cache, mir_cache_verify: @mir_cache_verify, mir_cache_eager: @mir_cache_eager)
122
+ mir_cache: mir_dump ? nil : mir_cache, mir_cache_verify: @mir_cache_verify, mir_cache_eager: @mir_eager || !!mir_dump)
130
123
  end
131
124
  if memsize > 1
132
125
  ins.store.memories[0].grow(memsize)
@@ -172,13 +165,18 @@ module Wardite
172
165
  instance = new_instance
173
166
  ret = instance.runtime.call(invoke, args)
174
167
  runtime = instance.runtime
175
- if runtime.is_a?(Wardite::Efflorescence::VM)
176
- $stderr.puts runtime.dump_mir if mir_dump
177
- $stderr.puts runtime.metrics.report if mir_profile
178
- end
168
+ $stderr.puts runtime.dump_mir if mir_dump
169
+ print_mir_profile(runtime.metrics) if mir_profile
179
170
  $stderr.puts "return value: #{ret.inspect}"
180
171
  end
181
172
 
173
+ # @rbs metrics: Wardite::Efflorescence::Metrics
174
+ # @rbs return: void
175
+ def print_mir_profile(metrics)
176
+ $stderr.puts metrics.report
177
+ $stderr.puts metrics.opcode_report
178
+ end
179
+
182
180
  # @rbs return: void
183
181
  def invoke_wasi
184
182
  instance = new_instance #: ::Wardite::Instance
@@ -209,7 +207,7 @@ module Wardite
209
207
  def start_wasi(instance)
210
208
  runtime = instance.runtime
211
209
  directory = snapshot
212
- if directory && runtime.is_a?(Wardite::Efflorescence::VM)
210
+ if directory
213
211
  cache = runtime.mir_cache
214
212
  if cache && instance.wasi && !instance.start_section
215
213
  require "wardite/efflorescence/snapshot" unless RUBY_ENGINE == "spinel"
@@ -222,7 +220,12 @@ module Wardite
222
220
  runtime.snapshot = recorder
223
221
  end
224
222
  end
225
- runtime.call(:_start, [])
223
+ begin
224
+ runtime.call(:_start, [])
225
+ ensure
226
+ # proc_exit leaves through exit, which runs this on CRuby and Spinel.
227
+ print_mir_profile(runtime.metrics) if mir_profile
228
+ end
226
229
  end
227
230
 
228
231
  # @rbs return: String?
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Wardite
5
+ module Efflorescence
6
+ # Select four-instruction sequences after register validation. Keeping all
7
+ # operand slots preserves PCs for branches, catches, and snapshot frames.
8
+ class ByteSequences
9
+ include Opcode
10
+ LENGTH = 4
11
+ # Indexed in the same order as Opcode::SEQUENCE_HEADS.
12
+ FORWARDED_READS = [2, 3, 3, 2].freeze #: Array[Integer]
13
+
14
+ # @rbs function: Function
15
+ # @rbs return: void
16
+ def self.fuse(function)
17
+ instructions = function.instructions
18
+ targets = nil #: Array[bool]?
19
+ pc = 0
20
+ while pc + LENGTH <= instructions.size
21
+ first = instructions.fetch(pc)
22
+ opcode = match(first[0], first, instructions.fetch(pc + 1), instructions.fetch(pc + 2), instructions.fetch(pc + 3))
23
+ if opcode >= 0
24
+ entries = targets || entry_points(function)
25
+ targets = entries
26
+ unless entries[pc + 1] || entries[pc + 2] || entries[pc + 3]
27
+ first[0] = opcode
28
+ pc += LENGTH
29
+ next
30
+ end
31
+ end
32
+ pc += 1
33
+ end
34
+ end
35
+
36
+ # Used both when lowering and when checking a restored bytecode body.
37
+ # @rbs head: Integer
38
+ # @rbs first: Instruction
39
+ # @rbs second: Instruction
40
+ # @rbs third: Instruction
41
+ # @rbs fourth: Instruction
42
+ # @rbs return: Integer
43
+ def self.match(head, first, second, third, fourth)
44
+ if head == I32_ADD && second[0] == I32_LOAD8_U && third[0] == I32_STORE8 && fourth[0] == I32_ADD_IMM &&
45
+ second[2] == first[1] && third[1] == second[1]
46
+ return I32_TABLE_STORE8
47
+ end
48
+ if head == I32_ADD_IMM && second[0] == I32_LOAD8_U && third[0] == I32_ADD && fourth[0] == I64_LOAD8_U &&
49
+ second[2] == first[1] && third[3] == second[1] && fourth[2] == third[1] &&
50
+ second[3] == 0 && fourth[3] == 0
51
+ return I64_BYTE_LOOKUP
52
+ end
53
+ if head == I32_ADD_IMM && second[0] == I32_LOAD8_U && third[0] == I32_MUL_IMM && fourth[0] == I32_ADD &&
54
+ second[2] == first[1] && third[2] == second[1] && fourth[3] == third[1] && second[3] == 0 &&
55
+ third[3].between?(0, 0xffffffff)
56
+ return I32_BYTE_MUL_ADD
57
+ end
58
+ if head == I32_LOAD8_U && second[0] == I32_ADD && third[0] == I32_STORE8 && fourth[0] == I32_ADD_IMM &&
59
+ second[2] == first[1] && third[1] == second[1] && first[3] == 0 && third[3] == 0
60
+ return I32_BYTE_ADD_STORE
61
+ end
62
+ -1
63
+ end
64
+
65
+ # @rbs function: Function
66
+ # @rbs return: Array[bool]
67
+ def self.entry_points(function)
68
+ targets = Array.new(function.instructions.size, false) #: Array[bool]
69
+ function.instructions.each do |insn|
70
+ position = Opcode.target_position(insn)
71
+ targets[insn[position]] = true if position >= 0
72
+ end
73
+ function.handlers.each do |handler|
74
+ handler.clauses.each { |clause| targets[clause.target_pc] = true }
75
+ end
76
+ targets
77
+ end
78
+ end
79
+ end
80
+ end