启动快照支持


【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() 配置一个主函数。该函数将被编译并序列化到快照中,但在构建时不会被调用。
  2. 在运行时,主函数将在用户计算机上的反序列化堆之上运行,以处理用户输入并生成输出。

启动快照脚本的一般约束也适用于用于为单一可执行应用构建快照的主脚本,主脚本可以使用 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.】