neyuki 0.6.3

A small Lua-like scripting language implemented in Rust
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Micro-benchmarks for `neyuki bench`: the front end, the VM and the libraries.

use crate::bytecode::Proto;
use crate::compiler::{compile_source, try_compile_to_proto};
use crate::lexer::Lexer;
use crate::parser::Parser;
use crate::vm::machine::VM;
use std::time::{Duration, Instant};

pub struct BenchResult {
    pub name: &'static str,
    pub iterations: usize,
    pub total_duration: Duration,
    pub avg_per_op: Duration,
    pub ops_per_sec: f64,
}

impl BenchResult {
    pub fn display(&self) {
        let avg_us = self.avg_per_op.as_nanos() as f64 / 1_000.0;
        let total_ms = self.total_duration.as_millis();
        println!(
            "{:<32} | {:>8} iters | {:>8.2} ms | {:>10.2} us/op | {:>12.0} ops/s",
            self.name, self.iterations, total_ms as f64, avg_us, self.ops_per_sec
        );
    }
}

fn measure<F: FnMut()>(name: &'static str, iterations: usize, mut f: F) -> BenchResult {
    if iterations == 0 {
        return BenchResult {
            name,
            iterations: 0,
            total_duration: Duration::ZERO,
            avg_per_op: Duration::ZERO,
            ops_per_sec: 0.0,
        };
    }

    // Warmup
    f();

    let start = Instant::now();
    for _ in 0..iterations {
        f();
    }
    let total_duration = start.elapsed();
    let avg_per_op = total_duration / (iterations as u32);
    let total_secs = total_duration.as_secs_f64();
    let ops_per_sec = if total_secs > 0.0 {
        iterations as f64 / total_secs
    } else {
        0.0
    };

    BenchResult {
        name,
        iterations,
        total_duration,
        avg_per_op,
        ops_per_sec,
    }
}

fn prepare_proto(code: &str) -> Result<Proto, String> {
    let stmts = compile_source(code)?;
    try_compile_to_proto(&stmts)
}

fn bench_vm_script(name: &'static str, iterations: usize, code: &str) -> BenchResult {
    let proto = match prepare_proto(code) {
        Ok(proto) => proto,
        Err(err) => {
            eprintln!("Benchmark '{name}' compile error: {err}");
            return BenchResult {
                name,
                iterations: 0,
                total_duration: Duration::ZERO,
                avg_per_op: Duration::ZERO,
                ops_per_sec: 0.0,
            };
        }
    };
    measure(name, iterations, || {
        let mut vm = VM::new();
        if let Err(err) = vm.execute(proto.clone()) {
            eprintln!("Benchmark '{name}' runtime error: {err}");
        }
    })
}

fn run_vm_code(code: &str) {
    if let Ok(proto) = prepare_proto(code) {
        let mut vm = VM::new();
        let _ = vm.execute(proto);
    }
}

// 1. Lexer Benchmark
pub fn bench_lexer() -> BenchResult {
    let source = r#"
        local sum = 0
        for i = 1, 100 do
            local x = i * 2 + 1
            if x % 3 == 0 then
                sum = sum + x
            else
                sum = sum - (x >> 1)
            end
        end
        return sum
    "#;
    measure("1. Lexer Tokenization", 2_000, || {
        let mut lexer = Lexer::new(source);
        let _ = lexer.tokenize();
    })
}

// 2. Parser Benchmark
pub fn bench_parser() -> BenchResult {
    let source = r#"
        function compute(a, b, c)
            local res = {}
            for i = 1, 50 do
                res[i] = (a + b) * c - i
            end
            return res
        end
        return compute(10, 20, 30)
    "#;
    measure("2. AST Parser", 1_000, || {
        let mut parser = Parser::new(source);
        let _ = parser.parse_program();
    })
}

// 3. Compiler & Constant Folding Benchmark
pub fn bench_compiler() -> BenchResult {
    let source = r#"
        local a = 10 * 20 + 30 - 5
        local b = 1 << 4 | 2
        local c = math.sqrt(144) + math.floor(3.14)
        local function calc(x)
            return x * 2 + a + b + c
        end
        return calc(100)
    "#;
    let stmts = match compile_source(source) {
        Ok(stmts) => stmts,
        Err(err) => {
            eprintln!("Benchmark '3. Bytecode Compiler & Folding' AST error: {err}");
            return BenchResult {
                name: "3. Bytecode Compiler & Folding",
                iterations: 0,
                total_duration: Duration::ZERO,
                avg_per_op: Duration::ZERO,
                ops_per_sec: 0.0,
            };
        }
    };
    measure("3. Bytecode Compiler & Folding", 1_000, || {
        let _ = try_compile_to_proto(&stmts);
    })
}

