WASM programs need to be small to be deployed onchain. Stylus applies brotli compression, which empirically reduces the footprint of common Rust WASMs by over 50%, the Stylus runtime obeys the EVM contract size limit of 24KB. This means that, after compression, all WASMs must not exceed 24KB.
Uncompressed WASM files also have an enforced limit of 128Kb.
We are actively working on improving WASM sizes generated by Rust code with the Stylus SDK. This document includes some built-in ways of optimizing your code and some third-party recommendations.
On modern platforms, tools like cargo
don’t have to worry about the size of the binaries they produce. This is because there’s many orders of magnitude more storage available than even the largest of binaries, and for most applications it’s media like images and videos that constitutes the majority of the footprint.
Nevertheless, systems programming languages compete for viability in the OS and embedded space, where resource constraints are extremely strict. Hence, while not the default options, tooling often provides mechanisms for reducing binary bloat. This document seeks to explain these options so that Stylus programmers can write apps that are affordable to deploy.
The Rust compiler supports various config options for shrinking binary sizes.
[profile.release]
codegen-units = 1 # prefer efficiency to compile time
panic = "abort" # use simple panics
opt-level = "z" # optimize for size ("s" may also work)
strip = true # remove debug info
lto = true # link time optimization
debug = false # no debug data
rpath = false # no run-time search path
debug-assertions = false # prune debug assertions
incremental = false # no incremental builds
[build]
target = "wasm32-unknown-unknown"
[target.wasm32-unknown-unknown]
rustflags = [
"-C", "link-arg=-zstack-size=8192", # shrink the heap
]
Additional unstable nightly flags may help too.
cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort
The above configures the standard library to not include panic strings, which are useless on chain but may take up considerable space.
cargo stylus deploy --nightly
Additional wasm-specific tooling exists to shrink binaries. Due to being 3rd party, users should use these at their own risk.
wasm-opt applies techniques to further reduce binary size, usually netting around 10%.