JavaScript: From Closures to Autonomous Systems

Chapter 5. From Interpreter to JIT

Part II. A Machine That Learns to Run Code6 min

05

From Interpreter to JIT

The first V8 had no interpreter. On the very first call it translated any function into machine code with a simple and fast compiler that the team called full-codegen. If a function was called often, it was later handed to a second, smart compiler. From 2010 that role was played by Crankshaft: it read the diary of the inline caches and built machine code for the types it saw there. Doing without an interpreter looked natural at the time. The team had come to the browser to compete on speed, and before 2008 JavaScript had been executed by interpreters, and slowness was considered an innate property of the language. Compilers that work right while the program is running are called just-in-time, and that is where the abbreviation JIT in the chapter title comes from.

Eight years later it turned out that machine code takes up too much memory. By the V8 team’s own estimate, the code that full-codegen produced for every function called even once occupied almost a third of all the memory Chrome allotted to JavaScript. On a desktop people put up with it, but on phones, where half a gigabyte had to cover everything, such waste was hard to miss. In 2016 V8 got an interpreter, Ignition. It translates JavaScript into compact bytecode and executes it, and the first to receive it were low-end Android phones in Chrome 53. Memory for unoptimized code on them shrank ninefold. In May 2017 Ignition, together with the new smart compiler TurboFan, became the only way V8 executes JavaScript, and full-codegen and Crankshaft were retired.

Bytecode, meanwhile, had been in JavaScript from the very beginning. Mocha’s ten-day prototype in 1995 also translated the program into bytecode: Netscape’s server team demanded it, because they needed applications to load quickly. V8 started by abandoning bytecode for the sake of speed and came back to it for the sake of memory. Only now bytecode became the bottom rung of a ladder that code climbs if it proves it deserves more.

Free, one step away

The rest of the book is free

Leave your email and I will send a link to confirm it. Once you click it, every chapter opens here, on this device, for a year. When a new book comes out I will write to this address; reply “stop” to any email and I will not write again.

Have an INITE account? Sign in and the book opens without an email.

I do not charge for the book. If you would like to support it, you can donate from any chapter.

How your email is stored