Is it possible to tell the OCaml compiler to inline a function, instead of hoping that its optimization process will do so itself?
How do I force OCaml to inline a function?
Asked Answered
You can both add an attribute to always inline a function
let f x = x [@@inline always]
(* which is equivalent to *)
let f x = x [@@inline]
or force a specific call to be inlined with another attribute
let a = (f[@inlined]) 1
If you want to check inlining decisions made by flambda, you can use the inlining-report
flag.
Thanks, with your answer I was able to find the relevant section in the manual. –
Overscore
I have also seen the form
let[@inline] f x = x
for function inlining (without 'd') –
Condolent let[@inline] f x = x
is a shorthand for let f x = x [@@inline]
. –
Inhabitant As of 2022, the relevant part of the manual is now that one. –
Tiresias
© 2022 - 2025 — McMap. All rights reserved.