gfwilliams / EspruinoCompiler

Node.js-based JavaScript->C++ compiler
25 stars 3 forks source link

Cache symbol table lookups #6

Open gfwilliams opened 9 years ago

gfwilliams commented 9 years ago

Originally: https://github.com/espruino/EspruinoTools/issues/8

Currently, if I write:

function f() {
  "compiled";
  a = 1;
  a = 2;
  a = 3;
}

It does 3 symbol table lookups for a. Ideally it'd look a up once and then keep it for future use.

Even more of an issue for something like:

function f() {
  "compiled";
  digitalWrite(LED1,0);
  digitalWrite(LED1,1);
  digitalWrite(LED1,0);
  digitalWrite(LED1,1);
}

Where both digitalWrite and LED1 get looked up each time.

While potentially that causes issues with something like this:

function setA() {
 a=1;
}

function f() {
  "compiled";
  console.log(a);
  setA();
  console.log(a);
}

The first use of a should have failed with a ReferenceError (but it doesn't in Espruino) so we're probably safe to ignore it.