开发者

type vs data performance in haskell

开发者 https://www.devze.com 2023-03-11 01:06 出处:网络
I found answers explaining difference betwee开发者_如何学编程n newtype and data in Haskell. But if I have the following type synonym:

I found answers explaining difference betwee开发者_如何学编程n newtype and data in Haskell. But if I have the following type synonym:

type Point = (Int,Int)

Would that be more efficient rather to use:

data Point = Pt (Int,Int) ?


Using type will be more efficient, as it incurs one less indirection than the data version.

Note that both are more inefficient than:

data Point = Point {-# UNPACK #-}!Int {-# UNPACK #-}!Int

as you can see from this earlier question on data representations.


Yes.

The Pt construction adds one word of overhead (in GHC) and the field (i.e. the pair) is stored as a pointer to a pair, adding one additional word, for a total of two words overhead (and an extra indirection to get to the values).

I recommend that you either use the type synonym or, better yet, define

data Point = Pt {-# UNPACK #-} !Int {-# UNPACK #-} !Int

This requires 4 words less than

type Point = (Int, Int)

and uses one less level of indirections (pointers).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号