Skip to content

Commit

Permalink
Use EntityHashMap instead of all HashMap with entities as keys
Browse files Browse the repository at this point in the history
For details see bevyengine/bevy#9903.
  • Loading branch information
Shatur committed Nov 24, 2023
1 parent 664458e commit 979c902
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Use `EntityHashMap` instead of all `HashMap` with entities as keys.

## [0.17.0] - 2023-11-13

### Added
Expand Down
14 changes: 7 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::Cursor;

use bevy::{
prelude::*,
utils::{Entry, HashMap},
utils::{hashbrown::hash_map::Entry, EntityHashMap},
};
use bevy_renet::{client_connected, renet::Bytes};
use bevy_renet::{renet::RenetClient, transport::NetcodeClientPlugin, RenetClientPlugin};
Expand Down Expand Up @@ -307,8 +307,8 @@ pub enum ClientSet {
/// Used only on client.
#[derive(Default, Resource)]
pub struct ServerEntityMap {
server_to_client: HashMap<Entity, Entity>,
client_to_server: HashMap<Entity, Entity>,
server_to_client: EntityHashMap<Entity, Entity>,
client_to_server: EntityHashMap<Entity, Entity>,
}

impl ServerEntityMap {
Expand Down Expand Up @@ -344,12 +344,12 @@ impl ServerEntityMap {
}

#[inline]
pub fn to_client(&self) -> &HashMap<Entity, Entity> {
pub fn to_client(&self) -> &EntityHashMap<Entity, Entity> {
&self.server_to_client
}

#[inline]
pub fn to_server(&self) -> &HashMap<Entity, Entity> {
pub fn to_server(&self) -> &EntityHashMap<Entity, Entity> {
&self.client_to_server
}

Expand All @@ -364,8 +364,8 @@ impl ServerEntityMap {
/// Spawns new client entity if a mapping doesn't exists.
pub struct ClientMapper<'a> {
world: &'a mut World,
server_to_client: &'a mut HashMap<Entity, Entity>,
client_to_server: &'a mut HashMap<Entity, Entity>,
server_to_client: &'a mut EntityHashMap<Entity, Entity>,
client_to_server: &'a mut EntityHashMap<Entity, Entity>,
}

impl<'a> ClientMapper<'a> {
Expand Down
4 changes: 2 additions & 2 deletions src/network_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod server_event;

use std::{marker::PhantomData, time::Duration};

use bevy::{prelude::*, utils::HashMap};
use bevy::{prelude::*, utils::EntityHashMap};
use bevy_renet::renet::SendType;

use crate::replicon_core::replication_rules::Mapper;
Expand Down Expand Up @@ -70,7 +70,7 @@ impl From<EventType> for SendType {
/// Maps server entities into client entities inside events.
///
/// Panics if a mapping doesn't exists.
pub struct EventMapper<'a>(pub &'a HashMap<Entity, Entity>);
pub struct EventMapper<'a>(pub &'a EntityHashMap<Entity, Entity>);

impl Mapper for EventMapper<'_> {
fn map(&mut self, entity: Entity) -> Entity {
Expand Down

0 comments on commit 979c902

Please sign in to comment.