Skip to content

Simplify bevy_tasks Features #19091

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 1 commit into
base: main
Choose a base branch
from

Conversation

bushrat011899
Copy link
Contributor

Objective

Now that bevy_platform::cfg is merged, we can start tidying up features. This PR starts with bevy_tasks.

Solution

  • Removed critical-section feature (it was just a re-export of bevy_platform anyway)
  • Removed std and web features, relying on bevy_platform::cfg to check for availability.
  • Added futures-lite feature to provide access to the block_on implementation from futures-lite.
  • Added a fallback implementation of block_on that just busy-waits.
  • Moved wasm-bindgen related dependencies out of bevy_tasks and moved them into bevy_platform under a new exports module.
  • Made async-io implicit feature explicit.

Testing

  • CI

Comment on lines -27 to +28
Self(receiver.into_future())
Self(receiver)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This did nothing, just an identity function. I'm not sure why it was being called other than just nobody noticed.

Comment on lines +54 to +63
/// Re-exports of crates that are useful across Bevy.
/// Not intended for external crates to use.
#[doc(hidden)]
pub mod exports {
crate::cfg::web! {
pub use js_sys;
pub use wasm_bindgen;
pub use wasm_bindgen_futures;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This addition is what will allow the web feature in particular to be brought under control. But there is room for adding other fundamental crates too (e.g., maybe serde to remove serialize features, etc.).

A similar technique could be used to remove bevy_reflect features as well.

@@ -43,7 +47,14 @@ critical-section = ["dep:critical-section", "portable-atomic/critical-section"]

## Enables use of browser APIs.
## Note this is currently only applicable on `wasm32` architectures.
web = ["dep:web-time", "dep:getrandom"]
web = [
"std",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed that thanks to web-time, our web feature isn't no_std at this moment. There is no_std support on their GitHub repo already merged though, so hopefully we can bring that functionality soon.

It's frustrating, because ideally we would just enable web-time iff std and web are enabled, but there is no way to do that with Cargo features at this time.

Comment on lines +21 to +24
#[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))] => {
/// Indicates multithreading support.
multi_threaded
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since aliases can be defined for arbitrary cfg statements, we can build in that multi_threaded only works on non-WASM, rather than needing to repeat this entire statement. Should make downstream consumption nicer too.

Comment on lines +125 to +145
/// Blocks on the supplied `future`.
/// This implementation will busy-wait until it is completed.
/// Consider enabling the `async-io` or `futures-lite` features.
pub fn block_on<T>(future: impl Future<Output = T>) -> T {
use core::task::{Poll, Context};

// Pin the future on the stack.
let mut future = core::pin::pin!(future);

// We don't care about the waker as we're just going to poll as fast as possible.
let waker = futures::noop_waker();
let cx = &mut Context::from_waker(&waker);

// Keep polling until the future is ready.
loop {
match future.as_mut().poll(cx) {
Poll::Ready(output) => return output,
Poll::Pending => core::hint::spin_loop(),
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is new functionality just to make the feature gates less hideous. Now consumers can always use block_on, the features just improve performance for particular environments.

@janhohenheim janhohenheim 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 D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes and removed D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels May 10, 2025
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-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants