Turbo Boost Next Node.js

Turbo Boost Next Node.js

Presentation file for JSDC.tw 2017 in taiwan.

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

November 03, 2017
Tweet

Transcript

  1. Turbo Boost Next Node.js jsdc.tw @ ୆େࣾ။ՊላӃ COSS https://speakerdeck.com/yosuke_furukawa/turbo-boost-next-node-dot-js

  2. Twitter: @yosuke_furukawa Github: yosuke-furukawa ໊લ: ݹ઒ཅհ

  3. Twitter: @yosuke_furukawa Github: yosuke-furukawa /PEFKT+BQBO6TFS(SPVQ-FBEFS

  4. Before this talk

  5. Before this talk 1MFBTF+PJOVT

  6. Turbo Boost⚡

  7. MJCVW IUUQQBSTFS 7 DBSFT 0QFO44- [MJC OHIUUQ OBQJ 4UBOEBSE-JCSBSJFT +BWB4DSJQU

    Node.js inside
  8. MJCVW IUUQQBSTFS 7 DBSFT 0QFO44- [MJC OHIUUQ OBQJ 4UBOEBSE-JCSBSJFT +BWB4DSJQU

    Node.js inside 7/FYU+BWB4DSJQU 0QUJNJ[FS
  9. V8 Next JavaScript Optimizer

  10. 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…
  11. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^

  12. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^ 
  13. GVODUJPOY PCK \ SFUVSOPCKB PCKC ^ Y \B C^ 

    Y \B C^  First, v8 runs the code for gathering feedbacks.
  14. 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`
  15. 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
  16. 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
  17. 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`
  18. 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.
  19. 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
  20. Previous Oprimizer: Crankshaft

  21. 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
  22. 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
  23. 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.
  24. 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.
  25. TurboFan & Ignition Interpreter

  26. 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
  27. 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
  28. 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
  29. None
  30. None
  31. MJCVW IUUQQBSTFS 7 DBSFT 0QFO44- [MJC OHIUUQ OBQJ 4UBOEBSE-JCSBSJFT +BWB4DSJQU

    Node.js inside /FYU)5511SPUPDPM 4VQQPSUFE
  32. HTTP Next Protocol Support (aka http2)

  33. http/1.1 problems

  34. 4FSWFS GET /main.js GET /main.css GET /image0.png

  35. 4FSWFS #SPXTFSDBOTFOESFRVFTUT POMZ

  36. 4FSWFS OFFEUPXBJUUPpOJTIQSFWJPVTSFRVFTU

  37. 4FSWFS "OEJGSFRVFTUJTTMPX CSPXTFS DBOVTFPOMZSFRVFTUT

  38. 4FSWFS "OEJGSFRVFTUJTTMPX CSPXTFS DBOVTFPOMZSFRVFTUT )FBEPG-JOF#MPDLJOH

  39. http/2 solves HoL Blocking

  40. Multiplexing requests on 1 TCP connection 4FSWFS 5$1$POOFDUJPOCVUNVMUJ SFRVFTUT

  41. Multiplexing requests on 1 TCP connection 4FSWFS *GTPNFSFRVFTUTBSFTMPX #65 XIPMFSFRVFTUTBSFOPUB⒎FDUFE

  42. DEMO

  43. )551

  44. )551

  45. )551XJUIQVTI

  46. Future . . .

  47. Worker will come in next Node …(maybe v10?) ayo.js has

    the implementation. Node.js has some proposals.
  48. Node.js has various use cases

  49. Node.js has various use cases &WFSZUPPMTGSBNFXPSLTIBT$16JOUFOTJWFUBTLT

  50. History of concurrent Node.js /PEFKT #FGPSFW 4JOHMF1SPDFTT 4JOHMF5ISFBE

  51. History of concurrent Node.js /PEFKT .BTUFS1SPDFTT $IJME 1SPDFTT $IJME 1SPDFTT

    $IJME 1SPDFTT $IJME 1SPDFTT
  52. History of concurrent Node.js /PEFKT .BTUFS1SPDFTT $IJME 1SPDFTT $IJME 1SPDFTT

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

    8PSLFS 8PSLFS
  54. History of concurrent Node.js /PEFKT .BTUFS1SPDFTT 8PSLFS 8PSLFS 8PSLFS 8PSLFS

    8PSLFS 8PSLFS .BTUFS1SPDFTTDBONBLFOFX8PSLFS UISFBE  8PSLFSHFOFSBUJPODPTUJTDIFBQFSUIBODIJMEQSPDFTT BOEDBOTIBSFUIFNFNPSZVTJOH4IBSFE"SSBZ#V⒎FS
  55. Performance Bench using ayo.js

  56. babel-core using single thread. target files (ES6) transpiled code (ES5)

  57. babel-core using child_process .BTUFS target files Send File transpiled

  58. babel-core using Worker /PEFKT .BTUFS target files transpiled Send File

  59. Result pMF OVNCFST CBCFMDPSF VTJOHTJOHMFUISFBE CBCFMDPSF VTJOHDIJMEQSPDFTT CBCFMDPSF VTJOHXPSLFS 

    NT NT NT  NT NT NT  NT NT NT  NT NT NT  NT NT NT
  60. 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 
  61. 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
  62. 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.
  63. Thank you

  64. 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
  65. References Demo Codes • DEMO Code: • https://github.com/yosuke-furukawa/check_v8 • https://github.com/yosuke-furukawa/http2-

    express-demo • https://github.com/yosuke-furukawa/worker- babel-demo