Skip to content

Commit c99e51e

Browse files
joyeecheungtargos
authored andcommitted
src: generate default snapshot with --predictable
To improve determinism of snapshot generation, add --predictable to the V8 flags used to initialize a process launched to generate snapshot. Also add a kGeneratePredictableSnapshot flag to ProcessInitializationFlags for this and moves the configuration of these flags into node::InitializeOncePerProcess() so that it can be shared by embedders. PR-URL: #48749 Refs: nodejs/build#3043 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent aab045e commit c99e51e

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/node.cc

+6
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,12 @@ static ExitCode InitializeNodeWithArgsInternal(
838838
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
839839
#endif
840840

841+
if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) ||
842+
per_process::cli_options->per_isolate->build_snapshot) {
843+
v8::V8::SetFlagsFromString("--predictable");
844+
v8::V8::SetFlagsFromString("--random_seed=42");
845+
}
846+
841847
// Specify this explicitly to avoid being affected by V8 changes to the
842848
// default value.
843849
V8::SetFlagsFromString("--rehash-snapshot");

src/node.h

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ enum Flags : uint32_t {
265265
// cppgc::InitializeProcess() before creating a Node.js environment
266266
// and call cppgc::ShutdownProcess() before process shutdown.
267267
kNoInitializeCppgc = 1 << 13,
268+
// Initialize the process for predictable snapshot generation.
269+
kGeneratePredictableSnapshot = 1 << 14,
268270

269271
// Emulate the behavior of InitializeNodeWithArgs() when passing
270272
// a flags argument to the InitializeOncePerProcess() replacement

tools/snapshot/node_mksnapshot.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ int main(int argc, char* argv[]) {
5050
setvbuf(stderr, nullptr, _IONBF, 0);
5151
#endif // _WIN32
5252

53-
v8::V8::SetFlagsFromString("--random_seed=42");
54-
v8::V8::SetFlagsFromString("--harmony-import-assertions");
5553
return BuildSnapshot(argc, argv);
5654
}
5755

@@ -65,7 +63,8 @@ int BuildSnapshot(int argc, char* argv[]) {
6563

6664
std::unique_ptr<node::InitializationResult> result =
6765
node::InitializeOncePerProcess(
68-
std::vector<std::string>(argv, argv + argc));
66+
std::vector<std::string>(argv, argv + argc),
67+
node::ProcessInitializationFlags::kGeneratePredictableSnapshot);
6968

7069
CHECK(!result->early_return());
7170
CHECK_EQ(result->exit_code(), 0);

0 commit comments

Comments
 (0)