summaryrefslogtreecommitdiff
path: root/ocaml/test/bench_nock_versions.ml
blob: c70163355eccff2f387fb904a5a57eb58da6c436 (plain)
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
(** Benchmark all three Nock implementations:
    1. nock.ml - trampoline with some tail-calls (needs OCAMLRUNPARAM)
    2. nock_iter.ml - fully iterative (work/result stacks)
    3. nock_tail.ml - continuation-passing style (CPS)
*)

open Nock_lib

let time_it name f =
  let start = Unix.gettimeofday () in
  let result = f () in
  let elapsed = Unix.gettimeofday () -. start in
  Printf.printf "  %-20s %.4fs\n" name elapsed;
  result

let () =
  Printf.printf "\n╔═══════════════════════════════════════════════════════╗\n";
  Printf.printf "║  Benchmarking Nock Implementations                    ║\n";
  Printf.printf "╚═══════════════════════════════════════════════════════╝\n\n";

  (* Test 1: Simple operations *)
  Printf.printf "[1] Simple increment *[42 [4 0 1]] (1000x):\n%!";
  let inc_fol = Noun.cell (Noun.atom 4) (Noun.cell (Noun.atom 0) (Noun.atom 1)) in
  let bus = Noun.atom 42 in

  Printf.printf "  Running nock.ml...%!";
  let _ = time_it "nock.ml" (fun () ->
    for _i = 1 to 1000 do
      ignore (Nock.nock_on bus inc_fol)
    done) in

  Printf.printf "  Running nock_iter.ml...%!";
  let _ = time_it "nock_iter.ml" (fun () ->
    for _i = 1 to 1000 do
      ignore (Nock_iter.nock_on bus inc_fol)
    done) in

  Printf.printf "  Running nock_tail.ml...%!";
  let _ = time_it "nock_tail.ml" (fun () ->
    for _i = 1 to 1000 do
      ignore (Nock_tail.nock_on bus inc_fol)
    done) in

  Printf.printf "\n";

  (* Test 2: Ivory pill lifecycle *)
  Printf.printf "\n[2] Ivory pill lifecycle (full boot):\n%!";

  Eio_main.run (fun env ->
    Printf.printf "  Loading ivory.pill...%!";
    let fs = Eio.Stdenv.fs env in
    let bytes = Eio.Path.(load (fs / "ivory.pill")) |> Bytes.of_string in
    Printf.printf " %d bytes\n%!" (Bytes.length bytes);
    Printf.printf "  Cuing pill...%!";
    let pill = Serial.cue bytes in
    Printf.printf " done\n%!";

    match pill with
    | Noun.Cell { h = _tag; t = core; _ } ->
        Printf.printf "  Building formula...%!";
        let formula = Noun.cell
          (Noun.atom 2)
          (Noun.cell
            (Noun.cell (Noun.atom 0) (Noun.atom 3))
            (Noun.cell (Noun.atom 0) (Noun.atom 2))) in
        Printf.printf " done\n%!";

        (* Skip mug computation - it's O(n) on first call for uncached noun *)
        (* Printf.printf "  Core mug: 0x%08lx\n" (Noun.mug core); *)

        (* nock.ml needs OCAMLRUNPARAM - skip for now *)
        Printf.printf "  %-20s (skipped - needs OCAMLRUNPARAM)\n%!" "nock.ml";

        Printf.printf "  Running nock_iter.ml (this will take a while)...%!";
        let kernel_iter = time_it "\n  nock_iter.ml" (fun () ->
          Nock_iter.nock_on core formula) in

        let kernel_tail = time_it "nock_tail.ml" (fun () ->
          Nock_tail.nock_on core formula) in

        Printf.printf "\n  Results:\n";
        Printf.printf "    nock_iter mug: 0x%08lx\n" (Noun.mug kernel_iter);
        Printf.printf "    nock_tail mug: 0x%08lx\n" (Noun.mug kernel_tail);

        if Noun.equal kernel_iter kernel_tail then
          Printf.printf "    ✓ Both produce identical kernels!\n"
        else
          Printf.printf "    ✗ MISMATCH - kernels differ!\n";

    | _ ->
        Printf.printf "  Unexpected pill structure\n"
  );

  Printf.printf "\n╔═══════════════════════════════════════════════════════╗\n";
  Printf.printf "║  Summary:                                             ║\n";
  Printf.printf "║  • nock_iter.ml - Fast, predictable, explicit stacks  ║\n";
  Printf.printf "║  • nock_tail.ml - Elegant CPS, constant stack space   ║\n";
  Printf.printf "║  Both work without OCAMLRUNPARAM!                     ║\n";
  Printf.printf "╚═══════════════════════════════════════════════════════╝\n\n"