Skip to content

Commit

Permalink
Merge pull request #87 from Anders429/hash
Browse files Browse the repository at this point in the history
Use `fnv` instead of `ahash`.
  • Loading branch information
Anders429 authored Aug 16, 2022
2 parents 65ace8e + 1743c30 commit 3a0a447
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

[dependencies]
ahash = {version = "0.8.0", default-features = false, features = ["compile-time-rng"]}
either = {version = "1.7.0", default-features = false}
fnv = {version = "1.0.7", default-features = false}
hashbrown = {version = "0.12.3", default-features = false, features = ["inline-more", "raw"]}
rayon = {version = "1.5.3", optional = true}
serde = {version = "1.0.143", default-features = false, features = ["alloc"], optional = true}
Expand Down
3 changes: 2 additions & 1 deletion src/archetype/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ mod tests {
use crate::{archetype::Identifier, registry};
use alloc::{vec, vec::Vec};
use core::ptr;
use fnv::FnvBuildHasher;
use hashbrown::HashSet;

macro_rules! create_components {
Expand Down Expand Up @@ -473,7 +474,7 @@ mod tests {
let identifier_a = unsafe { buffer.as_ref() };
let identifier_b = unsafe { buffer.as_ref() };

let mut hashset = HashSet::with_hasher(ahash::RandomState::new());
let mut hashset = HashSet::with_hasher(FnvBuildHasher::default());
hashset.insert(identifier_a);
assert!(hashset.contains(&identifier_b));
}
Expand Down
5 changes: 3 additions & 2 deletions src/archetype/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use core::{
mem::{ManuallyDrop, MaybeUninit},
slice,
};
use fnv::FnvBuildHasher;
use hashbrown::HashMap;

pub(crate) struct Archetype<R>
Expand All @@ -45,7 +46,7 @@ where
components: Vec<(*mut u8, usize)>,
length: usize,

component_map: HashMap<TypeId, usize, ahash::RandomState>,
component_map: HashMap<TypeId, usize, FnvBuildHasher>,
}

impl<R> Archetype<R>
Expand All @@ -64,7 +65,7 @@ where
components: Vec<(*mut u8, usize)>,
length: usize,
) -> Self {
let mut component_map = HashMap::with_hasher(ahash::RandomState::new());
let mut component_map = HashMap::with_hasher(FnvBuildHasher::default());
// SAFETY: `identifier.iter()` is generic over the same registry `R` that this associated
// function is being called on.
unsafe { R::create_component_map_for_identifier(&mut component_map, 0, identifier.iter()) };
Expand Down
14 changes: 6 additions & 8 deletions src/archetypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) use par_iter::ParIterMut;

use crate::{archetype, archetype::Archetype, entity, registry::Registry};
use core::hash::{BuildHasher, Hash, Hasher};
use fnv::FnvBuildHasher;
use hashbrown::raw::RawTable;
use iter::Iter;

Expand All @@ -23,7 +24,7 @@ where
R: Registry,
{
raw_archetypes: RawTable<Archetype<R>>,
hash_builder: ahash::RandomState,
hash_builder: FnvBuildHasher,
}

impl<R> Archetypes<R>
Expand All @@ -33,28 +34,25 @@ where
pub(crate) fn new() -> Self {
Self {
raw_archetypes: RawTable::new(),
hash_builder: ahash::RandomState::new(),
hash_builder: FnvBuildHasher::default(),
}
}

#[cfg(feature = "serde")]
pub(crate) fn with_capacity(capacity: usize) -> Self {
Self {
raw_archetypes: RawTable::with_capacity(capacity),
hash_builder: ahash::RandomState::new(),
hash_builder: FnvBuildHasher::default(),
}
}

fn make_hash(
identifier: archetype::IdentifierRef<R>,
hash_builder: &ahash::RandomState,
) -> u64 {
fn make_hash(identifier: archetype::IdentifierRef<R>, hash_builder: &FnvBuildHasher) -> u64 {
let mut state = hash_builder.build_hasher();
identifier.hash(&mut state);
state.finish()
}

fn make_hasher(hash_builder: &ahash::RandomState) -> impl Fn(&Archetype<R>) -> u64 + '_ {
fn make_hasher(hash_builder: &FnvBuildHasher) -> impl Fn(&Archetype<R>) -> u64 + '_ {
move |archetype| {
Self::make_hash(
// SAFETY: The `IdentifierRef` obtained here does not live longer than the `archetype`.
Expand Down
13 changes: 7 additions & 6 deletions src/entities/seal/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{component::Component, entities::Null};
use alloc::vec::Vec;
use core::{any::TypeId, mem::ManuallyDrop};
use fnv::FnvBuildHasher;
use hashbrown::HashMap;

pub trait Storage {
Expand All @@ -19,7 +20,7 @@ pub trait Storage {
/// which `component_map` has an entry whose index references it.
unsafe fn extend_components(
self,
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
components: &mut [(*mut u8, usize)],
length: usize,
);
Expand All @@ -41,22 +42,22 @@ pub trait Storage {
/// the given `component_map`.
unsafe fn to_identifier(
identifier: &mut [u8],
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
);
}

impl Storage for Null {
unsafe fn extend_components(
self,
_component_map: &HashMap<TypeId, usize, ahash::RandomState>,
_component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
_components: &mut [(*mut u8, usize)],
_length: usize,
) {
}

unsafe fn to_identifier(
_identifier: &mut [u8],
_component_map: &HashMap<TypeId, usize, ahash::RandomState>,
_component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
) {
}
}
Expand All @@ -68,7 +69,7 @@ where
{
unsafe fn extend_components(
self,
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
components: &mut [(*mut u8, usize)],
length: usize,
) {
Expand Down Expand Up @@ -106,7 +107,7 @@ where

unsafe fn to_identifier(
identifier: &mut [u8],
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
) {
let component_index = component_map.get(&TypeId::of::<C>()).unwrap();
let index = component_index / 8;
Expand Down
13 changes: 7 additions & 6 deletions src/entity/seal/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{component::Component, entity::Null};
use alloc::vec::Vec;
use core::{any::TypeId, mem::ManuallyDrop};
use fnv::FnvBuildHasher;
use hashbrown::HashMap;

pub trait Storage {
Expand All @@ -19,7 +20,7 @@ pub trait Storage {
/// which `component_map` has an entry whose index references it.
unsafe fn push_components(
self,
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
components: &mut [(*mut u8, usize)],
length: usize,
);
Expand All @@ -41,22 +42,22 @@ pub trait Storage {
/// the given `component_map`.
unsafe fn to_identifier(
identifier: &mut [u8],
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
);
}

impl Storage for Null {
unsafe fn push_components(
self,
_component_map: &HashMap<TypeId, usize, ahash::RandomState>,
_component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
_components: &mut [(*mut u8, usize)],
_length: usize,
) {
}

unsafe fn to_identifier(
_identifier: &mut [u8],
_component_map: &HashMap<TypeId, usize, ahash::RandomState>,
_component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
) {
}
}
Expand All @@ -68,7 +69,7 @@ where
{
unsafe fn push_components(
self,
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
components: &mut [(*mut u8, usize)],
length: usize,
) {
Expand Down Expand Up @@ -97,7 +98,7 @@ where

unsafe fn to_identifier(
identifier: &mut [u8],
component_map: &HashMap<TypeId, usize, ahash::RandomState>,
component_map: &HashMap<TypeId, usize, FnvBuildHasher>,
) {
let component_index = component_map.get(&TypeId::of::<C>()).unwrap();
let index = component_index / 8;
Expand Down
33 changes: 17 additions & 16 deletions src/query/claim.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{component::Component, entity, query::view};
use core::any::TypeId;
use fnv::FnvBuildHasher;
use hashbrown::HashSet;

pub trait Claim {
fn claim(
mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
);
}

Expand All @@ -14,8 +15,8 @@ where
C: Component,
{
fn claim(
_mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
immutable_claims.insert(TypeId::of::<C>());
}
Expand All @@ -26,8 +27,8 @@ where
C: Component,
{
fn claim(
mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
_immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
mutable_claims.insert(TypeId::of::<C>());
}
Expand All @@ -38,8 +39,8 @@ where
C: Component,
{
fn claim(
_mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
immutable_claims.insert(TypeId::of::<C>());
}
Expand All @@ -50,25 +51,25 @@ where
C: Component,
{
fn claim(
mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
_immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
mutable_claims.insert(TypeId::of::<C>());
}
}

impl Claim for entity::Identifier {
fn claim(
_mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
_immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
}
}

impl Claim for view::Null {
fn claim(
_mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
_mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
_immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
}
}
Expand All @@ -79,8 +80,8 @@ where
W: Claim,
{
fn claim(
mutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
immutable_claims: &mut HashSet<TypeId, ahash::RandomState>,
mutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
immutable_claims: &mut HashSet<TypeId, FnvBuildHasher>,
) {
V::claim(mutable_claims, immutable_claims);
W::claim(mutable_claims, immutable_claims);
Expand Down
Loading

0 comments on commit 3a0a447

Please sign in to comment.