◐ Shell
clean mode source ↗

doc: improve internal documentation on built-in snapshot · nodejs/node@b0b77d7

@@ -8,9 +8,46 @@ instead of executing code to bootstrap, it can deserialize the context from

88

an embedded snapshot, which readily contains the result of the bootstrap, so

99

that Node.js can start up faster.

101011-

Currently only the main context of the main Node.js instance supports snapshot

12-

deserialization, and the snapshot does not yet cover the entire bootstrap

13-

process. Work is being done to expand the support.

11+

The built-in snapshot consists of the following snapshots:

12+13+

## Isolate snapshot

14+15+

Which is used whenever a `v8::Isolate` is created using the data returned by

16+

`node::SnapshotBuilder::GetEmbeddedSnapshotData`.

17+18+

## Context snapshots

19+20+

Which are used when a `v8::Context` is created from a `v8::Isolate` deserialized

21+

from the snapshot. There are four context snapshots in the snapshot blob.

22+23+

1. The default context snapshot, used for contexts created by

24+

`v8::Context::New()`, it only contains V8 built-ins, matching the

25+

layout of the isolate snapshot.

26+

1. The vm context snapshot, which can be deserialized using

27+

`v8::Context::FromSnapshot(isolate, node::SnapshotData::kNodeVMContextIndex, ...)`.

28+

It captures initializations specific to vm contexts done by

29+

`node::contextify::ContextifyContext::CreateV8Context()`.

30+

1. The base context snapshot, which can be deserialized using

31+

`v8::Context::FromSnapshot(isolate, node::SnapshotData::kNodeBaseContextIndex, ...)`.

32+

It currently captures initializations done by `node::NewContext()`

33+

but is intended to include more as a basis for worker thread

34+

contexts.

35+

1. The main context snapshot, which can be deserialized using

36+

`v8::Context::FromSnapshot(isolate, node::SnapshotData::kNodeMainContextIndex, ...)`.

37+

This is the snapshot for the main context on the main thread, and

38+

captures initializations done by `node::CommonEnvironmentSetup::CreateForSnapshotting()`,

39+

most notably `node::CreateEnvironment()`, which runs the following scripts via

40+

`node::Realm::RunBootstrapping()` for the main context as a principal realm,

41+

so that at runtime, these scripts do not need to be run. Instead only the context

42+

initialized by them is deserialized at runtime.

43+

1. `internal/bootstrap/realm`

44+

2. `internal/bootstrap/node`

45+

3. `internal/bootstrap/web/exposed-wildcard`

46+

4. `internal/bootstrap/web/exposed-window-or-worker`

47+

5. `internal/bootstrap/switches/is_main_thread`

48+

6. `internal/bootstrap/switches/does_own_process_state`

49+50+

For more information about these contexts, see the comment in `src/node_context_data.h`.

14511552

## How it's built and used

1653

@@ -39,3 +76,22 @@ For debugging, Node.js can be built without Node.js's own snapshot if

3976

`--without-node-snapshot` is passed to `configure`. A Node.js executable

4077

with Node.js snapshot embedded can also be launched without deserializing

4178

from it if the command line argument `--no-node-snapshot` is passed.

79+80+

### When `node_mksnapshot` crashes..

81+82+

Due to this two-phase building process, sometimes when there is an issue

83+

in the code, the build may crash early at executing `node_mksnapshot` instead of crashing

84+

at executing the final executable `node`. If the crash can be reproduced when running

85+

the `node` executable built with `--without-node-snapshot`, it means the crash likely

86+

has nothing to do with snapshots, and only shows up in `node_mksnapshot` because it's

87+

the first Node.js executable being run.

88+89+

If the crash comes from a `mksnapshot` executable (notice that it doesn't have the `node_`

90+

prefix), that comes from V8's own snapshot building process, not the one in Node.js, and the

91+

fix likely needs to be in V8 or the build configurations of V8.

92+93+

If it `node_mksnapshot` crashes with an error message containing

94+

something like `Unknown external reference 0x107769200`, see

95+

[Registering binding functions used in bootstrap][] on how to fix it.

96+97+

[Registering binding functions used in bootstrap]: ../../src/README.md#registering-binding-functions-used-in-bootstrap