Simplify the way run conditions are stored in the schedule (#8594)

# Objective

`ScheduleGraph` currently stores run conditions in a
`Option<Vec<BoxedCondition>>`. The `Option` is unnecessary, since we can
just use an empty vector instead of `None`.
This commit is contained in:
JoJoJet 2023-05-11 13:17:15 -04:00 committed by GitHub
parent 636f711d26
commit 1644426761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -380,9 +380,9 @@ impl SystemNode {
#[derive(Default)] #[derive(Default)]
pub struct ScheduleGraph { pub struct ScheduleGraph {
systems: Vec<SystemNode>, systems: Vec<SystemNode>,
system_conditions: Vec<Option<Vec<BoxedCondition>>>, system_conditions: Vec<Vec<BoxedCondition>>,
system_sets: Vec<SystemSetNode>, system_sets: Vec<SystemSetNode>,
system_set_conditions: Vec<Option<Vec<BoxedCondition>>>, system_set_conditions: Vec<Vec<BoxedCondition>>,
system_set_ids: HashMap<BoxedSystemSet, NodeId>, system_set_ids: HashMap<BoxedSystemSet, NodeId>,
uninit: Vec<(NodeId, usize)>, uninit: Vec<(NodeId, usize)>,
hierarchy: Dag, hierarchy: Dag,
@ -465,20 +465,17 @@ impl ScheduleGraph {
.enumerate() .enumerate()
.filter_map(|(i, (system_node, condition))| { .filter_map(|(i, (system_node, condition))| {
let system = system_node.inner.as_deref()?; let system = system_node.inner.as_deref()?;
let condition = condition.as_ref()?.as_slice(); Some((NodeId::System(i), system, condition.as_slice()))
Some((NodeId::System(i), system, condition))
}) })
} }
/// Returns an iterator over all system sets in this schedule. /// Returns an iterator over all system sets in this schedule.
pub fn system_sets(&self) -> impl Iterator<Item = (NodeId, &dyn SystemSet, &[BoxedCondition])> { pub fn system_sets(&self) -> impl Iterator<Item = (NodeId, &dyn SystemSet, &[BoxedCondition])> {
self.system_set_ids.iter().map(|(_, node_id)| { self.system_set_ids.iter().map(|(_, &node_id)| {
let set_node = &self.system_sets[node_id.index()]; let set_node = &self.system_sets[node_id.index()];
let set = &*set_node.inner; let set = &*set_node.inner;
let conditions = self.system_set_conditions[node_id.index()] let conditions = self.system_set_conditions[node_id.index()].as_slice();
.as_deref() (node_id, set, conditions)
.unwrap_or(&[]);
(*node_id, set, conditions)
}) })
} }
@ -662,7 +659,7 @@ impl ScheduleGraph {
// system init has to be deferred (need `&mut World`) // system init has to be deferred (need `&mut World`)
self.uninit.push((id, 0)); self.uninit.push((id, 0));
self.systems.push(SystemNode::new(config.system)); self.systems.push(SystemNode::new(config.system));
self.system_conditions.push(Some(config.conditions)); self.system_conditions.push(config.conditions);
Ok(id) Ok(id)
} }
@ -708,8 +705,7 @@ impl ScheduleGraph {
self.update_graphs(id, graph_info)?; self.update_graphs(id, graph_info)?;
// system init has to be deferred (need `&mut World`) // system init has to be deferred (need `&mut World`)
let system_set_conditions = let system_set_conditions = &mut self.system_set_conditions[id.index()];
self.system_set_conditions[id.index()].get_or_insert_with(Vec::new);
self.uninit.push((id, system_set_conditions.len())); self.uninit.push((id, system_set_conditions.len()));
system_set_conditions.append(&mut conditions); system_set_conditions.append(&mut conditions);
@ -719,7 +715,7 @@ impl ScheduleGraph {
fn add_set(&mut self, set: BoxedSystemSet) -> NodeId { fn add_set(&mut self, set: BoxedSystemSet) -> NodeId {
let id = NodeId::Set(self.system_sets.len()); let id = NodeId::Set(self.system_sets.len());
self.system_sets.push(SystemSetNode::new(set.dyn_clone())); self.system_sets.push(SystemSetNode::new(set.dyn_clone()));
self.system_set_conditions.push(None); self.system_set_conditions.push(Vec::new());
self.system_set_ids.insert(set, id); self.system_set_ids.insert(set, id);
id id
} }
@ -852,17 +848,13 @@ impl ScheduleGraph {
match id { match id {
NodeId::System(index) => { NodeId::System(index) => {
self.systems[index].get_mut().unwrap().initialize(world); self.systems[index].get_mut().unwrap().initialize(world);
if let Some(v) = self.system_conditions[index].as_mut() { for condition in &mut self.system_conditions[index] {
for condition in v.iter_mut() { condition.initialize(world);
condition.initialize(world);
}
} }
} }
NodeId::Set(index) => { NodeId::Set(index) => {
if let Some(v) = self.system_set_conditions[index].as_mut() { for condition in self.system_set_conditions[index].iter_mut().skip(i) {
for condition in v.iter_mut().skip(i) { condition.initialize(world);
condition.initialize(world);
}
} }
} }
} }
@ -1123,11 +1115,7 @@ impl ScheduleGraph {
.filter(|&(_i, id)| { .filter(|&(_i, id)| {
// ignore system sets that have no conditions // ignore system sets that have no conditions
// ignore system type sets (already covered, they don't have conditions) // ignore system type sets (already covered, they don't have conditions)
id.is_set() id.is_set() && !self.system_set_conditions[id.index()].is_empty()
&& self.system_set_conditions[id.index()]
.as_ref()
.filter(|v| !v.is_empty())
.is_some()
}) })
.unzip(); .unzip();
@ -1215,7 +1203,7 @@ impl ScheduleGraph {
.zip(schedule.system_conditions.drain(..)) .zip(schedule.system_conditions.drain(..))
{ {
self.systems[id.index()].inner = Some(system); self.systems[id.index()].inner = Some(system);
self.system_conditions[id.index()] = Some(conditions); self.system_conditions[id.index()] = conditions;
} }
for (id, conditions) in schedule for (id, conditions) in schedule
@ -1223,7 +1211,7 @@ impl ScheduleGraph {
.drain(..) .drain(..)
.zip(schedule.set_conditions.drain(..)) .zip(schedule.set_conditions.drain(..))
{ {
self.system_set_conditions[id.index()] = Some(conditions); self.system_set_conditions[id.index()] = conditions;
} }
*schedule = self.build_schedule(components)?; *schedule = self.build_schedule(components)?;
@ -1231,13 +1219,13 @@ impl ScheduleGraph {
// move systems into new schedule // move systems into new schedule
for &id in &schedule.system_ids { for &id in &schedule.system_ids {
let system = self.systems[id.index()].inner.take().unwrap(); let system = self.systems[id.index()].inner.take().unwrap();
let conditions = self.system_conditions[id.index()].take().unwrap(); let conditions = std::mem::take(&mut self.system_conditions[id.index()]);
schedule.systems.push(system); schedule.systems.push(system);
schedule.system_conditions.push(conditions); schedule.system_conditions.push(conditions);
} }
for &id in &schedule.set_ids { for &id in &schedule.set_ids {
let conditions = self.system_set_conditions[id.index()].take().unwrap(); let conditions = std::mem::take(&mut self.system_set_conditions[id.index()]);
schedule.set_conditions.push(conditions); schedule.set_conditions.push(conditions);
} }