From 3a50065cc5c88aa780c223cf5da3df33fffb00c0 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 19 May 2023 04:35:16 +0200 Subject: [PATCH] Move `ClientIdMap` to shared, switch to `with_capacity_and_hasher` where possible --- Cargo.lock | 2 ++ kubi-server/src/auth.rs | 5 +++-- kubi-server/src/client.rs | 16 +--------------- kubi-server/src/world/chunk.rs | 2 +- kubi-shared/Cargo.toml | 6 +++--- kubi-shared/src/networking/client.rs | 17 ++++++++++++++++- kubi/Cargo.toml | 2 +- kubi/src/networking/player.rs | 13 +++++++++---- 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 635226b..1056986 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -996,6 +996,8 @@ dependencies = [ "anyhow", "bracket-noise", "glam", + "hashbrown 0.13.2", + "nohash-hasher", "postcard", "rand", "rand_xoshiro", diff --git a/kubi-server/src/auth.rs b/kubi-server/src/auth.rs index d912790..514dc27 100644 --- a/kubi-server/src/auth.rs +++ b/kubi-server/src/auth.rs @@ -14,10 +14,11 @@ use kubi_shared::{ transform::Transform, entity::{Entity, Health} }; use crate::{ - server::{ServerEvents, UdpServer, IsMessageOfType}, config::ConfigTable, - client::{ClientAddress, ClientIdMap, ClientAddressMap} + server::{ServerEvents, UdpServer, IsMessageOfType}, + client::{ClientAddress, ClientAddressMap} }; +pub use kubi_shared::networking::client::ClientIdMap; pub fn authenticate_players( storages: AllStoragesView, diff --git a/kubi-server/src/client.rs b/kubi-server/src/client.rs index 0757ebb..6e385d4 100644 --- a/kubi-server/src/client.rs +++ b/kubi-server/src/client.rs @@ -1,25 +1,11 @@ use shipyard::{Component, EntityId, Unique, AllStoragesView}; use hashbrown::HashMap; -use nohash_hasher::BuildNoHashHasher; use std::net::SocketAddr; -use kubi_shared::networking::client::ClientId; +pub use kubi_shared::networking::client::ClientIdMap; #[derive(Component, Clone, Copy)] pub struct ClientAddress(pub SocketAddr); -#[derive(Unique)] -pub struct ClientIdMap(pub HashMap>); -impl ClientIdMap { - pub fn new() -> Self { - Self(HashMap::with_hasher(BuildNoHashHasher::default())) - } -} -impl Default for ClientIdMap { - fn default() -> Self { - Self::new() - } -} - #[derive(Unique, Default)] pub struct ClientAddressMap(pub HashMap); impl ClientAddressMap { diff --git a/kubi-server/src/world/chunk.rs b/kubi-server/src/world/chunk.rs index bfecbcd..91eed17 100644 --- a/kubi-server/src/world/chunk.rs +++ b/kubi-server/src/world/chunk.rs @@ -25,7 +25,7 @@ impl Chunk { position, state: ChunkState::Nothing, blocks: None, - subscriptions: HashSet::with_hasher(BuildNoHashHasher::default()), + subscriptions: HashSet::with_capacity_and_hasher(4, BuildNoHashHasher::default()), } } } diff --git a/kubi-shared/Cargo.toml b/kubi-shared/Cargo.toml index 7c1fb5e..4ffb46e 100644 --- a/kubi-shared/Cargo.toml +++ b/kubi-shared/Cargo.toml @@ -4,8 +4,6 @@ version = "0.0.0" edition = "2021" publish = false -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] glam = { version = "0.23", features = ["debug-glam-assert", "fast-math", "serde"] } shipyard = { git = "https://github.com/leudz/shipyard", rev = "a4f4d27edcf", default-features = false, features = ["std"] } @@ -16,7 +14,9 @@ anyhow = "1.0" bracket-noise = "0.8" rand = { version = "0.8", default_features = false, features = ["std", "min_const_gen"] } rand_xoshiro = "0.6" +hashbrown = "0.13" +nohash-hasher = "0.2.0" [features] default = [] -nightly = ["rand/nightly", "rand/simd_support", "glam/core-simd"] +nightly = ["hashbrown/nightly", "rand/nightly", "rand/simd_support", "glam/core-simd"] diff --git a/kubi-shared/src/networking/client.rs b/kubi-shared/src/networking/client.rs index ac4029a..c4baa5c 100644 --- a/kubi-shared/src/networking/client.rs +++ b/kubi-shared/src/networking/client.rs @@ -1,6 +1,21 @@ -use shipyard::Component; +use shipyard::{Component, Unique, EntityId}; +use hashbrown::HashMap; +use nohash_hasher::BuildNoHashHasher; pub type ClientId = u16; #[derive(Component, Clone, Copy, Debug)] pub struct Client(pub ClientId); + +#[derive(Unique)] +pub struct ClientIdMap(pub HashMap>); +impl ClientIdMap { + pub fn new() -> Self { + Self(HashMap::with_capacity_and_hasher(16, BuildNoHashHasher::default())) + } +} +impl Default for ClientIdMap { + fn default() -> Self { + Self::new() + } +} diff --git a/kubi/Cargo.toml b/kubi/Cargo.toml index 9a9e6b8..5dba603 100644 --- a/kubi/Cargo.toml +++ b/kubi/Cargo.toml @@ -13,9 +13,9 @@ glam = { version = "0.23", features = ["debug-glam-assert", "fast-math"] } image = { version = "0.24", default_features = false, features = ["png"] } strum = { version = "0.24", features = ["derive"] } hashbrown = "0.13" +nohash-hasher = "0.2.0" rayon = "1.6" shipyard = { git = "https://github.com/leudz/shipyard", rev = "a4f4d27edcf", default-features = false, features = ["std", "proc", "thread_local"] } -nohash-hasher = "0.2.0" anyhow = "1.0" flume = "0.10" gilrs = { version = "0.10", default_features = false, features = ["xinput"] } diff --git a/kubi/src/networking/player.rs b/kubi/src/networking/player.rs index 590de5d..48d7e5e 100644 --- a/kubi/src/networking/player.rs +++ b/kubi/src/networking/player.rs @@ -1,15 +1,20 @@ use glam::Vec3; -use shipyard::{UniqueViewMut, View, IntoIter}; +use hashbrown::HashMap; +use nohash_hasher::BuildNoHashHasher; +use shipyard::{UniqueViewMut, View, IntoIter, Unique, EntityId, AllStoragesView}; use uflow::{SendMode, client::Event as ClientEvent}; use kubi_shared::networking::{ messages::{ClientToServerMessage, ServerToClientMessage, S_PLAYER_POSITION_CHANGED}, - channels::CHANNEL_MOVE, + channels::CHANNEL_MOVE, + client::{ClientId, ClientIdMap}, }; use crate::events::player_actions::PlayerActionEvent; use super::{UdpClient, NetworkEvent}; -pub fn init_net_player_map() { - //TODO +pub fn init_client_map( + storages: AllStoragesView, +) { + storages.add_unique(ClientIdMap::new()); } pub fn add_net_player() {