slimwasm
A WASM size profiler for wasm32-unknown-unknown and wasm32-wasip2 binaries.
slimwasm helps you answer: "Why is this .wasm 1.2 MB and who retains core::fmt?"
A spiritual successor to the archived twiggy project. Unlike cargo-bloat which is native-only, slimwasm is designed specifically for WebAssembly.
Installation
Or build from source:
Commands
slimwasm top - Show Largest Items
Display the largest items in a .wasm file, ranked by size:
Example output:
my_app.wasm - 156.5 KiB
Sections:
89.2 KiB 57.0% code
45.3 KiB 28.9% data
12.1 KiB 7.7% custom:name
5.2 KiB 3.3% type
Shallow Size (top 20):
12.3 KiB 7.9% [function] core::fmt::write
8.7 KiB 5.6% [function] core::fmt::Formatter::pad
6.2 KiB 4.0% [function] <&T as core::fmt::Display>::fmt
...
slimwasm why - Explain Item Retention
Find out why a specific item is included in your binary:
Example output:
Why is 'core::fmt' in the binary?
Matching items:
-> core::fmt::write (12.3 KiB)
-> core::fmt::Formatter::pad (8.7 KiB)
Retention paths (who keeps this alive):
Path 1 to 'core::fmt::write':
└─> my_app::render_error (calls next)
└─> my_app::handle_request (calls next)
└─> _start
Tip: To reduce size, consider removing or replacing the root callers.
slimwasm diff - Compare Two Binaries
See what changed between two versions of your WASM:
Example output:
WASM Size Diff
Old: old.wasm (145.2 KiB)
New: new.wasm (156.5 KiB)
Total: 145.2 KiB -> 156.5 KiB (+11.3 KiB / +7.8%)
Changes:
Added (3):
+ 8.2 KiB [function] new_feature::process
+ 2.1 KiB [function] new_feature::validate
+ 1.0 KiB [data] data[42]
Removed (1):
- 512 B [function] deprecated_func
Changed (5):
↑ +2.3 KiB [function] main_handler
↓ -128 B [function] utility_fn
slimwasm exports - Component Export Sizes
Dump export information for WASM Component Model binaries (wasm32-wasip2/WIT):
Real-World Demo: Killing core::fmt Bloat
One of the most common sources of WASM bloat is formatting machinery from core::fmt. Here's a step-by-step demo showing how to identify and eliminate it.
Step 1: Create a "Fat" WASM Binary
Create a new project:
Add to Cargo.toml:
[]
= ["cdylib"]
[]
= "0.2"
[]
= "z"
= true
Create src/lib.rs with formatting-heavy code:
use *;
Build:
Check the size:
# => 47K pkg/wasm_demo_bg.wasm
Step 2: Analyze with slimwasm
Output (abbreviated):
wasm_demo_bg.wasm - 47.2 KiB
Sections:
38.1 KiB 80.7% code
4.2 KiB 8.9% data
3.1 KiB 6.6% custom:name
Retained Size (top 20):
12.8 KiB 27.1% [function] core::fmt::write
9.2 KiB 19.5% [function] core::fmt::Formatter::pad
4.1 KiB 8.7% [function] alloc::fmt::format::format_inner
...
Almost 50% of the binary is core::fmt machinery!
Find out why it's retained:
Output:
Retention paths (who keeps this alive):
Path 1 to 'core::fmt::write':
└─> alloc::fmt::format::format_inner (calls next)
└─> wasm_demo::greet (calls next)
└─> export:greet
The format! macro in greet() pulls in all the formatting machinery.
Step 3: Create a "Slim" Version
Create src/lib.rs without format!:
use *;
Or even simpler, use itoa crate for number-to-string conversion without core::fmt.
Rebuild:
Step 4: Compare Results
Results:
Total: 47.2 KiB -> 12.1 KiB (-35.1 KiB / -74.4%)
Removed (25):
- 12.8 KiB [function] core::fmt::write
- 9.2 KiB [function] core::fmt::Formatter::pad
- 4.1 KiB [function] alloc::fmt::format::format_inner
...
74% size reduction by eliminating core::fmt!
Additional Tips for WASM Size Reduction
-
Use
panic = "abort"in your release profile:[] = "abort" -
Enable LTO (Link-Time Optimization):
[] = true -
Optimize for size:
[] = "z" # or "s" -
Strip debug info:
[] = true -
Use wasm-opt from Binaryen:
-
Avoid these common bloat sources:
format!(),println!(),panic!()with messagesDebugtrait implementationsstd::io::Error(pulls in formatting)- Stack unwinding (use
panic = "abort")
How It Works
slimwasm parses WASM binaries using wasmparser and builds a call graph from the code section. It computes:
- Shallow size: The raw byte size of each function/item
- Retained size: Size of an item plus everything it uniquely keeps alive (via dominator analysis)
- Retention paths: The call chain from exports/roots to any given function
For Component Model binaries (wasm32-wasip2), it additionally parses component sections to show component-level exports.
License
MIT. See LICENSE-MIT.