wardite 0.9.0 → 1.0.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 (153) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +80 -1
  5. data/Rakefile +5 -0
  6. data/benchmark/README.md +500 -0
  7. data/benchmark/build_call_profile.rb +42 -0
  8. data/benchmark/call_cost_worker.rb +27 -0
  9. data/benchmark/compare.rb +93 -0
  10. data/benchmark/compare_call_types.rb +212 -0
  11. data/benchmark/compare_dispatch.rb +123 -0
  12. data/benchmark/compare_engines.rb +119 -0
  13. data/benchmark/compare_optimizations.rb +139 -0
  14. data/benchmark/engine_phases.rb +30 -0
  15. data/benchmark/figures/README.md +35 -0
  16. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  17. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  18. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  19. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  20. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  21. data/benchmark/float_m5.wat +42 -0
  22. data/benchmark/i32_call_arities.wat +89 -0
  23. data/benchmark/i32_calls.wat +174 -0
  24. data/benchmark/i32_memory.wat +58 -0
  25. data/benchmark/i32_mix.wat +80 -0
  26. data/benchmark/i64_m4.wat +55 -0
  27. data/benchmark/investigate_calls.rb +231 -0
  28. data/benchmark/investigate_generated_guards.rb +218 -0
  29. data/benchmark/m3_call_worker.rb +38 -0
  30. data/benchmark/measure_m1.rb +51 -0
  31. data/benchmark/measure_m2.rb +55 -0
  32. data/benchmark/measure_m3_calls.rb +89 -0
  33. data/benchmark/measure_m4.rb +134 -0
  34. data/benchmark/measure_m5_boundary.rb +41 -0
  35. data/benchmark/measure_wasi_write.rb +75 -0
  36. data/benchmark/optimization_worker.rb +23 -0
  37. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  38. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  39. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  40. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  41. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  42. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  43. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  44. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  45. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  46. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  47. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  48. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  49. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  50. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  51. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  52. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  53. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  54. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  55. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  56. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  57. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  58. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  59. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  60. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  61. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  62. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  63. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  64. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  65. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  66. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  67. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  68. data/benchmark/summarize_call_profile.py +54 -0
  69. data/benchmark/validate_m1_eh.rb +34 -0
  70. data/benchmark/validate_m2.rb +119 -0
  71. data/benchmark/validate_m3.rb +44 -0
  72. data/benchmark/validate_m4.rb +95 -0
  73. data/benchmark/validate_m5.rb +108 -0
  74. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  75. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  76. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  77. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  78. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  79. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  80. data/docs/efflorescence-call-arities.md +115 -0
  81. data/docs/efflorescence-call-investigation.md +155 -0
  82. data/docs/efflorescence-call-transfer.md +128 -0
  83. data/docs/efflorescence-call-types.md +102 -0
  84. data/docs/efflorescence-coverage-ja.md +351 -0
  85. data/docs/efflorescence-generated-c-guards.md +405 -0
  86. data/docs/efflorescence-inlining.md +165 -0
  87. data/docs/efflorescence-ja.md +155 -0
  88. data/docs/efflorescence-m1.md +160 -0
  89. data/docs/efflorescence-m2.md +233 -0
  90. data/docs/efflorescence-m3.md +131 -0
  91. data/docs/efflorescence-m4.md +88 -0
  92. data/docs/efflorescence-m5.md +82 -0
  93. data/docs/efflorescence-m6.md +95 -0
  94. data/docs/efflorescence-operand-decoding.md +105 -0
  95. data/docs/efflorescence-phase2-ja.md +225 -0
  96. data/docs/efflorescence-phase2.md +240 -0
  97. data/docs/efflorescence-register-initialization.md +75 -0
  98. data/docs/efflorescence.md +144 -0
  99. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  100. data/lib/wardite/alu_f32.generated.rb +1 -0
  101. data/lib/wardite/alu_f64.generated.rb +1 -0
  102. data/lib/wardite/alu_i32.generated.rb +14 -9
  103. data/lib/wardite/alu_i64.generated.rb +18 -20
  104. data/lib/wardite/cli/cli.rb +61 -28
  105. data/lib/wardite/const.rb +1 -0
  106. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  107. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  108. data/lib/wardite/efflorescence/inspection.rb +165 -0
  109. data/lib/wardite/efflorescence/ir.rb +1151 -0
  110. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  111. data/lib/wardite/efflorescence/register_validation.rb +290 -0
  112. data/lib/wardite/efflorescence/store_access.rb +271 -0
  113. data/lib/wardite/efflorescence/translator.rb +859 -0
  114. data/lib/wardite/efflorescence/vm.rb +1006 -0
  115. data/lib/wardite/efflorescence/wasi.rb +108 -0
  116. data/lib/wardite/efflorescence.rb +11 -0
  117. data/lib/wardite/errors.rb +20 -0
  118. data/lib/wardite/float_bits.rb +46 -0
  119. data/lib/wardite/instruction.rb +35 -4
  120. data/lib/wardite/leb128.rb +33 -12
  121. data/lib/wardite/load.rb +203 -138
  122. data/lib/wardite/value.rb +169 -175
  123. data/lib/wardite/version.rb +1 -1
  124. data/lib/wardite/wasi/consts.rb +1 -0
  125. data/lib/wardite/wasm_module.rb +6 -3
  126. data/lib/wardite.rb +184 -70
  127. data/rbs_collection.lock.yaml +36 -96
  128. data/scripts/gen_alu.rb +66 -10
  129. data/sig/generated/wardite/cli/cli.rbs +8 -6
  130. data/sig/generated/wardite/const.rbs +2 -0
  131. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  132. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  133. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  134. data/sig/generated/wardite/efflorescence/ir.rbs +881 -0
  135. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  136. data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
  137. data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
  138. data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
  139. data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
  140. data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
  141. data/sig/generated/wardite/errors.rbs +12 -0
  142. data/sig/generated/wardite/float_bits.rbs +30 -0
  143. data/sig/generated/wardite/instruction.rbs +22 -1
  144. data/sig/generated/wardite/leb128.rbs +3 -1
  145. data/sig/generated/wardite/load.rbs +29 -4
  146. data/sig/generated/wardite/value.rbs +70 -11
  147. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  148. data/sig/generated/wardite.rbs +49 -12
  149. data/sig/vernier.rbs +2 -2
  150. data/spinel/README.md +30 -0
  151. data/spinel/bin/wardite.rb +6 -0
  152. data/spinel/spin.toml +7 -0
  153. metadata +126 -3
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "rbconfig"
5
+ require "shellwords"
6
+ require "tmpdir"
7
+
8
+ # All four loops have the same control flow and increment by one. Table calls
9
+ # add a selector, and host calls use the same one-argument ABI in both paths.
10
+ calls = {
11
+ "direct_defined" => "call $defined",
12
+ "direct_host" => "call $host",
13
+ "indirect_defined" => "i32.const 0 call_indirect (type $increment)",
14
+ "indirect_host" => "i32.const 1 call_indirect (type $increment)"
15
+ }
16
+ loops = calls.map do |name, call|
17
+ <<~WAT
18
+ (func (export "#{name}") (param $n i32) (param $sum i32) (result i32)
19
+ block $done
20
+ loop $repeat
21
+ local.get $n i32.eqz br_if $done
22
+ local.get $sum #{call} local.set $sum
23
+ local.get $n i32.const 1 i32.sub local.set $n
24
+ br $repeat
25
+ end
26
+ end
27
+ local.get $sum)
28
+ WAT
29
+ end
30
+ wat = <<~WAT
31
+ (module
32
+ (type $increment (func (param i32) (result i32)))
33
+ (import "env" "increment" (func $host (type $increment)))
34
+ (func $defined (type $increment) local.get 0 i32.const 1 i32.add)
35
+ (table 2 funcref)
36
+ (elem (i32.const 0) func $defined $host)
37
+ #{loops.join})
38
+ WAT
39
+
40
+ root = File.expand_path("..", __dir__)
41
+ worker = File.join(__dir__, "m3_call_worker.rb")
42
+ rounds = Integer(ENV.fetch("ROUNDS", "100000"))
43
+ runs = Integer(ENV.fetch("RUNS", "7"))
44
+ warmups = Integer(ENV.fetch("WARMUPS", "2"))
45
+ abort "invalid rounds, runs or warmups" unless rounds.between?(0, 0x7fffffff) && runs > 0 && warmups >= 0
46
+ checksum = (rounds + 123) & 0xffffffff
47
+
48
+ def capture!(*command)
49
+ out, err, status = Open3.capture3(*command)
50
+ abort "#{command.inspect}\n#{out}#{err}" unless status.success?
51
+ out
52
+ end
53
+
54
+ def stats(samples)
55
+ sorted = samples.sort
56
+ median = (sorted.fetch((sorted.size - 1) / 2) + sorted.fetch(sorted.size / 2)) / 2
57
+ [median, sorted.first, sorted.last].map { |seconds| format("%.3f", seconds * 1000) }
58
+ end
59
+
60
+ Dir.mktmpdir("wardite-m3-bench") do |dir|
61
+ source = File.join(dir, "calls.wat")
62
+ binary = File.join(dir, "calls.wasm")
63
+ native = File.join(dir, "m3-worker")
64
+ File.write(source, wat)
65
+ capture!("wasm-tools", "parse", source, "-o", binary)
66
+ compiler = Shellwords.split(ENV.fetch("SPINEL", "spinel"))
67
+ flags = Shellwords.split(ENV.fetch("SPINELFLAGS", "-O2"))
68
+ capture!(*compiler, "-I#{root}/lib", "--strict-require", *flags, worker, "-o", native)
69
+ runners = {
70
+ "CRuby" => [RbConfig.ruby, "--disable-yjit", "-I#{root}/lib", worker],
71
+ "Spinel" => [native]
72
+ }
73
+
74
+ puts "runtime,case,rounds,checksum,load_median_ms,load_min_ms,load_max_ms,execute_median_ms,execute_min_ms,execute_max_ms"
75
+ runners.each do |runtime, command|
76
+ samples = calls.keys.to_h { |name| [name, []] }
77
+ (runs + warmups).times do |iteration|
78
+ calls.keys.rotate(iteration).each do |name|
79
+ load, execute, actual = capture!(*command, binary, name, rounds.to_s).strip.split(",")
80
+ abort "checksum mismatch for #{runtime}/#{name}: #{actual}" unless Integer(actual) == checksum
81
+ samples.fetch(name) << [Float(load), Float(execute)] if iteration >= warmups
82
+ end
83
+ end
84
+ samples.each do |name, observations|
85
+ load, execute = observations.transpose
86
+ puts [runtime, name, rounds, checksum, *stats(load), *stats(execute)].join(",")
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Usage: bundle exec ruby -Ilib benchmark/measure_m4.rb /path/to/baseline [M3|M4|M5] [current-label]
4
+ require "wardite"
5
+ require "open3"
6
+ require "rbconfig"
7
+ require "shellwords"
8
+ require "tmpdir"
9
+
10
+ root = File.expand_path("..", __dir__)
11
+ baseline = File.expand_path(ARGV.fetch(0))
12
+ previous = ARGV.fetch(1, "M3")
13
+ current = ARGV.fetch(2) { { "M3" => "M4", "M4" => "M5", "M5" => "M6" }.fetch(previous) }
14
+ worker = File.join(__dir__, "m3_call_worker.rb")
15
+ rounds = Integer(ENV.fetch("ROUNDS", "100000"))
16
+ i64_rounds = Integer(ENV.fetch("I64_ROUNDS", "20000"))
17
+ runs = Integer(ENV.fetch("RUNS", "7"))
18
+ warmups = Integer(ENV.fetch("WARMUPS", "2"))
19
+ abort "invalid rounds or runs" unless [rounds, i64_rounds].all? { |n| n.between?(1, 0x7fffffff) } && runs > 0 && warmups >= 0
20
+
21
+ calls = {
22
+ "direct_defined" => "call $defined",
23
+ "direct_host" => "call $host",
24
+ "indirect_defined" => "i32.const 0 call_indirect (type $increment)",
25
+ "indirect_host" => "i32.const 1 call_indirect (type $increment)"
26
+ }
27
+ call_wat = <<~WAT
28
+ (module
29
+ (type $increment (func (param i32) (result i32)))
30
+ (import "env" "increment" (func $host (type $increment)))
31
+ (func $defined (type $increment) local.get 0 i32.const 1 i32.add)
32
+ (table 2 funcref)
33
+ (elem (i32.const 0) func $defined $host)
34
+ #{calls.map do |name, call|
35
+ <<~FUNC
36
+ (func (export "#{name}") (param $n i32) (param $sum i32) (result i32)
37
+ block $done
38
+ loop $repeat
39
+ local.get $n i32.eqz br_if $done
40
+ local.get $sum #{call} local.set $sum
41
+ local.get $n i32.const 1 i32.sub local.set $n
42
+ br $repeat
43
+ end
44
+ end
45
+ local.get $sum)
46
+ FUNC
47
+ end.join})
48
+ WAT
49
+
50
+ def capture!(*command)
51
+ output, error, status = Open3.capture3(*command)
52
+ abort "#{command.inspect}\n#{output}#{error}" unless status.success?
53
+ output.strip
54
+ end
55
+
56
+ def stats(samples)
57
+ sorted = samples.sort
58
+ median = (sorted.fetch((sorted.size - 1) / 2) + sorted.fetch(sorted.size / 2)) / 2
59
+ [median, sorted.first, sorted.last].map { |seconds| format("%.3f", seconds * 1000) }
60
+ end
61
+
62
+ def allocation_probe(binary, export, rounds, expected)
63
+ vm = Wardite.new(path: binary, engine: :efflorescence, enable_wasi: false).runtime
64
+ args = [rounds, 123]
65
+ vm.call_unboxed(export, args)
66
+ start = GC.stat(:total_allocated_objects)
67
+ actual = vm.call_unboxed(export, args)
68
+ allocated = GC.stat(:total_allocated_objects) - start
69
+ abort "allocation probe checksum mismatch" unless actual == expected
70
+
71
+ boxes = 0
72
+ trace = TracePoint.new(:call) { |event| boxes += 1 if event.defined_class == Wardite::I64 && event.method_id == :initialize }
73
+ trace.enable { vm.call_unboxed(export, args) }
74
+ unboxed = boxes
75
+ trace.enable { vm.call(export, args) }
76
+ boxed = boxes - unboxed
77
+ abort "unexpected I64 allocations for #{export}: unboxed=#{unboxed}, boxed=#{boxed}" unless unboxed == 0 && boxed == 1
78
+
79
+ vm = Wardite.new(path: binary, engine: :efflorescence, enable_wasi: false, mir_profile: true).runtime
80
+ abort "profile checksum mismatch" unless vm.call_unboxed(export, args) == expected
81
+ warn "#{export}: allocations=#{allocated}, I64 boxes=#{unboxed} unboxed/#{boxed} boxed, #{vm.metrics.report}"
82
+ end
83
+
84
+ Dir.mktmpdir("wardite-m4-bench") do |dir|
85
+ runners = {}
86
+ compiler = Shellwords.split(ENV.fetch("SPINEL", "spinel"))
87
+ flags = Shellwords.split(ENV.fetch("SPINELFLAGS", "-O2"))
88
+ { previous => baseline, current => root }.each do |version, path|
89
+ native = File.join(dir, "#{version}-worker")
90
+ capture!(*compiler, "-I#{path}/lib", "--strict-require", *flags, worker, "-o", native)
91
+ runners[["CRuby", version]] = [RbConfig.ruby, "--disable-yjit", "-I#{path}/lib", worker]
92
+ runners[["Spinel", version]] = [native]
93
+ end
94
+
95
+ call_source = File.join(dir, "calls.wat")
96
+ File.write(call_source, call_wat)
97
+ suites = {
98
+ File.join(__dir__, "i32_mix.wat") => [%w[inline_mix call_mix], rounds, false],
99
+ File.join(__dir__, "i32_call_arities.wat") => [%w[empty_loop call_zero call_one call_two call_three call_four], rounds, false],
100
+ call_source => [calls.keys, rounds, false],
101
+ File.join(__dir__, "i64_m4.wat") => [%w[alu division memory mixed_call], i64_rounds, true, previous == "M3"],
102
+ File.join(__dir__, "float_m5.wat") => [%w[float_alu float_convert float_mixed_call], i64_rounds, true, previous == "M4"]
103
+ }
104
+
105
+ suites.delete(File.join(__dir__, "float_m5.wat")) if previous == "M3"
106
+
107
+ puts "runtime,version,case,rounds,checksum,load_median_ms,load_min_ms,load_max_ms,execute_median_ms,execute_min_ms,execute_max_ms"
108
+ suites.each do |source, (exports, count, i64, current_only)|
109
+ binary = File.join(dir, "input.wasm")
110
+ capture!("wasm-tools", "parse", source, "-o", binary)
111
+ commands = current_only ? runners.select { |(_, version), _| version == current } : runners
112
+ exports.each do |export|
113
+ checksum = if source == call_source
114
+ (count + 123) % 2**32
115
+ else
116
+ result = Integer(capture!("wasmtime", "run", "--invoke", export, binary, count.to_s, "123"))
117
+ i64 ? result : result % 2**32
118
+ end
119
+ samples = commands.keys.to_h { |key| [key, []] }
120
+ (runs + warmups).times do |i|
121
+ commands.keys.rotate(i).each do |key|
122
+ load, execute, actual = capture!(*commands.fetch(key), binary, export, count.to_s).split(",")
123
+ abort "#{key}/#{export}: #{checksum} expected, #{actual} actual" unless Integer(actual) == checksum
124
+ samples.fetch(key) << [Float(load), Float(execute)] if i >= warmups
125
+ end
126
+ end
127
+ samples.each do |(runtime, version), observations|
128
+ load, execute = observations.transpose
129
+ puts [runtime, version, export, count, checksum, *stats(load), *stats(execute)].join(",")
130
+ end
131
+ allocation_probe(binary, export, count, checksum) if source.end_with?("/i64_m4.wat")
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # Run with a compiled benchmark/float_m5.wat.
5
+ require "wardite"
6
+
7
+ class FloatBoundaryBenchmark
8
+ # @rbs vm: Wardite::Efflorescence::VM
9
+ # @rbs rounds: Integer
10
+ # @rbs boxed: bool
11
+ # @rbs return: Float
12
+ def self.measure(vm, rounds, boxed)
13
+ arguments = [1.25]
14
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
+ index = 0
16
+ if boxed
17
+ while index < rounds
18
+ result = vm.call("float_identity", arguments)
19
+ raise "boxed result" unless result.is_a?(Wardite::F32) && result.value == 1.25
20
+ index += 1
21
+ end
22
+ else
23
+ while index < rounds
24
+ raise "unboxed result" unless vm.call_unboxed("float_identity", arguments) == 1.25
25
+ index += 1
26
+ end
27
+ end
28
+ (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000
29
+ end
30
+ end
31
+
32
+ vm = Wardite.new(path: ARGV.fetch(0), enable_wasi: false, engine: :efflorescence).runtime
33
+ raise "wrong engine" unless vm.is_a?(Wardite::Efflorescence::VM)
34
+ rounds = ARGV.fetch(1, "30000").to_i
35
+ 5.times do |trial|
36
+ boxed_first = trial.odd?
37
+ first = FloatBoundaryBenchmark.measure(vm, rounds, boxed_first)
38
+ second = FloatBoundaryBenchmark.measure(vm, rounds, !boxed_first)
39
+ unboxed, boxed = boxed_first ? [second, first] : [first, second]
40
+ puts "#{unboxed},#{boxed}"
41
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # CRuby. Compare the boxed and unboxed WASI paths in the same Efflorescence VM.
4
+ require "wardite"
5
+ require "wardite/wasi"
6
+ require "open3"
7
+ require "tmpdir"
8
+
9
+ wat = <<~WAT
10
+ (module
11
+ (import "wasi_snapshot_preview1" "fd_write" (func $write (param i32 i32 i32 i32) (result i32)))
12
+ (memory 1)
13
+ (data (i32.const 0) "\\20\\00\\00\\00\\04\\00\\00\\00")
14
+ (data (i32.const 32) "data")
15
+ (func (export "run") (param $rounds i32) (result i32) (local $bytes i32)
16
+ block $done
17
+ loop $repeat
18
+ local.get $rounds i32.eqz br_if $done
19
+ i32.const 1 i32.const 0 i32.const 1 i32.const 8 call $write
20
+ if unreachable end
21
+ local.get $bytes i32.const 8 i32.load i32.add local.set $bytes
22
+ local.get $rounds i32.const 1 i32.sub local.set $rounds
23
+ br $repeat
24
+ end
25
+ end
26
+ local.get $bytes))
27
+ WAT
28
+
29
+ rounds = Integer(ENV.fetch("ROUNDS", "100000"))
30
+ runs = Integer(ENV.fetch("RUNS", "5"))
31
+ abort "invalid rounds or runs" unless rounds.between?(1, 0x3fffffff) && runs.positive?
32
+ Dir.mktmpdir("wardite-wasi-bench") do |dir|
33
+ wat_path = File.join(dir, "write.wat")
34
+ wasm_path = File.join(dir, "write.wasm")
35
+ File.write(wat_path, wat)
36
+ output, status = Open3.capture2e("wasm-tools", "parse", wat_path, "-o", wasm_path)
37
+ abort output unless status.success?
38
+
39
+ measurements = {"boxed" => [], "unboxed" => []}
40
+ File.open(IO::NULL, "wb") do |sink|
41
+ sink.sync = true
42
+ (runs + 2).times do |index|
43
+ measurements.keys.rotate(index).each do |mode|
44
+ if mode == "boxed"
45
+ wasi = Wardite::WasiSnapshotPreview1.new
46
+ instance = Wardite.new(path: wasm_path, engine: :efflorescence, enable_wasi: false,
47
+ import_object: {wasi_snapshot_preview1: wasi})
48
+ else
49
+ instance = Wardite.new(path: wasm_path, engine: :efflorescence)
50
+ wasi = instance.wasi
51
+ end
52
+ wasi.fd_table[1] = sink
53
+ vm = instance.runtime
54
+ native = vm.ir.hosts.fetch(0).unboxed_wasi
55
+ abort "unexpected ABI for #{mode}" unless !!native == (mode == "unboxed")
56
+
57
+ objects = GC.stat(:total_allocated_objects)
58
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
59
+ checksum = vm.call_unboxed(:run, [rounds])
60
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
61
+ allocations = GC.stat(:total_allocated_objects) - objects
62
+ abort "expected #{rounds * 4} bytes, got #{checksum}" unless checksum == rounds * 4
63
+ measurements.fetch(mode) << [elapsed * 1000, allocations] if index >= 2
64
+ end
65
+ end
66
+ end
67
+ puts "abi,rounds,bytes,median_ms,min_ms,max_ms,median_allocations"
68
+ measurements.each do |mode, observations|
69
+ times, objects = observations.transpose.map(&:sort)
70
+ low, high = (runs - 1) / 2, runs / 2
71
+ median_ms = (times[low] + times[high]) / 2
72
+ median_objects = (objects[low] + objects[high]) / 2
73
+ puts "%s,%d,%d,%.3f,%.3f,%.3f,%d" % [mode, rounds, rounds * 4, median_ms, times.first, times.last, median_objects]
74
+ end
75
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "wardite"
5
+
6
+ wasm = ARGV.fetch(0)
7
+ export = ARGV.fetch(1)
8
+ rounds = ARGV.fetch(2).to_i
9
+ seed = ARGV.fetch(3).to_i
10
+ disabled = ARGV.fetch(4, "").split(",").map { |name| name.to_sym }
11
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
12
+ instance = File.open(wasm, "rb") do |file|
13
+ Wardite::BinaryLoader.load_from_buffer(file, enable_wasi: false)
14
+ end
15
+ loaded = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16
+ vm = Wardite::Efflorescence::VM.new(instance, disabled_optimizations: disabled)
17
+ translated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
18
+ before = RUBY_ENGINE == "spinel" ? 0 : GC.stat(:total_allocated_objects)
19
+ execution_started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
20
+ result = vm.call(export, [rounds, seed]) || raise("missing checksum")
21
+ finished = Process.clock_gettime(Process::CLOCK_MONOTONIC)
22
+ after = RUBY_ENGINE == "spinel" ? 0 : GC.stat(:total_allocated_objects)
23
+ puts "#{loaded - started},#{translated - loaded},#{finished - execution_started},#{result.value},#{after - before}"