Made a small benchmark to compare performance of record updates, with with
vs mutable fields. For some reason, the loop with mutable fields run 5x slower. Is Ocaml optimizing something I missed?
type query = {
select: string;
from: string;
where: string;
}
type query_mut = {
mutable select: string;
mutable from: string;
mutable where: string;
}
let _ =
for i = 0 to 1000000 do
let q : query = {select = ""; from = ""; where = ""} in
let q1 = {q with select = "1"} in
let q2 = {q1 with from = "table"} in
let q3 = {q2 with where = "a = b"} in
()
done;
(*
for i = 0 to 1000000 do
let q : query_mut = {select = ""; from = ""; where = ""} in
q.select <- "1";
q.from <- "table";
q.where <- "a = b";
()
done
*)
Compile with ocamlopt -O3 (or not, doesn’t affect run time).