Compiling in the background for a smoother user experience

Thursday, February 13, 2014

We aim to make Chrome the fastest browser possible. One way V8 helps with this goal is by compiling JavaScript into native machine code to execute scripts quickly. Historically, Chrome compiled JavaScript on the main thread, where it could interfere with the performance of the JavaScript application. In the latest Chrome Beta we've enabled concurrent compilation, which offloads a large part of the optimizing compilation phase to a background thread. The result is that JavaScript applications remain responsive and performance gets a boost.

Let’s have a look under the hood of V8 to understand how this works. To reduce the overall time spent compiling, V8 defers compilation of JavaScript functions until immediately before they are executed the first time. This compilation phase is fast but doesn’t focus on optimizing the code, just on getting it done quickly. In V8, pieces of code that are executed very often are compiled a second time by a specialized optimizing compiler. This second compilation pass makes use of many advanced optimization techniques, meaning it takes more time than the first pass but delivers much faster code.

Until now, V8 took turns compiling optimized Javascript code and executing it. For large pieces of code this could become a nuisance, and in complex applications like games it could even lead to stuttering and dropped frames. Concurrent compilation tackles this issue.

The graphs below show excerpts of V8’s vital signs when running Mandreel, part of the Octane benchmark suite, on the Nexus 5 phone. The graphs have been created using our new profiling visualization tool. The black bar represents execution of JavaScript, and ideally would be solid.

The first graph shows V8 running without concurrent compilation. V8 is fully occupied with optimizing a large piece of code, causing an execution pause of more than 600ms.

By enabling concurrent compilation, V8 still optimizes a large piece of code, but does it in a background thread. Execution continues alongside compilation, providing for a smoother user experience. For instance, concurrent compilation improved the Mandreel score of Octane 2.0 by 27% on a Nexus 5 and made graphic-intensive applications such as the Epic Citadel Demo run even smoother in Chrome.

Concurrent code compilation is another step towards reducing latency in Chrome and is part of various ongoing efforts to deliver more responsive, smoother web applications.

Posted by Yang Guo, Multi-threaded V8 Engineer

Post a Comment