fusevm 0.9.9

Language-agnostic bytecode VM with fused superinstructions and Cranelift JIT path
Documentation
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! fusevm benchmarks — tracks performance of the dispatch loop, fused ops,
//! string/array/hash operations, and JIT eligibility analysis.
//!
//! Run:   cargo bench
//! Compare against baseline:  cargo bench -- --save-baseline <name>
//! HTML report:  target/criterion/report/index.html

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use fusevm::{ChunkBuilder, Op, VMResult, Value, VM};

// ── Helpers ──

fn run_ops(ops: &[(Op, u32)]) -> Value {
    let mut b = ChunkBuilder::new();
    for (op, line) in ops {
        b.emit(op.clone(), *line);
    }
    let mut vm = VM::new(b.build());
    match vm.run() {
        VMResult::Ok(val) => val,
        _ => Value::Undef,
    }
}

// ── Arithmetic ──

fn bench_int_add_1k(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(2001);
    ops.push((Op::LoadInt(0), 1));
    for _ in 0..1000 {
        ops.push((Op::LoadInt(1), 1));
        ops.push((Op::Add, 1));
    }
    c.bench_function("int_add_1k", |b| {
        b.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_float_add_1k(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(2001);
    ops.push((Op::LoadFloat(0.0), 1));
    for _ in 0..1000 {
        ops.push((Op::LoadFloat(1.5), 1));
        ops.push((Op::Add, 1));
    }
    c.bench_function("float_add_1k", |b| {
        b.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_mixed_arith(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(501);
    ops.push((Op::LoadInt(1), 1));
    for _ in 0..100 {
        ops.push((Op::LoadInt(3), 1));
        ops.push((Op::Mul, 1));
        ops.push((Op::LoadInt(7), 1));
        ops.push((Op::Add, 1));
        ops.push((Op::LoadInt(2), 1));
        ops.push((Op::Sub, 1));
    }
    c.bench_function("mixed_arith_100", |b| {
        b.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Fused superinstructions ──

fn bench_fused_accum_sum(c: &mut Criterion) {
    // sum = 0; for i in 0..10_000 { sum += i }
    let ops = vec![
        (Op::PushFrame, 1),
        (Op::LoadInt(0), 1),
        (Op::SetSlot(0), 1),
        (Op::LoadInt(0), 1),
        (Op::SetSlot(1), 1),
        (Op::AccumSumLoop(0, 1, 10_000), 1),
        (Op::GetSlot(0), 1),
    ];
    c.bench_function("fused_accum_sum_10k", |b| {
        b.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_fused_slot_inc_loop(c: &mut Criterion) {
    // i = 0; while (i < 10_000) { i++ }
    let ops = vec![
        (Op::PushFrame, 1),
        (Op::LoadInt(0), 1),
        (Op::SetSlot(0), 1),
        // ip=3: loop body (nop)
        (Op::Nop, 1),
        // ip=4: backedge
        (Op::SlotIncLtIntJumpBack(0, 10_000, 3), 1),
        (Op::GetSlot(0), 1),
    ];
    c.bench_function("fused_slot_inc_10k", |b| {
        b.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_unfused_sum_loop(c: &mut Criterion) {
    // Same as fused_accum_sum but using individual ops to show the speedup
    let ops = vec![
        (Op::PushFrame, 1),
        (Op::LoadInt(0), 1),
        (Op::SetSlot(0), 1), // sum = 0
        (Op::LoadInt(0), 1),
        (Op::SetSlot(1), 1), // i = 0
        // ip=5: loop body
        (Op::GetSlot(0), 1),                          // push sum
        (Op::GetSlot(1), 1),                          // push i
        (Op::Add, 1),                                 // sum + i
        (Op::SetSlot(0), 1),                          // sum = result
        (Op::PreIncSlotVoid(1), 1),                   // i++
        (Op::SlotLtIntJumpIfFalse(1, 10_000, 12), 1), // if i < 10000 skip exit
        (Op::Jump(5), 1),                             // loop back
        // ip=12: exit
        (Op::GetSlot(0), 1),
    ];
    c.bench_function("unfused_sum_loop_10k", |b| {
        b.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── String operations ──

fn bench_concat_1k(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let const_idx = b.add_constant(Value::str("x"));
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(2001);
    ops.push((Op::LoadConst(const_idx), 1));
    for _ in 0..999 {
        ops.push((Op::LoadConst(const_idx), 1));
        ops.push((Op::Concat, 1));
    }
    c.bench_function("concat_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_fused_concat_const_loop(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let const_idx = b.add_constant(Value::str("x"));
    let ops = vec![
        (Op::PushFrame, 1),
        (Op::LoadConst(const_idx), 1),
        (Op::SetSlot(0), 1), // s = "x"
        (Op::LoadInt(0), 1),
        (Op::SetSlot(1), 1), // i = 0
        (Op::ConcatConstLoop(const_idx, 0, 1, 1000), 1),
        (Op::GetSlot(0), 1),
    ];
    c.bench_function("fused_concat_const_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_str_compare(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let a_idx = b.add_constant(Value::str("hello world"));
    let b_idx = b.add_constant(Value::str("hello world"));
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(3001);
    for _ in 0..1000 {
        ops.push((Op::LoadConst(a_idx), 1));
        ops.push((Op::LoadConst(b_idx), 1));
        ops.push((Op::StrEq, 1));
        ops.push((Op::Pop, 1));
    }
    // leave one result on stack
    ops.push((Op::LoadConst(a_idx), 1));
    ops.push((Op::LoadConst(b_idx), 1));
    ops.push((Op::StrEq, 1));
    c.bench_function("str_eq_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Array operations ──

fn bench_array_push_pop_1k(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let arr_name = b.add_name("arr");
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(4003);
    ops.push((Op::DeclareArray(arr_name), 1));
    // push 1000 elements
    for i in 0..1000 {
        ops.push((Op::LoadInt(i), 1));
        ops.push((Op::ArrayPush(arr_name), 1));
    }
    // pop all
    for _ in 0..1000 {
        ops.push((Op::ArrayPop(arr_name), 1));
        ops.push((Op::Pop, 1));
    }
    ops.push((Op::ArrayLen(arr_name), 1));
    c.bench_function("array_push_pop_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_array_index_1k(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let arr_name = b.add_name("arr");
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(5003);
    ops.push((Op::DeclareArray(arr_name), 1));
    // build array of 1000 elements
    for i in 0..1000 {
        ops.push((Op::LoadInt(i), 1));
        ops.push((Op::ArrayPush(arr_name), 1));
    }
    // random-access read 1000 times
    for i in 0..1000 {
        ops.push((Op::LoadInt(i % 1000), 1));
        ops.push((Op::ArrayGet(arr_name), 1));
        ops.push((Op::Pop, 1));
    }
    ops.push((Op::ArrayLen(arr_name), 1));
    c.bench_function("array_index_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_fused_push_int_range(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let arr_name = b.add_name("arr");
    let ops = vec![
        (Op::DeclareArray(arr_name), 1),
        (Op::PushFrame, 1),
        (Op::LoadInt(0), 1),
        (Op::SetSlot(0), 1),
        (Op::PushIntRangeLoop(arr_name, 0, 10_000), 1),
        (Op::ArrayLen(arr_name), 1),
    ];
    c.bench_function("fused_push_range_10k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_range_10k(c: &mut Criterion) {
    let ops = vec![(Op::LoadInt(0), 1), (Op::LoadInt(9999), 1), (Op::Range, 1)];
    c.bench_function("range_10k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Hash operations ──

fn bench_hash_set_get_1k(c: &mut Criterion) {
    let mut builder = ChunkBuilder::new();
    let hash_name = builder.add_name("h");
    let mut const_keys = Vec::new();
    for i in 0..100 {
        const_keys.push(builder.add_constant(Value::str(format!("key_{i}"))));
    }
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(1003);
    ops.push((Op::DeclareHash(hash_name), 1));
    // insert 100 keys
    for (i, &k) in const_keys.iter().enumerate() {
        ops.push((Op::LoadInt(i as i64), 1)); // value
        ops.push((Op::LoadConst(k), 1)); // key
        ops.push((Op::HashSet(hash_name), 1));
    }
    // lookup each key 10 times = 1000 lookups
    for _ in 0..10 {
        for &k in &const_keys {
            ops.push((Op::LoadConst(k), 1));
            ops.push((Op::HashGet(hash_name), 1));
            ops.push((Op::Pop, 1));
        }
    }
    ops.push((Op::LoadInt(0), 1));
    c.bench_function("hash_set_get_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Stack operations ──

fn bench_dup_swap_rot_1k(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(4004);
    ops.push((Op::LoadInt(1), 1));
    ops.push((Op::LoadInt(2), 1));
    ops.push((Op::LoadInt(3), 1));
    for _ in 0..1000 {
        ops.push((Op::Rot, 1));
        ops.push((Op::Swap, 1));
        ops.push((Op::Dup, 1));
        ops.push((Op::Pop, 1));
    }
    c.bench_function("stack_dup_swap_rot_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Slot variables ──

fn bench_slot_read_write_10k(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(40006);
    ops.push((Op::PushFrame, 1));
    ops.push((Op::LoadInt(0), 1));
    ops.push((Op::SetSlot(0), 1));
    ops.push((Op::LoadInt(0), 1));
    ops.push((Op::SetSlot(1), 1));
    for _ in 0..10_000 {
        ops.push((Op::GetSlot(0), 1));
        ops.push((Op::GetSlot(1), 1));
        ops.push((Op::Add, 1));
        ops.push((Op::SetSlot(0), 1));
    }
    ops.push((Op::GetSlot(0), 1));
    c.bench_function("slot_read_write_10k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Comparison ──

fn bench_int_cmp_1k(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(4001);
    for i in 0..1000 {
        ops.push((Op::LoadInt(i), 1));
        ops.push((Op::LoadInt(i + 1), 1));
        ops.push((Op::NumLt, 1));
        ops.push((Op::Pop, 1));
    }
    ops.push((Op::LoadInt(1), 1));
    c.bench_function("int_cmp_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Control flow ──

fn bench_jump_loop_10k(c: &mut Criterion) {
    // Manual counted loop with jumps: i=0; while(i<10000) { i++ }
    let ops = vec![
        (Op::PushFrame, 1),
        (Op::LoadInt(0), 1),
        (Op::SetSlot(0), 1),
        // ip=3: body
        (Op::PreIncSlotVoid(0), 1),
        (Op::SlotLtIntJumpIfFalse(0, 10_000, 6), 1),
        (Op::Jump(3), 1),
        // ip=6: exit
        (Op::GetSlot(0), 1),
    ];
    c.bench_function("jump_loop_10k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Function calls ──

fn bench_function_call_1k(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let inc_name = b.add_name("inc");
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(2005);
    ops.push((Op::LoadInt(0), 1));
    // 1000 calls to inc function
    for _ in 0..1000 {
        ops.push((Op::Call(inc_name, 1), 1));
    }
    ops.push((Op::Jump(0), 1)); // placeholder, will be patched
                                // inc function body at ip = 1002
    let func_ip = ops.len();
    ops.push((Op::Inc, 2));
    ops.push((Op::ReturnValue, 2));

    // Build manually to set up sub_entries and patch jump
    let mut builder = ChunkBuilder::new();
    let name = builder.add_name("inc");
    for (i, (op, line)) in ops.iter().enumerate() {
        if i == 1001 {
            // patch the jump to skip past function body
            builder.emit(Op::Jump(func_ip + 2), *line);
        } else {
            builder.emit(op.clone(), *line);
        }
    }
    builder.add_sub_entry(name, func_ip);
    let chunk = builder.build();

    c.bench_function("function_call_1k", |bencher| {
        bencher.iter(|| {
            let mut vm = VM::new(chunk.clone());
            let result = vm.run();
            black_box(result);
        })
    });
}

// ── LoadConst ──

fn bench_load_const_int_1k(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let idx = b.add_constant(Value::Int(42));
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(2001);
    for _ in 0..1000 {
        ops.push((Op::LoadConst(idx), 1));
        ops.push((Op::Pop, 1));
    }
    ops.push((Op::LoadConst(idx), 1));
    c.bench_function("load_const_int_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

fn bench_load_const_str_1k(c: &mut Criterion) {
    let mut b = ChunkBuilder::new();
    let idx = b.add_constant(Value::str("hello world"));
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(2001);
    for _ in 0..1000 {
        ops.push((Op::LoadConst(idx), 1));
        ops.push((Op::Pop, 1));
    }
    ops.push((Op::LoadConst(idx), 1));
    c.bench_function("load_const_str_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── MakeHash / MakeArray ──

fn bench_make_hash_100(c: &mut Criterion) {
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(201);
    for i in 0..100 {
        ops.push((Op::LoadInt(i), 1)); // value
        ops.push((Op::LoadInt(i * 100), 1)); // key (coerced to str)
    }
    ops.push((Op::MakeHash(200), 1));
    c.bench_function("make_hash_100", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

// ── Global variable access ──

fn bench_global_var_read_write_1k(c: &mut Criterion) {
    let mut builder = ChunkBuilder::new();
    let x = builder.add_name("x");
    let mut ops: Vec<(Op, u32)> = Vec::with_capacity(4003);
    ops.push((Op::LoadInt(0), 1));
    ops.push((Op::SetVar(x), 1));
    for _ in 0..1000 {
        ops.push((Op::GetVar(x), 1));
        ops.push((Op::LoadInt(1), 1));
        ops.push((Op::Add, 1));
        ops.push((Op::SetVar(x), 1));
    }
    ops.push((Op::GetVar(x), 1));
    c.bench_function("global_var_rw_1k", |bencher| {
        bencher.iter(|| {
            let val = run_ops(black_box(&ops));
            black_box(val);
        })
    });
}

criterion_group!(
    benches,
    // arithmetic
    bench_int_add_1k,
    bench_float_add_1k,
    bench_mixed_arith,
    // fused vs unfused
    bench_fused_accum_sum,
    bench_unfused_sum_loop,
    bench_fused_slot_inc_loop,
    bench_fused_concat_const_loop,
    bench_fused_push_int_range,
    // strings
    bench_concat_1k,
    bench_str_compare,
    // arrays
    bench_array_push_pop_1k,
    bench_array_index_1k,
    bench_range_10k,
    // hashes
    bench_hash_set_get_1k,
    bench_make_hash_100,
    // stack
    bench_dup_swap_rot_1k,
    // slots + globals
    bench_slot_read_write_10k,
    bench_global_var_read_write_1k,
    // comparisons
    bench_int_cmp_1k,
    // control flow
    bench_jump_loop_10k,
    // functions
    bench_function_call_1k,
    // constants
    bench_load_const_int_1k,
    bench_load_const_str_1k,
);
criterion_main!(benches);