From 1014437cc244ff518d35c64c080f4e7c1cd23a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sun, 16 Mar 2025 09:53:56 +0100 Subject: [PATCH 1/5] add some general advice on benchmarking build time --- docs/profiling.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/profiling.md b/docs/profiling.md index 689ed19bbd4c7..7e7ab027fc8b1 100644 --- a/docs/profiling.md +++ b/docs/profiling.md @@ -146,6 +146,16 @@ Graphics related work is not all CPU work or all GPU work, but a mix of both, an ## Compile time +### General advice + +- Run `cargo clean` before timing a command. +- If you are using a rustc wrapper (like `sccache`), disable it by setting `RUSTC_WRAPPER=""` +- To measure noise in duration, run commands more than once and take the average. [`hyperfine`](https://github.com/sharkdp/hyperfine) can do that for you with a cleanup between each execution (`hyperfine --cleanup "sleep 1; cargo clean" "cargo build"`). +- Avoid running benchmarks on a computer that can do power throttling or thermal throttling, like a laptop. +- Avoid running benchmarks with a processor that has different types of cores (efficiency vs performance), unless you can force the processor to use only one type of core. + +### Cargo timings + Append `--timings` to your app's cargo command (ex: `cargo build --timings`). If you want a "full" profile, make sure you run `cargo clean` first (note: this will clear previously generated reports). The command will tell you where it saved the report, which will be in your target directory under `cargo-timings/`. From 5029389236d00151e4d8aeee51482781684fe582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sun, 16 Mar 2025 09:54:26 +0100 Subject: [PATCH 2/5] rustc self-profile --- docs/profiling.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/profiling.md b/docs/profiling.md index 7e7ab027fc8b1..20deebe272759 100644 --- a/docs/profiling.md +++ b/docs/profiling.md @@ -163,3 +163,17 @@ The report is a `.html` file and can be opened and viewed in your browser. This will show how much time each crate in your app's dependency tree took to build. ![image](https://user-images.githubusercontent.com/2694663/141657811-f4e15e3b-c9fc-491b-9313-236fd8c01288.png) + +### rustc self-profile + +Cargo can generate a self-profile when building a crate. This is an unstalbe feature, but it can be used on a stable toolchain with `RUSTC_BOOTSTRAP`. + +The following command will generate a self-profile for the `bevy_render` crate: + +``` +RUSTC_BOOTSTRAP=1 cargo rustc --package bevy_render -- -Z self-profile -Z self-profile-events=default,args +``` + +This will generate a file named something like `bevy_render->.mm_profdata` in the current directory. You can convert this file to a Chrome profiler trace with [`crox`](https://github.com/rust-lang/measureme/blob/master/crox/README.md) and view the resulting trace in [perfetto](https://ui.perfetto.dev/). + +![rustc self-profile](https://github.com/user-attachments/assets/12645add-c647-4611-b533-9145cbcbac1c) From f5ca02cd2bffe6a5a5004a4d090df25b822fa759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sun, 16 Mar 2025 09:54:37 +0100 Subject: [PATCH 3/5] give cargo timings screenshot a name --- docs/profiling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/profiling.md b/docs/profiling.md index 20deebe272759..cadd2861608c8 100644 --- a/docs/profiling.md +++ b/docs/profiling.md @@ -162,7 +162,7 @@ The command will tell you where it saved the report, which will be in your targe The report is a `.html` file and can be opened and viewed in your browser. This will show how much time each crate in your app's dependency tree took to build. -![image](https://user-images.githubusercontent.com/2694663/141657811-f4e15e3b-c9fc-491b-9313-236fd8c01288.png) +![Cargo timings](https://user-images.githubusercontent.com/2694663/141657811-f4e15e3b-c9fc-491b-9313-236fd8c01288.png) ### rustc self-profile From e7d026963e03806fc6ff337453690753bfe95b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sun, 16 Mar 2025 09:54:45 +0100 Subject: [PATCH 4/5] llvm-lines and bloat --- docs/profiling.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/profiling.md b/docs/profiling.md index cadd2861608c8..0b408b83da88e 100644 --- a/docs/profiling.md +++ b/docs/profiling.md @@ -177,3 +177,11 @@ RUSTC_BOOTSTRAP=1 cargo rustc --package bevy_render -- -Z self-profile -Z self- This will generate a file named something like `bevy_render->.mm_profdata` in the current directory. You can convert this file to a Chrome profiler trace with [`crox`](https://github.com/rust-lang/measureme/blob/master/crox/README.md) and view the resulting trace in [perfetto](https://ui.perfetto.dev/). ![rustc self-profile](https://github.com/user-attachments/assets/12645add-c647-4611-b533-9145cbcbac1c) + +### cargo-llvm-lines + +[`cargo-llvm-lines`](https://github.com/dtolnay/cargo-llvm-lines) can show the number of LLVM lines generated by generic functions in your code. This can show you how much code is generated by each function, which can help you identify potential build performance issues. + +### cargo-bloat + +[`cargo-bloat`](https://github.com/RazrFalcon/cargo-bloat) can show the size of each function in your code. This can help you identify large functions that ends up in the final binary. From 972c97ea4611d175f6fd8e5d69f0e8560826e40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sun, 16 Mar 2025 12:13:50 +0100 Subject: [PATCH 5/5] ci --- docs/profiling.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/profiling.md b/docs/profiling.md index 0b408b83da88e..6d7c37a8e9772 100644 --- a/docs/profiling.md +++ b/docs/profiling.md @@ -59,6 +59,7 @@ For more details, check out the [tracing span docs](https://docs.rs/tracing/*/tr ### Tracy profiler The [Tracy profiling tool](https://github.com/wolfpld/tracy) is: + > A real time, nanosecond resolution, remote telemetry, hybrid frame and sampling profiler for games and other applications. There are binaries available for Windows, and installation / build instructions for other operating systems can be found in the [Tracy documentation PDF](https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf). @@ -166,11 +167,11 @@ This will show how much time each crate in your app's dependency tree took to bu ### rustc self-profile -Cargo can generate a self-profile when building a crate. This is an unstalbe feature, but it can be used on a stable toolchain with `RUSTC_BOOTSTRAP`. +Cargo can generate a self-profile when building a crate. This is an unstable feature, but it can be used on a stable toolchain with `RUSTC_BOOTSTRAP`. The following command will generate a self-profile for the `bevy_render` crate: -``` +```sh RUSTC_BOOTSTRAP=1 cargo rustc --package bevy_render -- -Z self-profile -Z self-profile-events=default,args ```