How to find out GHC's memory representations of data types?

前端 未结 2 720
感情败类
感情败类 2020-12-15 21:30

Recently, blog entries such as Computing the Size of a Hashmap explained how to reason about space complexities of commonly used container types. Now I\'m facing the questio

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 21:43

    Memory footprints of Haskell Data Types

    (The following applies to GHC, other compilers may use different storage conventions)

    Rule of thumb: a constructor costs one word for a header, and one word for each field. Exception: a constructor with no fields (like Nothing or True) takes no space, because GHC creates a single instance of these constructors and shares it amongst all uses.

    A word is 4 bytes on a 32-bit machine, and 8 bytes on a 64-bit machine.

    So e.g.

    data Uno = Uno a
    data Due = Due a b
    

    an Uno takes 2 words, and a Due takes 3.

    Also I believe it is possible to write a haskell function which performs the same tasks as sizeof or offsetof

提交回复
热议问题