
A crash course in just-in-time compilers (2017) - signa11
https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/
======
hinkley
One of my favorite little things about the calculus of JIT tuning is that a
faster interpreter can give you a better JIT. When the interpreter is 'good
enough' more often, then you compile fewer functions. If you compile fewer
functions, you can budget more resources for the ones you do, giving you
better performance.

Some have taken a different tack, use no interpreter at all, and instead dump
the simplest, fastest compile they possibly can on first execution.

------
Stratoscope
> Interpreters are quick to get up and running. You don’t have to go through
> that whole compilation step before you can start running your code. You just
> start translating that first line and running it.

> Because of this, an interpreter seems like a natural fit for something like
> JavaScript. It’s important for a web developer to be able to get going and
> run their code quickly.

> And that’s why browsers used JavaScript interpreters in the beginning.

I'm curious, is that true? How did the original JavaScript interpreter handle
code like this:

    
    
      var r = test();  // not defined yet
      alert( r );  // 42
      alert( typeof result );  // "undefined"
      
      function test() {
          result = 42;  // local because of 'var' below
          return result;
          var result;
      }
    

Was there a first pass to find "hoisted" function and var statements? Or did
it compile to bytecode? Or...?

~~~
Sebb767
I'm not an expert, but as far as I know, the first step is always to get an
abstract syntax tree, which is needed to check for syntax errors and have the
program in an usable representation. This means that your first pass always
reads all the code and allows handling this case, possibly without getting
into the hard work of optimizing, creating the variables etc.

That, or the early JS interpreters simply couldn't handle that case - I've
never heard of this, though.

~~~
cardiffspaceman
I don't think creation of a syntax tree is always explicit; I have read C
compiler source codes that use one pass with back-patching and the usable
representation is bytecode or machine code. One could reckon that the AST is
implicit during code generation as functions call each other to generate the
code. A valid JS interpreter could just do what the statements say using
dictionaries for each scope, linked together to allow levels of scope with the
right semantics.

~~~
jcranmer
It's worth noting that the scoping of names in C language is designed to be
solvable in one pass: you can't use a function before it's declared. Most
newer languages instead require a full pass to discover all the available
names, to allow functions to not require a separate declaration before all
uses.

------
thecupisblue
I once saw an article by this author, read all the rest and then forgot where
I found it. I spent days devouring the net. Thank you for posting this!

~~~
saagarjha
Psst…I think you may have been reaching for “scouring” ;)

~~~
thecupisblue
Thanks. Tho now imagining myself devouring the internet sounds like some tech-
god sci-fi scene.

