[Haskell-cafe] Why doesn't this consume all the computer's memory?

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Tue Nov 6 11:49:05 UTC 2018


Tyson Whitehead wrote:
> and, in the -ddump-simpl we see the `go Left` branch returns a thunk
> on both the right and left sides that hold onto the evaluation of
> (x:xs,ys) as we would expect
> 
> ```
> Left x_aqy ->
>   (GHC.Types.:
>     @ a_a1q8 x_aqy
>   (case ds1_d1rO of { (xs_aqz, ys_aqA) -> xs_aqz }),
>    case ds1_d1rO of { (xs_aqz, ys_aqA) -> ys_aqA });
> ```

These two expressions are selectors (accessing a field from a single
constructor data type), which the compiler recoginizes (not sure where
and how exactly) and implemented in terms of special stg_sel_<nnn>
closures. At runtime, applying such a closure results in special
THUNK_SELECTOR thunks. These thunks are evaluated by the garbage
collector (see rts/sm/Scav.c).

So this is a very special optimization in the garbage collector, not a
flaw in your general mental model.

Cheers,

Bertram


More information about the Haskell-Cafe mailing list