An Introduction to Turbofan
V8's Optimizer
What is Turbofan?
All JavaScript code is JIT compiled, meaning it is first converted into bytecode before being executed as machine instructions. In V8, the interpreter responsible for this bytecode is called Ignition, and as such we can refer to the intermediate bytecode as ignition bytecode.
One of the benefits of JIT compiling the javascript involves the collection of feedback from the Ignition interpreter. Often-executed functions get optimized to be faster and more efficient - this is the job of Turbofan.
Speculative Optimizations
The optimizations made by Turbofan are based off assumptions (hence speculative) - it will attempt to optimize the code for whatever object types and logic flows it expects to meet.
For example, let's say we're programming a game. In this game, you are a soldier, and your job is to hold off the invading aliens. Holding them off by force, the kill()
function is regularly called on aliens. Turbofan recognises this, and generates optimized code for running the kill()
function. With such a large number of calls to the function, the optimization is massively helpful. Go Turbofan!
But, once again, this optimization is speculative - the original code generated by Turbofan is optimized for running kill()
on aliens. Let's say that the player ends up being overwhelmed, and kill()
is called on the player themselves. Suddenly, turbofan's speculation is incorrect - the optimized code is incredbly quick, but only for aliens! The type passed to kill()
here is a Player
, so this optimization cannot be used. Turbofan has to destroy this optimzation and generate some ignition bytecode instead in a process called deoptimization.
Deoptimzation has a huge performance cost, and is very desirable to avoid. As a result, Turbofan will optimize the code once again, if kill()
continues to be called on both aliens and players. Turbofan will speculatively optimize the code to handle both alien and player objects, clawing back the performance benefits - maybe not entirely, but far better than generating ignition bytecode every time.
Sea of Nodes
TODO
Turbolizer
TODO
Last updated
Was this helpful?