启动快照支持


¥Startup snapshot support

useSnapshot 字段可用于启用启动快照支持。在这种情况下,启动最终可执行文件时将不会出现 main 脚本。相反,它将在构建计算机上生成单个可执行应用准备 blob 时运行。然后,生成的准备 blob 将包含捕获由 main 脚本初始化的状态的快照。注入准备 blob 的最终可执行文件将在运行时反序列化快照。

¥The useSnapshot field can be used to enable startup snapshot support. In this case the main script would not be when the final executable is launched. Instead, it would be run when the single executable application preparation blob is generated on the building machine. The generated preparation blob would then include a snapshot capturing the states initialized by the main script. The final executable with the preparation blob injected would deserialize the snapshot at run time.

useSnapshot 为 true 时,主脚本必须调用 v8.startupSnapshot.setDeserializeMainFunction() API 来配置用户启动最终可执行文件时需要运行的代码。

¥When useSnapshot is true, the main script must invoke the v8.startupSnapshot.setDeserializeMainFunction() API to configure code that needs to be run when the final executable is launched by the users.

应用在单个可执行应用中使用快照的典型模式是:

¥The typical pattern for an application to use snapshot in a single executable application is:

  1. 在构建时,在构建机器上,运行主脚本以将堆初始化为准备接受用户输入的状态。该脚本还应该使用 v8.startupSnapshot.setDeserializeMainFunction() 配置一个 main 函数。该函数将被编译并序列化到快照中,但不会在构建时调用。

    ¥At build time, on the building machine, the main script is run to initialize the heap to a state that's ready to take user input. The script should also configure a main function with v8.startupSnapshot.setDeserializeMainFunction(). This function will be compiled and serialized into the snapshot, but not invoked at build time.

  2. 在运行时,主函数将在用户计算机上的反序列化堆之上运行,以处理用户输入并生成输出。

    ¥At run time, the main function will be run on top of the deserialized heap on the user machine to process user input and generate output.

当用于为单个可执行应用构建快照时,启动快照脚本的一般约束也适用于主脚本,并且主脚本可以使用 v8.startupSnapshot API 来适应这些约束。参见 有关 Node.js 中启动快照支持的文档

¥The general constraints of the startup snapshot scripts also apply to the main script when it's used to build snapshot for the single executable application, and the main script can use the v8.startupSnapshot API to adapt to these constraints. See documentation about startup snapshot support in Node.js.