Repository navigation
Expand file tree
/
Copy pathvm_optimization_bench.rb
More file actions
513 lines (452 loc) Β· 9.27 KB
/
Copy pathvm_optimization_bench.rb
File metadata and controls
513 lines (452 loc) Β· 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# VM Optimization Benchmarks for mruby
# Usage: ./bin/mruby benchmark/vm_optimization_bench.rb
#
# Each benchmark is designed to isolate specific VM behaviors:
# - Dispatch overhead
# - Arithmetic operations
# - Method calls
# - Array/Hash access
# - Loop performance
# Benchmark infrastructure
def measure(name, iterations = 1)
# Warm up
3.times { yield }
# Force GC before measurement
GC.start
t0 = Time.now
iterations.times { yield }
elapsed = Time.now - t0
puts "#{name}: #{elapsed * 1000 / iterations} ms"
elapsed
end
N = 1_000_000
M = 100_000
puts "=" * 60
puts "mruby VM Optimization Benchmarks"
puts "=" * 60
puts
#=============================================================================
# 1. DISPATCH OVERHEAD BENCHMARKS
# Target: Tail-call threading, computed goto efficiency
#=============================================================================
puts "--- Dispatch Overhead ---"
# 1a. Empty loop (pure dispatch cost)
measure("empty_loop", 10) do
i = 0
while i < N
i += 1
end
end
# 1b. NOP-heavy (many instructions, minimal work)
measure("nop_sequence", 10) do
i = 0
while i < M
a = 1; b = 2; c = 3; d = 4; e = 5
a = 1; b = 2; c = 3; d = 4; e = 5
a = 1; b = 2; c = 3; d = 4; e = 5
a = 1; b = 2; c = 3; d = 4; e = 5
i += 1
end
end
#=============================================================================
# 2. ARITHMETIC BENCHMARKS
# Target: Type specialization, register variables, ADDI fusion
#=============================================================================
puts
puts "--- Arithmetic Operations ---"
# 2a. Integer addition (tests OP_ADD fast path)
measure("int_add", 10) do
x = 0
i = 0
while i < N
x = x + 1
i += 1
end
x
end
# 2b. Integer increment (tests potential OP_INCI fusion)
measure("int_increment", 10) do
x = 0
i = 0
while i < N
x += 1
i += 1
end
x
end
# 2c. Mixed arithmetic (tests type checking overhead)
measure("mixed_arith", 10) do
x = 0
y = 1.5
i = 0
while i < M
x = x + 1
y = y + 0.5
i += 1
end
x
end
# 2d. Comparison in loop (tests OP_LT + JMPNOT fusion potential)
measure("comparison_loop", 10) do
x = 0
while x < N
x += 1
end
x
end
# 2e. Multiple comparisons (branch prediction)
measure("multi_compare", 10) do
i = 0
count = 0
while i < M
count += 1 if i > 100
count += 1 if i < 50000
count += 1 if i == 25000
i += 1
end
count
end
#=============================================================================
# 3. METHOD CALL BENCHMARKS
# Target: Inline caching, method dispatch optimization
#=============================================================================
puts
puts "--- Method Calls ---"
class BenchClass
def empty_method
end
def simple_add(a, b)
a + b
end
def self.class_method
end
end
$obj = BenchClass.new
# 3a. Empty method call (pure dispatch overhead)
measure("empty_method_call", 10) do
obj = $obj
i = 0
while i < M
obj.empty_method
i += 1
end
end
# 3b. Method with arguments
measure("method_with_args", 10) do
obj = $obj
i = 0
while i < M
obj.simple_add(1, 2)
i += 1
end
end
# 3c. Self method call (tests OP_SENDSELF potential)
class SelfCallBench
def run
i = 0
while i < M
helper
i += 1
end
end
def helper
end
end
measure("self_method_call", 10) do
SelfCallBench.new.run
end
# 3d. Polymorphic call site (tests inline cache invalidation)
class Duck1
def quack; 1; end
end
class Duck2
def quack; 2; end
end
$duck1 = Duck1.new
$duck2 = Duck2.new
measure("polymorphic_call", 10) do
d1, d2 = $duck1, $duck2
i = 0
sum = 0
while i < M
sum += d1.quack
sum += d2.quack
i += 1
end
sum
end
#=============================================================================
# 4. ARRAY/HASH BENCHMARKS
# Target: GETIDX/SETIDX fast path, bounds checking
#=============================================================================
puts
puts "--- Array/Hash Access ---"
$ary = Array.new(1000) { |i| i }
$hash = {}
1000.times { |i| $hash[i] = i }
# 4a. Array read (sequential)
measure("array_read_seq", 10) do
ary = $ary
i = 0
sum = 0
while i < M
sum += ary[i % 1000]
i += 1
end
sum
end
# 4b. Array read (constant index - tests constant propagation)
measure("array_read_const", 10) do
ary = $ary
i = 0
sum = 0
while i < M
sum += ary[500]
i += 1
end
sum
end
# 4c. Array write
measure("array_write", 10) do
ary = Array.new(1000, 0)
i = 0
while i < M
ary[i % 1000] = i
i += 1
end
end
# 4d. Hash read
measure("hash_read", 10) do
h = $hash
i = 0
sum = 0
while i < M
sum += h[i % 1000]
i += 1
end
sum
end
#=============================================================================
# 5. LOOP PATTERN BENCHMARKS
# Target: Loop optimization, branch prediction
#=============================================================================
puts
puts "--- Loop Patterns ---"
# 5a. Simple while loop
measure("while_loop", 10) do
i = 0
while i < N
i += 1
end
end
# 5b. times iterator (block overhead)
measure("times_iterator", 10) do
sum = 0
M.times do |i|
sum += i
end
sum
end
# 5c. each iterator on array
$small_ary = (0...1000).to_a
measure("each_iterator", 10) do
ary = $small_ary
total = 0
1000.times do
ary.each { |x| total += x }
end
total
end
# 5d. Nested loops
measure("nested_loop", 10) do
sum = 0
i = 0
while i < 1000
j = 0
while j < 1000
sum += 1
j += 1
end
i += 1
end
sum
end
#=============================================================================
# 6. CONSTANT LOADING BENCHMARKS
# Target: Constant pre-computation, pool access
#=============================================================================
puts
puts "--- Constant Loading ---"
# 6a. Integer literals (tests LOADI optimization)
measure("int_literals", 10) do
i = 0
sum = 0
while i < M
sum += 1
sum += 2
sum += 3
sum += 42
sum += 100
i += 1
end
sum
end
# 6b. Large integer literals (tests LOADL from pool)
measure("large_int_literals", 10) do
i = 0
sum = 0
while i < M
sum += 1000000
sum += 2000000
sum += 3000000
i += 1
end
sum
end
# 6c. Float literals
measure("float_literals", 10) do
i = 0
sum = 0.0
while i < M
sum += 1.5
sum += 2.5
sum += 3.5
i += 1
end
sum
end
# 6d. String literals (allocation vs interning)
measure("string_literals", 5) do
i = 0
while i < 100000
s = "hello"
s = "world"
s = "test"
i += 1
end
end
#=============================================================================
# 7. BRANCH PREDICTION BENCHMARKS
# Target: mrb_likely/mrb_unlikely effectiveness
#=============================================================================
puts
puts "--- Branch Prediction ---"
# 7a. Predictable branch (always true)
measure("predictable_true", 10) do
i = 0
count = 0
while i < N
count += 1 if true
i += 1
end
count
end
# 7b. Predictable branch (always false)
measure("predictable_false", 10) do
i = 0
count = 0
while i < N
count += 1 if false
i += 1
end
count
end
# 7c. Unpredictable branch (50/50)
measure("unpredictable_50", 10) do
i = 0
count = 0
while i < M
count += 1 if i & 1 == 0
i += 1
end
count
end
# 7d. Rare branch (error path simulation)
measure("rare_branch", 10) do
i = 0
count = 0
while i < N
count += 1 if i == -1 # Never true
i += 1
end
count
end
#=============================================================================
# 8. REGISTER PRESSURE BENCHMARKS
# Target: Register variable optimization
#=============================================================================
puts
puts "--- Register Pressure ---"
# 8a. Few local variables (should fit in registers)
measure("few_locals", 10) do
i = 0
a = 0
while i < N
a += 1
i += 1
end
a
end
# 8b. Many local variables (register spilling)
measure("many_locals", 10) do
i = 0
a = 0; b = 0; c = 0; d = 0; e = 0
f = 0; g = 0; h = 0; j = 0; k = 0
l = 0; m = 0; n = 0; o = 0; p = 0
while i < M
a += 1; b += 1; c += 1; d += 1; e += 1
f += 1; g += 1; h += 1; j += 1; k += 1
l += 1; m += 1; n += 1; o += 1; p += 1
i += 1
end
a + b + c + d + e + f + g + h + j + k + l + m + n + o + p
end
#=============================================================================
# 9. COMPOSITE BENCHMARKS (Real-world-ish)
#=============================================================================
puts
puts "--- Composite Benchmarks ---"
# 9a. Fibonacci (recursion + arithmetic)
def fib(n)
return n if n < 2
fib(n - 1) + fib(n - 2)
end
measure("fibonacci_30", 3) do
fib(30)
end
# 9b. Tak function (heavy recursion)
def tak(x, y, z)
if y < x
tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y))
else
z
end
end
measure("tak_18_12_6", 3) do
tak(18, 12, 6)
end
# 9c. Array manipulation
measure("array_manipulation", 5) do
ary = []
10000.times { |i| ary << i }
ary.map! { |x| x * 2 }
ary.select { |x| x % 3 == 0 }.size
end
# 9d. String operations
measure("string_ops", 5) do
s = ""
10000.times { |i| s = s + i.to_s }
s.size
end
# 9e. Hash operations
measure("hash_ops", 5) do
h = {}
50000.times { |i| h[i.to_s] = i }
sum = 0
h.each { |k, v| sum += v }
sum
end
puts
puts "=" * 60
puts "Benchmark complete"
puts "=" * 60