Relationship between event loop,libuv and v8 engine
Asked Answered
B

4

31

I am learning through the architecture of Node.js. I have following questions.

  1. Is event loop a part of libuv or v8?
  2. Is event queue a part of event loop? are event queue generated by libuv or v8 engine or event loop itself?
  3. What is the connection between libuv and v8 engine?
  4. If event loop is single threaded, does libuv come into picture to create multiple threads to handle File I/O?
  5. Does browsers have event loop mechanism or just Node.js does?
Brelje answered 13/4, 2018 at 6:58 Comment(1)
For 2. the loop is just pulling things out of the queue and executing them. If the queue is empty and there are no background threads that could potentially add work to the queue then the loop ends.Luminance
E
28
  1. The event loop is, first and foremost, a high-level concept that's a fundamental part of the JavaScript programming model. Practically, every V8 embedder needs to implement an event loop. V8 provides a default implementation, which embedders can replace or extend.

  2. I don't understand the question. (I guess the answer is "yes", but what's the difference between "event loop" and "event queue"?)

  3. None. (Except that Node.js uses both.)

  4. Yes, the event loop is single-threaded.

  5. Yes, browsers have an event loop too (see question 1).

Elegy answered 13/4, 2018 at 18:36 Comment(3)
Could you cite the reference implementation for the event loop by V8 as default implementation? I could not find it. Thank youSouthernmost
It's part of "libplatform", see e.g. PumpMessageLoop: cs.chromium.org/chromium/src/v8/src/libplatform/…Elegy
Regarding #3 - there is a well defined connection. Node registers function bindings with V8 that directly call into libuv. This in effect makes V8 capable of accessing the file system amongst other things.Multitudinous
A
9

The V8 project and the libuv project are two of the most important dependencies of NodeJS.

I think it's important to have a basic understanding of what threads are before even getting into the Node Event Loop.

So let's think of a thread as a to-do list of instructions that need to be executed by the CPU and the CPU will run these threads one by one starting at the top on down. A single process can have multiple threads inside of it.

To understand threads, it's important to also understand a concept called scheduling, as your CPU can only process so many instructions per second.

Now what is the relationship between V8, libuv and the Event Loop?

Well, V8 and libuv I have established are dependencies of NodeJS, this is what makes it possible to run JavaScript outside the browser. Whenever we start up a Node program on our computer, Node automatically starts up one thread and executes some code inside that thread. Inside that single thread is something called the Event Loop, which can be thought of as a control structure which decides what our one thread should be doing at any point in time.

It is the absolute core of any Node program and every Node program has exactly one Event Loop.

So:

Is event loop a part of libuv or v8?

Yes, the Event Loop comes from a process started up by Node which has V8 and libuv as dependencies.

Is event queue a part of event loop? are event queue generated by libuv or v8 engine or event loop itself?

Event queue, if I understand the question is more relegated to your OS scheduler. which decides which are the most urgent tasks and run those first.

What is the connection between libuv and v8 engine?

Well, both of them are dependencies of NodeJS and both have been written in C++, V8 is 70% C++ and libuv is 100% C++.

If event loop is single threaded, does libuv come into picture to create multiple threads to handle File I/O?

So this can be confusing, the Node Event Loop is single threaded, but some of the Node Standard Library modules and some frameworks are not single threaded.

The libuv library gives Node underlying access to your operating system. The libuv module and the C++ side make use of the threadpool. It can be used for running computationally expensive tasks.

By default, libuv creates four threads in the threadpool. In addition to the threads used in the event loop there are four other threads that can be used to offload expensive calculations that need to occur inside the application.

Many of the functions in the Node Standard Library make use of this threadpool. So yes libuv comes into the picture to create a threadpool composed of four threads. So libuv provides a threadpool for offloading work to be done on very expensive function calls.

Does browsers have event loop mechanism or just Node.js does?

I only know of an event loop mechanism inside of NodeJS.

In summation, we have a 2015 MacBook Pro with dual cores. Imagine you run two computationally expensive functions, the first one runs on thread number one which is inside the thread pool, it goes through the OS Scheduler and to CPU core number one. Then the second function gets assigned to the second thread in the pool and it gets assigned to the second CPU core. So that threadpool is happening inside of libuv.

One important point, if you are making an http request, libuv sees that and neither libuv nor Node has any code to handle all of the super low level operations involved with a network request. Instead libuv delegates the http request to the underlying operating system.

In such a case, libuv is used to issue the request to the operating system and just waits for the OS to emit a response that comes back on the request. So because libuv delegates this to your OS, it's your OS that decides whether to add a new thread or not. This is a case, an http request where we are not constrained to libuv's four threaded threadpool. All the work is being done by the operating system itself and we are not touching the threadpool at all.

Armenian answered 31/7, 2021 at 3:23 Comment(0)
C
4

This is actually not as simple as given in the post selected as the answer. I hope my remarks will be a bit more exact. Hopefully I understood Sam Roberts (IBM) correctly in his talk on the Node event loop.

To view the talk yourself you can go here: https://www.youtube.com/watch?v=P9csgxBgaZ8

This is an addon to the answer given by @jmrk

Libuv delegates tasks to the underlying operating system. The operating system then becomes responsible for sending a notification when an event occurs you are listening to. It does this for a lot of the operations you perform in Node. Like: sockets (net/dgram/http/tls/https/child_process pipes, stdin, out, err), timeouts and intervals.

However not everything can be delagated like this to the underlying OS. Sometimes it is required to create a thread (there are 4 treads by default but you can change this using UV_THREADPOOL_SIZE). Not pollable are file system operations, dns.lookup() and some crypto functions.

Cress answered 31/10, 2019 at 12:30 Comment(0)
S
0

Go through these points:-

  1. V8 engine is the engine for coaches of train. It has certain responsibilites including providing event loop to run asynchronous task.

  2. Event loop is the core to perform async tasks. As soon as,C++ Web APIs finish a function(task) , callback is called.It is moved to event queue and waits untill stack becomes empty. Thus, event queue is part of event loop and are generated by event loop.

  3. V8 engine is used to execute the javascript code we write and libuv is a lbrary used to provide multi threading feature in Nodejs to execute long running processes.

  4. Event loop is single threaded but Nodejs is not single threaded as it has a libuv threadpool in its runtime which is responsible for multi threading.

  5. Browsers APIs also provide event loop.

Solis answered 13/11, 2019 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.