In order to compile a minimal build – i.e. a build optimized for size – perhaps for
no-std embedded targets or for
compiling to WASM, it is essential that the correct linker flags are used in
[profile.release] lto = "fat" # turn on Link-Time Optimizations codegen-units = 1 # trade compile time with maximum optimization opt-level = "z" # optimize for size
For embedded systems that must optimize for code size, the architecture is commonly 32-bit.
only_i32 to prune away large sections of code implementing functions for other numeric types
Opt out of as many features as possible, if they are not needed, to reduce code size because, remember, by default all code is compiled into the final binary since what a script requires cannot be predicted. If a language feature will never be needed, omitting it is a prudent strategy to optimize the build for size.
Where the usage scenario does not call for loading externally-defined modules, use
save some bytes. Disable script-defined functions (
no_function) and possibly closures
no_closure) when the features are not needed. Both of these have some code size savings but not much.
For embedded scripts that are not expected to cause errors, the
no_position feature can be used
to disable position tracking during parsing. No line number/character position information is kept
for error reporting purposes. This may result in a slightly smaller build due to elimination of code
related to position tracking.
Selectively include other necessary functionalities by picking specific packages to minimize the footprint.
Packages are shared (even across threads via the
sync feature), so they only have to be created once.