$30 off During Our Annual Pro Sale. View Details »

Node 8 and Friends

Node 8 and Friends

What's new in Node.js?

Minqi Pan

June 28, 2017
Tweet

More Decks by Minqi Pan

Other Decks in Programming

Transcript

  1. Node 8 and Friends
    Minqi Pan

    View Slide

  2. View Slide

  3. Invented by Ryan Dahl
    Inaugurated on JSConf EU
    8 years ago

    View Slide

  4. 2009 2017
    Google Trends

    View Slide

  5. Agenda
    • to be covered
    • new spotlights of Node 8 and recent releases
    • new topics of the Node.js ecosystem
    • NOT to be covered
    • technical bug fixes or improvements on details

    View Slide

  6. View Slide

  7. a JavaScript engine
    developed by The Chromium Project
    for the Google Chrome web browser
    since 9 years ago

    View Slide

  8. v8: every 1½ months
    6.0 2017 / 6 / 9
    5.9 2017 / 4 / 27
    5.8 2017 / 3 / 20

    View Slide

  9. v8: every 1½ months
    6.1 ca. 2017 / 7 / 21

    View Slide

  10. View Slide

  11. a web browser developed by Google
    since 9 years ago

    View Slide

  12. Chrome: every 1½ months
    59 2017 / 6 / 5
    58 2017 / 4 / 19
    57 2017 / 3 / 9

    View Slide

  13. Chrome: 

    1 release behind v8
    59 2017 / 6 / 5
    ca. release date of
    v8 6.0
    58 2017 / 4 / 19
    ca. release date of
    v8 5.9
    57 2017 / 3 / 9
    ca. release date of
    v8 5.8

    View Slide

  14. 58 5.8.283.38
    59 5.9.211.35
    60 6.0.286.15
    61 6.1.120

    View Slide

  15. (Version of Chrome) / 10 = Version of v8

    View Slide

  16. Chrome DevTools Protocol
    • Node deprecated old debugger protocol in favor of

    Chrome DevTools Protocol via websocket
    (v8_inspector from Blink)
    • chrome://inspect

    View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. Chrome Trace Event
    • Centralize tracing information generated by V8,
    Node core, and user-space code.
    • Traces are visualizable
    • chrome://tracing/

    View Slide

  21. View Slide

  22. (Possibly) Unified APM
    • APM monkey-patches Node.js to monitor
    performance
    • instrumentation via v8 trace events API made
    possible
    • possible to provide a unified instrumentation API
    based on v8 trace events

    View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. 8.1.1 Current 5.8
    6.11.0 LTS 5.1
    4.8.3 4.5

    View Slide

  27. Node.js:
    2~4 releases behind v8
    8.1.0 / v8 5.8 2017 / 6 / 7
    ca. release date of
    v8 6.0
    8.0.0 / v8 5.8 2017 / 5 / 30
    ca. release date of
    v8 6.0
    7.10.0 / v8 5.5 2017 / 5 / 2
    ca. release date of
    v8 5.9

    View Slide

  28. More help from Google
    • Silver member of Node.js Foundation
    • Google Cloud Platform supports Node.js on
    Compute Engine, Container Engine, App Engine
    and Cloud Functions
    • … w/ SDK and monitoring (Google Stackdriver)

    View Slide

  29. View Slide

  30. Active integrations between
    v8 and Node.js
    • https://github.com/v8/node/
    • https://github.com/nodejs/node-v8
    • Branch “canary-base” of
    • https://github.com/nodejs/node

    View Slide

  31. Node.js Jenkins CI
    a v8-updating
    robot
    https://github.com/nodejs-ci

    View Slide

  32. ES2017

    View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. View Slide

  37. View Slide

  38. View Slide

  39. (async () => {})()

    View Slide

  40. View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. Chrome 61

    View Slide

  46. View Slide

  47. http://node.green/

    View Slide

  48. View Slide

  49. node 8 waits for v8 5.8
    • --turbo --ignition
    • will be enabled by default in v8 5.9
    • Crankshaft-script => TurboFan-Script

    View Slide

  50. node 8 waits for v8 5.8
    • V8 team is preparing special versions of 5.8 that
    are ABI compatible to 6.0 (Node.js LTS)
    • Allow Node.js to upgrade to the new pipeline as a
    non-breaking change
    • simpler script execution pipeline; reduce jank;
    make the interchange between V8’s various
    components more efficient

    View Slide

  51. View Slide

  52. Ignition
    • Reducing memory usage w/ compact byte-code
    • Enables Android devices with small memory
    • Improves the startup speed via byte-code caching
    • refactoring v8

    View Slide

  53. Ignition
    • cf. Apple’s JavaScriptCore + SquirrelFish (Nitro)

    cf. Mozilla’s SpiderMonkey + IonMonkey

    cf. Microsoft’s ChakraCore + its JIT
    • cf. .java -> javac -> .class (bytecode)

    .class -> hot -> JIT -> machine code
    • cf. .rb -> RubyVM::InstructionSequence

    View Slide

  54. Design of Ignition
    • Concise
    • so that Bytecode size have a small memory
    footprint (machine code generated by V8’s full-
    codegen compiler is verbose)
    • so that we always compile it eagerly
    • so that initial startup is faster

    View Slide

  55. Chromium #593477
    Open Chrome 51 (canary)
    (1) Load www.facebook.com multiple times in
    the browser
    (2) exit the browser, open about:tracing and
    record all enabled by default categories

    View Slide

  56. Chromium #593477
    Result:
    (1) initial load, v8.CompileScript takes 165 ms
    (2) repeated loads, V8.ParseLazy takes 376 ms

    View Slide

  57. Chromium #593477
    Why?
    - Entirely compile eagerly are too costly (Being
    included in the code cache increases both
    the footprint on the hard disk and takes
    longer to serialize and deserialize. let alone
    the memory…)
    - So only the toplevel code gets compiled
    - Functions contained is compiled until the
    function is actually called
    - Lazily compiling inner functions still requires
    a new parsing pass

    View Slide

  58. Facebook uses a transpiler to combine individual
    modules into a single file. E.g.
    Chromium #593477

    View Slide

  59. Old Architecture

    View Slide

  60. Multiple Parsing
    Machine Code

    View Slide

  61. might never run again
    N-level-nested function is parsed N times

    View Slide

  62. New Architecture

    View Slide

  63. Memory Problem
    JIT-ed code

    View Slide

  64. Memory Optimized

    View Slide

  65. Mobile Top 10 Benchmark

    View Slide

  66. run-once or non-hot code now stored more
    compactly in bytecode form

    View Slide

  67. Startup Speed Problem
    actual executing

    View Slide

  68. Startup Speed Optimized

    View Slide

  69. View Slide

  70. TurboFan
    • V8's new optimizing compiler
    • Architecture-independent macro-assembly
    instructions: handlers of Ignition’s bytecode
    • compiles Ignition instructions to the target
    architecture
    • performing instruction selection and machine
    register allocation

    View Slide

  71. TurboFan
    • replace Full Codegen and Crankshaft
    • Optimize what Crankshaft cannot optimize
    • support ES2015+
    • w/ Escape analysis

    View Slide

  72. View Slide

  73. Async Hooks
    • Node 8’s API for setting up hooks to get structural
    tracing information about the life of handle objects
    • monitoring the operation of the Node.js event loop
    • tracking asynchronous requests and handles
    through their complete lifecycle

    View Slide

  74. Lifecycle
    • init
    • before
    • after
    • destroy

    View Slide

  75. Node.js Native API (NAPI)
    “ABI stable abstraction layer of native module”

    View Slide

  76. Node.js Native API (NAPI)
    • NAN: insulate from v8 API Changes
    • Node 8’s NAPI: insulate from v8 itself
    • Stable on both API and ABI
    • Supports Node-ChakraCore, SpiderNode

    View Slide

  77. Node.js Native API (NAPI)
    • Use C instead of C++ to avoid Name Mangling
    • All v8 JavaScript value become napi_value
    • deprecate v8 exceptions, all NAPI returns
    napi_status
    • redesigned scope, now uses
    napi_open_handle_scope

    View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. YourProject/*
    YourProject.squashfs
    libsquash
    Node.js Runtime
    YourProject.exe
    Statically Link
    Statically Link

    View Slide

  84. nodec v1.0.0
    • upgrade runtime to node 8
    • add runtime support for native modules
    • allow executing files within the enclosed package
    • allow reusing itself as an Node.js interpreter
    • add auto-update feature

    View Slide

  85. View Slide

  86. https://github.com/pmq20/libautoupdate
    Libautoupdate

    View Slide

  87. Compiling to Bytecode?
    • FE has limited battery, memory, network resources.
    • FE usually won’t ship a lot of unused code
    • Node.js has big node_modules folder so cost of
    eagerly compiling all is still huge
    • But we have mmap; works good on a single file
    • IP (Intellectual Property) Protection

    View Slide

  88. View Slide

  89. “ vm.Script could be used to hide the source by
    shipping only bytecode”
    https://github.com/nodejs/node/issues/11842
    “ AST, IR, bytecode and friends - standards and
    inovation”
    https://github.com/nodejs/CTC/issues/104

    View Slide

  90. nodec v2.0.0 plan
    • Eagerly compile everything into Bytecode,
    including node_modules
    • Put them inside a SquashFS (i.e. your.exe)
    • OS mmaps your.exe when executed
    • Enjoy low memory footprint and fast startup
    • No source code shipped

    View Slide

  91. cf. Ko1’s Ruby AOT

    View Slide

  92. cf. Ko1’s Ruby AOT

    View Slide

  93. View Slide

  94. http://enclose.io/
    Enclose

    View Slide

  95. runner runner
    runner
    http://enclose.io/
    Enclose

    View Slide

  96. View Slide

  97. View Slide

  98. View Slide

  99. 谢谢

    View Slide