[erlang-questions] potential compiler improvement?

Attila Rajmund Nohl attila.r.nohl@REDACTED
Mon Oct 26 20:45:03 CET 2009

2009/10/26, James Hague <james.hague@REDACTED>:
> In a pattern match, all referenced values in tuples are loaded into BEAM
> registers. This happens right up front as part of the matching process. And
> it happens even if the values aren't used until some time later, and even if
> they're not used at all in a particular branch of code.  Here's an example:
> test({A, B, C, D, E, F, G}) ->
>    case A of
>       this -> B + C + D;
>       that -> E + F + G
>    end.
> In this case, all seven tuple elements, A-G, are loaded into registers
> before the "case" is executed. This is even though three values are unneeded
> in each of the two possible branches.
> I'm no expert on how the compiler works, so I'm not going to hazard a guess
> at the difficulty of this.

I don't know much about the compiler, but I think it hasn't got the
slightest idea of A being 'this', 'that' or something completely
different, so it can't really optimize. Actually if A is not 'this' or
'that', you'll get a crash report which includes all parameters, so it
is possible that all seven variables are used.

More information about the erlang-questions mailing list