Interchange structured data between Haskell and C
- by Eonil
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.
Can Haskell specify memory layout explicitly? (to be matched exactly with C struct)
Is this real memory layout? Or any kind of conversion required? (performance penalty)
If Q#2 is true, Any performance penalty when the memory layout specified explicitly?
What's the syntax #{alignment foo}? Where can I find the document about this?
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.