How do I force OCaml to inline a function?
Asked Answered
O

1

8

Is it possible to tell the OCaml compiler to inline a function, instead of hoping that its optimization process will do so itself?

Overscore answered 26/1, 2019 at 19:48 Comment(0)
I
20

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.

Inhabitant answered 26/1, 2019 at 20:43 Comment(4)
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.