Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use -Z threads=0 option in config_fast_builds.toml #11541

Merged
merged 2 commits into from
Jan 29, 2024

Conversation

doonv
Copy link
Contributor

@doonv doonv commented Jan 26, 2024

Objective

Improve compile times.

Solution

The -Z threads=0 option has been introduced into nightly rust somewhat recently, and was showcased in this rust-lang article. This option improves multithreading in rust and speeds up compilation. I added this option to config_fast_builds.toml so others can also use this option to improve compile times.

-Z threads=0 automatically uses the same amount of threads as the amount of threads in your system (See rustc source code).

Benchmarks

Disclaimer: This section was written before I knew of -Z threads=0, so it uses -Z threads=8 instead.

I compiled bevy with/without the -Z threads=8 and saw about a 7% improvement in compliation times on my Kubuntu system with a 13th Gen Intel® Core™ i5-13400.

Also the compile times go down over time, probably because I had other things running in the background.

Without -Z threads=8

  • 42.33s
  • 40.90s
  • 38.27s
  • 38.07s
  • 37.17s
  • 37.67s
  • 36.63s
  • 37.24s

Average: 38.535

With -Z threads=8

  • 36.77s
  • 39.50s
  • 38.86s
  • 35.61s
  • 34.37s
  • 34.32s
  • 34.44s
  • 33.74s

Average: 35.95125

@@ -16,6 +17,7 @@ rustflags = [
rustflags = [
"-Clink-arg=-fuse-ld=/usr/local/opt/llvm/bin/ld64.lld", # Use LLD Linker
"-Zshare-generics=y", # (Nightly) Make the current crate share its generic instantiations
"-Zthreads=8", # (Nightly) Use improved multithreading with 8 threads.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any MacOS users test if this works well? It might reduce performance on MacOS or even not work at all.

Copy link
Member

@BD103 BD103 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing it on my M1 Mac, this option actually seems to slow down the compilation process. I built with the --timings option to generate the following charts. The time increases from 58.4 seconds to 63.6 seconds.

Before:

image image

After:

image image

Edit: Redid timings so the only difference between "Before" and "After" was commenting out -Zthreads=0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an average or just one test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was just one test. Here's my results running cargo clean && cargo +nightly build with and without -Zthreads=0 commented out.

Before

  • 1:03s
  • 1:06s
  • 1:11s
  • 1:12s
  • 1:08s
  • 0:59.4s
  • 1:03s

Average: 1:06.05s

After

  • 1:05s
  • 1:11s
  • 1:13s
  • 1:20s
  • 1:07s
  • 1:11s
  • 1:12s

Average: 1:11.29s

linker = "rust-lld.exe" # Use LLD Linker
rustflags = [
"-Zshare-generics=n",
"-Zthreads=8", # (Nightly) Use improved multithreading with 8 threads.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows users should also test this out

@matiqo15 matiqo15 added C-Performance A change motivated by improving speed, memory usage or compile times A-App Bevy apps and plugins labels Jan 26, 2024
@alice-i-cecile alice-i-cecile added A-Meta About the project itself C-Docs An addition or correction to our documentation and removed A-App Bevy apps and plugins labels Jan 27, 2024
@Bcompartment
Copy link

I suggest to:

-Z threads= u128::MAX

With this we can compile instantly, I guess ....

But seriously talking, why 8 ? Is there any way to automatically set it to max (best) number possible for given CPU?

@doonv
Copy link
Contributor Author

doonv commented Jan 27, 2024

why 8 ?

That's the recommended amount.

Is there any way to automatically set it to max (best) number possible for given CPU?

After reading some rustc source code, I realised that threads=0 uses the amount of threads in your system (16 for me). And it performs similarly.

@doonv doonv changed the title Use -Z threads=8 option in config_fast_builds.toml Use -Z threads=0 option in config_fast_builds.toml Jan 27, 2024
@james7132 james7132 added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 29, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 29, 2024
Merged via the queue into bevyengine:main with commit d7c65e4 Jan 29, 2024
23 checks passed
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
)

# Objective

Improve compile times.

## Solution

The `-Z threads=0` option has been introduced into nightly rust somewhat
recently, and was showcased in this [rust-lang
article](https://blog.rust-lang.org/2023/11/09/parallel-rustc.html).
This option improves multithreading in rust and speeds up compilation. I
added this option to `config_fast_builds.toml` so others can also use
this option to improve compile times.

`-Z threads=0` automatically uses the same amount of threads as the
amount of threads in your system (See [rustc source
code](https://github.com/rust-lang/rust/blob/6b4f1c5e782c72a047a23e922decd33e7d462345/compiler/rustc_session/src/options.rs#L617)).

### Benchmarks

> **Disclaimer:** This section was written before I knew of `-Z
threads=0`, so it uses `-Z threads=8` instead.

I compiled bevy with/without the `-Z threads=8` and saw about a 7%
improvement in compliation times on my Kubuntu system with a 13th Gen
Intel® Core™ i5-13400.

Also the compile times go down over time, probably because I had other
things running in the background.

#### Without `-Z threads=8` 

- 42.33s
- 40.90s
- 38.27s
- 38.07s
- 37.17s
- 37.67s
- 36.63s
- 37.24s

**Average**: 38.535

#### With `-Z threads=8`

- 36.77s
- 39.50s
- 38.86s
- 35.61s
- 34.37s
- 34.32s
- 34.44s
- 33.74s

**Average**: 35.95125
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Meta About the project itself C-Docs An addition or correction to our documentation C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants