[pr-performance] Profile-guided release build: scripts\pgo.ps1, and the instrumented build writes its profile at the bench exit #65

Manually merged
TrustNo1 merged 0 commits from iridiumlotus/Darner:pgo-build into main 2026-12-03 18:37:01 +00:00
Contributor

Contributor role (optional)

  • Fluxer ID: 1472859084623200260

What it changes

Makes a profile-guided build of the release exe possible, and adds a script for it. Profile-guided optimisation tells the compiler which code ran, and how often, in a run of the game; it lays out and inlines the program by that. No source differs between the plain and the guided exe, and nothing changes in a build that does not ask for it. (This was pull request 344 on CodeFloe, open when that host went away: the same commit, rebased on main 295621e.)

What was in the way: an instrumented build (-Cprofile-generate) writes its profile from an atexit handler, and no way this program ends runs one. Closing the window and the settings menu end with TerminateProcess, and DARNER_EXIT_AFTER_WORLD_S ends with std::process::exit, which is ExitProcess on Windows. An instrumented run left no profile of the game at all.

  • crates/darner-gpu/src/host.rs, exit_after_world: under #[cfg(darner_pgo_gen)] the bench exit calls the profile runtime's writer (__llvm_profile_write_file) before it ends the process, and logs whether it wrote. The cfg is declared for check-cfg in crates/darner-gpu/build.rs.
  • scripts\pgo.ps1 instrument builds with -Cprofile-generate --cfg darner_pgo_gen; then the game is run with DARNER_EXIT_AFTER_WORLD_S=<seconds> (every run adds to the same profile; with scripts\play-nvidia.ps1 that is -Build release, as it starts the fast build of #21 otherwise); scripts\pgo.ps1 use merges the profile with llvm-profdata and builds with -Cprofile-use. -TargetDir keeps target\release\darner.exe as it is. The header has the steps.
  • docs/README-nvidia.md: two lines under Build.

Why a cfg in RUSTFLAGS and not a cargo feature: a feature changes the crates' symbol names, the profile is matched by them, and the profile of a build with the feature did not apply to the build without it (that exe built in six minutes and ran like the plain one). RUSTFLAGS do not change the names.

How it was tested

  • Tested revision: this branch (one commit on main 295621e) for the checks below. The game was run with the same hook and the same script on a tree at main 28ef3dd plus local launcher commits, and before that at 58a9c07; not on this branch's base.
  • Commands and results: cargo clippy -p darner-gpu --all-targets --all-features --locked -- -D warnings reports nothing; cargo xtask policy passes; cargo check -p darner-gpu --locked with RUSTFLAGS=--cfg darner_pgo_gen compiles the hook. cargo test not run: without the cfg no code differs.
  • Gameplay: RTX 5080 16 GB, Ryzen 7 5800X3D, 32 GB, Windows 11. Series S, 1920x1080 output, DLSS Balanced, frame generation off, ray tracing off. Toolchain as pinned; llvm-profdata from an LLVM 19.1.7 install read the raw profile and rustc took the merged one.
  • Before and after, plain and guided release exes of one tree, alternated in one session, the same save. As the draws differ between runs (the idle camera cuts between views), I fit ms/frame = a + b x draws + c x [guided] over the 5-second reports; c is given with one standard error.
tree, profile scene frame time, guided minus plain list translation per call
main 28ef3dd, standing 140 s + walk 330 s standing, 3 pairs -1.24 ms +- 0.21 (-3.6 %) -8.9 %
main 28ef3dd, same profile recorded walk of 330 s, 1 pair -2.98 ms +- 0.64 (-7.6 %) -8.5 %
main 58a9c07, one standing run of 140 s standing, 2 pairs -1.77 ms +- 0.30 (-5.3 %) -9.7 %
main 58a9c07, same profile the walk (not in that profile), 1 pair -1.18 ms +- 0.49 (-3.6 %) -8.3 %

Frame rates of the three standing pairs at 28ef3dd: guided 31.0 / 31.3 / 30.5 fps, plain 27.9 / 29.6 / 29.5 fps (7,200 to 9,200 draws a frame).

  • No errors in any of these logs; screenshots of the runs showed no change to the picture.
  • Costs, measured: the instrumented exe runs at about 19 to 21 fps where the plain one runs at 31. The instrumented build took about 7 min (plain release build: about 6 min). The build with the profile took 45 to 69 min (one compiler thread in the fat LTO step, about 3 GB). The exe grows from about 20 MB to about 22 MB.
  • Also tried on top, no gain: -Ctarget-cpu=native with the profile (two pairs: -0.42 ms +- 0.21 a frame).
  • Shader wave sizes tested: not a shader change.
  • Coverage gaps: AMD or Intel cards, Series X, ray tracing, frame generation, driving or flying, sessions longer than ten minutes; how well a profile holds over many commits (a function that changed loses its counts, with a warning); the script's first choice of llvm-profdata, the toolchain's own from rustup component add llvm-tools (not installed here; the one in Program Files\LLVM was used); Linux (the script is PowerShell; where exit runs atexit handlers the hook is not needed).

Checklist

  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none for the same thing among the 52 open ones (titles and bodies searched for "PGO", "profile-guided", "profile-generate", "profile-use"). Nearest, both merged in main already: #21 (the fast build profile; the release profile, which this builds, is as it was, and the script's header now says the guided exe is the one -Build release starts) and #28 (the log flushed before the title's own exit; another exit path, not exit_after_world).

🤖 Generated with Claude Code

## Contributor role (optional) - Fluxer ID: 1472859084623200260 ## What it changes Makes a profile-guided build of the release exe possible, and adds a script for it. Profile-guided optimisation tells the compiler which code ran, and how often, in a run of the game; it lays out and inlines the program by that. No source differs between the plain and the guided exe, and nothing changes in a build that does not ask for it. (This was pull request 344 on CodeFloe, open when that host went away: the same commit, rebased on main 295621e.) What was in the way: an instrumented build (`-Cprofile-generate`) writes its profile from an atexit handler, and no way this program ends runs one. Closing the window and the settings menu end with `TerminateProcess`, and `DARNER_EXIT_AFTER_WORLD_S` ends with `std::process::exit`, which is `ExitProcess` on Windows. An instrumented run left no profile of the game at all. - `crates/darner-gpu/src/host.rs`, `exit_after_world`: under `#[cfg(darner_pgo_gen)]` the bench exit calls the profile runtime's writer (`__llvm_profile_write_file`) before it ends the process, and logs whether it wrote. The cfg is declared for check-cfg in `crates/darner-gpu/build.rs`. - `scripts\pgo.ps1 instrument` builds with `-Cprofile-generate --cfg darner_pgo_gen`; then the game is run with `DARNER_EXIT_AFTER_WORLD_S=<seconds>` (every run adds to the same profile; with `scripts\play-nvidia.ps1` that is `-Build release`, as it starts the fast build of #21 otherwise); `scripts\pgo.ps1 use` merges the profile with llvm-profdata and builds with `-Cprofile-use`. `-TargetDir` keeps `target\release\darner.exe` as it is. The header has the steps. - `docs/README-nvidia.md`: two lines under Build. Why a cfg in RUSTFLAGS and not a cargo feature: a feature changes the crates' symbol names, the profile is matched by them, and the profile of a build with the feature did not apply to the build without it (that exe built in six minutes and ran like the plain one). RUSTFLAGS do not change the names. ## How it was tested - Tested revision: this branch (one commit on main 295621e) for the checks below. The game was run with the same hook and the same script on a tree at main 28ef3dd plus local launcher commits, and before that at 58a9c07; not on this branch's base. - Commands and results: `cargo clippy -p darner-gpu --all-targets --all-features --locked -- -D warnings` reports nothing; `cargo xtask policy` passes; `cargo check -p darner-gpu --locked` with `RUSTFLAGS=--cfg darner_pgo_gen` compiles the hook. `cargo test` not run: without the cfg no code differs. - Gameplay: RTX 5080 16 GB, Ryzen 7 5800X3D, 32 GB, Windows 11. Series S, 1920x1080 output, DLSS Balanced, frame generation off, ray tracing off. Toolchain as pinned; llvm-profdata from an LLVM 19.1.7 install read the raw profile and rustc took the merged one. - Before and after, plain and guided release exes of one tree, alternated in one session, the same save. As the draws differ between runs (the idle camera cuts between views), I fit ms/frame = a + b x draws + c x [guided] over the 5-second reports; c is given with one standard error. | tree, profile | scene | frame time, guided minus plain | list translation per call | |---|---|---|---| | main 28ef3dd, standing 140 s + walk 330 s | standing, 3 pairs | -1.24 ms +- 0.21 (-3.6 %) | -8.9 % | | main 28ef3dd, same profile | recorded walk of 330 s, 1 pair | -2.98 ms +- 0.64 (-7.6 %) | -8.5 % | | main 58a9c07, one standing run of 140 s | standing, 2 pairs | -1.77 ms +- 0.30 (-5.3 %) | -9.7 % | | main 58a9c07, same profile | the walk (not in that profile), 1 pair | -1.18 ms +- 0.49 (-3.6 %) | -8.3 % | Frame rates of the three standing pairs at 28ef3dd: guided 31.0 / 31.3 / 30.5 fps, plain 27.9 / 29.6 / 29.5 fps (7,200 to 9,200 draws a frame). - No errors in any of these logs; screenshots of the runs showed no change to the picture. - Costs, measured: the instrumented exe runs at about 19 to 21 fps where the plain one runs at 31. The instrumented build took about 7 min (plain release build: about 6 min). The build with the profile took 45 to 69 min (one compiler thread in the fat LTO step, about 3 GB). The exe grows from about 20 MB to about 22 MB. - Also tried on top, no gain: `-Ctarget-cpu=native` with the profile (two pairs: -0.42 ms +- 0.21 a frame). - Shader wave sizes tested: not a shader change. - Coverage gaps: AMD or Intel cards, Series X, ray tracing, frame generation, driving or flying, sessions longer than ten minutes; how well a profile holds over many commits (a function that changed loses its counts, with a warning); the script's first choice of llvm-profdata, the toolchain's own from `rustup component add llvm-tools` (not installed here; the one in Program Files\LLVM was used); Linux (the script is PowerShell; where exit runs atexit handlers the hook is not needed). ## Checklist - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none for the same thing among the 52 open ones (titles and bodies searched for "PGO", "profile-guided", "profile-generate", "profile-use"). Nearest, both merged in main already: #21 (the fast build profile; the release profile, which this builds, is as it was, and the script's header now says the guided exe is the one `-Build release` starts) and #28 (the log flushed before the title's own exit; another exit path, not `exit_after_world`). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Profile-guided optimisation tells the compiler which code ran, and how
often, in a run of the game, and it lays out and inlines the program by
that. No source differs between the plain and the guided exe.

What was in the way: an instrumented build (-Cprofile-generate) writes its
profile from an atexit handler, and no way this program ends runs one.
Closing the window and the settings menu end with TerminateProcess, and
DARNER_EXIT_AFTER_WORLD_S ends with std::process::exit, which is
ExitProcess on Windows. So an instrumented run left no profile of the game.

- crates/darner-gpu/src/host.rs: under `#[cfg(darner_pgo_gen)]` the bench
  exit (DARNER_EXIT_AFTER_WORLD_S) calls the profile runtime's writer before
  it ends the process. A cfg passed in RUSTFLAGS and not a cargo feature: a
  feature changes the crates' symbol names, the profile is matched by them,
  and a profile from a build with a feature did not apply to the build
  without it (tried: that exe built in six minutes and ran like the plain
  one). Declared for check-cfg in crates/darner-gpu/build.rs.
- scripts\pgo.ps1 instrument / use: the instrumented build, then the merge
  of the profile and the build with it. Its header has the steps.
- docs/README-nvidia.md: two lines under Build.

Nothing changes in a build without the cfg.
TrustNo1 manually merged commit 1a5286a78d into main 2026-12-03 18:37:01 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!65
No description provided.