Description
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.
2. Enable node API in multi-thread incrementally.
3. 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 -
- What is the I/O API's expected behavior in multi-thread
- Replace Buffer's underlying
ArrayBuffer
bySharedArrayBuffer
- Revisit the crypto API in multi-thread
- ...
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).