This is a template wrapper that leverages Cargo's xtask pattern to manage powerful customized build commands.
This template heavelly relies on the nightly toolchain.
You can run these custom workflows using cargo xtask <subcommand> (assuming you've setup a .cargo/config.toml alias) or cargo run --manifest-path xtask/Cargo.toml -- <subcommand>.
Usage:
cargo xtask --help
Usage: xtask <COMMAND>
Commands:
fast-dev Run dev build with fast linking. Syntax: -- [cargo-args] -- [program-args]
min-size Build release optimized for minimum binary size. Syntax: -- [cargo-args]
speed Build release optimized for execution speed. Syntax: -- [cargo-args]
clippy Run clippy with fast-dev flags
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
EXTRA ARGS SYNTAX:
cargo xtask <PROFILE> [profile-flags] -- [cargo-args] -- [program-args]
EXAMPLES:
cargo xtask fast-dev -- --features foo -- --verbose
cargo xtask min-size --upx -- --features bar --package my-crate
cargo xtask speed --native -- --features foo -- --ignoredThe template supports three heavily optimized profiles:
Command: cargo xtask fast-dev and cargo xtask clippy
Purpose: Maximize compilation speed during repetitive local development cycles.
- It dynamically assigns the fastest available linking backends:
rust-lldon Windows andwildon Linux. - Compiles via the cranelift codegen backend (
-Zcodegen-backend=cranelift), enabling multithreading (-Zthreads=32) to achieve extremely quick iteration speeds. - Does not care about final binary size or performance metrics; it purely cares about compile time.
Optimization Tips:
- OS Constraints: Ensure you have installed the respective linkers on your system (such as
wildfor Linux) or else the build invocation might fail. Windows will automatically userust-lldwhich is included with nightly Cargo. - Advanced Windows Tips: For even faster link times on Windows, consider using
radlinker. Additionally, moving your project to a Windows Dev Drive (which builds on the ReFS file system) will significantly improve your overall disk I/O and parallel compilation times. You can also create an Antivirus exclusion for your project folder, which will significantly reduce the overhead for spawning processes. - You may consider removing the cranelift backend flag if some specific crates you rely on do not support it properly yet (looking at you
aws-lc-rs). - Use
sccachefor caching. - If you don't care about debug symbols, you can also add
debug = 0andstrip = "debuginfo"to your Cargo profile configuration to further speed up compilation. - If you heavily use procedural macros in your project (e.g., if you use serde), it might be worth it to play around with opt-levels in your Cargo.toml [1]:
[profile.dev.build-override]
opt-level = 3Command: cargo xtask min-size [--target <target>] [--upx]
Purpose: Achieve the smallest possible final binary size.
- Drops out the standard library panics and debugging symbols (
-Zbuild-std=std,panic_abort,trim-paths=true). - Employs size-specific compiler features (
optimize_for_size) along with link-time optimization (-Clto=true) and aggressive packing (pack-relative-relocs). - Optionally accepts a
--upxflag to do an ultra-brute compression pass via the UPX compressor.
Optimization Tips:
- Best Use Case: Perfect for distributing binaries over the network, lightweight standalone tools, or Tauri wrappers where payload weight translates to consumer friction.
Other Targets
- I haven't yet tested these configurations for wasm.
Command: cargo xtask speed [--target <target>] [--native]
Purpose: Compile for absolute maximum execution speed at runtime, ignoring binary size and compilation time.
- Same hermetic
-Zbuild-stdrebuild strategy asmin-size, skipping pre-compiled std artifacts for potentially deeper cross-crate optimization. - Bumps compilation bounds directly to
-Copt-level=3and applies heavy--lto=fat. --native: Append this flag (cargo xtask speed --native) to instruct rustc to deploy cpu-specific instructions unique to your host CPU architecture (-Ctarget-cpu=native).
Optimization Tips:
- Caveat for
--native: Only use--nativeif you plan to run the software on the EXACT SAME machine that compiled it! Distribution to other machines will frequently result in immediate SIGILL crashes if their processors lack the newer instruction features (e.g. AVX-512). - Use alternative allocators like
mimallocfor better performance. - Use alternative hashers like
foldhashfor better performance.
You can easily adapt this template if you're using alternative frameworks, such as Tauri. In xtask/src/main.rs, modify the global constants to point to the desired workflow:
const RUN_CMD: &[&str] = &["cargo", "tauri", "dev"];
const BUILD_CMD: &[&str] = &["cargo", "tauri", "build"];
// Update `BINARY_NAME` accordingly!
// you can also specify the cargo profile if you want to use custom ones (e.g. `--profile=release-lto` or `--profile=min-size`).- https://nnethercote.github.io/perf-book
- https://github.com/johnthagen/min-sized-rust
- https://corrode.dev/blog/tips-for-faster-rust-compile-times/
- Test wasm targets, especially with
wasm-optpost-processing for size and speed profiles. - Add
leptosandtrunkspecific support.