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

Turbo Boost Next Node.js

Turbo Boost Next Node.js

Presentation file for JSDC.tw 2017 in taiwan.

Yosuke Furukawa

November 03, 2017
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. How v8 optimizes? • V8 tries to optimize code on

    runtime • However JavaScript is dynamic typed language • Optimized Code has some dynamic checks to validate assumptions. • If a dynamic check and a validation are failed, v8 deoptimized the code…
  2. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  First, v8 runs the code for gathering feedbacks.
  3. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  First, v8 runs the unoptimized code for gathering feedbacks. V8 guesses this function has 2 props and values are `number`
  4. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  GPS WBSJJJ  Y \B C^ First, v8 runs the unoptimized code for gathering feedbacks. When the function is hot, V8 optimized the function
  5. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  GPS WBSJJJ  Y \B C^ First, v8 runs the unoptimized code for gathering feedbacks. When the function is hot, V8 optimized the function V8 optimizes this x function 1. need to load a and b props 2. a and b value is `number` 3. sum a and b and return the value
  6. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  GPS WBSJJJ  Y \B C^  Y \B޷٣খ᝷แ C^ First, v8 runs the unoptimized code for gathering feedbacks. When the function is hot, V8 optimized the function However JavaScript can take dynamic parameters. this {a: '޷٣খ᝷แ'} is not `number`
  7. However JavaScript can take dynamic parameters. this {a: '޷٣খ᝷แ'} is

    not `number` GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^  Y \B C^  GPS WBSJJJ  Y \B C^  Y \B޷٣খ᝷แ C^ First, v8 runs the unoptimized code for gathering feedbacks. When the function is hot, V8 optimized the function Oh… my optimization code has gone… need to go back to unoptimized code.
  8. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  GPS WBSJJJ  Y \B C^  Y \BBJFFFFFF C^ First, v8 runs the unoptimized code for gathering feedbacks. When the function is hot, V8 optimized the function Oh… my optimization code has gone… need to be back to unoptimized code. However JavaScript can execute dynamic parameters. those parameters are changed `a` prop value to `string` %FPQUJNJ[BUJPO
  9. GVODUJPOQPT Y Z \ DPOTUPCK\^ PCKYY PCKZZ PCK[ SFUVSOPCK ^

    1BSTFS JavaScript Source Code Abstract Syntax Tree 'VMM$PEF(FO $SBOLTIBGU $PNQJMFS kind = FUNCTION name = FactIf Instructions (size = 144) 0x29026660 0 55 push ebp 0x29026661 1 89e5 mov ebp,esp 0x29026663 3 56 push esi 0x29026664 4 57 push edi kind = OPTIMIZED_FUNCTION name = FactIf stack_slots = 1 Instructions (size = 512) 0x29027a00 0 55 push ebp 0x29027a01 1 89e5 mov ebp,esp 0x29027a03 3 56 push esi Unoptimized Machine Code Optimized Machine Code Optimize De-optimize
  10. Unoptimized Machine Code Optimized Machine Code GVODUJPOQPT Y Z \

    DPOTUPCK\^ PCKYY PCKZZ PCK[ SFUVSOPCK ^ 1BSTFS JavaScript Source Code Abstract Syntax Tree 'VMM$PEF(FO $SBOLTIBGU $PNQJMFS #include <iostream> using namespace std; int main(){ int a = 7; int &n = a; mov L1, L2 0x2ddf8802cf6e rem L1 0x2ddf8802cf6e add L2 0x2ddf8802cf6e mov L1, L2 0x2ddf8802cf6e rem L1 0x2ddf8802cf6e add L2 0x2ddf8802cf6e Optimize De-optimize QSPEVDFEBMPUPGDPEFT  DPTUJOHNFNPSZʜ &$."4DSJQUOPU TVQQPSUFE NBOZQPUFOUJBMSJTLTGPS EFPQUJNJ[BUJPO
  11. Crankshaft problems • Don’t use let/const • Don’t use rest/spread

    parameters • Don’t use try/catch and try/finally • Don’t use for-in • If JIT killer kills optimizer, goes 
 down to terrible performance.
  12. Crankshaft JIT problems • Don’t use let/const • Don’t use

    rest/spread parameters • Don’t use try/catch or try/finally • Don’t use for-in • If JIT killer kills optimizer, goes 
 down to terrible performance. Optimization Killer Do not support ES2015+ Deoptimization cost is high.
  13. GVODUJPOQPT Y Z \ DPOTUPCK\^ PCKYY PCKZZ PCK[ SFUVSOPCK ^

    1BSTFS JavaScript Source Code Abstract Syntax Tree *HOJUJPO*OUFSQSFUFS 5VSCP'BO$PNQJMFS Parameter count 2 Frame size 8 12 E> 0x2ddf8802cf6e @ StackCheck 19 S> 0x2ddf8802cf6f @ LdaSmi [1] 0x2ddf8802cf71 @ Star r0 34 E> 0x2ddf8802cf73 @ LdaNamedProperty 28 E> 0x2ddf8802cf77 @ Add r0, [6] mov L1, L2 0x2ddf8802cf6e rem L1 0x2ddf8802cf6e add L2 0x2ddf8802cf6e mov L1, L2 0x2ddf8802cf6e rem L1 0x2ddf8802cf6e add L2 0x2ddf8802cf6e Bytecode Optimized Machine Code Optimize De-optimize
  14. GVODUJPOQPT Y Z \ DPOTUPCK\^ PCKYY PCKZZ PCK[ SFUVSOPCK ^

    1BSTFS JavaScript Source Code Abstract Syntax Tree *HOJUJPO*OUFSQSFUFS 5VSCP'BO$PNQJMFS Parameter count 2 Frame size 8 12 E> 0x2ddf8802cf6e @ StackCheck 19 S> 0x2ddf8802cf6f @ LdaSmi [1] 0x2ddf8802cf71 @ Star r0 34 E> 0x2ddf8802cf73 @ LdaNamedProperty 28 E> 0x2ddf8802cf77 @ Add r0, [6] mov L1, L2 0x2ddf8802cf6e rem L1 0x2ddf8802cf6e add L2 0x2ddf8802cf6e mov L1, L2 0x2ddf8802cf6e rem L1 0x2ddf8802cf6e add L2 0x2ddf8802cf6e Bytecode Optimized Machine Code Optimize De-optimize CZUFDPEFJTOPUTPIJHI DPTUUIBODPEF &$."4DSJQUTVQQPSUFE %FPQUJNJ[BUJPOSJTLJT MPXFS
  15. Crankshaft JIT problems • Don’t use let/const • Don’t use

    REST parameters • Don’t use try/catch or try/finally • Don’t use for-in • If JIT killer kills optimizer, goes 
 down to terrible performance. Eliminate JIT killers Support ES2015+ Deoptimization cost is not so high. ✓ ✓ ✓ ✓ ✓ 4PMWFE
  16. Worker will come in next Node …(maybe v10?) ayo.js has

    the implementation. Node.js has some proposals.
  17. History of concurrent Node.js /PEFKT .BTUFS1SPDFTT $IJME 1SPDFTT $IJME 1SPDFTT

    $IJME 1SPDFTT $IJME 1SPDFTT .BTUFS1SPDFTT DBOGPSLDIJMEQSPDFTT W
  18. History of concurrent Node.js /PEFKT .BTUFS1SPDFTT 8PSLFS 8PSLFS 8PSLFS 8PSLFS

    8PSLFS 8PSLFS .BTUFS1SPDFTTDBONBLFOFX8PSLFS UISFBE  8PSLFSHFOFSBUJPODPTUJTDIFBQFSUIBODIJMEQSPDFTT BOEDBOTIBSFUIFNFNPSZVTJOH4IBSFE"SSBZ#V⒎FS
  19. Result pMF OVNCFST CBCFMDPSF VTJOHTJOHMFUISFBE CBCFMDPSF VTJOHDIJMEQSPDFTT CBCFMDPSF VTJOHXPSLFS 

    NT NT NT  NT NT NT  NT NT NT  NT NT NT  NT NT NT
  20. Result pMF OVNCFST CBCFMDPSF VTJOHTJOHMFUISFBE CBCFMDPSF VTJOHDIJMEQSPDFTT CBCFMDPSF VTJOHXPSLFS 

    NT NT NT  NT NT NT  NT NT NT  NT NT NT  NT NT NT UBSHFUpMFT TJOHMF UISFBEXJOT OPSNBMCBCFM 
  21. Result pMF OVNCFST CBCFMDPSF VTJOHTJOHMFUISFBE CBCFMDPSF VTJOHDIJMEQSPDFTT CBCFMDPSF VTJOHXPSLFS 

    NT NT NT  NT NT NT  NT NT NT  NT NT NT  NT NT NT UBSHFUpMFT  XPSLFSXJOT
  22. Summary • Node.js has some performance features. • V8 keeps

    improvement the JavaScript Performance, Turbofan + Ignition is fast. • HTTP2 will solve Head of Line blocking,
 http network is more faster. • Future Node.js may support Worker. 
 Worker can use CPU more efficiently.
  23. References • Deoptimization: https://docs.google.com/presentation/d/ 1Z6oCocRASCfTqGq1GCo1jbULDGS-w-nzxkbVF7Up0u0/edit#slide=id.p • v8-bailout-reasons: https://github.com/vhf/v8-bailout-reasons • v8

    optimization killers: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers • http2: https://http2.github.io/ • Developing the fastest http2 server: https://www.slideshare.net/kazuho/developing-the- fastest-http2-server • Exploring Future Node.js: https://speakerdeck.com/yosuke_furukawa/exploring-future-node • ayojs: https://github.com/ayojs/ayo • Shared Array Buffer and Atomics: http://2ality.com/2017/01/shared-array-buffer.html