MoosicBox Bloaty
A binary size analysis tool for Rust workspace packages that helps track and compare the size impact of features.
Overview
The MoosicBox Bloaty package provides binary size analysis for Rust packages in a workspace. It measures the size impact of individual features by building packages with different feature combinations and comparing the resulting library sizes.
Features
- Feature size analysis: Measures the size impact of each feature on rlib targets
- Multiple output formats: Supports text, JSON, and JSONL report formats
- Package filtering: Select specific packages using patterns or explicit lists
- Feature filtering: Skip features using patterns or explicit lists
- Workspace integration: Analyzes all workspace members automatically
- External tool integration: Supports cargo-bloat, cargo-llvm-lines, and cargo-size
Installation
From Source
# Clone the repository
# Build the tool
# The binary will be at target/release/bloaty
Usage
Basic Usage
Analyze all workspace packages:
Package Selection
Analyze specific packages:
# Single package
# Multiple packages
# Using patterns
Feature Filtering
Skip specific features:
# Skip specific features
# Skip features matching a pattern
External Tools
Run external analysis tools (requires installation):
# Run cargo-bloat
# Run multiple tools
Output Formats
Control report output:
# Generate all formats (default)
# Only text report
# JSON and JSONL reports
# Custom report filename
This generates files like:
my_analysis.txt(text report)my_analysis.json(complete JSON report)my_analysis.jsonl(streaming JSONL format)
Skip Packages
Exclude packages from analysis:
# Skip specific packages
# Skip packages matching pattern
Report Format
Text Report
The text report shows base sizes and feature impact:
Package: moosicbox_core
===================
Target: moosicbox_core
-------------------
Base size: 1.2 MB
Feature: async | Size: 1.3 MB | Diff: +100 KB
Feature: db | Size: 1.5 MB | Diff: +300 KB
JSON Report
The JSON report provides structured data:
JSONL Report
The JSONL report provides streaming event data for real-time processing:
{"type":"package_start","name":"moosicbox_core","timestamp":1234567890}
{"type":"target_start","package":"moosicbox_core","target":"moosicbox_core","timestamp":1234567890}
{"type":"base_size","package":"moosicbox_core","target":"moosicbox_core","size":1258291,"timestamp":1234567890}
{"type":"feature","package":"moosicbox_core","target":"moosicbox_core","feature":"async","size":1360384,"diff":102093,"timestamp":1234567890}
{"type":"target_end","package":"moosicbox_core","target":"moosicbox_core","timestamp":1234567890}
{"type":"package_end","name":"moosicbox_core","timestamp":1234567890}
Dependencies
Core dependencies (automatically managed by Cargo):
anyhow- Error handlingbytesize- Human-readable size formattingcargo_metadata- Workspace metadata accessclap- Command-line argument parsingglob- File pattern matchinglog- Logging facadepretty_env_logger- Logging implementationregex- Pattern matching for filtersserde_json- JSON output formattingswitchy_time- Time utilities
Optional external tools:
cargo-bloat- For detailed binary bloat analysiscargo-llvm-lines- For LLVM IR line count analysiscargo-size- For size profiling
How It Works
- Package Discovery: Uses
cargo_metadatato find workspace members - Feature Extraction: Identifies available features from each package's
Cargo.toml - Baseline Build: Builds each target with no features enabled and measures size
- Feature Analysis: Builds each target with individual features and compares sizes
- Binary Tool Execution: For binary, cdylib, and dylib targets, runs selected external cargo analysis tools
- Report Generation: Outputs results in requested formats
Contributing
Contributions are welcome! Areas for improvement:
- Add support for feature combinations (currently only analyzes individual features)
- Implement historical size tracking and regression detection
- Add visualization of size trends over time
- Optimize build performance with better caching
- Add more detailed breakdown of size contributors