From be7d20f0c57cc834e943426090fe2debf76ca98d Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 20 Dec 2024 21:27:48 +0100 Subject: perf(ecs): use hashbrown's HashMap & HashSet Hashbrown's HashMap & HashSet is faster than std's. I guess this is because std (which uses hashbrown) does not enable hashbrown's default-hasher flag and instead uses a slower hasher. --- Cargo.lock | 26 +++++++++++++++++++++++++- ecs/Cargo.toml | 1 + ecs/src/component/storage.rs | 3 ++- ecs/src/lib.rs | 3 ++- ecs/src/query/options.rs | 3 ++- ecs/src/system/stateful.rs | 2 +- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8067f0..03d9d89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anes" version = "0.1.6" @@ -257,6 +263,7 @@ version = "0.1.0" dependencies = [ "criterion", "ecs-macros", + "hashbrown 0.15.2", "linkme", "paste", "seq-macro", @@ -322,6 +329,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "foldhash" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" + [[package]] name = "game-newest" version = "0.1.0" @@ -384,6 +397,17 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + [[package]] name = "hermit-abi" version = "0.4.0" @@ -412,7 +436,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.3", ] [[package]] diff --git a/ecs/Cargo.toml b/ecs/Cargo.toml index 3bf3b19..68d74f9 100644 --- a/ecs/Cargo.toml +++ b/ecs/Cargo.toml @@ -12,6 +12,7 @@ paste = "1.0.14" thiserror = "1.0.49" tracing = { version = "0.1.39", optional = true } linkme = "0.3.29" +hashbrown = "0.15.2" ecs-macros = { path = "../ecs-macros" } util-macros = { path = "../util-macros" } diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 54fa834..dcf0181 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -1,10 +1,11 @@ use std::any::type_name; use std::borrow::Borrow; use std::cell::RefCell; -use std::collections::{HashMap, HashSet}; use std::slice::Iter as SliceIter; use std::vec::IntoIter as OwnedVecIter; +use hashbrown::{HashMap, HashSet}; + use crate::archetype::Id as ArchetypeId; use crate::component::{ Component, diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 3926344..43a00f1 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -2,12 +2,13 @@ use std::any::{type_name, TypeId}; use std::cell::RefCell; -use std::collections::HashMap; use std::fmt::Debug; use std::mem::ManuallyDrop; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use hashbrown::HashMap; + use crate::actions::Action; use crate::component::storage::Storage as ComponentStorage; use crate::component::{ diff --git a/ecs/src/query/options.rs b/ecs/src/query/options.rs index ead0ac7..772d091 100644 --- a/ecs/src/query/options.rs +++ b/ecs/src/query/options.rs @@ -1,6 +1,7 @@ -use std::collections::HashSet; use std::marker::PhantomData; +use hashbrown::HashSet; + use crate::component::Component; use crate::EntityComponent; diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs index 5eae1da..80ac346 100644 --- a/ecs/src/system/stateful.rs +++ b/ecs/src/system/stateful.rs @@ -1,7 +1,7 @@ use std::any::{Any, TypeId}; -use std::collections::HashMap; use std::panic::{RefUnwindSafe, UnwindSafe}; +use hashbrown::HashMap; use seq_macro::seq; use crate::component::Component; -- cgit v1.2.3-18-g5258