Wasm output size problem

Zig 0.17 increases one my wasm lib size from 71k to 81k (about the same as 0.14), whereas 0.15 and 0.16 each shrinked it about 5k.

So far not bad. I’m just curious the reasons. I guess it is a balance between percentages of code generation and (non-generated) runtime code in the std lib.

Get ready, it’s probably gonna increase a lot more… At least in debug mode, that is.

#37032 - default to self-hosted wasm in debug mode

2 Likes

did you check the difference? e.g.

wasm-opt --func-metrics --print-function-map

To get those kind of size, I guess you already had the .strip = true Module.CreateOption, did it change in 0.17?

1 Like

Ah, it seems the 81k is the debug size (built with zig build wasm), with the -Doptimize=ReleaseSmall option, the size is 68k.

It is strange. in build.zig, I have set the .optimize of the wasm module as .small. I don’t understand why -Doptimize=ReleaseSmall is still needed on the zig build command line.

    const wasmTarget = b.resolveTargetQuery(.{
        .cpu_arch = .wasm32,
        .os_tag = .freestanding,
    });
    const wasmOptimize: std.lang.Optimize = .small;

    const wasmLibModule = b.addModule("tmd-wasm", .{
        .root_source_file = b.path("library/tmd-wasm/wasm.zig"),
        .target = wasmTarget,
        .optimize = wasmOptimize,
    });

The wasmLibModule depends on some other modules:

    wasmLibModule.addImport("tmd", tmdLibModule);
    wasmLibModule.addImport("list", listLibModule);
    wasmLibModule.addImport("tree", treeLibModule);

Do the dependency modules inherit the .optimize option of the wasmLibModule module? Do I need to create small-optimize versions for the dependency modules, to avoid using the -Doptimize=ReleaseSmall option in the zig build wasm command?

My mistake. Without specifying the .optimize option, dependency modules do inherit the .optimize option of the wasmLibModule module:

    const tmdLibModule = b.addModule("tmd", .{
        .root_source_file = b.path("library/tmd-core/tmd.zig"),
        //.target = target,
        //.optimize = optimize,
    });
1 Like