Repository navigation
src: experimental green threads implementation #67288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jasnell
wants to merge
39
commits into
nodejs:main
Choose a base branch
from
jasnell:green-threads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+14,151
−40
Open
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
a5593db
src: add NewIsolate() overload taking a platform delegate
jasnell fa58066
lib,src: add experimental node:green_threads module
jasnell 6f652f4
lib,src: schedule isolates on any pool thread
jasnell 5b6537e
lib,src: fix teardown, timer and task lifetime issues
jasnell 1aff5a9
lib,src: add CPU and memory limits and a warm pool
jasnell 96cafbe
benchmark: add green_threads benchmarks
jasnell 0e24cf9
lib,src: resolve setWarmPoolSize() once the pool is full
jasnell 4631217
lib,src: fix ArrayBuffer memory limit before the first GC
jasnell c454a18
lib,src: add isolate groups
jasnell 59fa57b
lib,src: stop green threads that ignore termination
jasnell 67f8dee
lib,src: add setUnresponsiveTimeouts()
jasnell bc5d851
src: fix build with the V8 sandbox enabled
jasnell 484667d
lib,src: initialize warm isolates in the background
jasnell 68067a8
lib,src: add EventTarget and AbortController
jasnell 579ef2b
lib,src: add RPC
jasnell 7bd1c1a
lib,src: transfer ArrayBuffers and share SharedArrayBuffers
jasnell f52e10c
worker: allow MessagePortData owners other than MessagePorts
jasnell bd66770
lib,src: add MessageChannel and MessagePort
jasnell c177974
lib,src: add ES modules and exports
jasnell daa219f
lib,src: keep the properties of errors
jasnell 9bc6614
lib,src: start green threads from a bootstrapped snapshot
jasnell 8fc3bae
lib,src: set the time origin when green threads start
jasnell 4ce50df
lib,src: build the startup snapshots of green threads
jasnell f651038
lib,src: use a small snapshot in the default isolate group
jasnell ba4ecd2
lib,src: add NODE_GREEN_THREADS_POOL_SIZE
jasnell c554c4b
src: only query the memory for heap defaults when used
jasnell 9e649e4
lib,src: keep the young generation limit
jasnell 52cea99
lib,src: add warm pools for isolate groups and memory limits
jasnell baf2be9
lib,src: don't copy the runtime's sources into snapshots
jasnell f423788
src,doc: fix lint and formatting of green threads
jasnell cc475f0
src: don't build green threads' snapshot for embedders
jasnell 8028d75
lib,src: add BroadcastChannel to green threads
jasnell 05eaf4a
lib,src: close the ports of abandoned green threads
jasnell 9b5284c
lib,src: add greenThread.close()
jasnell 2e840b1
lib,src: export RPC objects only once a message is serialized
jasnell 03d76fe
lib: keep imported AbortSignals alive until they abort
jasnell abc175f
src,lib: add the --allow-green-threads permission
jasnell a42c05a
lib,src: add greenThreads.setStallPolicy()
jasnell 43eb49f
doc: explain how green threads avoid and break stalls
jasnell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
| const { once } = require('events'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| kind: ['green-thread', 'worker'], | ||
| concurrency: [1, 16], | ||
| n: [10000], | ||
| }, { | ||
| flags: ['--experimental-green-threads', '--no-warnings'], | ||
| }); | ||
|
|
||
| // Sends `n` messages in total, spread over `concurrency` threads, each of | ||
| // which echoes every message back before it gets the next one. | ||
| async function main({ kind, concurrency, n }) { | ||
| let threads; | ||
| if (kind === 'green-thread') { | ||
| const { GreenThread } = require('node:green_threads'); | ||
| threads = Array.from({ length: concurrency }, () => new GreenThread( | ||
| 'onmessage = ({ data }) => postMessage(data);')); | ||
| } else { | ||
| const { Worker } = require('worker_threads'); | ||
| threads = Array.from({ length: concurrency }, () => new Worker(` | ||
| const { parentPort } = require('worker_threads'); | ||
| parentPort.on('message', (data) => parentPort.postMessage(data)); | ||
| `, { eval: true })); | ||
| await Promise.all(threads.map((w) => once(w, 'online'))); | ||
| } | ||
| const perThread = Math.ceil(n / concurrency); | ||
| bench.start(); | ||
| await Promise.all(threads.map(async (thread) => { | ||
| for (let i = 0; i < perThread; i++) { | ||
| const reply = once(thread, 'message'); | ||
| thread.postMessage(i); | ||
| await reply; | ||
| } | ||
| })); | ||
| bench.end(perThread * concurrency); | ||
| await Promise.all(threads.map((thread) => thread.terminate())); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| direction: ['green-to-host', 'host-to-green'], | ||
| concurrency: [1, 16], | ||
| n: [10000], | ||
| }, { | ||
| flags: ['--experimental-green-threads', '--no-warnings'], | ||
| }); | ||
|
|
||
| // Makes `n` RPC calls in total, spread over `concurrency` green threads, each | ||
| // of which makes or receives one call at a time. | ||
| async function main({ direction, concurrency, n }) { | ||
| const { GreenThread } = require('node:green_threads'); | ||
| const perThread = Math.ceil(n / concurrency); | ||
| let started; | ||
| const ready = new Promise((resolve) => { started = resolve; }); | ||
| const echoes = []; | ||
| const greenThreads = Array.from({ length: concurrency }, () => { | ||
| return new GreenThread(direction === 'green-to-host' ? ` | ||
| onmessage = async ({ data: n }) => { | ||
| for (let i = 0; i < n; i++) await env.echo(i); | ||
| postMessage(0); | ||
| }; | ||
| ` : 'env.register((value) => value);', { | ||
| env: { | ||
| echo: (value) => value, | ||
| register(echo) { | ||
| echoes.push(echo); | ||
| if (echoes.length === concurrency) started(); | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| if (direction === 'host-to-green') await ready; | ||
| bench.start(); | ||
| await Promise.all(greenThreads.map(async (greenThread, index) => { | ||
| if (direction === 'green-to-host') { | ||
| const done = new Promise((resolve) => { | ||
| greenThread.once('message', resolve); | ||
| }); | ||
| greenThread.postMessage(perThread); | ||
| await done; | ||
| } else { | ||
| const echo = echoes[index]; | ||
| for (let i = 0; i < perThread; i++) await echo(i); | ||
| } | ||
| })); | ||
| bench.end(perThread * concurrency); | ||
| await Promise.all(greenThreads.map((greenThread) => greenThread.terminate())); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| 'use strict'; | ||
| const common = require('../common.js'); | ||
| const { once } = require('events'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| kind: ['green-thread', 'green-thread-warm', 'worker', 'vm-context'], | ||
| n: [50], | ||
| }, { | ||
| flags: ['--experimental-green-threads', '--no-warnings'], | ||
| }); | ||
|
|
||
| // Starts `n` of `kind` at once, and measures until each has sent a message. | ||
| async function main({ kind, n }) { | ||
| switch (kind) { | ||
| case 'green-thread': | ||
| case 'green-thread-warm': { | ||
| const { GreenThread, setWarmPoolSize } = require('node:green_threads'); | ||
| if (kind === 'green-thread-warm') await setWarmPoolSize(n); | ||
| bench.start(); | ||
| const greenThreads = Array.from( | ||
| { length: n }, () => new GreenThread('postMessage(0);')); | ||
| await Promise.all(greenThreads.map((g) => once(g, 'message'))); | ||
| bench.end(n); | ||
| await setWarmPoolSize(0); | ||
| await Promise.all(greenThreads.map((g) => g.terminate())); | ||
| break; | ||
| } | ||
| case 'worker': { | ||
| const { Worker } = require('worker_threads'); | ||
| bench.start(); | ||
| const workers = Array.from({ length: n }, () => new Worker( | ||
| "require('worker_threads').parentPort.postMessage(0);", | ||
| { eval: true })); | ||
| await Promise.all(workers.map((w) => once(w, 'message'))); | ||
| bench.end(n); | ||
| await Promise.all(workers.map((w) => w.terminate())); | ||
| break; | ||
| } | ||
| case 'vm-context': { | ||
| const vm = require('vm'); | ||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| vm.runInContext('0', vm.createContext()); | ||
| } | ||
| bench.end(n); | ||
| break; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the new experimental status: