potential compiler improvement?

James Hague <>
Mon Oct 26 17:31:59 CET 2009


I've been looking a lot of BEAM code lately, so I can better understand how
the VM works. I have generally been trying to avoid looking at generated
code quality too much, because I can easily get obsessed with it (see
http://prog21.dadgum.com/50.html). But I have noticed one general thing
which might be worth investigating in the future.

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.

James


More information about the erlang-questions mailing list