This document describes the WASM compression implementation for SubStream Protocol Contracts, designed to reduce deployment fees on the Stellar network by optimizing contract binary sizes.
The WASM compression system uses wasm-opt from the Binaryen toolkit to apply aggressive optimizations to contract binaries, resulting in smaller WASM files that cost less to deploy on Stellar.
- Aggressive Optimization: Uses
wasm-opt -Ozwith additional optimization flags - Size Reporting: Shows before/after file sizes and compression percentages
- CI/CD Integration: Automated compression in GitHub Actions
- Flexible Configuration: Customizable optimization levels and output directories
- Multiple Contracts: Handles all WASM files in the target directory
- Binaryen: Provides
wasm-optfor WASM optimization - Stellar CLI: Builds the contracts to WASM
- Make: Automation of build and compression steps
-
Install Binaryen:
# macOS brew install binaryen # Ubuntu/Debian sudo apt-get install binaryen # Other platforms # Download from https://github.com/WebAssembly/binaryen/releases
-
Verify installation:
wasm-opt --version
The GitHub Actions workflow automatically installs Binaryen for WASM optimization.
# Build and compress in one step
cd contracts/substream_contracts
make build-compressed
# Or build first, then compress
make build
make build-compressed# Basic usage
./scripts/compress_wasm.sh
# With custom options
./scripts/compress_wasm.sh \
--contract-dir contracts/substream_contracts \
--output-dir target/compressed \
--optimization-level Oz
# Show help
./scripts/compress_wasm.sh --helpAvailable optimization levels for wasm-opt:
O0: No optimization (fastest compilation)O1: Basic optimizationO2: More optimizationO3: Aggressive optimizationOs: Optimize for sizeOz: Optimize for size aggressively (recommended for deployment)
The compression applies these optimization flags:
-Oz: Aggressive size optimization--vacuum: Remove redundant items--dae: Dead code elimination--remove-unused-names: Remove unused names--remove-unused-types: Remove unused types--merge-blocks: Merge blocks--simplify-locals: Simplify local variables--coalesce-locals: Coalesce local variables
contracts/substream_contracts/
├── target/
│ ├── wasm32v1-none/release/
│ │ └── substream_contracts.wasm # Original WASM
│ └── compressed/
│ └── substream_contracts.optimized.wasm # Compressed WASM
├── Makefile # Build automation
└── src/
└── lib.rs # Contract source
The GitHub Actions workflow (.github/workflows/test.yml) includes:
- Binaryen Installation: Installs
wasm-optand related tools - Contract Building: Builds the contract using Stellar CLI
- WASM Compression: Runs the compression process
- Artifact Upload: Saves compressed WASM files as workflow artifacts
- name: Install Binaryen for WASM optimization
run: |
sudo apt-get update
sudo apt-get install -y binaryen
- name: Build and Compress WASM
run: |
cd contracts/substream_contracts
make build-compressed
- name: Upload Compressed WASM files
uses: actions/upload-artifact@v3
with:
name: compressed-wasm
path: contracts/substream_contracts/target/compressed/Based on similar Stellar contracts, you can expect:
- Size Reduction: 10-30% smaller WASM files
- Deployment Cost: Proportional reduction in deployment fees
- Runtime Performance: Minimal to no impact on execution speed
- Gas Costs: No increase in transaction gas costs
🚀 SubStream Protocol WASM Compression Script
=============================================
🔨 Building contract...
📦 Compressing WASM files with optimization level: Oz
Optimizing substream_contracts.wasm...
✅ Original: 45678 bytes, Compressed: 34234 bytes, Reduction: 25%
🎉 Compression complete!
📊 Summary:
Total original size: 45678 bytes
Total compressed size: 34234 bytes
Total reduction: 25%
Compressed files saved to: target/compressed
✨ Done! Your optimized WASM files are ready for deployment.
- Regular Builds: Use
make buildduring development - Pre-deployment: Always use
make build-compressedbefore deployment - Size Monitoring: Track compression ratios over time
- Testing: Deploy compressed WASM to testnet first
- Code Review: Smaller source code often results in smaller WASM
- Dependencies: Minimize external dependencies
- Feature Flags: Use conditional compilation for unused features
- Profile: Use
cargo bloatto identify large dependencies
# Install Binaryen
brew install binaryen # macOS
sudo apt-get install binaryen # Ubuntu# Check Rust targets
rustup target add wasm32v1-none
rustup target add wasm32-unknown-unknown# Make script executable
chmod +x scripts/compress_wasm.shFor debugging, use lower optimization levels:
./scripts/compress_wasm.sh --optimization-level O1# Build compressed WASM
make build-compressed
# Deploy using compressed file
stellar contract deploy \
--wasm-file target/compressed/substream_contracts.optimized.wasm \
--source-account your_account \
--network testnetThe compressed WASM files can be automatically deployed using the workflow artifacts:
- Download
compressed-wasmartifact from GitHub Actions - Extract the optimized WASM files
- Deploy using your preferred deployment tool
When contributing to the compression system:
- Test compression ratios with your changes
- Verify that compressed contracts still function correctly
- Update documentation for any new optimization flags
- Consider the impact on deployment costs
This WASM compression implementation is part of the SubStream Protocol Contracts project and follows the same license terms.