flan/spike/embed/gc_ml.ml
Joseph Ferano d272a5b1e5 Six probes for whether the OCaml compiler can live in the game's process
Item 12 asks five questions and says to answer them with a spike rather than a
rewrite. spike/embed/ is that spike: one script, six binaries, each one built to
fail loudly at the thing it is asking about. It is deliberately not a dune
target -- the root dune only excludes old-ocaml/, so a dune file here would land
in @default and make the spike part of the build. It drives ocamlfind and clang
by hand against the flan.cmxa dune already produces.

The probes, in the order they would kill the idea: the smallest possible link, a
C main() reaching one OCaml function; the whole compiler linked in and doing
real work; the same again with lib/dynload_stubs.c from the unmerged dlopen
branch, because that is the only C the compiler itself is built from; the game
keeping the main thread while caml_startup happens on a pthread beside it; the
SIGSEGV disposition read on both sides of caml_startup; and an 8 MiB arena
checked byte for byte across a compaction.

No result is written down yet. This is the apparatus.
2026-09-12 20:27:45 +07:00

22 lines
881 B
OCaml

(* Step 6: the OCaml GC beside Flan's arenas.
Allocate hard, then compact -- the most disruptive thing the collector does,
since compaction is what actually moves blocks. C checks its arena after. *)
external note : nativeint -> unit = "spike_note_arena"
let () =
Callback.register "spike_churn" (fun (rounds : int) ->
let keep = ref [] in
for i = 1 to rounds do
(* Garbage, plus a little that survives, so the heap really grows. *)
for _ = 1 to 2000 do ignore (Bytes.create 512) done;
if i mod 10 = 0 then keep := Bytes.create 4096 :: !keep
done;
Gc.full_major ();
Gc.compact ();
let s = Gc.quick_stat () in
Printf.sprintf
"allocated %.0f words, %d major collections, %d compactions, heap %d words"
s.Gc.minor_words s.Gc.major_collections s.Gc.compactions s.Gc.heap_words);
ignore note