lightweight struct handling in FFI?

Alastair Reid alastair at
Thu Sep 19 06:54:21 EDT 2002

I made a start on writing an FFI howto at:

It is very rough, doesn't answer your specific question, has many
typos and omissions, etc. but some of the links in the Tips and Tricks
section (section 4) might help.  

To answer your question directly, I think it all depends on what
you're doing.  

Sometimes the semantics of the operations make it easy.  For example,
if there is any notion of object identity (and it often isn't stated
whether there is or not), you usually have to keep the master copy in
the C world and not duplicate.

If there is no notion of identity, no modification to the object,
etc., it is often easiest to keep it on the Haskell side and copy over
to the C side on each operation because it's easier.  There may be a
performance hit in doing this depending on the number of times it is
copied in each direction but I don't think we have any numbers for a
range of implementations (ghc numbers may be available).

Without any effort at measurement, I'd say that using malloc is pretty
slow (because C's malloc is pretty slow) and that ForeignPtr's are
fairly low overhead but more than just copying a small object over.

Alastair Reid                 alastair at  
Reid Consulting (UK) Limited

More information about the FFI mailing list