Interchange structured data between Haskell and C

Posted by Eonil on Stack Overflow See other posts from Stack Overflow or by Eonil
Published on 2010-12-21T17:27:28Z Indexed on 2010/12/22 0:54 UTC
Read the original article Hit count: 225

Filed under:
|
|
|

First, I'm a Haskell beginner.

I'm planning integrating Haskell into C for realtime game. Haskell does logic, C does rendering. To do this, I have to pass huge complexly structured data (game state) from/to each other for each tick (at least 30 times per second). So the passing data should be lightweight. This state data may laid on sequential space on memory. Both of Haskell and C parts should access every area of the states freely.

In best case, the cost of passing data can be copying a pointer to a memory. In worst case, copying whole data with conversion.

I'm reading Haskell's FFI(http://www.haskell.org/haskellwiki/FFICookBook#Working_with_structs) The Haskell code look specifying memory layout explicitly.

I have a few questions.

  1. Can Haskell specify memory layout explicitly? (to be matched exactly with C struct)
  2. Is this real memory layout? Or any kind of conversion required? (performance penalty)
  3. If Q#2 is true, Any performance penalty when the memory layout specified explicitly?
  4. What's the syntax #{alignment foo}? Where can I find the document about this?
  5. If I want to pass huge data with best performance, how should I do that?

*PS Explicit memory layout feature which I said is just C#'s [StructLayout] attribute. Which is specifying in-memory position and size explicitly. http://www.developerfusion.com/article/84519/mastering-structs-in-c/

I'm not sure Haskell has matching linguistic construct matching with fields of C struct.

© Stack Overflow or respective owner

Related posts about c

    Related posts about syntax