Approaches to Optimizing V8 JavaScript Engine

. JavaScript is one of the most popular programming languages in the world. Started as a simple scripting language for web browsers it now becomes language of choice for millions of engineers in the web, mobile and server-side development. However its interpretational nature doesn’t always provide adequate performance. To speed up execution of JavaScript programs there were developed several optimization techniques in recent years. One example of modern high-performing JavaScript engine is a V8 engine used in Google Chrome browser and node.js web server among others. This is an open source project which implemented some advanced optimization methods including Just-in-Time compilation, Polymorphic Inline Caches, optimized recompilation of hot code regions, On Stack Replacement &c. In previous year we were involved in project of optimizing performance of V8 JavaScript engine on major benchmark suites including Octane, SunSpider and Kraken. The project was quite time limited, however we achieved about 10% total performance improvement compared to open source version. We have decided to focus on following approaches to achieve the project’s goal: optimized build of V8 itself, because total running time is shared between compilation and execution; tuning of V8 runtime options which default values may not be always optimal; implementation of additional scalar optimizations. All of these approaches have made contribution to final result.


Introduction
JavaScript is one of the most popular programming languages in the world [1]. Started as a simple scripting language for web browsers it now becomes language of choice for millions of engineers in the web, mobile and server-side development. However its interpretational nature doesn't always provide adequate performance.
To speed up execution of JavaScript programs there were developed several optimization techniques in recent years. One example of modern high-performing JavaScript engine is a V8 engine [2] used in Google Chrome browser and node.js web server among others. This is an open source project which implemented some advanced optimization methods including Just-in-Time compilation [3], Polymorphic Inline Caches [4], optimized recompilation of hot code regions, On Stack Replacement [5] &c.
In previous year we were involved in project of optimizing performance of V8 JavaScript engine on major benchmark suites including Octane [6], SunSpider [7] and Kraken [8]. The project was quite time limited, however we achieved about 10% total performance improvement compared to open source version. The rest of paper is organized as follow: in Section 2 there is an architectural overview of V8, in Section 3 we enumerate and reason our approaches with more detailed discussion in Sections 4, 5, and 6. We conclude in Section 7.

V8 engine architecture
In contrast to other JavaScript engines V8 implements compilation to native code from the beginning. It consists of two JIT compilers: the first (called Full code generator) performs fast non-optimized compilation for every encountered JavaScript function, while the second one (called Crankshaft) compiles and optimizes only those functions (and loops) which already ran some amount of time and are likely to run further.

Fig. 1 V8 Engine Architecture
The overall work of V8 engine is as follows (Fig.1):  Every new script is preliminary scanned to separate each individual function.
 The function that should run is compiled into Abstract Syntax Tree (AST) form.
 AST is compiled into native machine code instrumented with counters for function calls and loop back edges.
 Also on method call sites V8 inserts special dispatch structure called Polymorphic Inline Cache (PIC). This cache is initialized with call to generic dispatch routine. After each invocation PIC is populated with direct call to type specific receiver up to some predefined limit. In such way PICs collect runtime type information of objects.
 The result code then runs.
 When instrumentation counters reach some predefined threshold, "hot" function or loop is selected for optimized recompilation.
 For this purpose V8 one more time recompiles selected function in AST form. But in this case it also performs optimizations.
 It compiles AST into Static Single Assignment (SSA) form (called Hydrogen) and propagates type information collected by PICs along SSA edges.
 Then it performs several optimizations on this SSA form using type information.
 After that it generates low level representation (called Lithium), does Register Allocation and generates optimized native code which then runs.
Note that V8 optimizing compiler performs much less transformational passes than common ahead-of-time compilers (e.g. gcc, clang/llvm). The reasons behind this we further discuss in Section 6.

Approaches to speed up V8 engine
To investigate possible areas of V8 optimization we have performed V8 engine profiling on ARM platform with three different profiling tools: Perf [9], ARM Streamline [10] and Gprof [11]. Each of those has advantages and disadvantages over others but results are very close: V8 JavaScript engine has no 'hot' functions in itself that need to be optimized. Different methods show different functions in order of share to total execution time. This is clear evidence that individual function's contribution is very small compared to precision of measurement. Thus optimization of individual functions can't achieve much increase in performance.
In following  We have decided to focus on following approaches to achieve the project's goal:  Optimized build of V8 itself, because total running time is shared between compilation and execution.
 Tuning of V8 runtime options which default values may not be always optimal.
 Implementation of additional scalar optimizations. All of these approaches have made contribution to final result.

Optimized build
We have decided to investigate Link Time Optimization [12] and platform options tuning [13]. The latter gave us small outcome (~0.5%) while former have decreased performance.
We have made investigation on Arndale ARM (Samsung Exynos 5250 CPU) development board running Linux with Linaro gcc 4.7 toolchain for the first investigation and the same board running Android 4.4 with Android NDK 9 Linaro toolchain for the second one. We have specified the following platform options:  -O3 for highest optimization level  -mcpu=cortex-a15 for target CPU.

Runtime parameters tuning
V8 engine has quite large set of parameters which guides JIT compilation and execution of JavaScript programs. We have found that their default values are not adequate in all cases, e.g. we have found that disabling lazy compilation can substantially improve performance. As noted in Section 2 V8 performs preliminary parsing of each new script source to separate each individual function. However when we specify parameter '--no-lazy', it instead compiles all functions at once in given script. Enabling this mode has various impacts on different benchmark. We can see big degradation of CodeLoad test score by about 40% while in the same time huge increase 2.5 times of MandreelLatency test score. The overall increase about 5% was also reproduced on Galaxy Note 3 devices running Android 4.4.

Scalar optimizations
We have tried to implement several well-known scalar optimizations in V8 however with varying success. In contrast to ahead of time compilers for classic imperative languages such as C/C++, Pascal, Ada &c., just-in-time compiler has to share time among analysis, optimization and execution. That's why sophisticated optimizations which require thorough analysis don't necessarily lead to increasing performance in such case. As noted in Section 2 the V8 engine performs optimized compilation of 'hot' regions similar to off-line compiles did. At this stage PICs already collected type information so we can apply well-known scalar optimization techniques in AST and SSA representations. The platform used in benchmark was Samsung Galaxy Note 3 with Qualcomm Snapdragon (N9005) CPU. Devices run Android 4.4.2 (KitKat). Octane benchmark suite used in tests was Version 9 download from corresponding repository. For development we use Android NDK r9c on Linux x86_64 Ububtu 12.04 TLS

Algebraic Simplification
The Algebraic Simplification uses algebraic identities like a -0 = a to simplify expressions. This transformation was implemented in V8 parser when it builds AST representation for Crankshaft. As was noted above at this point we have collected type information so we can safely optimize algebraic expression given that operands are numeric. Despite the large amount of optimized expressions in Octane benchmark suite the final result was very small.

Common Subexpression Elimination
V8 engine already has implemented Global Value Numbering optimization which eliminates redundant code. However there are related but not identical optimizations such as Constant Propagation and Common Subexpression Elimination. For their differences see [14]. Because V8 already has some kind of Constant Propagation we decided to implement Global Common Subexpression Elimination in SSA form.
We have found that running this optimization before and after Global Value Numbering gives net effect about 2% performance improvement.

Fast call frame for ARM
In our investigations we also have found interesting instruction sequence that speeds up call frame management on original ARMv7 CPUs. To support EABI [15]

Conclusion
We have found that even in the presence of type information in V8 optimizing compiler application of traditional scalar optimizations in JavaScript gives diminishing returns. On the other hand successful application of optimized build gives us evidence that there is a space for optimizations in JavaScript engines.