Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How does V8 optimize JavaScript code?

How does V8 optimize JavaScript code?

Eugene Obrezkov

May 22, 2016
Tweet

More Decks by Eugene Obrezkov

Other Decks in Programming

Transcript

  1. Who am I? • Developer Advocate at Onix-Systems, Kirovohrad; •

    Kittik author (engine for ASCII presentations); • NodeJS outside collaborator; • Contributor in Sails ecosystem (in the past); • Open source maniac;
  2. –Wikipedia “AST is a tree representation of the abstract syntactic

    structure of source code. Each node of the tree denotes a construct occurring in the source code.”
  3. AST -> Full Codegen Full Codegen is a generic compiler

    in V8. The main purpose of it is to compile JavaScript to native code as quick as possible, generating the code without optimizations. It takes AST, walks over the nodes and emits calls to a macroassembler directly.
  4. Profiling thread alongside with Full-Codegen When running your application, V8

    also runs profiling thread. Profiling thread collects information about your functions, arguments and types that go through them.
  5. Crankshaft compiler Crankshaft compiler is an optimization compiler in V8.

    It takes AST and type feedback and compiles optimized code for your function. Optimized code of function replaces un-optimized one using on-stack replacement (OSR) then.
  6. Hydrogen compiler Takes AST and type feedback and generates high-

    level intermediate representation (HIR) which consists of a control-flow graph (CFG) in static single assignment form (SSA). During the HIR generation, actual optimizations are applied, such as constant folding, method inlining, dynamic type feedback, etc… The result is an optimized CFG that is used as input to the next compiler - Lithium.
  7. Lithium compiler Lithium compiler takes optimized HIR and translates it

    to machine-specific low-level intermediate representation (LIR). During LIR generation, Crankshaft can still apply some low-level optimizations to it. After LIR was generated, Crankshaft generates a sequence of native instructions for each Lithium instruction and gets executed.
  8. Summary • JavaScript is fast enough, comparing to C++; •

    Full Codegen compiler compiles JavaScript to native code without optimizations; • Profiling thread collects information about types, allowing to optimize your code; • Crankshaft is a black box where optimizations are applied;