Search code examples
ocamlinline-functions

How do I force OCaml to inline a function?


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


Solution

  • 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.