A Rust game is unplayably slow in Cargo's dev profile; how do profile overrides speed it up while keeping your own crates easy to debug?
answer
- dependencies rarely rebuild
- a package table under the profile
- the star means non-members
- generics compile where they are used
basics
~20 sAdd [profile.dev.package."*"] with opt-level = 3 to the root Cargo.toml: every non-member dependency is optimised once, while your workspace crates stay at opt-level 0 with full debug info. Raise [profile.dev] opt-level to 1 if your own code is also too slow.
solid answer
~40 sProfile **overrides** let one profile use different settings per package. In the workspace root I would write `[profile.dev.package."*"]` with `opt-level = 3`: the `"*"` matches every dependency that is not a workspace member, so engine, physics and image crates get optimised once and then cached, because dependencies rarely rebuild. My own crates keep the `dev` defaults, `opt-level = 0` and full debug info, so edit-compile cycles stay quick and the debugger works. If my own code is the hot spot, `[profile.dev] opt-level = 1` helps without wrecking debugging. A named table such as `[profile.dev.package.engine]` targets one crate and beats `"*"`. Overrides cannot set `panic`, `lto` or `rpath`, and generic code from a dependency is compiled with the settings of the crate that instantiates it.
code
toml · 9 lines[workspace]
members = ["game", "engine"]
resolver = "3"
[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3go deeper
Recall that the dev profile does no optimisation and that a [profile.dev.package."*"] table in the root Cargo.toml can optimise dependencies alone.
Explain the override tables, named, star and build-override, their precedence, and why optimising dependencies costs little after the first build.
Diagnose why an override did nothing: generics instantiated in your crate, a table in a member manifest, or a key overrides cannot carry; verify with cargo build -v.
Decide the team's dev-profile defaults for a performance-sensitive codebase so playtests are fast without giving up debugging or incremental builds.
## Why a game crawls in a dev build A **profile** is the set of compiler settings Cargo applies to a build, and `cargo run` uses the `dev` profile, whose `opt-level = 0` means no optimisation at all. For most programs that is fine. A game is different: it runs physics, rendering and maths in tight loops sixty times a second, and unoptimised iterator chains, bounds checks and un-inlined small functions can make a frame take many times longer than in a release build. Switching to `cargo run --release` fixes the speed but costs debug info and incremental rebuilds of your own code. ## Overrides: different settings for different packages Cargo lets a profile carry per-package **overrides** in the workspace root manifest: - `[profile.dev.package.NAME]` changes settings for one package (the name is a Package ID Spec, so `"foo:2.1.0"` targets one version). - `[profile.dev.package."*"]` changes settings for **every package that is not a workspace member** — in practice, all your dependencies. - `[profile.dev.build-override]` changes settings for build scripts, proc macros and their dependencies. The usual fix for a slow game is the `"*"` table with `opt-level = 3` (or `2`). Dependencies are compiled once and cached, so you pay the optimisation cost on the first build and rarely again, while your own crates keep fast incremental builds and full debug info. ## Which entry wins When several tables could apply to one crate, Cargo takes the first match in this order: 1. `[profile.dev.package.name]` — a named package; 2. `[profile.dev.package."*"]` — any non-member; 3. `[profile.dev.build-override]` — build scripts, proc macros and their dependencies; 4. `[profile.dev]` — the profile itself; 5. Cargo's built-in defaults. A build run with `cargo build -v` makes the result visible. With `[profile.dev] opt-level = 1` and `"*"` at `3`, every dependency (`regex`, `regex_syntax`, `regex_automata`, `aho_corasick`, `memchr`) received `-C opt-level=3`, the two workspace members received `-C opt-level=1`, and every crate kept `-C debuginfo=2`, because the override changed only `opt-level`. ## Limits worth knowing | limit | what happens | |---|---| | `panic`, `lto`, `rpath` in an override | Cargo rejects the manifest, for example: `` `panic` may not be specified in a `package` profile `` | | override in a member's own Cargo.toml | ignored with a warning; profiles are read only from the workspace root | | build dependencies | `build-override` may force a crate shared with normal dependencies to be built twice | | generic code | compiled in the crate that instantiates it, with that crate's settings | The last row is the subtle one. If a maths library is mostly generic types, such as vector and matrix types over a type parameter, then the code for your concrete `f32` vectors is generated **in your crate**, at your crate's `opt-level`. Raising the library's level may then change little. The Cargo book adds that at `opt-level` 2 or 3 a crate neither shares its monomorphised generics nor reuses other crates' copies, and suggests trying `opt-level = 1` when tuning dependencies for development; for generic-heavy code, raising your own crate to `1` is often what actually helps. ## A working setup - In the root manifest, `[profile.dev.package."*"]` with `opt-level = 3`. - Optionally `[profile.dev]` with `opt-level = 1` for your own crates; debug info stays on. - If one of your own crates is the hot path, a named override such as `[profile.dev.package.engine]` with `opt-level = 3` for that crate only. - If build scripts or proc macros are slow to *run*, `[profile.dev.build-override]` with a higher `opt-level`; by default all profiles build them at `opt-level = 0`. ## Confirming it worked An override that silently does nothing is the common failure, so check rather than assume: 1. Run `cargo build -v` (after `cargo clean -p NAME` if a crate is already up to date) and read the `-C opt-level=` value on each crate's `rustc` line. 2. Confirm the tables sit in the **workspace root** manifest; a member's copy is ignored with the warning `profiles for the non root package will be ignored`. 3. Compare frame times before and after on the same scene; if nothing changed, the hot code is probably generic code compiled in your own crate. Because dependency artifacts are cached per profile setting, changing the override triggers a one-off rebuild of every dependency; after that, day-to-day rebuilds touch only your own crates again. Nothing here touches the release profile: playtests stay on `cargo run`, with breakpoints and line numbers intact in your own code.
- Why might raising one generic-heavy maths crate to `opt-level = 3` barely help?Generic code is compiled where it is instantiated. Your concrete vector and matrix types are generated in your own crate, at your crate's `opt-level`, so the override on the library mostly affects its non-generic code. Raising your own crate, often to `opt-level = 1`, usually does more.
- How do you make build scripts and proc macros run faster in a dev build?Use `[profile.dev.build-override]` with a higher `opt-level`. By default every profile builds them at `opt-level = 0` to keep compile time low. The cost: a crate used both as a normal and as a build dependency may now be compiled twice, once per setting.
- Can you turn on LTO for one heavy dependency with an override?No. Cargo rejects `lto`, `panic` and `rpath` inside a `package` override with an error such as `` `lto` may not be specified in a `package` profile ``. LTO and the panic strategy act on the final linked artifact, so they belong on the profile as a whole.
- [profile.dev.package."*"] also optimises the workspace's own crates.
- An override can enable lto for one heavy dependency.
- Raising a generic library's opt-level always speeds up code that uses it.
- A dependency can ship its own profile override that Cargo applies.
- Overrides switch off debug info for the overridden crates.
- The only fix for a slow dev game is cargo run --release.