Sub topic 2
Running Bytecode in an Interpreter
This chapter assumes you can write JavaScript functions and run scripts with Node.js. It also assumes you have read the previous chapter, which described how V8 parses source into an abstract syntax tree (AST) and turns that tree into bytecode. Here we look at what happens to that bytecode next: V8's interpreter, Ignition, executes it.
Why V8 interprets first
Most functions in a program run only a few times. Compiling every one of them to optimized machine code would slow down startup and use a lot of memory. Bytecode is compact, and generating it is cheap. V8 therefore compiles a function to bytecode lazily, the first time it is called, and Ignition runs it straight away.
Before Ignition shipped in 2016–2017, V8 compiled every function directly to unoptimized machine code. That code took several times more memory than the equivalent bytecode, which hurt most on phones.
The register machine with an accumulator
Ignition is a register machine. Each function gets a frame of virtual registers (r0, r1, ...) for its locals and temporaries, plus parameter registers (a0, a1, ...). One special register, the accumulator, holds the implicit input and output of most instructions. Because the accumulator is implicit, many bytecodes need only one explicit operand, which keeps them short.
You can print a function's bytecode yourself:
// Run with: node --print-bytecode --print-bytecode-filter=add add.js
function add(a, b) {
return a + b;
}
add(1, 2);The output looks roughly like this. Exact offsets and formatting vary between V8 versions.
Ldar a1
Add a0, [0]
Return| Bytecode | Meaning |
|---|---|
| `Ldar a1` | Load parameter `b` into the accumulator |
| `Add a0, [0]` | Add parameter `a` to the accumulator, recording feedback in slot 0 |
| `Return` | Return the value in the accumulator |
Dispatch: how each instruction runs
Every bytecode has a handler, a small piece of machine code that V8 generates at build time. After a handler does its work, it reads the next bytecode, looks up that bytecode's handler in a dispatch table, and jumps to it.
This loop has a cost. Every instruction pays for a lookup and an indirect jump, and the handlers have to be generic. For example, Add must handle integers, floating-point numbers, strings, and objects with valueOf. That generality is the main reason interpreted code runs slower than specialized machine code.
Feedback slots: the interpreter takes notes
The [0] in Add a0, [0] is a feedback slot. While Add runs, it records the kinds of values it saw, such as "both small integers." Property loads, calls, and comparisons have slots too. The interpreter does not use these notes to speed itself up much. The faster tiers covered in later chapters read them to decide what to optimize and how.
Common misconceptions
- "Interpreted means slow everywhere." Ignition is fast enough for the large majority of code, which never gets hot enough to need optimizing.
- "Bytecode is generated for the whole file up front." V8 parses inner functions only lightly at first, and it generates their bytecode on the first call.
Exercise
In the bytecode Ldar a1 / Add a0, [0] / Return, where is the result of a + b stored before Return executes, and what is [0] for?
Show answer
The result is in the accumulator, because Add adds its register operand to the accumulator and leaves the sum there. [0] is the index of a feedback slot, where the interpreter records the operand types it saw so that the optimizing tiers can specialize the code later.
Exercise
Why does V8 compile functions to bytecode lazily rather than generating optimized machine code for every function at startup?
Show answer
Most functions run rarely. Bytecode is cheap to produce and compact in memory, so interpreting first gives fast startup and a small memory footprint. Expensive compilation is saved for the functions that turn out to be hot.