GHC Haskell exposes the prim package, which contains definitions of unboxed values, such as Int#, Char#, etc.
Int, Char, etc., types in regular Haskell? An assumption would be that they're faster, but why?In simple terms, a value of type Int may be an unevaluated expression. The actual value isn't calculated until you "look at" the value.
A value of type Int# is an evaluated result. Always.
As a result of this, a Int a data structure that lives on the heap. An Int# is... just a 32-bit integer. It can live in a CPU register. You can operate on it with a single machine instruction. It has almost no overhead.
By contrast, when you write, say, x + 1, you're not actually computing x + 1, you're creating a data structure on the heap that says "when you want to compute this, do x + 1".
Put simply, Int# is faster, because it can't be lazy.
When should you use it? Almost never. That's the compiler's job. The idea being that you write nice high-level Haskell code involving Int, and the compiler figures out where it can replace Int with Int#. (We hope!) If it doesn't, it's almost always easier to throw in a few strictness annotations rather than play with Int# directly. (It's also non-portable; only GHC uses Int# - although currently there aren't really any other widely used Haskell compilers.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With