JavaScript performance Optimization--by Google V8 Team Manager

Source: Internet
Author: User

Original: Https://developers.google.com/v8/?hl=zh-CN

Be Prepared before writing code[9:35]

Understand how V8 optimzes Javascript;

Write code mindfully;

Learn you tools and how they can help you;

Be Prepared-hidden classes[10:30]

Hidden Classes make JavaScript Faster.

(Note: Hidden class can be understood as a description of the description of abstract classes within the VM, sharing hidden classes to make VMS more efficient)

Limit Compile-time Type Information

It ' s expensive to reason on JavaScript types at compile time ...

    • V8 internally creates hidden classes for objects at runtime.
    • Objects with the same hidden class can use the same optimzed generated code.

Initialize all object, in constructor functions;

Always Initialize members in the same order;

(If you add the different orders, you create a different tree of hidden classes.

And at the end, you'll have a objects with a different hidden classes that can ' t use the same optimized code)

Be prepared-numbers[15:30]

We use a technique called tagging. So inside of V8 we pass around values of 32-bit numbers and objects.

But we want to is able to use the same of the bits to represent both. And that's the we can has one code path that can handle, in many cases, the objects and integers. So, we do are we use the bottom bit.

And each of the values has s special meaning. If the bit is SE, it's an object pointer. If It's clear, it's what we call small integer or SMI. And that ' s a 31-bit signed integer. Now if you have a numeric value of ' re passing around, assigning to a member so is bigger-it ' s a numeric value tha T ' s bigger than signed bits-then it doesn ' t fit in one of these smis. And we have to create the what ' s called a box for it. We box the number. We turn it into a double and we create a new object to put that number inside of it. And what follows from that was the next speed trap to avoid, which are make sure, whenever possible, you use 31-bit signed n Umbers for performance critical calculations.

Prefer numberic values that can be represented as 31-bit signed integers.

Be prepared-arrays[17:25]
    • Fast elements:linear Storage for compact key sets.
    • Dictionary Elements:hash Table Storage otherwise.

Use contiguous Keys starting at 0 for Arrays. (

Don ' t pre-allocate large Arrays (e.g. > 64K elements) to their maxium size, instead grow as you go.

Don ' t delete elements in arrays, especially numberic arrays.

Double Array Unboxing

    • Array ' s hidden class tracks elements types
    • Arrays contraining only Doubles is unboxed
    • Unboxing causes hidden class change

Initialize using array literals for small fixed-sized arrays

Preallocate small arrays to correct size before using them

Don ' t store non-numeric values (objects) in numeric arrays

Be Prepared-full compiler[26:36]

V8 has tow compilers

    • ' Full ' compiler can generate good code for any Javascript
    • Optimizing compiler produces great code for most JavaScript

"Full" compiler starts executing Code ASAP

    • Quickly generates good but not great JIT code
    • Assumes (almost) nothing on types at compilation time
    • Uses Inline Caches (or ICs) to refine knowledge about types and program runs

Inline Caches (ICs) handle Types efficiently

    • Type-dependent Code for Operations
    • Validate type assumptions First, then does work
    • Change at runtime via backpathcing as more types is discovered

Monomorphic Better Than polymophic

    • Operations is monomophic if the hidden class was always the same
    • Otherwise they is polymorphic
functionreturn x + y;} Add (+);         // + in Add are monomorphicAdd ("A", "B")    //+ in Add becomes polymorphic

Prefer monomorphic over polymorphic whenever is possible.

Type Feedback makes Code Faster

    • Types taken from ICs
    • Operations speculatively Get inlined
    • Monomophic functions and constructors can be inlined entirely
    • Inlininig enables other optimizations

Logging What Gets Optimized

D8--trace-opt prime.js

Logs names of optimized functions to stdout

Not everything Can Optimized

Some features prevent the optimizing compiler from running (a "bail-out")

Avoid the Speed Trap

optimizing Compiler "Bail-out" on functions with try{} catch{} blocks.

Maximizing Performance with Exceptions

function perf_sensitive () {  //doperformance-sensitivework here}try{     Catch (e) {  //handle exceptionshere}

How to Find bailouts

D8--trace-bailout Prime.js

Logs optimizing compiler bailouts

Invalid assumptions leads to deoptimization[37:55]

Deoptimization ...

    • ... throws away optimized code
    • Resumes execution at the "full" compiler code
    • Reoptimization might is triggered again later, but for the short term, execution slows down.

Passing V8 Options to Chrome

"/applicaitons/google chrome.app/contents/macos/google Chrome" \--js-flags= "--trace-opt--trace-deopt-- Trace-bailout "

Avoid the Speed Trap

Avoid hidden class changes in functions after they is optimized

Identify and Understand[39:50]

"Identify and understand" for V8

    • Ensure problem is JavaScript
    • Reduce to Pure JavaScript (no dom!)
    • Collect Metrics
    • Locate Bottleneck (s)

Prime Generator--profile It

%out/ia32.release/d8 Prime. JS--prof287107

Using teh built-in sampling profiler

    • Takes Sampe every millisecond
    • Writes V8.log

What to expect from the primes Code

functionPrimes () {... This. Addprime =function(i) { This. primes[ This. prime_count++] =i;} This. isprimedivisible =function(candidate) { for(vari = 1; I <= This. Prime_count; ++i) {if(Candidate% This. primes[i]) = = 0) {return true; }}return false;}};functionMain () {p=NewPrimes (); varc = 1;  while(P.getprimecount () < 25000) {if(!p.isprimedivisible (c))    {P.addprime (c); } C++; } print (P.getprime (P.getprimecount ()-1));}

Prediction:most time spent in main

    • All properties and Functions monomorphic
    • All numeric operations is SMIs
    • All functions can inlined
    • No deoptimizations or bailouts

(Output omitted @42:50)

Can you spot the bug?

 This function (candidate) {  for (var. Prime_count; + +i) {  if     thisreturntrue; }  returnfalse;}

(Hint:primes is an array of length Prime_count)

% Out/ia32.release/d8 primes-2. js--prof287107 (omitted)

JavaScript is 60% faster than C + +

C++

% g++ primes.   cc -o primes time./primes287107Real 0m2.955suser 0m2.952ssys 0m.001s

Javascript

 time D8 primes-2. js287107real 0m1.829suser 0m1.827ssys  0m0.010s 

JavaScript is 17% slower than optimized C + +

Fix what matters[49:59]

Optimize Your algorithm

 This. isprimedivisible =function(candidate) { for(vari = 1; I < This. Prime_count; ++i) {    var current_prime = this. Primes[i];  if(Current_prime*current_prime > candidate) {  return false ; }    if(Candidate% This. primes[i] = = 0)return true; }return false;}

Final Results

(output omitted)

That's more than a 350x speed-up!

Keep Your Eyes on the Road

    • Be prepared
    • Identify and understand the Crux
    • Fix What matters

JavaScript performance Optimization--by Google V8 Team Manager

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.