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

Node.js Internals

Tamar Twena-Stern
April 07, 2019
170

Node.js Internals

Tamar Twena-Stern

April 07, 2019
Tweet

Transcript

  1. Tamar Twena-Stern • Software Engineer - manager and architect •

    Architect @PaloAltoNetworks • Was a CTO of my own startup • Passionate about Node.js ! • Twitter: @SternTwena
  2. Tamar Tena-Stern • On Maternity Leave • Have 3 kids

    • Loves to play my violin • Javascript Israel community leader
  3. Problems •The system allocates CPU and memory resources for every

    new thread •When the system is stressed – overhead of thread scheduling and context switching •The system waste resources for allocating threads instead of doing actual work
  4. Single Threaded ? • Not really single threaded • Several

    threaded : • Event Loop • The workers thread pool
  5. Event Loop Thread • Every request registers a callback which

    executes immediately • The event loop execute JavaScript callbacks • Offloads I/O operations to worker thread pool. • Handle callbacks for asynchronous I/O operations from multiple requests.
  6. Worker Thread Pool • Thread pool to perform heavy operations

    • I/O • CPU intensive operations • Bounded by fixed capacity • A node module can submit a task to libUV API
  7. Submitting A Request To The Worker Pool • Use a

    set of ‘basic’ modules that work with the event loop • Examples: • Fs • Dns • Crypto • And more • Submit a task to libUV using c++ add-on
  8. • Libuv in a nutshell : – Multi platform C

    library – Provides support for async I/O based on event loop • Supports : – Epoll (Linux) – Kqueue (OSX) – Windows IOCP – Solaris event ports
  9. Timers Phase • setTimeout, setInterval • Timer’s callback will run

    as soon as they can be scheduled after the threshold • Timer’s callback scheduling controlled by the “poll” phase
  10. I/O Callback Phase • Executes system error callbacks • Example

    : TCP socket connection error. • Normal I/O operation callbacks are executed in the poll phase.
  11. Check Phase And Close Phase • Check Phase - Execute

    callbacks for setImmediate timers • Close Phase – Handles an abruptly close of a socket or a handle
  12. What Is Just-In-Time Compilation ? • Compilation during run time

    • Combines two approaches : • Ahead of compilation • Interpreter
  13. Chrome • Open source JavaScript engine • Developed originally for

    Google Chrome and chromium • Also used for • Couchbase • MongoDB • Node.js
  14. The JIT Compilation In V8 Source Code Tracing Interpreter Function

    Repeats - Hot Code JIT Compiler Optimised Code
  15. What Is An Optimised Compiler? • When a code is

    hot – it is worth doing multiple optimisations • Tracing will send it to optimising compiler • Creates an even faster version of the code • Tracing pulls it when the function code runs
  16. Ignition Interpreter • Interpreter for V8 • Translates to low

    level Bytecode • Enabling the following code to be stored more compactly in Bytecode • Run once code • Non hot code
  17. Turbofan Compiler • Make hot code run as fast as

    possible • Relies on input type information collected via inline caches while functions run via the Ignition interpreter. • Generates the best possible code handling the different types it encountered • The fewer function input type variations the compiler has to consider, the smaller and faster the resulting code will be
  18. How To Help Turbofan Optimise Hot Code ? •The fewer

    function input type variations lead to smaller and faster resulting code. •Keeping your functions monomorphic or at least polymorphic •Monomorphic: one input type •Polymorphic: two to four input types •Megamorphic: five or more input types
  19. Optimisation And De-optimisation • Optimisation - All assumptions fulfilled –

    Compiled code runs. • Deoptimisation – Not all assumptions fulfilled – Compiled code erased Assumptions Fulfilled Optimisation Assumptions Break De- Optimisation
  20. Avoid De-optimisation • When a code is optimised and de-optimised

    – it ended up being slower then just use the baseline compiled version • Most browsers and engines will stop trying after several iterations of optimising and de-optimizing
  21. The Stack • Every executing function pushes its local variables

    and arguments • Maintains two pointers – Stack Pointer and Base Pointer • Divided into stack frames • No Garbage Collection - self cleaning • Hold the key to the garbage collection process - Starts from active objects that has pointers to the stack.
  22. Generational GC System • Lifetime of objects determines their place

    on the heap • New Space – short lived Objects • Old Space – long lived Objects • Scan on ‘New Space’ called ‘Scavenge’ • Very fast – takes less then a millisecond • Scan on ‘Old Space’ called ‘Mark Sweep’ • Slower scan
  23. Object Life On The Heap Object Allocated On New Space

    ‘Scavenge’ Scan To Detect Live Objects On New Space Object survives 2 ‘Scavenge’ scans and still alive Moved To ‘Old Space’ More Space Needed On Old Space Mark Sweep Scan Starts to Free Space On Heap