Is it possible to tell the OCaml compiler to inline a function, instead of hoping that its optimization process will do so itself?
Asked
Active
Viewed 2,050 times
1 Answers
19
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](https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec261). – Kevin Ji Jan 27 '19 at 02:14
-
2I have also seen the form `let[@inline] f x = x` for function inlining (without 'd') – user3240588 Apr 10 '19 at 15:41
-
2`let[@inline] f x = x` is a shorthand for `let f x = x [@@inline]`. – octachron Apr 10 '19 at 16:04
-
2As of 2022, the relevant part of the manual is now [that one](https://ocaml.org/manual/flambda.html). – Maëlan Jan 11 '22 at 14:45