A Wasm size profiler for MoonBit — section breakdown, function ranking, call graph and retained-size analysis
Dependencies
moon add BigSaltyMan/moonsizemoon build --release
./_build/native/release/build/cmd/main/main.exe app.wasmusage: moonsize <file.wasm> [--top <n>] [--retained] [--dead-code]
[--compress] [--call-graph <path>] [--html <path>]
[--max-size <size>] [--baseline <path.wasm>]
Report the section table of a WebAssembly binary, rank its heaviest
functions, roll the bytes up by module, and follow the call graph to
find what can be deleted.
--top <n> how many functions to rank (default 10)
--retained what deleting each function would free
--dead-code functions no root can reach
--compress what the file and each section cost gzip-compressed
--call-graph <path> write the call graph as .dot or .json
--html <path> write the charts as a self-contained HTML report
--max-size <size> fail with exit code 3 above this size, where size is
bytes or a KB/MB/GB count such as 10KB or 1.5MB
--baseline <path> compare against another module and print the change;
--max-size then bounds the growth, not the file
Exit codes: 0 ok, 1 unreadable input, 2 bad command line, 3 over budget.moonsize app.wasm --retained --dead-code # the full text report
moonsize app.wasm --html report.html # charts, opens in a browser
moonsize app.wasm --call-graph graph.dot # for Graphvizfn main {
let bytes : Bytes = read("app.wasm")
match @moonsize.parse_module(bytes) {
Ok(parsed) =>
match @moonsize.analyze(bytes, parsed) {
Ok(analysis) => {
let options = @moonsize.Options::default("app.wasm")
// What the five heaviest functions cost, as text or as numbers:
// `analysis.stats` and `analysis.retained` are there to be read.
println(@moonsize.render_top_functions(analysis.stats, analysis.data.length(), 5))
println(@moonsize.render_html(analysis, options.path))
}
Err(error) => println(@moonsize.describe_error(error))
}
Err(error) => println(@moonsize.describe_error(error))
}
}COMPRESSED SIZE
raw 10675 B
gzip 5207 B (48.7% of raw, ratio 2.05x)
BY SECTION
SECTION RAW GZIP RATIO SHARE(GZIP)
code 5166 2712 1.90x 52.0%
custom (name) 4975 1983 2.50x 38.0%
data 252 174 1.44x 3.3%
custom (producers) 71 95 0.74x 1.8%
type 59 66 0.89x 1.2%
function 50 63 0.79x 1.2%
import 37 61 0.60x 1.1%
export 21 45 0.46x 0.8%
global 13 34 0.38x 0.6%
element 8 32 0.25x 0.6%
table 7 31 0.22x 0.5%
memory 5 29 0.17x 0.5%
datacount 3 27 0.11x 0.5%Retained size
# INDEX SIZE RETAINED DIES SHARE IND FUNCTION
1 47 158 5162 46 48.3% ____moonbit__main
2 39 301 1773 10 16.6% int::Int::to__string_2einner
3 37 24 887 8 8.3% println
4 34 9 819 5 7.6% moonbit.println
5 33 206 810 4 7.5% moonbit.fprintln
6 28 49 717 7 6.7% moonbit.decref
7 29 409 668 6 6.2% moonbit.gc.free
8 43 633 633 0 5.9% int__to__string__dec
DIES counts the other functions that become unreachable with this one.
IND marks a function a call_indirect could reach.
Dead code
0 of 47 functions are unreachable from the roots
0 bytes (0.0% of file)
every function is reachable
moonsize app.wasm --max-size 100KB # a ceiling on the file
moonsize app.wasm --baseline main.wasm --max-size 5KB # at most 5 KB of growthSIZE COMPARISON
baseline current delta
total 10675 11039 +364 (+3.4%)
code 5166 5299 +133 (+2.5%)
custom (name) 4975 5205 +230 (+4.6%)
data 252 252 0 (0.0%)
custom (producers) 71 71 0 (0.0%)
...- uses: moonbit-community/setup-moonbit@v1
- name: The example stays inside its budget
run: moon run cmd/main -- --max-size 100KB examples/fib.wasm- uses: moonbit-community/setup-moonbit@v1
- run: moon build --target wasm --release
- name: Size budget
run: moonsize _build/wasm/release/build/app/app.wasm --max-size 250KB- name: Build the base branch
run: |
git worktree add "$RUNNER_TEMP/base" "origin/${{ github.base_ref }}"
(cd "$RUNNER_TEMP/base" && moon build --target wasm --release)
- name: Size budget
run: |
moonsize _build/wasm/release/build/app/app.wasm \
--baseline "$RUNNER_TEMP/base/_build/wasm/release/build/app/app.wasm" \
--max-size 5KBmoon build --target wasm # -> examples wasm
moon run cmd/main -- that.wasm --baseline base.wasm --max-size 5KBmoon check --target native --deny-warn
moon testmoon build --target wasm
moon run cmd/main -- --html examples/report.html examples/fib.wasm
# then re-capture examples/report.png from that pagepub struct Analysis {
data : Bytes
parsed : WasmModule
stats : Array[FunctionStat]
graph : CallGraph
roots : Set[Int]
reachable : Set[Int]
retained : Array[RetainedSize]
indirect : Set[Int]
}pub struct Export {
name : String
kind : Int
index : Int
}pub struct FunctionBody {
index : Int
body_size : Int
total_size : Int
offset : Int
}pub struct FunctionStat {
index : Int
name : String
module_name : String
body_size : Int
total_size : Int
offset : Int
}pub struct IndirectSite {
caller : Int
table_index : Int
type_index : Int
offset : Int
candidates : Array[Int]
}pub enum Opcode {
Unreachable
Nop
Block
Loop
If
Else
Try
Catch
Throw
Rethrow
ThrowRef
End
Br
BrIf
BrTable
Return
Call
CallIndirect
ReturnCall
ReturnCallIndirect
CallRef
ReturnCallRef
Drop
Select
Delegate
CatchAll
SelectT
LocalGet
LocalSet
LocalTee
GlobalGet
GlobalSet
TableGet
TableSet
I32Load
I64Load
F32Load
F64Load
I32Load8S
I32Load8U
I32Load16S
I32Load16U
I64Load8S
I64Load8U
I64Load16S
I64Load16U
I64Load32S
I64Load32U
I32Store
I64Store
F32Store
F64Store
I32Store8
I32Store16
I64Store8
I64Store16
I64Store32
MemorySize
MemoryGrow
I32Const
I64Const
F32Const
F64Const
I32Eqz
I32Eq
I32Ne
I32LtS
I32LtU
I32GtS
I32GtU
I32LeS
I32LeU
I32GeS
I32GeU
I64Eqz
I64Eq
I64Ne
I64LtS
I64LtU
I64GtS
I64GtU
I64LeS
I64LeU
I64GeS
I64GeU
F32Eq
F32Ne
F32Lt
F32Gt
F32Le
F32Ge
F64Eq
F64Ne
F64Lt
F64Gt
F64Le
F64Ge
I32Clz
I32Ctz
I32Popcnt
I32Add
I32Sub
I32Mul
I32DivS
I32DivU
I32RemS
I32RemU
I32And
I32Or
I32Xor
I32Shl
I32ShrS
I32ShrU
I32Rotl
I32Rotr
I64Clz
I64Ctz
I64Popcnt
I64Add
I64Sub
I64Mul
I64DivS
I64DivU
I64RemS
I64RemU
I64And
I64Or
I64Xor
I64Shl
I64ShrS
I64ShrU
I64Rotl
I64Rotr
F32Abs
F32Neg
F32Ceil
F32Floor
F32Trunc
F32Nearest
F32Sqrt
F32Add
F32Sub
F32Mul
F32Div
F32Min
F32Max
F32Copysign
F64Abs
F64Neg
F64Ceil
F64Floor
F64Trunc
F64Nearest
F64Sqrt
F64Add
F64Sub
F64Mul
F64Div
F64Min
F64Max
F64Copysign
I32WrapI64
I32TruncF32S
I32TruncF32U
I32TruncF64S
I32TruncF64U
I64ExtendI32S
I64ExtendI32U
I64TruncF32S
I64TruncF32U
I64TruncF64S
I64TruncF64U
F32ConvertI32S
F32ConvertI32U
F32ConvertI64S
F32ConvertI64U
F32DemoteF64
F64ConvertI32S
F64ConvertI32U
F64ConvertI64S
F64ConvertI64U
F64PromoteF32
I32ReinterpretF32
I64ReinterpretF64
F32ReinterpretI32
F64ReinterpretI64
I32Extend8S
I32Extend16S
I64Extend8S
I64Extend16S
I64Extend32S
RefNull
RefIsNull
RefFunc
RefEq
RefAsNonNull
BrOnNull
BrOnNonNull
I32TruncSatF32S
I32TruncSatF32U
I32TruncSatF64S
I32TruncSatF64U
I64TruncSatF32S
I64TruncSatF32U
I64TruncSatF64S
I64TruncSatF64U
MemoryInit
DataDrop
MemoryCopy
MemoryFill
TableInit
ElemDrop
TableCopy
TableGrow
TableSize
TableFill
StructNew
StructNewDefault
StructGet
StructGetS
StructGetU
StructSet
ArrayNew
ArrayNewDefault
ArrayNewFixed
ArrayNewData
ArrayNewElem
ArrayGet
ArrayGetS
ArrayGetU
ArraySet
ArrayLen
ArrayFill
ArrayCopy
ArrayInitData
ArrayInitElem
RefTest
RefTestNull
RefCast
RefCastNull
BrOnCast
BrOnCastFail
AnyConvertExtern
ExternConvertAny
RefI31
I31GetS
I31GetU
}pub enum Operands {
None_
BlockType
Label
LabelTable
FunctionIndex
CallIndirect
LocalIndex
GlobalIndex
TableIndex
TagIndex
MemoryIndex
MemoryArg
I32Const
I64Const
F32Const
F64Const
HeapType
RefType
TypeIndex
TypeFieldPair
TypePair
TypeCountPair
TypeDataPair
TypeElemPair
DataIndex
ElemIndex
DataMemoryPair
MemoryPair
ElemTablePair
TablePair
CastPair
TypeVector
}pub struct Options {
path : String
top : Int
retained : Bool
dead_code : Bool
call_graph : String?
html : String?
max_size : Int?
baseline : String?
compress : Bool
}pub struct Reader {
data : Bytes
pos : Int
}pub struct RetainedSize {
index : Int
own_size : Int
retained : Int
retained_functions : Int
}pub struct Section {
id : Int
kind : SectionId
offset : Int
payload_size : Int
total_size : Int
custom_name : String?
}pub struct SectionCompression {
label : String
raw : Int
gzip : Int
}pub enum SectionId {
Custom
Type
Import
Function
Table
Memory
Global
Export
Start
Element
Code
Data
DataCount
Tag
}pub struct TableType {
element_type : Int
minimum : Int
maximum : Int?
}pub enum WasmError {
UnexpectedEnd(Int, Int)
BadMagic
InvalidSectionId(Int)
LebOverflow(Int)
InvalidImportKind(Int)
UnknownOpcode(Int, Int)
UnknownPrefixedOpcode(Int, Int, Int)
}fn build(data : Bytes, bodies : Array[FunctionBody], elements : Array[ElementSegment]) -> Result[CallGraph, WasmError]fn collect_function_stats(parsed : WasmModule, data : Bytes) -> Result[Array[FunctionStat], WasmError]fn compress_gzip(data : Bytes) -> Result[Bytes, String]fn compressed_size(data : Bytes) -> Intfn decode_instructions(bytes : Bytes, body_offset : Int, body_size : Int) -> Result[Array[Instruction], WasmError]fn demangle_full(symbol : String) -> Stringfn display_name(name : String) -> Stringfn module_of(name : String) -> Stringfn parse_code_section(data : Bytes, section : Section, base_index? : Int) -> Result[Array[FunctionBody], WasmError]fn parse_count(text : String) -> Int?fn parse_element_section(data : Bytes, section : Section) -> Result[Array[ElementSegment], WasmError]fn parse_size(text : String) -> Int?fn render_compression(raw_size : Int, gzip_size : Int, sections : Array[SectionCompression]) -> Stringfn render_dead_code(stats : Array[FunctionStat], reachable : Set[Int], file_size : Int, top : Int) -> Stringfn render_json(path : String, stats : Array[FunctionStat], graph : CallGraph, roots : Set[Int], reachable : Set[Int], retained : Array[RetainedSize]) -> Stringfn render_retained(stats : Array[FunctionStat], retained : Array[RetainedSize], indirect : Set[Int], file_size : Int, top : Int) -> Stringfn retained_sizes(graph : CallGraph, sizes : Map[Int, Int], roots : Set[Int]) -> Array[RetainedSize]fn usage() -> StringInstall
Download zipA Wasm size profiler for MoonBit — section breakdown, function ranking, call graph and retained-size analysis
Dependencies