[MLton] cost of _export

Bernard Berthomieu <[email protected]> Fri, 05 Dec 2014 16:57:01 +0100
Newsgroups gmane.comp.lang.ml.mlton.devel
Message-ID <[email protected]>
Hello,

I'd like to use the "_export" feature of mlton ffi to call SML functions
from C, but I'm surprised by its cost in terms of allocations.
The following simple example illustrates this.

The files x.sml and x.c are attached. I'm creating application x by:

     mlton -default-ann 'allowFFI true' -export-header x.h -stop tc x.sml
     gcc -c x.c
     mlton -default-ann 'allowFFI true' x.sml x.o


Then, running `x @MLton gc-summary -- 1000000` prints:

done
GC type        time ms     number          bytes          bytes/sec
-------------    -------    -------    --------------- ---------------
copying            146      4,348         55,184,904 377,978,794
mark-compact          0          0                  0               -
minor              0          0                  0 -
total time: 3,009 ms
total GC time: 1,382 ms (45.9%)
max pause time: 0 ms
total bytes allocated: 762,041,120 bytes
max bytes live: 12,772 bytes
max heap size: 188,416 bytes
max stack size: 296 bytes
num cards marked: 0
bytes scanned: 0 bytes
bytes hash consed: 0 bytes


The actual application may call SML from C that many times or even more.
Can somebody explain me the reason for the amount of bytes allocated;
where does it come from ?

Best,
   Bernard.

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk

_______________________________________________
MLton-devel mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-devel
x.c (text/plain, 71 B)
#include "x.h"

int C_add (int x,int y) {
return call_mladd (x,y);
}
x.sml (text/plain, 538 B)
(* calls C function C_add on (x,y); C_add itself calls SML (op +) *)
fun add (x,y) =
    let val sum = _import "C_add" private : int * int -> int;
	val ccall = _export "call_mladd" private : (int * int -> int) -> unit;
    in ccall (op +);
       sum (x,y)
    end;

(* decrements n from value passed to 0 using above add function *)
fun go () =
    let val n = Option.valOf (Int.fromString (hd (CommandLine.arguments ()))) handle _ => 1
	fun loop n = if n < 0 then print "done\n" else loop (add (n,~1))
    in loop n
    end;

go ();