Hi folks,
I am writing some numerical computation code in OCaml that operates over arrays that are exposed globally. However, I am finding that what I believe to be a write barrier causing a significant slowdown.
A minimal example illustrating the issue:
open Core
open Core_bench.Std
let inner_b n () =
let v : int ref = ref 0 in
for i = 0 to n do v := !v + !v done
let v : int ref = ref 0
let outer_b n () =
for i = 0 to n do v := !v + !v done
let main () =
let n = 100 in
Command.run (Bench.make_command [
Bench.Test.create ~name:"inner test" (inner_b n);
Bench.Test.create ~name:"outer test" (outer_b n)
])
let _ = main ()
We can see that timings are greatly affected:
ββββββββββββββ¬βββββββββββ¬βββββββββββββ
β Name β Time/Run β Percentage β
ββββββββββββββΌβββββββββββΌβββββββββββββ€
β inner test β 56.05ns β 18.83% β
β outer test β 297.69ns β 100.00% β
ββββββββββββββ΄βββββββββββ΄βββββββββββββ
I could imagine a few workwarounds to this issue, but given that I am not expert on the GC I thought I would ask for advice first.