Skip to content

Commit 2178663

Browse files
Remove bevy_core (#16897)
# Objective - Fixes #16892 ## Solution - Removed `TypeRegistryPlugin` (`Name` is now automatically registered with a default `App`) - Moved `TaskPoolPlugin` to `bevy_app` - Moved `FrameCountPlugin` to `bevy_diagnostic` - Deleted now-empty `bevy_core` ## Testing - CI ## Migration Guide - `TypeRegistryPlugin` no longer exists. If you can't use a default `App` but still need `Name` registered, do so manually with `app.register_type::<Name>()`. - References to `TaskPoolPlugin` and associated types will need to import it from `bevy_app` instead of `bevy_core` - References to `FrameCountPlugin` and associated types will need to import it from `bevy_diagnostic` instead of `bevy_core` ## Notes This strategy was agreed upon by Cart and several other members in [Discord](https://discord.com/channels/691052431525675048/692572690833473578/1319137218312278077).
1 parent d4b07a5 commit 2178663

File tree

39 files changed

+304
-318
lines changed

39 files changed

+304
-318
lines changed

assets/scenes/load_scene_example.scn.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
entities: {
88
4294967296: (
99
components: {
10-
"bevy_core::name::Name": (
10+
"bevy_ecs::name::Name": (
1111
hash: 17588334858059901562,
1212
name: "joe",
1313
),

crates/bevy_animation/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ keywords = ["bevy"]
1313
bevy_app = { path = "../bevy_app", version = "0.15.0-dev" }
1414
bevy_asset = { path = "../bevy_asset", version = "0.15.0-dev" }
1515
bevy_color = { path = "../bevy_color", version = "0.15.0-dev" }
16-
bevy_core = { path = "../bevy_core", version = "0.15.0-dev" }
1716
bevy_derive = { path = "../bevy_derive", version = "0.15.0-dev" }
1817
bevy_log = { path = "../bevy_log", version = "0.15.0-dev" }
1918
bevy_math = { path = "../bevy_math", version = "0.15.0-dev" }

crates/bevy_app/Cargo.toml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,20 @@ std = [
5555

5656
## `critical-section` provides the building blocks for synchronization primitives
5757
## on all platforms, including `no_std`.
58-
critical-section = ["bevy_tasks?/critical-section", "bevy_ecs/critical-section"]
58+
critical-section = [
59+
"portable-atomic?/critical-section",
60+
"bevy_tasks?/critical-section",
61+
"bevy_ecs/critical-section",
62+
]
5963

6064
## `portable-atomic` provides additional platform support for atomic types and
6165
## operations, even on targets without native support.
62-
portable-atomic = ["bevy_tasks?/portable-atomic", "bevy_ecs/portable-atomic"]
66+
portable-atomic = [
67+
"dep:portable-atomic",
68+
"dep:portable-atomic-util",
69+
"bevy_tasks?/portable-atomic",
70+
"bevy_ecs/portable-atomic",
71+
]
6372

6473
[dependencies]
6574
# bevy
@@ -77,6 +86,12 @@ thiserror = { version = "2", default-features = false }
7786
variadics_please = "1.1"
7887
tracing = { version = "0.1", default-features = false, optional = true }
7988
log = { version = "0.4", default-features = false }
89+
portable-atomic = { version = "1", default-features = false, features = [
90+
"fallback",
91+
], optional = true }
92+
portable-atomic-util = { version = "0.2.4", features = [
93+
"alloc",
94+
], optional = true }
8095

8196
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
8297
ctrlc = { version = "3.4.4", optional = true }
@@ -86,6 +101,9 @@ wasm-bindgen = { version = "0.2" }
86101
web-sys = { version = "0.3", features = ["Window"] }
87102
console_error_panic_hook = "0.1.6"
88103

104+
[dev-dependencies]
105+
crossbeam-channel = "0.5.0"
106+
89107
[lints]
90108
workspace = true
91109

crates/bevy_app/src/app.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ impl Default for App {
103103
app.sub_apps.main.update_schedule = Some(Main.intern());
104104

105105
#[cfg(feature = "bevy_reflect")]
106-
app.init_resource::<AppTypeRegistry>();
106+
{
107+
app.init_resource::<AppTypeRegistry>();
108+
app.register_type::<Name>();
109+
}
107110

108111
#[cfg(feature = "reflect_functions")]
109112
app.init_resource::<AppFunctionRegistry>();

crates/bevy_app/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ mod plugin;
2424
mod plugin_group;
2525
mod schedule_runner;
2626
mod sub_app;
27+
#[cfg(feature = "bevy_tasks")]
28+
mod task_pool_plugin;
2729
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
2830
mod terminal_ctrl_c_handler;
2931

@@ -34,6 +36,8 @@ pub use plugin::*;
3436
pub use plugin_group::*;
3537
pub use schedule_runner::*;
3638
pub use sub_app::*;
39+
#[cfg(feature = "bevy_tasks")]
40+
pub use task_pool_plugin::*;
3741
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
3842
pub use terminal_ctrl_c_handler::*;
3943

@@ -52,4 +56,8 @@ pub mod prelude {
5256
sub_app::SubApp,
5357
Plugin, PluginGroup,
5458
};
59+
60+
#[cfg(feature = "bevy_tasks")]
61+
#[doc(hidden)]
62+
pub use crate::{NonSendMarker, TaskPoolOptions, TaskPoolPlugin};
5563
}

crates/bevy_core/src/task_pool_options.rs renamed to crates/bevy_app/src/task_pool_plugin.rs

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
1+
#![cfg_attr(
2+
feature = "portable-atomic",
3+
expect(
4+
clippy::redundant_closure,
5+
reason = "portable_atomic_util::Arc has subtly different implicit behavior"
6+
)
7+
)]
8+
9+
use crate::{App, Last, Plugin};
10+
11+
use alloc::string::ToString;
12+
use bevy_ecs::prelude::*;
113
use bevy_tasks::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool, TaskPoolBuilder};
2-
use bevy_utils::tracing::trace;
14+
use core::{fmt::Debug, marker::PhantomData};
15+
use log::trace;
16+
17+
#[cfg(feature = "portable-atomic")]
18+
use portable_atomic_util::Arc;
319

20+
#[cfg(not(feature = "portable-atomic"))]
421
use alloc::sync::Arc;
5-
use core::fmt::Debug;
22+
23+
#[cfg(not(target_arch = "wasm32"))]
24+
use bevy_tasks::tick_global_task_pools_on_main_thread;
25+
26+
/// Setup of default task pools: [`AsyncComputeTaskPool`], [`ComputeTaskPool`], [`IoTaskPool`].
27+
#[derive(Default)]
28+
pub struct TaskPoolPlugin {
29+
/// Options for the [`TaskPool`](bevy_tasks::TaskPool) created at application start.
30+
pub task_pool_options: TaskPoolOptions,
31+
}
32+
33+
impl Plugin for TaskPoolPlugin {
34+
fn build(&self, _app: &mut App) {
35+
// Setup the default bevy task pools
36+
self.task_pool_options.create_default_pools();
37+
38+
#[cfg(not(target_arch = "wasm32"))]
39+
_app.add_systems(Last, tick_global_task_pools);
40+
}
41+
}
42+
/// A dummy type that is [`!Send`](Send), to force systems to run on the main thread.
43+
pub struct NonSendMarker(PhantomData<*mut ()>);
44+
45+
/// A system used to check and advanced our task pools.
46+
///
47+
/// Calls [`tick_global_task_pools_on_main_thread`],
48+
/// and uses [`NonSendMarker`] to ensure that this system runs on the main thread
49+
#[cfg(not(target_arch = "wasm32"))]
50+
fn tick_global_task_pools(_main_thread_marker: Option<NonSend<NonSendMarker>>) {
51+
tick_global_task_pools_on_main_thread();
52+
}
653

754
/// Defines a simple way to determine how many threads to use given the number of remaining cores
855
/// and number of total cores
@@ -37,7 +84,14 @@ impl TaskPoolThreadAssignmentPolicy {
3784
/// Determine the number of threads to use for this task pool
3885
fn get_number_of_threads(&self, remaining_threads: usize, total_threads: usize) -> usize {
3986
assert!(self.percent >= 0.0);
40-
let mut desired = (total_threads as f32 * self.percent).round() as usize;
87+
let proportion = total_threads as f32 * self.percent;
88+
let mut desired = proportion as usize;
89+
90+
// Equivalent to round() for positive floats without libm requirement for
91+
// no_std compatibility
92+
if proportion - desired as f32 >= 0.5 {
93+
desired += 1;
94+
}
4195

4296
// Limit ourselves to the number of cores available
4397
desired = desired.min(remaining_threads);
@@ -50,7 +104,7 @@ impl TaskPoolThreadAssignmentPolicy {
50104
}
51105

52106
/// Helper for configuring and creating the default task pools. For end-users who want full control,
53-
/// set up [`TaskPoolPlugin`](super::TaskPoolPlugin)
107+
/// set up [`TaskPoolPlugin`]
54108
#[derive(Clone, Debug)]
55109
pub struct TaskPoolOptions {
56110
/// If the number of physical cores is less than `min_total_threads`, force using
@@ -208,3 +262,42 @@ impl TaskPoolOptions {
208262
}
209263
}
210264
}
265+
266+
#[cfg(test)]
267+
mod tests {
268+
use super::*;
269+
use bevy_tasks::prelude::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool};
270+
271+
#[test]
272+
fn runs_spawn_local_tasks() {
273+
let mut app = App::new();
274+
app.add_plugins(TaskPoolPlugin::default());
275+
276+
let (async_tx, async_rx) = crossbeam_channel::unbounded();
277+
AsyncComputeTaskPool::get()
278+
.spawn_local(async move {
279+
async_tx.send(()).unwrap();
280+
})
281+
.detach();
282+
283+
let (compute_tx, compute_rx) = crossbeam_channel::unbounded();
284+
ComputeTaskPool::get()
285+
.spawn_local(async move {
286+
compute_tx.send(()).unwrap();
287+
})
288+
.detach();
289+
290+
let (io_tx, io_rx) = crossbeam_channel::unbounded();
291+
IoTaskPool::get()
292+
.spawn_local(async move {
293+
io_tx.send(()).unwrap();
294+
})
295+
.detach();
296+
297+
app.run();
298+
299+
async_rx.try_recv().unwrap();
300+
compute_rx.try_recv().unwrap();
301+
io_rx.try_recv().unwrap();
302+
}
303+
}

crates/bevy_asset/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ js-sys = "0.3"
6565
notify-debouncer-full = { version = "0.4.0", optional = true }
6666

6767
[dev-dependencies]
68-
bevy_core = { path = "../bevy_core", version = "0.15.0-dev" }
6968
bevy_log = { path = "../bevy_log", version = "0.15.0-dev" }
7069

7170
[lints]

crates/bevy_asset/src/asset_changed.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ mod tests {
296296
use core::num::NonZero;
297297

298298
use crate::{AssetApp, Assets};
299-
use bevy_app::{App, AppExit, Last, Startup, Update};
300-
use bevy_core::TaskPoolPlugin;
299+
use bevy_app::{App, AppExit, Last, Startup, TaskPoolPlugin, Update};
301300
use bevy_ecs::schedule::IntoSystemConfigs;
302301
use bevy_ecs::{
303302
component::Component,

crates/bevy_asset/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,7 @@ mod tests {
633633
AssetPlugin, AssetServer, Assets,
634634
};
635635
use alloc::sync::Arc;
636-
use bevy_app::{App, Update};
637-
use bevy_core::TaskPoolPlugin;
636+
use bevy_app::{App, TaskPoolPlugin, Update};
638637
use bevy_ecs::{
639638
event::EventCursor,
640639
prelude::*,

crates/bevy_core/Cargo.toml

Lines changed: 0 additions & 42 deletions
This file was deleted.

crates/bevy_core/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)