Personal tools

Performance/FFI

From HaskellWiki

< Performance(Difference between revisions)
Jump to: navigation, search
(fix cat syntax)
m (Heading levels as per wiki standards)
Line 1: Line 1:
 
[[Category:Performance|FFI]]
 
[[Category:Performance|FFI]]
= Improving performance for FFI code =
+
== Improving performance for FFI code ==
   
== Use "unsafe" foreign imports ==
+
=== Use "unsafe" foreign imports ===
   
 
If you annotate a foreign import declaration with the <tt>unsafe</tt> 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.
 
If you annotate a foreign import declaration with the <tt>unsafe</tt> 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.

Revision as of 00:12, 8 February 2007

1 Improving performance for FFI code

1.1 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.