[erlang-questions] Calling C code

Mikael Pettersson <>
Mon Jan 22 11:20:28 CET 2007


On Mon, 22 Jan 2007 10:34:43 +0100, Vlad Dumitrescu wrote:
> I have a tricky question. I have a distributed application that needs
> to do some simulations. Not very extensive, but a lot of them. Doing
> it in Erlang is way too slow and talking through a port is also too
> slow, but I really need the distribution and the higher level stuff
> that Erlang provides.
> 
> Question 1: by using a linked-in driver, are the calls direct, or do
> they go through the same layers as to a port?
> 
> If the answer above is "as to a port", then I had this idea: since
> hipe is compiling to native code, it might be possible to convince it
> to create a stub that will just call a statically linked C routine.
> 
> Now this isn't anything that should be a general feature of Erlang,
> but for this particular application it would be acceptable to use a
> tweaked VM.
> 
> Question 2 (going mainly to the hipe team), is whether this is feasible at all.

I think you would be much better off just adding a few BIFs that
contain your C code, or calls into your C code.

When native code calls C code there some important glue in between.
That glue is implemented by a per-C-function stub statically compiled
into the runtime system. But you can't just call random C stuff from
these stubs, they're very much only intended for calling BIFs and
a small set of helper procedures for the HiPE compiler.

Alternatively you can try to rework your application to batch requests
so that the calling mechanism isn't performance-critical any more.

/Mikael



More information about the erlang-questions mailing list