... | ... | @@ -7,8 +7,8 @@ This page explains the motivation and implementation of unpacking for sum types. |
|
|
GHC does a good job of unpacking product types. Given a declaration like
|
|
|
|
|
|
```wiki
|
|
|
data T1 = C1 a b
|
|
|
data T2 = C2 {-# UNPACK #-} !T1
|
|
|
data T1 a b = C1 a b
|
|
|
data T2 a b = C2 {-# UNPACK #-} !(T1 a b)
|
|
|
```
|
|
|
|
|
|
`C2` will have a representation where all the overhead of the `C1` constructor, both the pointer to it in the `C2` constructor and the info table pointer in the `C1` constructor, has been removed. This saves two words and one indirection compared to a packed representation, which uses five words.
|
... | ... | @@ -17,8 +17,8 @@ data T2 = C2 {-# UNPACK #-} !T1 |
|
|
Unfortunately, a similar example using sum types cannot be unpacked today:
|
|
|
|
|
|
```wiki
|
|
|
data T1 = Some a | None
|
|
|
data T2 = C !T1 -- Cannot UNPACK here
|
|
|
data T1 a = Some a | None
|
|
|
data T2 a = C !(T1 a) -- Cannot UNPACK here
|
|
|
```
|
|
|
|
|
|
|
... | ... | |