Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory layout of boxed and unboxed ints? [duplicate]

Tags:

haskell

ghc

My understanding is that an Int value is a pointer to a thunk (double indirection) and an unboxed Int# is just a pointer to a 32/64 bit int. Is that correct? How does the pointer encode the fact that it's referring to an unboxed value?

The Haskell standard states that an Int is "A fixed-precision integer type with at least the range [-2^29 .. 2^29-1]". Is there some optimization in GHC where those extra bits are used to eliminate the indirections?

like image 628
Daniel Avatar asked Jun 28 '13 00:06

Daniel


1 Answers

The GHC documentation has some good information. But basically, you're correct in saying that an Int value is a pointer to a thunk. However, an unboxed value is not a pointer to the unboxed value, it is the unboxed value itself. Also, the Haskell standard report merely gives the lower limit on the range of Int. IIRC, GHC Int's have more than 30-bits.

I don't think GHC uses the extra bits of unboxed types to store any metadata, but it does use the bits of pointers to do so. See this page for more details.

like image 111
Travis Athougies Avatar answered Oct 24 '22 23:10

Travis Athougies