// 4. VM Arithmetic & Loop Benchmark
pub fn bench_arithmetic() -> BenchResult {
    let code = r#"
        local sum = 0
        local i = 0
        while i < 10000 do
            sum = sum + (i * 3 - (i >> 1))
            i = i + 1
        end
        return sum
    "#;
    bench_vm_script("4. VM Arithmetic & Loop", 100, code)
}

// 5. VM Fibonacci Benchmark (Iterative)
pub fn bench_fibonacci() -> BenchResult {
    let code = r#"
        local a = 0
        local b = 1
        local i = 0
        while i < 1000 do
            local next = a + b
            a = b
            b = next
            i = i + 1
        end
        return b
    "#;
    bench_vm_script("5. VM Fibonacci (Iterative)", 200, code)
}

// 6. VM Table Benchmark (Creation, Hash Lookups, Array Insertions)
pub fn bench_tables() -> BenchResult {
    let code = r#"
        local t = {}
        for i = 1, 500 do
            t[i] = i * 2
            t["key_" .. tostring(i)] = i * 3
        end
        local acc = 0
        for i = 1, 500 do
            acc = acc + t[i] + t["key_" .. tostring(i)]
        end
        return acc
    "#;
    bench_vm_script("6. VM Table Operations", 50, code)
}

// 7. VM String Operations Benchmark (Concatenation, Sub, Formatting)
pub fn bench_strings() -> BenchResult {
    let code = r#"
        local string = require("@neyuki/string")
        local s = "hello, world, neyuki programming language!"
        local out = ""
        for i = 1, 200 do
            local sub = string.sub(s, 1, 12)
            out = string.format("%s-%d", sub, i)
        end
        return out
    "#;
    bench_vm_script("7. VM String Lib Operations", 50, code)
}

// 8. VM Coroutine Benchmark (Create, Yield, Resume throughput)
pub fn bench_coroutines() -> BenchResult {
    let code = r#"
        local co = require("@neyuki/coroutine")
        local gen = co.create(function()
            local x = 0
            while true do
                x = x + 1
                co.yield(x)
            end
        end)
        local sum = 0
        for i = 1, 1000 do
            local ok, val = co.resume(gen)
            sum = sum + val
        end
        return sum
    "#;
    bench_vm_script("8. VM Coroutines (1000 cycles)", 50, code)
}

// 9. VM Garbage Collection Benchmark (Cyclic reference collection)
pub fn bench_gc() -> BenchResult {
    let code = r#"
        for i = 1, 100 do
            local a = {}
            local b = {}
            a.b = b
            b.a = a
        end
        collectgarbage("collect")
    "#;
    bench_vm_script("9. VM Cyclic Garbage Collector", 50, code)
}

// 10. Crypto SHA-256 Hashing Benchmark
pub fn bench_crypto() -> BenchResult {
    let code = r#"
        local crypto = require("@neyuki/crypto")
        local data = "Neyuki high performance cryptographic hashing benchmark payload string 1234567890."
        for i = 1, 100 do
            local h = crypto.hash(data, "sha256")
        end
    "#;
    bench_vm_script("10. VM Crypto SHA-256 (100x)", 50, code)
}

// 11. Buffer Operations Benchmark (Raw memory read/write & copy)
pub fn bench_buffer() -> BenchResult {
    let code = r#"
        local buffer = require("@neyuki/buffer")
        local b1 = buffer.create(1024)
        local b2 = buffer.create(1024)
        for i = 0, 255 do
            buffer.writeu32(b1, i * 4, i * 100)
        end
        for i = 1, 50 do
            buffer.copy(b2, 0, b1, 0, 1024)
        end
        return buffer.readu32(b2, 0)
    "#;
    bench_vm_script("11. VM Buffer Raw Memory Ops", 100, code)
}

// 12. JSON Serialization & Deserialization Benchmark
pub fn bench_json() -> BenchResult {
    let code = r#"
        local json = require("@neyuki/json")
        local record = {
            id = 12345,
            name = "benchmark_test_object",
            active = true,
            scores = {10, 20, 30, 40, 50},
            meta = {
                created_at = "2026-09-17",
                version = "1.0.0"
            }
        }
        for i = 1, 50 do
            local encoded = json.encode(record)
            local decoded = json.decode(encoded)
        end
    "#;
    bench_vm_script("12. VM JSON Encode/Decode", 50, code)
}

