Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3612728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:02:13+00:00 2026-05-18T22:02:13+00:00

First, I’m a Haskell beginner. I’m planning integrating Haskell into C for realtime game.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:02:14+00:00Added an answer on May 18, 2026 at 10:02 pm

    I would strongly recommend using a preprocessor. I like c2hs, but hsc2hs is very common because it’s included with ghc. Greencard appears to be abandoned.

    To answer your questions:

    1) Yes, through the definition of the Storable instance. Using Storable is the only safe mechanism to pass data through the FFI. The Storable instance defines how to marshal data between a Haskell type and raw memory (either a Haskell Ptr, ForeignPtr, or StablePtr, or a C pointer). Here’s an example:

    data PlateC = PlateC {
      numX :: Int,
      numY :: Int,
      v1   :: Double,
      v2   :: Double } deriving (Eq, Show)
    
    instance Storable PlateC where
      alignment _ = alignment (undefined :: CDouble)
      sizeOf _ = {#sizeof PlateC#}
      peek p =
        PlateC <$> fmap fI ({#get PlateC.numX #} p)
               <*> fmap fI ({#get PlateC.numY #} p)
               <*> fmap realToFrac ({#get PlateC.v1 #} p)
               <*> fmap realToFrac ({#get PlateC.v2 #} p)
      poke p (PlateC xv yv v1v v2v) = do
        {#set PlateC.numX #} p (fI xv)
        {#set PlateC.numY #} p (fI yv)
        {#set PlateC.v1 #}   p (realToFrac v1v)
        {#set PlateC.v2 #}   p (realToFrac v2v)
    

    The {# ... #} fragments are c2hs code. fI is fromIntegral. The values in the get and set fragments refer to the following struct from an included header, not the Haskell type of the same name:

    struct PlateCTag ;
    
    typedef struct PlateCTag {
      int numX;
      int numY;
      double v1;
      double v2;
    } PlateC ;
    

    c2hs converts this to the following plain Haskell:

    instance Storable PlateC where
      alignment _ = alignment (undefined :: CDouble)
      sizeOf _ = 24
      peek p =
        PlateC <$> fmap fI ((\ptr -> do {peekByteOff ptr 0 ::IO CInt}) p)
               <*> fmap fI ((\ptr -> do {peekByteOff ptr 4 ::IO CInt}) p)
               <*> fmap realToFrac ((\ptr -> do {peekByteOff ptr 8 ::IO CDouble}) p)
               <*> fmap realToFrac ((\ptr -> do {peekByteOff ptr 16 ::IO CDouble}) p)
      poke p (PlateC xv yv v1v v2v) = do
        (\ptr val -> do {pokeByteOff ptr 0 (val::CInt)}) p (fI xv)
        (\ptr val -> do {pokeByteOff ptr 4 (val::CInt)}) p (fI yv)
        (\ptr val -> do {pokeByteOff ptr 8 (val::CDouble)})   p (realToFrac v1v)
        (\ptr val -> do {pokeByteOff ptr 16 (val::CDouble)})   p (realToFrac v2v)
    

    The offsets are of course architecture-dependent, so using a pre-processer allows you to write portable code.

    You use this by allocating space for your data type (new,malloc, etc.) and pokeing the data into the Ptr (or ForeignPtr).

    2) This is the real memory layout.

    3) There is a penalty for reading/writing with peek/poke. If you have a lot of data, it’s better to convert only what you need, e.g. reading just one element from a C array instead of marshalling the entire array to a Haskell list.

    4) Syntax depends upon the preprocessor you choose. c2hs docs. hsc2hs docs. Confusingly, hsc2hs uses the syntax #stuff or #{stuff}, while c2hs uses {#stuff #}.

    5) @sclv’s suggestion is what I would do as well. Write a Storable instance and keep a pointer to the data. You can either write C functions to do all the work and call them through the FFI, or (less good) write low-level Haskell using peek and poke to operate on just the parts of the data you need. Marshalling the whole thing back and forth (i.e. calling peek or poke on the entire data structure) will be expensive, but if you only pass pointers around the cost will be minimal.

    Calling imported functions through the FFI has a significant penalty unless they’re marked “unsafe”. Declaring an import “unsafe” means that the function should not call back into Haskell or undefined behavior results. If you’re using concurrency or parallelism, it also means that all Haskell threads on the same capability (i.e. CPU) will block until the call returns, so it should return fairly quickly. If those conditions are acceptable an “unsafe” call is relatively fast.

    There are a lot of packages on Hackage that deal with this sort of thing. I can recommend hsndfile and hCsound as exhibiting good practice with c2hs. It’s probably easier if you look at a binding to a small C library you’re familiar with though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I am using Windows XP. I have multiple hard drives and it
First of all, I know how to build a Java application. But I have
First off, there's a bit of background to this issue available on my blog:
First let me say that I really feel directionless on this question. I am
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I understand the reasons why an interface or abstract class (in the
First off if you're unaware, samba or smb == Windows file sharing, \\computer\share etc.
First off: I'm using a rather obscure implementation of javascript embedded as a scripting
First off, I'm working on an app that's written such that some of your
First of all, I don't need a textual comparison so Beyond Compare doesn't do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.