espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.74k stars 741 forks source link

JIT: If a function/method is built-in, reference it directly #2398

Open gfwilliams opened 11 months ago

gfwilliams commented 11 months ago

Right now, JIT creates code on demand, but it looks everything up on the fly.

As an example:

function turnOn() { "jit"
  digitalWrite(LED1,1);
}

roughly translates to:

or.....

function draw() { "jit"
  g.drawLine(0,0,100,100);
}

roughly translates to:

I think it's probably safe to assume that in both cases we could look the relevant thing up at JIT time and if it is built into the interpreter (or it's defined const), we could just use it direct?

edit: sometimes code does overload an old implementation (eg maybe g.drawLine may be patched with a fixed version) but as long as that is done before JIT parses the function we'd be fine

It should translate to a pretty drastic speed improvement.

gfwilliams commented 2 months ago

There is now a partial implementation of this. digitalWrite(LED1,1); now uses digitalWrite and LED1 directly (rather than searching).

Handling g.drawLine is more problematic as we're not sure if g is going to change. It's pretty clear for g.drawLine, but x.toString() will expect to call the toString function for whatever x is. For console.log/etc where console is itself a builtin this makes a lot of sense though.

We currently store our vars list as just a list of var names, but I think to do this properly we'd want to change it to include full paths, like "console.log" - which then get appended only if we know that the first part was a builtin too. All a lot more work though.