// 13. VM End-to-End Pipeline Benchmark (Lex, Parse, Compile & Execute)
pub fn bench_end_to_end() -> BenchResult {
    let code = r#"
        local sum = 0
        for i = 1, 100 do
            sum = sum + i
        end
        return sum
    "#;
    measure("13. VM End-to-End Pipeline", 200, || {
        run_vm_code(code);
    })
}

// 14. VM Recursive Fibonacci Benchmark (Call stack frames, function dispatch, tree recursion)
pub fn bench_fibonacci_recursive() -> BenchResult {
    let code = r#"
        const function fibonacci(n: int): int
            if n <= 1 then
                return n
            end
            return fibonacci(n - 1) + fibonacci(n - 2)
        end
        return fibonacci(18)
    "#;
    bench_vm_script("14. VM Recursive Fibonacci", 50, code)
}

// 15. VM BigInt Arithmetic & Widening Benchmark (Multi-limb ops, dynamic widening, pow, shifts, div/mod)
pub fn bench_bigint() -> BenchResult {
    let code = r#"
        local base = 2^100
        local factor = 3^60
        local sum = 0
        for i = 1, 1000 do
            local widened = (1 << (60 + (i % 6))) * (i + 1)
            local product = (base + widened) * (factor - i)
            local quotient = product // (base >> 20)
            sum = sum + (quotient % 1000000007)
            base = base + (quotient >> 15)
        end
        return sum
    "#;
    bench_vm_script("15. VM BigInt Operations", 50, code)
}

// 16. VM Table Lookup Throughput Benchmark (Balanced array indexing, named fields, dynamic keys, nested accesses)
pub fn bench_table_lookup() -> BenchResult {
    let code = r#"
        local arr = {}
        for i = 1, 1000 do
            arr[i] = i * 7
        end

        local obj = {
            id = 1001,
            name = 2002,
            score = 3003,
            status = 4004,
            count = 5005,
            weight = 6006,
            tag = 7007,
            level = 8008,
            role = 9009,
            priority = 1010,
            nested = {
                a = 11,
                b = 22,
                c = 33,
                d = 44,
                inner = { x = 100, y = 200 }
            }
        }

        local keys = { "id", "name", "score", "status", "count", "weight", "tag", "level", "role", "priority" }

        local sum = 0
        for _ = 1, 200 do
            for j = 1, 10 do
                sum = sum + arr[j * 99]
            end
            sum = sum + obj.id + obj.name + obj.score + obj.status + obj.count
            sum = sum + obj.weight + obj.tag + obj.level + obj.role + obj.priority
            for _, k in keys do
                sum = sum + obj[k]
            end
            sum = sum + obj.nested.a + obj.nested.b + obj.nested.c + obj.nested.d
            sum = sum + obj.nested.inner.x + obj.nested.inner.y
        end
        return sum
    "#;
    bench_vm_script("16. VM Table Lookup Throughput", 50, code)
}

