How is Node.js inherently faster when it still relies on Threads internally?

前端 未结 6 1669
野性不改
野性不改 2020-11-29 14:30

I just watched the following video: Introduction to Node.js and still don\'t understand how you get the speed benefits.

Mainly, at one point Ryan Dahl (Node.js\' cre

6条回答
  •  执念已碎
    2020-11-29 15:01

    Note! This is an old answer. While it's still true in the rough outline, some details might have changed because of Node's rapid development in the last few years.

    It is using threads because:

    1. The O_NONBLOCK option of open() does not work on files.
    2. There are third-party libraries which don't offer non-blocking IO.

    To fake non-blocking IO, threads are neccessary: do blocking IO in a separate thread. It is an ugly solution and causes much overhead.

    It's even worse on the hardware level:

    • With DMA the CPU asynchronously offloads IO.
    • Data is transferred directly between the IO device and the memory.
    • The kernel wraps this in a synchronous, blocking system call.
    • Node.js wraps the blocking system call in a thread.

    This is just plain stupid and inefficient. But it works at least! We can enjoy Node.js because it hides the ugly and cumbersome details behind an event-driven asynchronous architecture.

    Maybe someone will implement O_NONBLOCK for files in the future?...

    Edit: I discussed this with a friend and he told me that an alternative to threads is polling with select: specify a timeout of 0 and do IO on the returned file descriptors (now that they are guaranteed not to block).

提交回复
热议问题