[Haskell-beginners] Meaning of '!' in record defintion? UNPACK?
Daniel Fischer
daniel.is.fischer at web.de
Thu Feb 26 06:33:01 EST 2009
Am Donnerstag, 26. Februar 2009 12:07 schrieb Colin Paul Adams:
> >>>>> "Thomas" == Thomas Davie <tom.davie at gmail.com> writes:
>
> Thomas> The {-# UNPACK #-} tells the compiler that it can unpack
> Thomas> the Int – meaning that a Position will be neatly packed
> Thomas> into 12 bytes.
>
> What would be the difference if there was no UNPACK pragma?
Section 8.12.10 of the users' guide says:
"The UNPACK indicates to the compiler that it should unpack the contents of a
constructor field into the constructor itself, removing a level of
indirection."
It has more, and also says when it's not a good idea to use it.
Int is defined as
data Int = I# Int#
where Int# is a raw machine int. If you use the {-# UNPACK #-} pragma, you
tell GHC that you'd very much like Position to be stored as constructor +
three contiguous raw machine integers. Mostly, it will do so.
If you don't use the pragma, i.e. have
data Position =
Position { posOffset :: !Int
, posRow :: !Int
, posColumn :: !Int
}
, GHC may or may not decide to store it thus, with -O2 it's not too unlikely,
I think. But it's also not unlikely that it will be stored as constructor +
three pointers to three evaluated Ints, which is much better than pointers to
thunks, but not as good as having the raw values directly by the constructor.
Cheers,
Daniel
More information about the Beginners
mailing list