nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.4k stars 29.51k forks source link

multi-thread support proposal #20239

Closed fs-eire closed 5 years ago

fs-eire commented 6 years ago

Multi-thread support in Node.js is not a new requirement. There was a previous proposal #13143 trying to bring multi-threading capability to node, and there are several projects outside node doing this effort as listed here as well.

The worker proposal, however, seems not go smoothly. I consider one of the reasons is because the change turns out to be too huge. I would like to share another approach of bringing multi-thread capability into node.

The basic idea is to break the work into several stages - **1. Enable multi-thread capability for addon.

  1. Enable node API in multi-thread incrementally.
  2. Add multi-thread node API when we think it's ready.**

The first step is to enable multi-thread capability for addon, that is to say, to make it possible in addon to create another javascript thread which applies node's context. To achieve this, node's initialization and module loader need to be updated multi-thread readiness. Native API/napi should also be updated to expose necessary information.

The next step is to tune node API so that they can be helpful in multi-thread. There can be a lot of discussions and decision making, like -

We are not going to do all those on once. They can be separated to small and independent problems.

The final step is like a natural result, when dependencies and blocking issues are resolved.

The advantage of this approach is, we will not break any feature, or add incomplete feature in every step, while we can keep every changes small and clean. Enabling multi-thread for addon also enables addon developers explore and discover use cases and problems that may happen. JS users should not be affected since no new APIs are exposed before stage (3).

/cc @addaleax @Trott @bnoordhuis @jasnell

addaleax commented 6 years ago

Enable multi-thread capability for addon.

Yes, that would be a neat thing for Node to support anyway – we should definitely allow embedders and addons to use Node.js in multiple threads, whether that’s for Workers or not :+1:

The worker proposal, however, seems not go smoothly. I consider one of the reasons is because the change turns out to be too huge.

Yes, and I’m not happy about that either – But the current main obstacle is pretty well-defined: Edge cases in old Windows code in libuv. So, if we were to say that not supporting Windows to begin with is an option, we could actually move forward now.

I mean, I’m glad about concrete suggestions or help. But it probably is worth pointing out that working Worker code for Node does exist at this point.

addaleax commented 6 years ago

Also, I’ve been mentioning it in the TSC meetings, and I know @mhdawson was trying to reach out to MS people – if you know somebody who is somewhat familiar with debugging Node.js and in particular libuv on Windows, and is familiar with async I/O using the Windows C API, that might be all we need to bring https://github.com/nodejs/node/pull/19377 over the line.

(Even without the PR, those seem to be real bugs on Windows to begin with – they just don’t manifest themselves yet.)

mhdawson commented 6 years ago

@aruneshchandra ,@digitalinfinity any help on the windows side for #19377 would be greatly appreciated.

bzoz commented 6 years ago

I've picked up #19377, I'm investigating it right now.

fs-eire commented 6 years ago

@addaleax I would like to contribute to the part "Enable multi-thread capability for addon". There are 2 changes need to be done:

For the first one I believe I can find some implementation from Ayo.js. For the second one, it may be a good idea to have a discussion about the napi design.

hope this can help

addaleax commented 6 years ago

@fs-eire Yes, I agree with 100 % of what you just said. :) The patch is probably outdated compared to nodejs/node@master, but you could find relevant pieces in https://github.com/ayojs/ayo/pull/118 for the dynamic library stuff.

Also, PRs you might want to be aware of:

mhdawson commented 6 years ago

@fs-eire I'd suggest you attend the next N-API meeting (Thursday at 1:30 EST, Description:https://hangouts.google.com/hangouts/_/event/c0eevtrlajniu7h8cjrdk0f56c8) to kick off the discussion with the N-API team. (I'm hoping that time works for you)