Lifting thunks out of thunks to reduce their size.
This is a spin off of this thread: ticket:10980#comment:131234
The idea is to add a transformation to STG (or Core) that replaces
let t1 = let t2 = e1 in e2 in e3
let t2 = e1 t1 = e2 in e3
Can we give an easy criterion when this transformation is beneficial?
The problem is that this will increase allocation in the case when
t1 is not called, as it is more space efficient to pack the union of the free variables of
e2 into one closure, instead of having one for the free variables of
e1 and one for those of
We could say “Do this transformation if the free variables of
e1” are disjoint. Then we’d allocate two extra words (one for the info table of
t2, and one pointer to that in the closure for
t1), but have some nice gains if
t1 is indeed called.
But this rule would not fire in the originalexample, because the
Num a dictionary is also a free variable, which would now have to be copied into both closures!
I guess one could try and see whether real-world programs benefit from this transformation, and how many shared free variables between
e2 are heuristically ok.