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
8e2366a
src: add NewIsolate() overload taking a platform delegate
jasnell d0e4387
lib,src: add experimental node:green_threads module
jasnell 3ff4408
lib,src: schedule isolates on any pool thread
jasnell a2b3204
lib,src: fix teardown, timer and task lifetime issues
jasnell 01260f1
lib,src: add CPU and memory limits and a warm pool
jasnell e75a190
benchmark: add green_threads benchmarks
jasnell 9d384b6
lib,src: resolve setWarmPoolSize() once the pool is full
jasnell eae3d86
lib,src: fix ArrayBuffer memory limit before the first GC
jasnell b83dc93
lib,src: add isolate groups
jasnell b33ed60
lib,src: stop green threads that ignore termination
jasnell f178b06
lib,src: add setUnresponsiveTimeouts()
jasnell e4be24d
src: fix build with the V8 sandbox enabled
jasnell 54f0845
lib,src: initialize warm isolates in the background
jasnell febaf87
lib,src: add EventTarget and AbortController
jasnell 4b0640e
lib,src: add RPC
jasnell 087fa3b
lib,src: transfer ArrayBuffers and share SharedArrayBuffers
jasnell 198fd24
worker: allow MessagePortData owners other than MessagePorts
jasnell ed5dcd8
lib,src: add MessageChannel and MessagePort
jasnell cacf5cb
lib,src: add ES modules and exports
jasnell f746044
lib,src: keep the properties of errors
jasnell 16706a9
lib,src: start green threads from a bootstrapped snapshot
jasnell 3b99b65
lib,src: set the time origin when green threads start
jasnell 4d89efa
lib,src: build the startup snapshots of green threads
jasnell 9e2218e
lib,src: use a small snapshot in the default isolate group
jasnell ece5a70
lib,src: add NODE_GREEN_THREADS_POOL_SIZE
jasnell d280172
src: only query the memory for heap defaults when used
jasnell 0859a0d
lib,src: keep the young generation limit
jasnell 3aa63ab
lib,src: add warm pools for isolate groups and memory limits
jasnell 38f5b50
lib,src: don't copy the runtime's sources into snapshots
jasnell 7d87efd
src,doc: fix lint and formatting of green threads
jasnell f0952fe
src: don't build green threads' snapshot for embedders
jasnell 5b1cb41
lib,src: add BroadcastChannel to green threads
jasnell 16ad43a
lib,src: close the ports of abandoned green threads
jasnell b27ffdf
lib,src: add greenThread.close()
jasnell 0592b7b
lib,src: export RPC objects only once a message is serialized
jasnell 147899e
lib: keep imported AbortSignals alive until they abort
jasnell 6d31907
src,lib: add the --allow-green-threads permission
jasnell 086d1d6
lib,src: add greenThreads.setStallPolicy()
jasnell c555adf
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: