Performance/FFI

From HaskellWiki
< Performance
Revision as of 10:37, 30 June 2006 by Simonmar (talk | contribs) (mention "unsafe")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Improving performance for FFI code

Use "unsafe" foreign imports

If you annotate a foreign import declaration with the unsafe keyword, this indicates to the compiler that (1) the call will not invoke another Haskell function, directly or indirectly, and (2) you don't mind if any other running Haskell threads in the system are blocked for the duration of the call.

These restrictions enable the compiler to generate a much more efficient call. In particular, GHC will generate a simple inline function call for an unsafe call, but a safe call by contrast is quite heavyweight: it involves saving some state on the Haskell stack, and a couple of calls into the runtime are made in addition to the foreign call itself.