如何强制OCaml内联函数?

kev*_*nji 4 ocaml inline-functions

是否有可能告诉OCaml编译器内联函数,而不是希望它的优化过程本身会这样做?

oct*_*ron 13

您可以添加一个属性以始终内联函数

let f x = x [@@inline always]
(* which is equivalent to *)
let f x = x [@@inline]
Run Code Online (Sandbox Code Playgroud)

或强制将特定调用与另一个属性内联

let a = (f[@inlined]) 1
Run Code Online (Sandbox Code Playgroud)

如果要检查由flambda做出的内联决策,可以使用该inlining-report标志.

  • 我还看到了函数内联的形式“let[@inline] fx = x”(没有“d”) (2认同)
  • `let[@inline] fx = x` 是 `let fx = x [@@inline]` 的简写。 (2认同)
  • 截至 2022 年,手册的相关部分现在是[那个](https://ocaml.org/manual/flambda.html)。 (2认同)