why does the scrutinee of a CoreSyn.Case get bound to a variable?

Max Bolingbroke batterseapower at hotmail.com
Sun Feb 27 19:11:45 CET 2011


Consider this code:

On 27 February 2011 05:46, Adam Megacz <megacz at cs.berkeley.edu> wrote:
>   let x = escrut
>     in case x of
>       Foo a b -> ebranch1

This allocates a thunk for escrut then immediately case scrutinises
that thunk, forcing it

However, this code:

case x at escrut {
    Foo a b -> ebranch1
  }

(Where x is the case scrutinee binder) just enteres the code for
escrut with a scrutinisation frame on the stack, with no intermediate
heap allocation. Thus this code is faster.

So having the case wildcard lets GHC generate better code. Another
reason is if GHC sees:

case x at escrut {
  Foo a b -> ... Foo a b ...
}

It can rewrite the (Foo a b) it sees in the branch as x directly,
without having to go back and introduce a let-binding for escrut
before the case expression. So it is more convenient to prevent
reboxing when you have the scrutinee binder.

I'm sure Simon will have some other reasons it is good, but those are
the 2 that come to mind.

Cheers,
Max



More information about the Cvs-ghc mailing list