Skip to content

Commit

Permalink
change caches impl to dashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Sep 13, 2023
1 parent 365bef8 commit da5a360
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 330 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/target
/.tuffous/config_gui.json
/.vscode
/.idea
/.VSCodeCounter
/Cargo.lock
*/Cargo.lock
Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ bytes = "1.4"
glam = "0.24"
anyhow = "*"
fastsnbt = "*"
fastnbt-rc = { version = "*", git = "https://github.com/rimecraft-rs/fastnbt" }
fastnbt = "*"
parking_lot = "0.12"
lazy-regex = "2"
lazy-regex = "3"
cesu8 = "*"
dashmap = "5.4"
bimap = "0.6"
Expand Down
2 changes: 1 addition & 1 deletion core/impls.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ TODO

## Nbt

implemented by `fastnbt_rc`.
implemented by `fastnbt`.

- [x] NbtCompound exts

Expand Down
6 changes: 1 addition & 5 deletions core/src/block/entity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
any::TypeId,
hash::Hash,
ops::{Deref, DerefMut},
};
use std::{any::TypeId, hash::Hash};

use crate::prelude::*;

Expand Down
105 changes: 54 additions & 51 deletions core/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub trait Attach {
}

/// Manager of components.
#[derive(Default)]
pub struct Components {
components: HashMap<crate::Id, (Box<dyn Attach + Send + Sync>, TypeId)>,
}
Expand All @@ -36,9 +37,7 @@ impl Components {
/// To create with external features,
/// see [`Self::builder()`].
pub fn new() -> Self {
Self {
components: HashMap::new(),
}
Default::default()
}

/// Creates a new [`ComponentsBuilder`].
Expand Down Expand Up @@ -68,35 +67,27 @@ impl Components {
where
T: Attach + Send + Sync + 'static,
{
self.components
.get(id)
.map(|value| {
if value.1 == TypeId::of::<T>() {
Some(unsafe { &*(&*value.0 as *const (dyn Attach + Send + Sync) as *const T) })
} else {
None
}
})
.flatten()
self.components.get(id).and_then(|value| {
if value.1 == TypeId::of::<T>() {
Some(unsafe { &*(&*value.0 as *const (dyn Attach + Send + Sync) as *const T) })
} else {
None
}
})
}

/// Get a mutable static typed component from this instance.
pub fn get_mut<T>(&mut self, id: &crate::Id) -> Option<&mut T>
where
T: Attach + Send + Sync + 'static,
{
self.components
.get_mut(id)
.map(|value| {
if value.1 == TypeId::of::<T>() {
Some(unsafe {
&mut *(&mut *value.0 as *mut (dyn Attach + Send + Sync) as *mut T)
})
} else {
None
}
})
.flatten()
self.components.get_mut(id).and_then(|value| {
if value.1 == TypeId::of::<T>() {
Some(unsafe { &mut *(&mut *value.0 as *mut (dyn Attach + Send + Sync) as *mut T) })
} else {
None
}
})
}
}

Expand All @@ -108,7 +99,7 @@ impl Encode for Components {
let Component(event) =
self.get::<Component<
crate::Event<dyn Fn(&mut HashMap<crate::Id, Bytes>) -> anyhow::Result<()>>,
>>(&*NET_SEND_ID)
>>(&NET_SEND_ID)
.expect("net send event component not found");

let mut hashmap = HashMap::new();
Expand All @@ -127,7 +118,7 @@ impl NetSync for Components {
crate::MutOnly<
crate::Event<dyn Fn(&mut HashMap<crate::Id, Bytes>) -> anyhow::Result<()>>,
>,
>>(&*NET_RECV_ID)
>>(&NET_RECV_ID)
.expect("net recv event component not found");

let mut hashmap = HashMap::<crate::Id, Bytes>::decode(buf)?;
Expand All @@ -143,16 +134,15 @@ impl serde::Serialize for Components {
let Component(event) = self
.get::<Component<
crate::Event<
dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt_rc::error::Result<()>,
dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt::error::Result<()>,
>,
>>(&*NBT_SAVE_ID)
>>(&NBT_SAVE_ID)
.expect("net send event component not found");

let mut hashmap = HashMap::new();

use serde::ser::Error;
event.invoker()(&mut hashmap)
.map_err(|err| <S as serde::Serializer>::Error::custom(err))?;
event.invoker()(&mut hashmap).map_err(<S as serde::Serializer>::Error::custom)?;
hashmap.serialize(serializer)
}
}
Expand All @@ -169,24 +159,37 @@ impl crate::nbt::Update for Components {
.get_mut::<Component<
crate::MutOnly<
crate::Event<
dyn Fn(
&mut HashMap<crate::Id, NbtElement>,
) -> fastnbt_rc::error::Result<()>,
dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt::error::Result<()>,
>,
>,
>>(&*NBT_READ_ID)
>>(&NBT_READ_ID)
.expect("net recv event component not found");

use serde::{de::Error, Deserialize};
let mut hashmap = HashMap::deserialize(deserializer)?;
event.as_mut().invoker()(&mut hashmap)
.map_err(|err| <D as serde::Deserializer<'_>>::Error::custom(err))
.map_err(<D as serde::Deserializer<'_>>::Error::custom)
}
}

impl From<ComponentsBuilder> for Components {
fn from(value: ComponentsBuilder) -> Self {
value.build()
}
}

static ATTACH_EVENTS: parking_lot::RwLock<crate::Event<dyn Fn(TypeId, &mut Components)>> =
parking_lot::RwLock::new(crate::Event::new(|listeners| {
Box::new(move |type_id, components| {
for listener in listeners {
listener(type_id, components)
}
})
}));

/// [`Components`] builder for creating with external features.
pub struct ComponentsBuilder {
inner: Components,
pub inner: Components,
}

impl ComponentsBuilder {
Expand All @@ -210,19 +213,21 @@ impl ComponentsBuilder {
self
}

pub fn register_defaults<T>(mut self) -> Self
where
T: 'static,
{
ATTACH_EVENTS.read().invoker()(TypeId::of::<T>(), &mut self.inner);
self
}

/// Build this instance into [`Components`].
#[inline]
pub fn build(self) -> Components {
self.inner
}
}

impl Into<Components> for ComponentsBuilder {
fn into(self) -> Components {
self.build()
}
}

/// Represents a simple component without extra
/// attach features, which has an empty
/// implementation of [`Attach`].
Expand Down Expand Up @@ -507,16 +512,14 @@ where
let _ = span.enter();

if let Some(Component(event)) = components.get_mut::<Component<
crate::Event<
dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt_rc::error::Result<()>,
>,
crate::Event<dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt::error::Result<()>>,
>>(&*NBT_SAVE_ID)
{
event.register(Box::new(move |map| {
let this = unsafe { &*ptr };
map.insert(
this.1.clone(),
this.0.serialize(&mut fastnbt_rc::value::Serializer)?,
this.0.serialize(&mut fastnbt::value::Serializer)?,
);

Ok(())
Expand All @@ -528,7 +531,7 @@ where
if let Some(Component(event)) = components.get_mut::<Component<
crate::MutOnly<
crate::Event<
dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt_rc::error::Result<()>,
dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt::error::Result<()>,
>,
>,
>>(&*NBT_READ_ID)
Expand Down Expand Up @@ -615,7 +618,7 @@ where
}

fn nbt_event_comp() -> Component<
crate::Event<dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt_rc::error::Result<()>>,
crate::Event<dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt::error::Result<()>>,
> {
Component(crate::Event::new(|listeners| {
Box::new(move |map| {
Expand All @@ -630,7 +633,7 @@ fn nbt_event_comp() -> Component<

fn nbt_event_comp_mut() -> Component<
crate::MutOnly<
crate::Event<dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt_rc::error::Result<()>>,
crate::Event<dyn Fn(&mut HashMap<crate::Id, NbtElement>) -> fastnbt::error::Result<()>>,
>,
> {
Component(crate::MutOnly::new(crate::Event::new(|listeners| {
Expand Down Expand Up @@ -732,7 +735,7 @@ mod tests {
let id_2 = crate::Id::new("test", "comp2".to_string());
components_0.register(id_2.clone(), Component(514_i32));

let nbt = fastnbt_rc::to_value(components_0).unwrap();
let nbt = fastnbt::to_value(components_0).unwrap();

let mut components_1 = Components::builder().nbt_storing().build();

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod entity;
pub mod fluid;
pub mod item;
/// Thin wrapper between Rimecraft modules
/// and [`fastnbt_rc`] and [`fastsnbt`].
/// and [`fastnbt`] and [`fastsnbt`].
pub mod nbt;
pub mod net;
/// Registry stuffs for managing almost all parts of in-game components.
Expand Down
Loading

0 comments on commit da5a360

Please sign in to comment.