Skip to content

Commit

Permalink
Switch to StableHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Apr 13, 2021
1 parent b82fd67 commit 9fecf62
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
11 changes: 6 additions & 5 deletions crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_utils::HashMap;
use bevy_utils::StableHashMap;

use crate::{
bundle::BundleId,
Expand Down Expand Up @@ -132,13 +133,11 @@ pub struct Archetype {
RelationKindId,
(
Option<ArchetypeComponentInfo>,
HashMap<Entity, ArchetypeComponentInfo>,
StableHashMap<Entity, ArchetypeComponentInfo>,
),
>,
}

// FIXME(Relationships) hashmap iters are nondet and causing tests to fail

pub struct KindTargetsIter<'a> {
no_target: Option<()>,
targets: std::collections::hash_map::Keys<'a, Entity, ArchetypeComponentInfo>,
Expand Down Expand Up @@ -177,7 +176,8 @@ impl Archetype {
for ((kind_id, target), archetype_component_id) in
table_components.iter().zip(table_archetype_components)
{
let components = components.get_or_insert_with(*kind_id, || (None, HashMap::default()));
let components =
components.get_or_insert_with(*kind_id, || (None, StableHashMap::default()));

let arch_comp_info = ArchetypeComponentInfo {
storage_type: StorageType::Table,
Expand All @@ -196,7 +196,8 @@ impl Archetype {
.iter()
.zip(sparse_set_archetype_components)
{
let components = components.get_or_insert_with(*kind_id, || (None, HashMap::default()));
let components =
components.get_or_insert_with(*kind_id, || (None, StableHashMap::default()));
let arch_comp_info = ArchetypeComponentInfo {
storage_type: StorageType::SparseSet,
archetype_component_id,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
world::{Mut, World},
};
use bevy_ecs_macros::all_tuples;
use bevy_utils::HashMap;
use bevy_utils::{HashMap, StableHashMap};
use smallvec::SmallVec;
use std::{
any::TypeId,
Expand Down Expand Up @@ -671,7 +671,7 @@ pub enum Either<T, U> {
pub enum RelationAccess<'w, 's, T: Component> {
Table {
current_idx: usize,
columns: &'w (Option<Column>, HashMap<Entity, Column>),
columns: &'w (Option<Column>, StableHashMap<Entity, Column>),
iter: Either<crate::storage::ColIter<'w>, std::slice::Iter<'s, Entity>>,
p: PhantomData<&'w T>,
},
Expand Down
7 changes: 3 additions & 4 deletions crates/bevy_ecs/src/storage/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
entity::Entity,
storage::{BlobVec, SparseSet},
};
use bevy_utils::{AHasher, HashMap};
use bevy_utils::{AHasher, HashMap, StableHashMap};
use std::{
cell::UnsafeCell,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -159,7 +159,6 @@ impl Column {
}
}

// FIXME(Relationships) hashmap iters are nondet and causing tests to fail
pub struct ColIter<'a> {
no_target_col: Option<&'a Column>,
target_cols: std::collections::hash_map::Iter<'a, Entity, Column>,
Expand All @@ -177,7 +176,7 @@ impl<'a> Iterator for ColIter<'a> {
}

pub struct Table {
pub(crate) columns: SparseSet<RelationKindId, (Option<Column>, HashMap<Entity, Column>)>,
pub(crate) columns: SparseSet<RelationKindId, (Option<Column>, StableHashMap<Entity, Column>)>,
entities: Vec<Entity>,
archetypes: Vec<ArchetypeId>,
grow_amount: usize,
Expand Down Expand Up @@ -239,7 +238,7 @@ impl Table {
pub fn add_column(&mut self, component_kind: &RelationshipKindInfo, target: Option<Entity>) {
let column = self
.columns
.get_or_insert_with(component_kind.id(), || (None, HashMap::default()));
.get_or_insert_with(component_kind.id(), || (None, StableHashMap::default()));

match target {
Some(target) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod world_cell;
#[cfg(test)]
mod tests;

use bevy_utils::HashMap;
use bevy_utils::{HashMap, StableHashMap};
pub use entity_ref::*;
pub use pointer::*;
pub use spawn_batch::*;
Expand Down Expand Up @@ -867,7 +867,7 @@ impl World {
),
storage_type: StorageType::Table,
}),
HashMap::default(),
StableHashMap::default(),
),
);
*archetype_component_count += 1;
Expand Down
25 changes: 12 additions & 13 deletions crates/bevy_ecs/src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,28 +231,27 @@ fn table_relation_access() {
);
assert!(matches!(accessor.next(), None));
//
let (child, accessor) = iter.next().unwrap();
let (child, mut accessor) = iter.next().unwrap();
assert!(child == child2);
let foo = accessor.collect::<Vec<_>>();
assert_eq!(
foo[0],
accessor.next().unwrap(),
(
Some(parent2),
Some(random_parent),
&ChildOf {
despawn_recursive: false
despawn_recursive: true
}
)
);
assert_eq!(
foo[1],
accessor.next().unwrap(),
(
Some(random_parent),
Some(parent2),
&ChildOf {
despawn_recursive: true
despawn_recursive: false
}
)
);
assert!(foo.len() == 2);
assert!(matches!(accessor.next(), None));
assert!(matches!(iter.next(), None));
}

Expand Down Expand Up @@ -354,18 +353,18 @@ fn sparse_relation_access() {
assert_eq!(
accessor.next().unwrap(),
(
Some(parent1),
Some(random_parent),
&ChildOf {
despawn_recursive: true
despawn_recursive: false
}
)
);
assert_eq!(
accessor.next().unwrap(),
(
Some(random_parent),
Some(parent1),
&ChildOf {
despawn_recursive: false
despawn_recursive: true
}
)
);
Expand Down

0 comments on commit 9fecf62

Please sign in to comment.