Why does this code lead to a 100MB wasm file?

If you compile the following code:

const std = @import("std");

const allocator: std.mem.Allocator = .{
    .ptr = undefined,
    .vtable = &std.heap.WasmAllocator.vtable,
};
var sfb = std.heap.stackFallback(1024 * 1024 * 100, allocator);

pub fn main() void {
    _ = sfb.get();
}

using the command:

zig build-exe test.zig -target wasm32-wasi -O ReleaseSmall

you’d get a really big WASM file:

-rwxr--r--   1 cleong cleong 104858509 Aug 18 22:16  test.wasm
-rw-rw-r--   1 cleong cleong 104859396 Aug 18 22:16  test.wasm.o
-rw-rw-r--   1 cleong cleong       247 Aug 18 22:16  test.zig

Why is that?

I think it’s because the global sfb variable has a [1024 * 1024 * 100]u8 in it - which is 100mb. So that’s just like declaring a global 100mb string - those bytes appear verbatim in the output file. You should get the same behavior for non-wasm builds.

What are you trying to do?