-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Optimizing Startup Speed by Snapshot
by damonlei, junkunzhang
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.
First of all, how does Unity(web) work?
- Download resources: wasm, resource data
- Run the js code generated by emscripten
- (very slow) execute callMain and register requestAnimationFrame(rAF)
- 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.
You can discuss it directly in Google Group You can also contact me directly: danxionglei@gmail.com or WeChat account: ldx_me
README.md ``