Skip to content

Commit 9efed83

Browse files
committed
no cows on this farm
1 parent 655b3eb commit 9efed83

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

crates/bevy_app/src/app.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use bevy_ecs::{
44
prelude::*,
55
schedule::{
66
apply_state_transition, common_conditions::run_once as run_once_condition,
7-
run_enter_schedule, BoxedScheduleLabel, IntoSystemConfig, IntoSystemSetConfigs,
8-
ScheduleLabel,
7+
run_enter_schedule, IntoSystemConfig, IntoSystemSetConfigs, ScheduleId, ScheduleLabel,
98
},
109
};
1110
use bevy_utils::{tracing::debug, HashMap, HashSet};
@@ -69,11 +68,11 @@ pub struct App {
6968
/// The schedule that systems are added to by default.
7069
///
7170
/// This is initially set to [`CoreSchedule::Main`].
72-
pub default_schedule_label: BoxedScheduleLabel,
71+
pub default_schedule_label: ScheduleId,
7372
/// The schedule that controls the outer loop of schedule execution.
7473
///
7574
/// This is initially set to [`CoreSchedule::Outer`].
76-
pub outer_schedule_label: BoxedScheduleLabel,
75+
pub outer_schedule_label: ScheduleId,
7776
sub_apps: HashMap<AppLabelId, SubApp>,
7877
plugin_registry: Vec<Box<dyn Plugin>>,
7978
plugin_name_added: HashSet<String>,
@@ -163,7 +162,7 @@ impl SubApp {
163162
pub fn run(&mut self) {
164163
self.app
165164
.world
166-
.run_schedule_ref(&*self.app.outer_schedule_label);
165+
.run_schedule_by_id(self.app.outer_schedule_label);
167166
self.app.world.clear_trackers();
168167
}
169168

@@ -223,8 +222,8 @@ impl App {
223222
sub_apps: HashMap::default(),
224223
plugin_registry: Vec::default(),
225224
plugin_name_added: Default::default(),
226-
default_schedule_label: Box::new(CoreSchedule::Main),
227-
outer_schedule_label: Box::new(CoreSchedule::Outer),
225+
default_schedule_label: ScheduleId::of(&CoreSchedule::Main),
226+
outer_schedule_label: ScheduleId::of(&CoreSchedule::Outer),
228227
is_building_plugin: false,
229228
}
230229
}
@@ -245,7 +244,7 @@ impl App {
245244
{
246245
#[cfg(feature = "trace")]
247246
let _bevy_frame_update_span = info_span!("main app").entered();
248-
self.world.run_schedule_ref(&*self.outer_schedule_label);
247+
self.world.run_schedule_by_id(self.outer_schedule_label);
249248
}
250249
for (_label, sub_app) in self.sub_apps.iter_mut() {
251250
#[cfg(feature = "trace")]
@@ -375,7 +374,7 @@ impl App {
375374
pub fn add_system<P>(&mut self, system: impl IntoSystemConfig<P>) -> &mut Self {
376375
let mut schedules = self.world.resource_mut::<Schedules>();
377376

378-
if let Some(default_schedule) = schedules.get_mut(&*self.default_schedule_label) {
377+
if let Some(default_schedule) = schedules.get_mut_by_id(self.default_schedule_label) {
379378
default_schedule.add_system(system);
380379
} else {
381380
let schedule_label = &self.default_schedule_label;
@@ -403,7 +402,7 @@ impl App {
403402
pub fn add_systems<P>(&mut self, systems: impl IntoSystemConfigs<P>) -> &mut Self {
404403
let mut schedules = self.world.resource_mut::<Schedules>();
405404

406-
if let Some(default_schedule) = schedules.get_mut(&*self.default_schedule_label) {
405+
if let Some(default_schedule) = schedules.get_mut_by_id(self.default_schedule_label) {
407406
default_schedule.add_systems(systems);
408407
} else {
409408
let schedule_label = &self.default_schedule_label;
@@ -498,7 +497,7 @@ impl App {
498497
pub fn configure_set(&mut self, set: impl IntoSystemSetConfig) -> &mut Self {
499498
self.world
500499
.resource_mut::<Schedules>()
501-
.get_mut(&*self.default_schedule_label)
500+
.get_mut_by_id(self.default_schedule_label)
502501
.unwrap()
503502
.configure_set(set);
504503
self
@@ -508,7 +507,7 @@ impl App {
508507
pub fn configure_sets(&mut self, sets: impl IntoSystemSetConfigs) -> &mut Self {
509508
self.world
510509
.resource_mut::<Schedules>()
511-
.get_mut(&*self.default_schedule_label)
510+
.get_mut_by_id(self.default_schedule_label)
512511
.unwrap()
513512
.configure_sets(sets);
514513
self
@@ -992,8 +991,9 @@ impl App {
992991
) -> &mut Self {
993992
let mut schedules = self.world.resource_mut::<Schedules>();
994993

995-
if schedules.get(&label).is_none() {
996-
schedules.insert(label.dyn_clone(), Schedule::new());
994+
let id = ScheduleId::of(&label);
995+
if !schedules.contains_by_id(id) {
996+
schedules.insert_by_id(id, Schedule::new());
997997
}
998998

999999
let schedule = schedules.get_mut(&label).unwrap();

crates/bevy_ecs/src/schedule/schedule.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ impl Schedules {
4343
/// and the old schedule is returned. Otherwise, `None` is returned.
4444
pub fn insert(&mut self, label: impl ScheduleLabel, schedule: Schedule) -> Option<Schedule> {
4545
let id = ScheduleId::of(&label);
46-
self.insert_with_id(id, schedule)
46+
self.insert_by_id(id, schedule)
4747
}
4848

4949
/// Inserts a schedule into the map.
5050
///
5151
/// If the map already had an entry for `id`, `schedule` is inserted,
5252
/// and the old schedule is returned. Otherwise, `None` is returned.
53-
pub fn insert_with_id(&mut self, id: ScheduleId, schedule: Schedule) -> Option<Schedule> {
53+
pub fn insert_by_id(&mut self, id: ScheduleId, schedule: Schedule) -> Option<Schedule> {
5454
if self.inner.contains_key(&id) {
5555
warn!("schedule with label {:?} already exists", id);
5656
}
@@ -60,27 +60,47 @@ impl Schedules {
6060
/// Removes the schedule corresponding to the `label` from the map, returning it if it existed.
6161
pub fn remove(&mut self, label: &dyn ScheduleLabel) -> Option<Schedule> {
6262
let id = ScheduleId::of(label);
63+
self.remove_by_id(id)
64+
}
65+
66+
/// Removes the schedule corresponding to the `id` from the map, returning it if it existed.
67+
pub fn remove_by_id(&mut self, id: ScheduleId) -> Option<Schedule> {
6368
if !self.inner.contains_key(&id) {
6469
warn!("schedule with label {:?} not found", id);
6570
}
6671
self.inner.remove(&id)
6772
}
6873

69-
/// Does a schedule with the provided label already exist?
74+
/// Does a schedule associated with the provided `label` already exist?
7075
pub fn contains(&self, label: &dyn ScheduleLabel) -> bool {
7176
let id = ScheduleId::of(label);
77+
self.contains_by_id(id)
78+
}
79+
80+
/// Does a schedule associated with the provided `id` already exist?
81+
pub fn contains_by_id(&self, id: ScheduleId) -> bool {
7282
self.inner.contains_key(&id)
7383
}
7484

7585
/// Returns a reference to the schedule associated with `label`, if it exists.
7686
pub fn get(&self, label: &dyn ScheduleLabel) -> Option<&Schedule> {
7787
let id = ScheduleId::of(label);
88+
self.get_by_id(id)
89+
}
90+
91+
/// Returns a reference to the schedule associated with `id`, if it exists.
92+
pub fn get_by_id(&self, id: ScheduleId) -> Option<&Schedule> {
7893
self.inner.get(&id)
7994
}
8095

8196
/// Returns a mutable reference to the schedule associated with `label`, if it exists.
8297
pub fn get_mut(&mut self, label: &dyn ScheduleLabel) -> Option<&mut Schedule> {
8398
let id = ScheduleId::of(label);
99+
self.get_mut_by_id(id)
100+
}
101+
102+
/// Returns a mutable reference to the schedule associated with `id`, if it exists.
103+
pub fn get_mut_by_id(&mut self, id: ScheduleId) -> Option<&mut Schedule> {
84104
self.inner.get_mut(&id)
85105
}
86106

crates/bevy_ecs/src/schedule/set.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::any::TypeId;
2-
use std::borrow::Cow;
32
use std::collections::hash_map::DefaultHasher;
43
use std::fmt::Debug;
54
use std::hash::{Hash, Hasher};
@@ -16,7 +15,7 @@ use crate::system::{
1615
ExclusiveSystemParamFunction, IsExclusiveFunctionSystem, IsFunctionSystem, SystemParamFunction,
1716
};
1817

19-
type Interner = RwLock<HashMap<UniqueValueHash, Cow<'static, str>>>;
18+
type Interner = RwLock<HashMap<UniqueValueHash, &'static str>>;
2019
static INTERNER: OnceCell<Interner> = OnceCell::new();
2120

2221
/// The [`TypeId`](std::any::TypeId) of a value and its [`DynHash`] output when hashed with the [`DefaultHasher`].
@@ -42,15 +41,12 @@ impl ScheduleId {
4241
let type_id = TypeId::of::<S>();
4342

4443
let key = UniqueValueHash(type_id, hash);
45-
let mut map = INTERNER.get_or_init(|| default()).write().unwrap();
46-
let str = map
47-
.entry(key)
48-
.or_insert_with(|| {
49-
let string = format!("{:?}", label);
50-
let str: &'static str = Box::leak(string.into_boxed_str());
51-
str.into()
52-
})
53-
.as_ref();
44+
let mut map = INTERNER.get_or_init(default).write().unwrap();
45+
let str = *map.entry(key).or_insert_with(|| {
46+
let string = format!("{:?}", label);
47+
let str: &'static str = Box::leak(string.into_boxed_str());
48+
str
49+
});
5450

5551
ScheduleId(key, str)
5652
}
@@ -115,15 +111,12 @@ impl SystemSetId {
115111
let type_id = TypeId::of::<S>();
116112

117113
let key = UniqueValueHash(type_id, hash);
118-
let mut map = INTERNER.get_or_init(|| default()).write().unwrap();
119-
let str = map
120-
.entry(key)
121-
.or_insert_with(|| {
122-
let string = format!("{:?}", set);
123-
let str: &'static str = Box::leak(string.into_boxed_str());
124-
str.into()
125-
})
126-
.as_ref();
114+
let mut map = INTERNER.get_or_init(default).write().unwrap();
115+
let str = *map.entry(key).or_insert_with(|| {
116+
let string = format!("{:?}", set);
117+
let str: &'static str = Box::leak(string.into_boxed_str());
118+
str
119+
});
127120

128121
SystemSetId(key, str)
129122
}

crates/bevy_ecs/src/world/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,35 +1721,33 @@ impl World {
17211721
///
17221722
/// Panics if the requested schedule does not exist, or the [`Schedules`] resource was not added.
17231723
pub fn run_schedule(&mut self, label: impl ScheduleLabel) {
1724-
self.run_schedule_ref(&label);
1724+
let id = ScheduleId::of(&label);
1725+
self.run_schedule_by_id(id);
17251726
}
17261727

1727-
/// Runs the [`Schedule`] associated with the `label` a single time.
1728+
/// Runs the [`Schedule`] associated with the `id` a single time.
17281729
///
1729-
/// Unlike the `run_schedule` method, this method takes the label by reference, which can save a clone.
1730+
/// Unlike the `run_schedule` method, this method takes avoids a clone.
17301731
///
1731-
/// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its label,
1732+
/// The [`Schedule`] is fetched from the [`Schedules`] resource of the world by its ID,
17321733
/// and system state is cached.
17331734
///
17341735
/// For simple testing use cases, call [`Schedule::run(&mut world)`](Schedule::run) instead.
17351736
///
17361737
/// # Panics
17371738
///
17381739
/// Panics if the requested schedule does not exist, or the [`Schedules`] resource was not added.
1739-
pub fn run_schedule_ref(&mut self, label: &dyn ScheduleLabel) {
1740+
pub fn run_schedule_by_id(&mut self, id: ScheduleId) {
17401741
let mut schedule = self
17411742
.resource_mut::<Schedules>()
1742-
.remove(label)
1743-
.unwrap_or_else(|| panic!("The schedule with the label {label:?} was not found."));
1744-
1745-
let id = ScheduleId::of(label);
1743+
.remove_by_id(id)
1744+
.unwrap_or_else(|| panic!("The schedule with the label {id:?} was not found."));
17461745

17471746
// TODO: move this span to Schdule::run
17481747
#[cfg(feature = "trace")]
1749-
let _span = bevy_utils::tracing::info_span!("schedule", name = ?label).entered();
1748+
let _span = bevy_utils::tracing::info_span!("schedule", name = ?id).entered();
17501749
schedule.run(self);
1751-
self.resource_mut::<Schedules>()
1752-
.insert_with_id(id, schedule);
1750+
self.resource_mut::<Schedules>().insert_by_id(id, schedule);
17531751
}
17541752
}
17551753

0 commit comments

Comments
 (0)