CEK loop: use local int ref instead of Atomic for step counter

Atomic.fetch_and_add on every CEK step added unnecessary overhead.
The step counter is per-invocation (not shared across threads), so
a plain int ref with incr is sufficient and faster.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-29 20:04:05 +00:00
parent 1ad90ed23d
commit edf3354050

View File

@@ -382,15 +382,16 @@ and expand_macro mac raw_args env =
and cek_run state =
let s = ref state in
(try
let n = ref 0 in
while not (match cek_terminal_p !s with Bool true -> true | _ -> false) do
s := cek_step !s;
let n = Atomic.fetch_and_add _step_count 1 in
if n land 4095 = 0 then begin
incr n;
if !n land 4095 = 0 then begin
let lim = Atomic.get _step_limit in
if lim > 0 && n >= lim then
raise (Eval_error (Printf.sprintf "Render step limit exceeded (%d steps)" n));
if n land 1048575 = 0 && n > 0 then
Printf.eprintf "[cek] %d steps, phase=%s control=%s\n%!" n
if lim > 0 && !n >= lim then
raise (Eval_error (Printf.sprintf "Render step limit exceeded (%d steps)" !n));
if !n land 1048575 = 0 then
Printf.eprintf "[cek] %d steps, phase=%s control=%s\n%!" !n
(match cek_phase !s with String p -> p | _ -> "?")
(String.sub (Sx_runtime.value_to_str (cek_control !s)) 0
(min 80 (String.length (Sx_runtime.value_to_str (cek_control !s)))))