// 17. VM GC Allocation Pressure Benchmark (Nursery churn, live retention, threshold triggers, cycle sweep)
pub fn bench_gc_pressure() -> BenchResult {
    let code = r#"
        local keep = {}
        for i = 1, 15000 do
            local obj = {
                id = i,
                data = { val = i * 3, extra = i + 10 },
                tags = { i, i * 2, i * 3 }
            }
            if i % 4 == 0 then
                keep[#keep + 1] = obj
            end
        end

        local sum = 0
        for _, item in keep do
            sum = sum + item.id + item.data.val + item.tags[2]
        end

        collectgarbage("collect")
        return sum
    "#;
    bench_vm_script("17. VM GC Allocation Pressure", 30, code)
}

// Run the full benchmark suite and display formatted results
pub fn run_all_benchmarks() {
    println!(
        "========================================================================================="
    );
    println!(
        "                               NEYUKI BENCHMARK SUITE                                    "
    );
    println!(
        "========================================================================================="
    );
    println!(
        "{:<32} | {:>14} | {:>11} | {:>16} | {:>12}",
        "Benchmark Name", "Iterations", "Total Time", "Latency (avg)", "Throughput"
    );
    println!(
        "---------------------------------+----------------+-------------+------------------+-------------"
    );

    let results = [
        bench_lexer(),
        bench_parser(),
        bench_compiler(),
        bench_arithmetic(),
        bench_fibonacci(),
        bench_tables(),
        bench_strings(),
        bench_coroutines(),
        bench_gc(),
        bench_crypto(),
        bench_buffer(),
        bench_json(),
        bench_end_to_end(),
        bench_fibonacci_recursive(),
        bench_bigint(),
        bench_table_lookup(),
        bench_gc_pressure(),
    ];

    for res in &results {
        res.display();
    }

    println!(
        "========================================================================================="
    );
    println!("All benchmarks completed successfully.");
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vm::value::Value;

    #[test]
    fn test_benchmarks_smoke() {
        // Ensure all benchmark functions execute cleanly without panic
        assert!(bench_lexer().ops_per_sec > 0.0);
        assert!(bench_parser().ops_per_sec > 0.0);
        assert!(bench_compiler().ops_per_sec > 0.0);
        assert!(bench_arithmetic().ops_per_sec > 0.0);
        assert!(bench_fibonacci().ops_per_sec > 0.0);
        assert!(bench_tables().ops_per_sec > 0.0);
        assert!(bench_strings().ops_per_sec > 0.0);
        assert!(bench_coroutines().ops_per_sec > 0.0);
        assert!(bench_gc().ops_per_sec > 0.0);
        assert!(bench_crypto().ops_per_sec > 0.0);
        assert!(bench_buffer().ops_per_sec > 0.0);
        assert!(bench_json().ops_per_sec > 0.0);
        assert!(bench_end_to_end().ops_per_sec > 0.0);
        assert!(bench_fibonacci_recursive().ops_per_sec > 0.0);
        assert!(bench_bigint().ops_per_sec > 0.0);
        assert!(bench_table_lookup().ops_per_sec > 0.0);
        assert!(bench_gc_pressure().ops_per_sec > 0.0);
    }

    #[test]
    fn test_new_benchmarks_correctness() -> Result<(), String> {
        let run = |code: &str| -> Result<Value, String> {
            let proto = prepare_proto(code)?;
            let mut vm = VM::new();
            vm.execute(proto)
        };

        // 14. Recursive Fibonacci: fib(18) == 2584
        let fib_code = r#"
            const function fibonacci(n: int): int
                if n <= 1 then
                    return n
                end
                return fibonacci(n - 1) + fibonacci(n - 2)
            end
            return fibonacci(18)
        "#;
        assert_eq!(run(fib_code)?, Value::Int(2584));

        // 15. BigInt: dynamic widening and multi-limb arithmetic
        let bigint_code = r#"
            local base = 2^100
            local factor = 3^60
            local sum = 0
            for i = 1, 10 do
                local widened = (1 << (60 + (i % 6))) * (i + 1)
                local product = (base + widened) * (factor - i)
                local quotient = product // (base >> 20)
                sum = sum + (quotient % 1000000007)
                base = base + (quotient >> 15)
            end
            return sum
        "#;
        match run(bigint_code)? {
            Value::Int(v) => assert!(v > 0),
            Value::BigInt(_) => {}
            other => return Err(format!("expected int or bigint, got {:?}", other)),
        }

        // 16. Table lookup throughput: array, named fields, dynamic keys, nested accesses
        let table_code = r#"
            local arr = {}
            for i = 1, 100 do arr[i] = i * 7 end
            local obj = {
                id = 1001, name = 2002, score = 3003, status = 4004, count = 5005,
                weight = 6006, tag = 7007, level = 8008, role = 9009, priority = 1010,
                nested = { a = 11, b = 22, c = 33, d = 44, inner = { x = 100, y = 200 } }
            }
            local keys = { "id", "name", "score", "status", "count", "weight", "tag", "level", "role", "priority" }
            local sum = 0
            for _ = 1, 5 do
                for j = 1, 10 do sum = sum + arr[j * 9] end
                sum = sum + obj.id + obj.name + obj.score + obj.status + obj.count
                sum = sum + obj.weight + obj.tag + obj.level + obj.role + obj.priority
                for _, k in keys do sum = sum + obj[k] end
                sum = sum + obj.nested.a + obj.nested.b + obj.nested.c + obj.nested.d
                sum = sum + obj.nested.inner.x + obj.nested.inner.y
            end
            return sum
        "#;
        match run(table_code)? {
            Value::Int(v) => assert!(v > 0),
            other => return Err(format!("expected int, got {:?}", other)),
        }

        // 17. GC allocation pressure: live retention and cycle sweep
        let gc_code = r#"
            local keep = {}
            for i = 1, 100 do
                local obj = {
                    id = i,
                    data = { val = i * 3, extra = i + 10 },
                    tags = { i, i * 2, i * 3 }
                }
                if i % 4 == 0 then
                    keep[#keep + 1] = obj
                end
            end
            local sum = 0
            for _, item in keep do
                sum = sum + item.id + item.data.val + item.tags[2]
            end
            collectgarbage("collect")
            return sum
        "#;
        match run(gc_code)? {
            Value::Int(v) => assert!(v > 0),
            other => return Err(format!("expected int, got {:?}", other)),
        }

        Ok(())
    }
}