efficiency

malcolm-ffi at cs.york.ac.uk malcolm-ffi at cs.york.ac.uk
Fri Dec 1 09:59:44 EST 2000


> > To me it seems as if we should seperate conversion from
> > [Char] to [CChar] from the actual marshalling of CChar.
> > Why is it an advantage to lump it all together?
> 
> Efficiency, and the fact that combined conversion+marshalling
> operations are convenient anyway.

I don't want to talk about [Char] -> [CChar] specifically, but the
mention of efficiency reminds me of something that has been bothering
me for a little while.

In the current design of the FFI, it is assumed that most of the
marshalling work is going to be done in Haskell.  For example, to
return more than one result from a C function, the results are packaged
as a C struct, passed as an Addr (or Ptr) to Haskell, which then
dereferences from the Addr to each individual component of the struct.

Now in GHC, the treatment of the IO monad may make this acceptably
fast, but in other implementations (like nhc98 (of course)), this
is just horribly inefficient, and can slow up FFI calls by an order
of magnitude or more.  (Comparing to previous implementations using
either the builtin 'primitive' mechanism or GreenCard.)  I agree that
it is better in principle to have the marshalling code in Haskell
because it has much better type-safety than C.  But in practice,
I have found other ways to do the marshalling, mainly because of the
efficiency question.

Do other people have any thoughts about the efficiency of the current
scheme?

Regards,
    Malcolm




More information about the FFI mailing list