[Haskell-cafe] computational overhead of the "data" declarations
Don Stewart
dons at galois.com
Tue Oct 28 19:46:04 EDT 2008
eford:
> > So, to answer your question, the only computational overhead with a
> > data declaration is the extra memory and time to store and process the
> > constructor tags. It usually isn't noticeable, but sometimes the
> > difference can be important.
>
> Is there also potentially overhead due to laziness? I mean, in this
> case:
>
> newtype Fast = Fast Int
>
> If you have a Fast value, you know you've got a real Int, not a thunk,
> right? But here:
>
> data Slow = Slow Int
>
> Doesn't a Slow value remain unevaluated until you pattern match out the
> Int?
Also, remember that GHC is an optimising, strictness-analysing compiler,
making 'data' cheap or non-existent. Tags are also represented as bits in
pointers to heap values, so 'tags' are very cheap, if they're still needed
(e.g. when testing on Just/Nothing).
E.g.
Slow = Slow { unSlow :: Int }
deriving Show
bigger :: Slow -> Slow
bigger (Slow n) = Slow (n * 52)
small = Slow 7
main = print (unSlow $ bigger small)
Is compiled too:
lvl :: String
lvl = $wshowSignedInt 0 364 ([] @ Char)
So there's no runtime overhead.
-- Don
More information about the Haskell-Cafe
mailing list