c - OCamlcstub performance -


does using multiple call external functions written in c affects performance of ocaml program?

for instance, let's assume want create function creates float list using previous value in list compute next iteration. reason, wish function come cstub.

does make difference in term of performance whether write in c or if mix c external functions ocaml code?

i guess question related happens when compiling:

ocamlc -o hello.byte -c hello.cma cstub.o 

said differently, there material difference between doing:

external next_iter: float -> float = "next_iter" let make_list n first_val =     let rec aux acc current_val n =         if n = 0 (* assume n never <0 *)             acc         else             let new_val = next_iter current_val in             aux (new_val :: acc) new_val (n-1) in aux [] first_val n 

and

external make_list: float -> int -> float list = "make_list" (* full implementation in c *) 

side question, if cstub looks this:

#include <caml/mlvalues.h>  camlprim value add_3(value x) {     int = int_val(x);     return val_int(x+3); } 

is location of returned value shared ocaml code or ocaml reallocate new part of memory before using value?

i asking because expect second option inefficient when using make_list solution cstub.c large list (if implemented way).

in general, calling c function ocaml has small constant overhead. first of all, c calling conventions differ ocaml calling convention , less efficient. when c function called, compiler needs store registers might clobbered call, needs restore them afterward. also, if c function allocates values in ocaml heap (that assumed default) call wrapped code setups , clears garbage collector roots. if function doesn't allocate, may mark external specification [@@noalloc] attribute remove unnecessary gc setup. finally, ocaml compiler can't inline (obviously) external calls, optimization opportunities missed, code specialization , allocation elimination. form in numbers, call wrapping code 10 assembly instructions. if c function compatible in size, overhead might significant, may consider either make call non-allocatable or consider rewriting in ocaml. in general, c functions bigger overhead negligible. final note, ocaml not python , efficient, there or never need reimplement algorithm in c. external interface used calling existing libraries, not available in c; invoking system calls; calling high-performance mathematical libraries , on.

side question

is location of returned value shared ocaml code or ocaml reallocate new part of memory before using value?

in example, returned value immediate value , stored in cpu register, i.e., not allocated. int_val , val_int simple macros translate between c int representation , ocaml int representation, i.e., shifts value left , sets least significant bit.

but in general, if value allocated caml_alloc , friends, value allocated in ocaml heap , not copied (unless gc performing moving own purposes).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -