[Haskell-cafe] FFI: Overhead of foreign unsafe imports

Yves Parès yves.pares at gmail.com
Sun Feb 26 19:36:38 CET 2012


Hello,
When I was using C code from Python, the overhead put on calling C code by
Python was significant.
To simplify, say I have on C-side two procedures f and g, I do all the
stuff to call them in a row from Python, well I'm better off factorizing:
adding on C side a wrapper h procedure that calls them both, and then call
h from Python, because then I will have half as much overhead:

Instead of SwitchToC -> call f -> SwitchToPython -> SwitchToC -> call g ->
SwitchToPython,
the factorization leads to SwitchToC -> call f -> call g -> SwitchToPython,
which gives the same result yet is different performance-wise because each
switching has a cost.

This is painful, because if another time I have to call f and j (another
function), then I have to make another wrapper.

In Haskell world, now, given that my functions f and g would have been
imported using *unsafe*:

foreign import unsafe "f" f :: Thing -> Foo -> IO ()
foreign import unsafe "g" g :: Stuff -> Bar -> IO ()
foreign import unsafe "h" h :: Thing -> Foo -> Stuff -> Bar -> IO ()

Are
doStuff = f x y >> g z w
and
doStuff = h x y z w
equivalent, or is there an overhead (e.g. due to IO monad, or due to the
way the FFI does the calls) when compiled (if need be with optimizations)
with GHC?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120226/9db168c6/attachment.htm>


More information about the Haskell-Cafe mailing list