Skip to content

Commit 8b7df9b

Browse files
committed
green_threads: don't copy the runtime's sources into snapshots
V8 serializes external strings as ordinary strings unless their resources are external references. The snapshots of green threads only had the runtime's callbacks as external references, so they contained copies of the sources of its builtins, about 65 KB, which every green thread deserialized into its heap. Add Node.js' external references, which include the sources of all builtins, so that the snapshots refer to the sources in the binary. This makes each green thread use 60 KB less memory (1.34 MB rather than 1.40 in a release build) and the startup snapshots 65 KB smaller. Assisted-by: Claude Opus 5.5 Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 6f26ea5 commit 8b7df9b

1 file changed

Lines changed: 39 additions & 28 deletions

File tree

‎src/node_green_thread.cc‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,35 +3148,46 @@ class GreenIsolate final : public IsolatePlatformDelegate,
31483148
static constexpr size_t kGreenSnapshotContextIndex = 0;
31493149
static constexpr size_t kGreenSnapshotRuntimeIndex = 0;
31503150

3151-
// The external references of the green snapshot: the callbacks of the
3152-
// functions that InstallGlobals() and RunBootstrap() create.
3151+
// The external references of green snapshots: the callbacks of the
3152+
// functions that InstallGlobals() and RunBootstrap() create, and Node.js'
3153+
// own, which include the sources of builtins. With those, the snapshot
3154+
// refers to the sources of the runtime in the binary instead of containing
3155+
// copies of them, which every green thread would deserialize.
31533156
static const intptr_t* SnapshotExternalReferences() {
3154-
static const intptr_t references[] = {
3155-
reinterpret_cast<intptr_t>(PostMessageCallback),
3156-
reinterpret_cast<intptr_t>(CloseCallback),
3157-
reinterpret_cast<intptr_t>(QueueMicrotaskCallback),
3158-
reinterpret_cast<intptr_t>(SetTimeoutCallback),
3159-
reinterpret_cast<intptr_t>(SetIntervalCallback),
3160-
reinterpret_cast<intptr_t>(ClearTimerCallback),
3161-
reinterpret_cast<intptr_t>(ConsoleCallback),
3162-
reinterpret_cast<intptr_t>(RpcSendCallback),
3163-
reinterpret_cast<intptr_t>(MessageChannelCallback),
3164-
reinterpret_cast<intptr_t>(PortPostMessageCallback),
3165-
reinterpret_cast<intptr_t>(PortStartCallback),
3166-
reinterpret_cast<intptr_t>(PortCloseCallback),
3167-
reinterpret_cast<intptr_t>(CompileModuleCallback),
3168-
reinterpret_cast<intptr_t>(CreateJsonModuleCallback),
3169-
reinterpret_cast<intptr_t>(ModuleRequestsCallback),
3170-
reinterpret_cast<intptr_t>(LinkModuleCallback),
3171-
reinterpret_cast<intptr_t>(InstantiateModuleCallback),
3172-
reinterpret_cast<intptr_t>(EvaluateModuleCallback),
3173-
reinterpret_cast<intptr_t>(ModuleNamespaceCallback),
3174-
reinterpret_cast<intptr_t>(ResolveUrlCallback),
3175-
reinterpret_cast<intptr_t>(InternalBindingCallback),
3176-
reinterpret_cast<intptr_t>(RequireCallback),
3177-
0,
3178-
};
3179-
return references;
3157+
static const std::vector<intptr_t> references = [] {
3158+
const intptr_t callbacks[] = {
3159+
reinterpret_cast<intptr_t>(PostMessageCallback),
3160+
reinterpret_cast<intptr_t>(CloseCallback),
3161+
reinterpret_cast<intptr_t>(QueueMicrotaskCallback),
3162+
reinterpret_cast<intptr_t>(SetTimeoutCallback),
3163+
reinterpret_cast<intptr_t>(SetIntervalCallback),
3164+
reinterpret_cast<intptr_t>(ClearTimerCallback),
3165+
reinterpret_cast<intptr_t>(ConsoleCallback),
3166+
reinterpret_cast<intptr_t>(RpcSendCallback),
3167+
reinterpret_cast<intptr_t>(MessageChannelCallback),
3168+
reinterpret_cast<intptr_t>(PortPostMessageCallback),
3169+
reinterpret_cast<intptr_t>(PortStartCallback),
3170+
reinterpret_cast<intptr_t>(PortCloseCallback),
3171+
reinterpret_cast<intptr_t>(CompileModuleCallback),
3172+
reinterpret_cast<intptr_t>(CreateJsonModuleCallback),
3173+
reinterpret_cast<intptr_t>(ModuleRequestsCallback),
3174+
reinterpret_cast<intptr_t>(LinkModuleCallback),
3175+
reinterpret_cast<intptr_t>(InstantiateModuleCallback),
3176+
reinterpret_cast<intptr_t>(EvaluateModuleCallback),
3177+
reinterpret_cast<intptr_t>(ModuleNamespaceCallback),
3178+
reinterpret_cast<intptr_t>(ResolveUrlCallback),
3179+
reinterpret_cast<intptr_t>(InternalBindingCallback),
3180+
reinterpret_cast<intptr_t>(RequireCallback),
3181+
};
3182+
std::vector<intptr_t> result(std::begin(callbacks), std::end(callbacks));
3183+
// Ends with 0.
3184+
const std::vector<intptr_t>& node_references =
3185+
SnapshotBuilder::CollectExternalReferences();
3186+
result.insert(result.end(), node_references.begin(),
3187+
node_references.end());
3188+
return result;
3189+
}();
3190+
return references.data();
31803191
}
31813192

31823193
public:

0 commit comments

Comments
 (0)