[erlang-questions] potential compiler improvement?

Robert Virding rvirding@REDACTED
Mon Oct 26 22:02:35 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.
>

Now the compiler just looks at which elements from the tuple may accessed
and only extracts those. So if you only used A, B and C it would only
extract the first 3 elements. It could save the whole argument and only
extract the arguments when they are used but this would entail saving the
whole tuple for the future as long as it may be used. In the general case it
could maybe create more instructions, or maybe less, depending on what
follows. The analysis would be more difficult, though probably not
excessively so. Now the analysis and code generation is easier. It could
also delay freeing the tuple which would mean more work the garbage
collector.

I have absolutely no idea of the results of these trade-offs.

Robert


More information about the erlang-questions mailing list