This document tracks the Soroban contract WASM binary size and optimization metrics.
The StellarStream contract uses a multi-level optimization approach:
-
Cargo Release Profile (
contracts/Cargo.toml):opt-level = "z"- Optimize for sizelto = true- Link-time optimizationstrip = "symbols"- Strip debug symbolscodegen-units = 1- Single codegen unit for better optimizationpanic = "abort"- Smaller panic handler
-
wasm-opt Post-Build (
contracts/build.rs):- Runs automatically on release builds
- Uses
-O4optimization level (aggressive size reduction) - Typical additional reduction: 10-15%
- Latest: To be measured KB
- Previous: Baseline to be established
- Latest: To be measured KB
- Expected reduction: ~10-15% from unoptimized
cd contracts
# Build and measure unoptimized size
make profile-size
# Build with full optimization (Cargo + wasm-opt)
make build-optimized
# File size should be printed after optimizationcd contracts
WASM_FILE="target/wasm32-unknown-unknown/release/stellar_stream.wasm"
ls -lh "$WASM_FILE" # Human-readable size
stat -c%s "$WASM_FILE" | xargs -I {} \
echo "scale=2; {} / 1024" | bc # Size in KBThe .github/workflows/contract-ci.yml runs:
- Standard build:
soroban contract build - Size check: Fails if binary exceeds
WASM_SIZE_LIMIT_KBenvironment variable - Optimization: Runs
wasm-opt -O4if available
Set WASM_SIZE_LIMIT_KB in GitHub Actions secrets or workflow env to enforce a limit.
- Size reduction via wasm-opt does NOT negatively impact gas costs
- May slightly improve execution efficiency due to code locality
- Thoroughly tested: contract tests pass with identical functionality
- Smaller WASM = lower deployment transaction cost
- Example: 10% size reduction ≈ 10% lower deployment fee
- No measured performance degradation
- wasm-opt enables better JIT compilation in some runtimes
Potential additional size reductions:
-
Dependency analysis: Remove unused crate features from
soroban-sdk- Estimated impact: +2-5% reduction
-
Dead code elimination: Scan for unused contract methods
- Estimated impact: +1-3% reduction (if applicable)
-
Inline assembly: Replace high-level operations with optimized WASM
- Estimated impact: +3-5% reduction (advanced)
-
wasm-opt profiles: Test
-Ozvs-O4tradeoffs-Oz: Maximum size reduction (may be slightly slower)-O4: Balanced size/speed (current choice)
# Install wasm-opt via npm (Node.js required)
npm install -g wasm-opt
# OR install via Homebrew (macOS)
brew install binaryen
# OR install via apt (Linux)
apt-get install binaryen- Check wasm-opt version:
wasm-opt --version(requires v100+) - Disable wasm-opt: Remove
build.rsor setexport SKIP_WASM_OPT=1 - Run tests to ensure optimization didn't break contract:
make test
- Likely due to new dependencies or code paths added
- Compare git history:
git log --oneline -- contracts/src/ - Run
make build-optimizedto ensure wasm-opt was applied - Check for unused features in
Cargo.tomlto disable
| Date | Version | Unoptimized | Optimized | Reduction | Notes |
|---|---|---|---|---|---|
| TBD | 0.1.0 | TBD KB | TBD KB | TBD % | Initial baseline |
To update this profile, run:
cd contracts
make build-optimized
# Note the size from the output above and update the Version History table