Skip to content

Optimizing Startup Speed by Snapshot

Danxiong Lei edited this page Feb 1, 2023 · 2 revisions

Use Snapshot for startup performance optimization

by damonlei, junkunzhang

background

We use Emscripten to migrate Unity games to the web platform, but the startup performance is very slow, about 10s or so. Especially callMain, the main function contains a lot of Unity startup logic, so it takes a lot of time. So we designed the Snapshot solution to allow users to skip the callMain stage and run it directly.

how we did it

First of all, how does Unity(web) work?

  1. Download resources: wasm, resource data
  2. Run the js code generated by emscripten
  3. (very slow) execute callMain and register requestAnimationFrame(rAF)
  4. In the rAF function of each frame, execute the Update function of Unity

During development, we will serialize all states after callMain is executed into snapshots. When user starts, use snapshot for deserialization. In this way, callMain can be skipped and Unity logic can be executed directly.

The "state" here includes two types, which are the wasm state and the js state. The state of Wasm is serialized in Wasm.Memory, which is an ArrayBuffer, which is convenient for serialization. And the state of JS can also be obtained. Since only limited modules such as GL, MemFS, and requestAnimationFrame are used by Unity, we can insert our own js code into these modules to obtain the state in js and serialize it into the snapshot.

At present, this solution has been tested online on our Unity (web) game, which can prove that this idea is completely feasible. In the future, we will continue to improve this document to see if it can also be effective for other Wasm applications that are not Unity games.

discuss

You can discuss it directly in Google Group You can also contact me directly: [email protected] or WeChat account: ldx_me