Skip to content

Commit

Permalink
Switch usage of std HashMap/HashSet default hasher, to aHash algo (#258)
Browse files Browse the repository at this point in the history
switch to ahash for HashMaps and HashSets via a new bevy_utils crate
  • Loading branch information
RobDavenport authored Aug 29, 2020
1 parent c40e29b commit 4aabe98
Show file tree
Hide file tree
Showing 68 changed files with 153 additions and 101 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bevy_sprite = { path = "crates/bevy_sprite", version = "0.1" }
bevy_transform = { path = "crates/bevy_transform", version = "0.1" }
bevy_text = { path = "crates/bevy_text", version = "0.1" }
bevy_ui = { path = "crates/bevy_ui", version = "0.1" }
bevy_utils = { path = "crates/bevy_utils", version = "0.1" }
bevy_window = { path = "crates/bevy_window", version = "0.1" }

# bevy (optional)
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bevy_app = { path = "../bevy_app", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.1" }
bevy_property = { path = "../bevy_property", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

# other
uuid = { version = "0.8", features = ["v4", "serde"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::{
};
use anyhow::Result;
use bevy_ecs::{Res, Resource, Resources};
use bevy_utils::{HashMap, HashSet};
use crossbeam_channel::TryRecvError;
use parking_lot::RwLock;
use std::{
collections::{HashMap, HashSet},
env, fs, io,
path::{Path, PathBuf},
sync::Arc,
Expand Down Expand Up @@ -185,7 +185,7 @@ impl AssetServer {
#[cfg(feature = "filesystem_watcher")]
pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) {
use notify::event::{Event, EventKind, ModifyKind};
let mut changed = HashSet::new();
let mut changed = HashSet::default();

while let Some(filesystem_watcher) = asset_server.filesystem_watcher.read().as_ref() {
let result = match filesystem_watcher.receiver.try_recv() {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use bevy_app::{prelude::Events, AppBuilder};
use bevy_ecs::{FromResources, IntoQuerySystem, ResMut, Resource};
use bevy_type_registry::RegisterType;
use std::collections::HashMap;
use bevy_utils::HashMap;

/// Events that happen on assets of type `T`
pub enum AssetEvent<T: Resource> {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ bevy_derive = { path = "../bevy_derive", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_property = { path = "../bevy_property", version = "0.1" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }
4 changes: 2 additions & 2 deletions crates/bevy_core/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy_ecs::prelude::*;
use bevy_property::Properties;
use bevy_utils::{HashMap, HashSet};
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
fmt::Debug,
ops::{Deref, DerefMut},
};
Expand All @@ -29,7 +29,7 @@ where
T: IntoIterator<Item = L>,
{
fn from(value: T) -> Self {
let mut labels = HashSet::new();
let mut labels = HashSet::default();
for label in value {
labels.insert(label.into());
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ profiler = ["bevy_ecs/profiler"]
bevy_app = { path = "../bevy_app", version = "0.1" }
bevy_core = { path = "../bevy_core", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

# other
uuid = { version = "0.8", features = ["v4", "serde"] }
parking_lot = "0.10"
parking_lot = "0.10"
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy_utils::HashMap;
use std::{
collections::{HashMap, VecDeque},
collections::VecDeque,
time::{Duration, SystemTime},
};
use uuid::Uuid;
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/system_profiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{Diagnostic, DiagnosticId, Diagnostics};
use bevy_ecs::{Profiler, Res, ResMut};
use bevy_utils::HashMap;
use parking_lot::RwLock;
use std::{borrow::Cow, collections::HashMap, sync::Arc, time::Instant};
use std::{borrow::Cow, sync::Arc, time::Instant};

#[derive(Debug)]
struct SystemRunInfo {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ profiler = []

[dependencies]
bevy_hecs = { path = "hecs", features = ["macros", "serialize"], version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }
rand = "0.7.2"
rayon = "1.3"
crossbeam-channel = "0.4.2"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ serialize = ["serde"]

[dependencies]
bevy_hecs_macros = { path = "macros", version = "0.1.3", optional = true }
hashbrown = { version = "0.8.0", default-features = false, features = ["ahash", "inline-more"] }
bevy_utils = { path = "../../bevy_utils", version = "0.1" }
lazy_static = { version = "1.4.0", optional = true, features = ["spin_no_std"] }
serde = { version = "1", features = ["derive"], optional = true}
rand = "0.7.3"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {

#path::lazy_static::lazy_static! {
static ref ELEMENTS: [TypeId; #n] = {
let mut dedup = std::collections::HashSet::new();
let mut dedup = #path::bevy_utils::HashSet::default();
for &(ty, name) in [#((std::any::TypeId::of::<#tys>(), std::any::type_name::<#tys>())),*].iter() {
if !dedup.insert(ty) {
panic!("{} has multiple {} fields; each type must occur at most once!", stringify!(#ident), name);
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ecs/hecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ use crate::alloc::{
vec,
vec::Vec,
};
use bevy_utils::{HashMap, HashMapExt};
use core::{
any::{type_name, TypeId},
cell::UnsafeCell,
mem,
ptr::{self, NonNull},
};

use hashbrown::HashMap;

use crate::{borrow::AtomicBorrow, query::Fetch, Access, Component, Query};

/// A collection of entities having the same component types
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/hecs/src/entities.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// modified by Bevy contributors

use bevy_utils::HashMap;
use core::fmt;
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::error::Error;

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/hecs/src/entity_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use crate::alloc::{
vec,
vec::Vec,
};

use bevy_utils::HashSet;
use core::{
any::TypeId,
mem::{self, MaybeUninit},
ptr,
};

use hashbrown::HashSet;

use crate::{archetype::TypeInfo, Component, DynamicBundle};

/// Helper for incrementally constructing a bundle of components with dynamic component types
Expand Down Expand Up @@ -59,7 +59,7 @@ impl EntityBuilder {
storage: Box::new([]),
info: Vec::new(),
ids: Vec::new(),
id_set: HashSet::new(),
id_set: HashSet::default(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/hecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub use world::{ArchetypesGeneration, Component, ComponentError, Iter, SpawnBatc
// Unstable implementation details needed by the macros
#[doc(hidden)]
pub use archetype::TypeInfo;
#[doc(hidden)]
pub use bevy_utils;
#[cfg(feature = "macros")]
#[doc(hidden)]
pub use lazy_static;
Expand Down
7 changes: 3 additions & 4 deletions crates/bevy_ecs/hecs/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
// modified by Bevy contributors

use crate::alloc::vec::Vec;
use bevy_utils::{HashMap, HashSet};
use core::{any::TypeId, convert::TryFrom, fmt, mem, ptr};

#[cfg(feature = "std")]
use std::error::Error;

use hashbrown::{HashMap, HashSet};

use crate::{
archetype::Archetype,
entities::{Entities, Location},
Expand Down Expand Up @@ -363,7 +362,7 @@ impl World {
entity: Entity,
components: impl DynamicBundle,
) -> Result<(), NoSuchEntity> {
use hashbrown::hash_map::Entry;
use std::collections::hash_map::Entry;

let loc = self.entities.get_mut(entity)?;
unsafe {
Expand Down Expand Up @@ -461,7 +460,7 @@ impl World {
/// assert_eq!(*world.get::<bool>(e).unwrap(), true);
/// ```
pub fn remove<T: Bundle>(&mut self, entity: Entity) -> Result<T, ComponentError> {
use hashbrown::hash_map::Entry;
use std::collections::hash_map::Entry;

let loc = self.entities.get_mut(entity)?;
unsafe {
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_ecs/src/resource/resources.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{FetchResource, ResourceQuery};
use crate::system::SystemId;
use bevy_hecs::{Archetype, Ref, RefMut, TypeInfo};
use bevy_utils::HashMap;
use core::any::TypeId;
use std::{collections::HashMap, ptr::NonNull};
use std::ptr::NonNull;

/// A Resource type
pub trait Resource: Send + Sync + 'static {}
Expand Down Expand Up @@ -64,7 +65,7 @@ impl Resources {
ResourceData {
archetype: Archetype::new(types),
default_index: None,
system_id_to_archetype_index: HashMap::new(),
system_id_to_archetype_index: HashMap::default(),
}
});

Expand Down
7 changes: 2 additions & 5 deletions crates/bevy_ecs/src/schedule/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use crate::{
system::{System, SystemId, ThreadLocalExecution},
};
use bevy_hecs::World;
use bevy_utils::{HashMap, HashSet};
use parking_lot::Mutex;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
sync::Arc,
};
use std::{borrow::Cow, sync::Arc};

/// An ordered collection of stages, which each contain an ordered list of [System]s.
/// Schedules are essentially the "execution plan" for an App's systems.
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/system/system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::resource::Resources;
use bevy_hecs::{Access, Query, World};
use bevy_utils::HashSet;
use fixedbitset::FixedBitSet;
use std::{any::TypeId, borrow::Cow, collections::HashSet};
use std::{any::TypeId, borrow::Cow};

/// Determines the strategy used to run the `run_thread_local` function in a [System]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_input/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ default = []
serialize = ["serde"]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.1" }
bevy_ecs = { path = "../bevy_ecs", version = "0.1" }
bevy_math = { path = "../bevy_math", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

# other
serde = { version = "1", features = ["derive"], optional = true }
3 changes: 2 additions & 1 deletion crates/bevy_input/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{collections::HashSet, hash::Hash};
use bevy_utils::HashSet;
use std::hash::Hash;

/// A "press-able" input of type `T`
pub struct Input<T> {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_property/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ keywords = ["bevy"]
bevy_ecs = {path = "../bevy_ecs", version = "0.1"}
bevy_math = {path = "../bevy_math", version = "0.1"}
bevy_property_derive = {path = "bevy_property_derive", version = "0.1"}
bevy_utils = {path = "../bevy_utils", version = "0.1"}

# other
erased-serde = "0.3"
bevy_ron = {path = "../bevy_ron", version = "0.1.3"}
serde = "1"
smallvec = {version = "1.4", features = ["serde"]}
smallvec = {version = "1.4", features = ["serde"]}
3 changes: 2 additions & 1 deletion crates/bevy_property/src/dynamic_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::{
property_serde::{DynamicPropertiesDeserializer, DynamicPropertiesSerializer, Serializable},
DeserializeProperty, Properties, Property, PropertyIter, PropertyType, PropertyTypeRegistry,
};
use bevy_utils::HashMap;
use serde::de::DeserializeSeed;
use std::{any::Any, borrow::Cow, collections::HashMap};
use std::{any::Any, borrow::Cow};

pub struct DynamicProperties {
pub type_name: String,
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_property/src/impl_property/impl_property_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use std::{
any::Any,
collections::{BTreeMap, HashMap, HashSet},
hash::Hash,
hash::{BuildHasher, Hash},
ops::Range,
};

Expand Down Expand Up @@ -105,10 +105,11 @@ where

// impl_property!(SEQUENCE, VecDeque<T> where T: Clone + Send + Sync + Serialize + 'static);
impl_property!(Option<T> where T: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static);
impl_property!(HashSet<T> where T: Clone + Eq + Send + Sync + Hash + Serialize + for<'de> Deserialize<'de> + 'static);
impl_property!(HashMap<K, V> where
impl_property!(HashSet<T, H> where T: Clone + Eq + Send + Sync + Hash + Serialize + for<'de> Deserialize<'de> + 'static, H: Clone + Send + Sync + Default + BuildHasher + 'static);
impl_property!(HashMap<K, V, H> where
K: Clone + Eq + Send + Sync + Hash + Serialize + for<'de> Deserialize<'de> + 'static,
V: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static,);
V: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static,
H: Clone + Send + Sync + Default + BuildHasher + 'static);
impl_property!(BTreeMap<K, V> where
K: Clone + Ord + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static,
V: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static);
Expand Down
6 changes: 2 additions & 4 deletions crates/bevy_property/src/type_registry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{DeserializeProperty, Property};
use std::{
any::TypeId,
collections::{HashMap, HashSet},
};
use bevy_utils::{HashMap, HashSet};
use std::any::TypeId;

#[derive(Default)]
pub struct PropertyTypeRegistry {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bevy_property = { path = "../bevy_property", version = "0.1" }
bevy_transform = { path = "../bevy_transform", version = "0.1" }
bevy_type_registry = { path = "../bevy_type_registry", version = "0.1" }
bevy_window = { path = "../bevy_window", version = "0.1" }
bevy_utils = { path = "../bevy_utils", version = "0.1" }

# rendering
spirv-reflect = "0.2.3"
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/batch/batcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::Batch;
use bevy_utils::HashMap;
use smallvec::{smallvec, SmallVec};
use std::{borrow::Cow, collections::HashMap, hash::Hash};
use std::{borrow::Cow, hash::Hash};

// TODO: add sorting by primary / secondary handle to reduce rebinds of data

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/camera/active_cameras.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Camera;
use bevy_ecs::{Entity, Query, ResMut};
use std::collections::HashMap;
use bevy_utils::HashMap;

#[derive(Default)]
pub struct ActiveCameras {
Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use bevy_asset::{AssetEvent, Assets, Handle};
use bevy_core::AsBytes;
use bevy_ecs::{Local, Query, Res, ResMut};
use bevy_math::*;
use std::{borrow::Cow, collections::HashSet};
use bevy_utils::HashSet;
use std::borrow::Cow;
use thiserror::Error;

pub const VERTEX_BUFFER_ASSET_INDEX: usize = 0;
Expand Down Expand Up @@ -506,7 +507,7 @@ pub fn mesh_resource_provider_system(
vertex_buffer_descriptor
}
};
let mut changed_meshes = HashSet::new();
let mut changed_meshes = HashSet::<Handle<Mesh>>::default();
let render_resource_context = &**render_resource_context;
for event in state.mesh_event_reader.iter(&mesh_events) {
match event {
Expand Down
Loading

0 comments on commit 4aabe98

Please sign in to comment.