How to decrease the `type_size` of WebAssembly?

Hi, I recently try to run my wasm file with wasmer. And I got a problem here. The size of my wasm is huge, about 20MB. And when I try to run it with wasmer, the error occurs.

// wasmer version: 3.1.0 
error: failed to run `xxx.wasm`
│   1: module instantiation failed (compiler: cranelift)
╰─▶ 2: Validation error: effective type size too large (at offset 495969)

And I check the source code of wasmer and wasmparser, I found out that pub const MAX_TYPE_SIZE: u32 = 100_000;. That maybe the limits of the type_size. But I still can't fix my problems here, I don't know what it means.

So is there any ways to decrease the type_size of my wasm?

Judging by the name, it's probably the maximal allowed size (as in mem::size_of()) of any type used in your crate. Are you declaring huge arrays (e.g. several 10s of thousands elements) by any chance?

I don't think I have declared any huge arrays, just normal codes.

Are you using futures? (async/await) They can generate huge types.

Every async_call().await can make its caller larger due to inlining state of the call into the parent. If you use Box::pin(async_call()).await then it won't inline the state, and the parent caller's future will be smaller, at a small cost of allocation and indirection.

1 Like

Acutually I compile C/C++ to wasm, so that shouldn't be my case either.

This is about the sizes of wasm types, not about the sizes of types in the language compiled to webassembly. Looking at the source of wasmparser, it looks like it computes the combined size of all function types for imports and exports. In other words what matters is the amount of functions imported amd exported from the wasm module and how much arguments they have. What kind of thing did you compile that you have so many imports or exports?

2 Likes

Oh, that makes sense. When I link the wasm files, I passed --export-all to make sure all the functions I need will be exported. That maybe the reason, I will replace it with __attribute__((export_name(""))) to have a try.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.