Wasm Snapshot really works, reducing startup time by 20-50%

86 views
Skip to first unread message

Damon Lei

unread,
Dec 22, 2022, 10:23:14 AM12/22/22
to emscripten-discuss

Hi

I am trying to use the Wasm Snapshot to reduce the startup time, and it succeeded. Wasm Apps run correctly, reducing startup time by 20-50%.

My job is to help developers export Unity games to webgl mode to run on our App (WeChat in China).
Unfortunately, after exports, wasm callMain is very slow, taking an average of 5 seconds.

So I spent a few months experimenting with the Wasm Snapshot solution, which means, take a snapshot of Wasm App during development.
At runtime, callMain is no longer executed, but a snapshot.data is downloaded and restored. The snapshot.data includes Wasm Memory, FS, GL, and other JS States.
At the same time, I will cut out the data in wasm and some resources of Unity's res-bundle, because they are no longer needed.

In the 5 Unity games tested, no problems were found. Startup time-consuming optimization is considerable.

The snapshot.data size is about 4MB after compression(brotli).
The optimization is about 2-5sec (20%-50%). See the end for specific data.

Wasm Snapshot is an awesome solution, and I want you to know that it really works.
At the same time, I would like to know whether the startup is a common problem? And can it be used as a new feature of Emscripten to benefit more users?

截屏2022-12-22 17.34.57.png

Sam Clegg

unread,
Dec 22, 2022, 10:30:40 AM12/22/22
to emscripte...@googlegroups.com
Hi Damon

This sounds really great.  When you say "Wasm Snapshot" are you talking about some tool that you wrote yourself or some existing tool such as wizer?  Would you be interested in contributing this tool to emscripten, or perhaps some documentation on how to go about doing this?

cheers,
sam

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/cdaeda97-f40f-42ac-8194-2bb771d49aebn%40googlegroups.com.

Damon Lei

unread,
Dec 25, 2022, 10:34:59 PM12/25/22
to emscripten-discuss
Hi Sam,

sorry for the late reply. 

Wasm Snapshot is a tool I wrote myself.
This tool modifies the exported framework.js, including preRun, callMain, initRuntime, GL.createContext, FS.init, etc.
In other words, Wasm Snapshot will also serialize the state in JS when callingMain, and restore it when it is used.

This solution works in browsers (Chrome and WebKit). No native modification required.

I'd love to hear your thoughts and comments on this solution.

Next, I will do an online test of the program. I will post it here as soon as I get the results.

Finally, I'm very interested in contributing to emscripten, but the exact method is for another discussion.

Damon Lei

unread,
Jan 29, 2023, 2:43:14 AM1/29/23
to emscripten-discuss
Hi,
We tested the WasmSnapshot solution in the production environment.
1. Tested with more than 500,000 users, no user feedback has been found so far
2. It takes 2 seconds to optimize the startup time. Before optimization, the game startup takes about 10s. On Android, the optimization is 13.6%, and the iOS optimization is 22.2%

This solution works very well, and I want to document this solution on Emscrpten's documentation, such as this “link (Emscripten/OptimizingCode/UnsafeOptimize)” (or some other documents), so that more people know that this method is feasible. 

What do you think of this?

Thanks,
在2022年12月22日星期四 UTC+8 23:30:40<s...@google.com> 写道:

Sam Clegg

unread,
Jan 29, 2023, 11:52:40 AM1/29/23
to emscripte...@googlegroups.com
On Sat, Jan 28, 2023 at 11:43 PM Damon Lei <danxi...@gmail.com> wrote:
Hi,
We tested the WasmSnapshot solution in the production environment.
1. Tested with more than 500,000 users, no user feedback has been found so far
2. It takes 2 seconds to optimize the startup time. Before optimization, the game startup takes about 10s. On Android, the optimization is 13.6%, and the iOS optimization is 22.2%

This solution works very well, and I want to document this solution on Emscrpten's documentation, such as this “link (Emscripten/OptimizingCode/UnsafeOptimize)” (or some other documents), so that more people know that this method is feasible. 

What do you think of this?

If you think this is something that could be upstreamed into emscripten itself, then maybe create PR to start adding support?

if it's just a matter of documentation you technique then perhaps we could start by adding a wiki page here:  https://github.com/emscripten-core/emscripten/wiki/_pages

Damon Lei

unread,
Jan 30, 2023, 10:08:41 PM1/30/23
to emscripten-discuss

This wiki looks good
I will explain the principle and implementation ideas of this solution in the wiki. And continue to improve it in this year.

In addition, does Emscripten consider officially supporting snapshots? This will greatly change the JS logic.
My current solution is only to make snapshots for Unity games. By processing syscall, MemFS, GL, requestAnimationFrame and other interfaces, the state in js is stored in snapshots. This is enough for Unity, but for other wasm applications It's not enough.
So, are you planning to do this? I think I can do PRs if needed.

Thanks alot
Reply all
Reply to author
Forward
0 new messages