3 ms·
In OCaml, there's the option to directly call the native function without any setup cost or overhead, in fact, some of the standard library's function are offer
by toolslive 8y ago
In OCaml, there's the option to directly call the native function without any setup cost or overhead, in fact, some of the standard library's function are offered this way.
For example:
external sqrt : float -> float = "caml_sqrt_float"
"sqrt" [@@unboxed] [@@noalloc]
- anonymouz 8y agoThe overhead that is being talked about in the blog post is mostly the impossibility to inline/optimize the C code of the callee, setting up the stack frame, etc. How would OCaml avoid that?
- toolslive 8y agoIt does a reasonable job: external my_sqrt : float -> float = "caml_sqrt_float" "sqrt" [@@unboxed] [@@noalloc] let () = let x= my_sqrt 1.0 in Printf.printf "x = %f" x becomes something like: ... camlInline__entry: .cfi_startproc subq $8, %rsp .cfi_adjust_cfa_offset 8 .L102: movsd .L103(%rip), %xmm0 sqrtsd %xmm0, %xmm0 movsd %xmm0, (%rsp) movq camlInline__6@GOTPCREL(%rip), %rbx movq camlPervasives@GOTPCREL(%rip), %rax movq 208(%rax), %rax call camlPrintf__fprintf_1291@PLT ....