Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an unboxed value in GHC Haskell and when should we use them?

Tags:

haskell

ghc

GHC Haskell exposes the prim package, which contains definitions of unboxed values, such as Int#, Char#, etc.

  • How do they differ from the default Int, Char, etc., types in regular Haskell? An assumption would be that they're faster, but why?
  • When should one reach down to use these instead of the boxed regular alternatives?
  • How does using boxed vs unboxed value affect the program?
like image 537
Jakub Arnold Avatar asked Nov 28 '14 17:11

Jakub Arnold


1 Answers

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.)

like image 163
MathematicalOrchid Avatar answered Sep 29 '22 01:09

MathematicalOrchid