Skip to content

Assert a minimum thread count for TaskPoolOptions #17771

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

jmskov
Copy link

@jmskov jmskov commented Feb 10, 2025

Objective

TaskPool policies assign one thread per group, so three threads are assigned at minimum. Adds an assertion for a minimum thread count for TaskPoolOptions. Passing 0, 1 or 2 for thread count has no real effect.

Testing

  • Module tests pass

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Tasks Tools for parallel and async work D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 10, 2025
@@ -159,6 +159,7 @@ impl Default for TaskPoolOptions {
impl TaskPoolOptions {
/// Create a configuration that forces using the given number of threads.
pub fn with_num_threads(thread_count: usize) -> Self {
assert!(thread_count > 2);
Copy link
Member

@alice-i-cecile alice-i-cecile Feb 10, 2025

Choose a reason for hiding this comment

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

This needs a comment about why it's being set; ideally in the message to users.

add panic docstring

Co-authored-by: Alice Cecile <[email protected]>
@mockersf
Copy link
Member

why an assert rather than

  • an assert_dbg?
  • returning an Option/Result?
  • ignoring values below 3, and using 3 by default in those cases?

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 10, 2025
@jmskov
Copy link
Author

jmskov commented Feb 11, 2025

why an assert rather than

  • an assert_dbg?
  • returning an Option/Result?
  • ignoring values below 3, and using 3 by default in those cases?

I opened this after using the with_num_threads method at the plugin setup stage. I was confused why 1, 2 (and 0) all worked the same (effectively defaulted to 3) until I looked at the source. Since this method is exposed all the time, assert was the simplest way to avoid this ambiguity for others.

Not sure if debug_assert is more or less appropriate. 😄

Edit: Or a warning, just so the user knows what is happening.

@mockersf
Copy link
Member

Edit: Or a warning, just so the user knows what is happening.

I would prefer the warning I think.

An assert usually means "if the assert fail, it wouldn't have been possible to continue executing the code in a safe way", it's not the case here

@alice-i-cecile
Copy link
Member

Seconding the warning; I think that's a more appropriate failure level.

@jmskov
Copy link
Author

jmskov commented Feb 11, 2025

Is it possible to raise a warning (or other message for that matter) from a constructor that is passed to add_plugins, i.e. from with_num_threads below?

.add_plugins(DefaultPlugins.set(TaskPoolPlugin {
            task_pool_options: TaskPoolOptions::with_num_threads(4),
        }))

I've tried both log::warn and tracing::warn, and neither work here. Both work in the create_default_pools system called later.

@jmskov
Copy link
Author

jmskov commented Feb 11, 2025

I suppose it has to do with both TaskPoolPlugin and LogPlugin being in the DefaultPlugins set, because adding plugins after this can raise log messages using a constructor method. At this point, maybe it's best just to add a note to the documentation of with_num_threads and call it a day 😊

@alice-i-cecile
Copy link
Member

I suppose it has to do with both TaskPoolPlugin and LogPlugin being in the DefaultPlugins set, because adding plugins after this can raise log messages using a constructor method. At this point, maybe it's best just to add a note to the documentation of with_num_threads and call it a day 😊

You can probably swap the ordering of the two plugins and make this work properly :)

@jmskov
Copy link
Author

jmskov commented Feb 12, 2025

Updated to a warning.

You can probably swap the ordering of the two plugins and make this work properly :)

I can raise the warning if I add plugins separately, but the following does not raise a warning (or any trace, debug, etc.)

.add_plugins(DefaultPlugins.set(TaskPoolPlugin {    // This does not raise a warning!
             task_pool_options: TaskPoolOptions::with_num_threads(1),
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Tasks Tools for parallel and async work C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants