Skip to content

Commit

Permalink
fix: lazy component buffer in static memory
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Feb 12, 2024
1 parent 1c1e5bd commit adb0ff8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 62 deletions.
7 changes: 1 addition & 6 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,6 @@ impl<T: ComponentValue> Component<T> {
pub fn name(&self) -> &'static str {
self.vtable.name
}

/// Returns all metadata components
pub fn get_meta(&self) -> ComponentBuffer {
self.vtable.meta.get(self.desc())
}
}

impl<T: ComponentValue> Metadata<T> for Component<T> {
Expand Down Expand Up @@ -451,7 +446,7 @@ impl ComponentDesc {
self.key.target.is_some()
}

pub(crate) fn get_meta(&self) -> ComponentBuffer {
pub(crate) fn create_meta(&self) -> ComponentBuffer {
self.vtable.meta.get(*self)
}

Expand Down
51 changes: 9 additions & 42 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,11 @@ macro_rules! component {
$(#[$outer])*
$vis fn $name($obj: $crate::Entity) -> $crate::Component<$ty> {

static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX);
fn meta(_desc: $crate::component::ComponentDesc) -> $crate::buffer::ComponentBuffer {
let mut _buffer = $crate::buffer::ComponentBuffer::new();

<$crate::metadata::Name as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer);
<$crate::Component<$ty> as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer);

$(
$(
<$metadata as $crate::metadata::Metadata::<$ty>>::attach(_desc, &mut _buffer);
)*
)*

_buffer
}

static META: $crate::vtable::LazyComponentBuffer = $crate::vtable::LazyComponentBuffer::new(meta);
static VTABLE: &$crate::vtable::ComponentVTable<$ty> =
&$crate::vtable::ComponentVTable::new(stringify!($name), &META);
use $crate::entity::EntityKind;
use $crate::relation::RelationExt;

static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX);
static VTABLE: &$crate::vtable::ComponentVTable<$ty> = $crate::component_vtable!($name: $ty $(=> [$($metadata),*])?);
$crate::Component::static_init(&COMPONENT_ID, EntityKind::COMPONENT, VTABLE).of($obj)
}

Expand All @@ -142,26 +126,10 @@ macro_rules! component {

$(#[$outer])*
$vis fn $name() -> $crate::Component<$ty> {
static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX);
fn meta(_desc: $crate::component::ComponentDesc) -> $crate::buffer::ComponentBuffer {
let mut _buffer = $crate::buffer::ComponentBuffer::new();

<$crate::metadata::Name as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer);
<$crate::Component<$ty> as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer);

$(
$(
<$metadata as $crate::metadata::Metadata::<$ty>>::attach(_desc, &mut _buffer);
)*
)*

_buffer
}

static META: $crate::vtable::LazyComponentBuffer = $crate::vtable::LazyComponentBuffer::new(meta);
static VTABLE: &$crate::vtable::ComponentVTable<$ty> =
&$crate::vtable::ComponentVTable::new(stringify!($name), &META);
use $crate::entity::EntityKind;

static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX);
static VTABLE: &$crate::vtable::ComponentVTable<$ty> = $crate::component_vtable!($name: $ty $(=> [$($metadata),*])?);
$crate::Component::static_init(&COMPONENT_ID, EntityKind::COMPONENT, VTABLE)
}

Expand Down Expand Up @@ -204,11 +172,10 @@ macro_rules! component_vtable {

}

static META: $crate::vtable::LazyComponentBuffer = $crate::vtable::LazyComponentBuffer::new(meta);
static VTABLE: &$crate::vtable::ComponentVTable<$ty> =
&$crate::vtable::ComponentVTable::new(stringify!($name), &META);
static VTABLE: $crate::vtable::ComponentVTable<$ty> =
$crate::vtable::ComponentVTable::new(stringify!($name), $crate::vtable::LazyComponentBuffer::new(meta));

VTABLE
&VTABLE
}

};
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod test {
foo: String => [crate::Debuggable],
}

let meta = foo().get_meta();
let meta = foo().desc().create_meta();

assert!(meta.get(debuggable()).is_some());
assert_eq!(meta.get(name()), Some(&"foo".into()));
Expand Down
6 changes: 3 additions & 3 deletions src/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct UntypedVTable {
pub(crate) dangling: fn() -> NonNull<u8>,
/// A metadata is a component which is attached to the component, such as
/// metadata or name
pub(crate) meta: &'static LazyComponentBuffer,
pub(crate) meta: LazyComponentBuffer,
}

impl UntypedVTable {
Expand All @@ -55,7 +55,7 @@ impl UntypedVTable {
/// Creates a new vtable of type `T`
pub(crate) const fn new<T: ComponentValue>(
name: &'static str,
meta: &'static LazyComponentBuffer,
meta: LazyComponentBuffer,
) -> Self {
unsafe fn drop_ptr<T>(x: *mut u8) {
x.cast::<T>().drop_in_place()
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<T> core::ops::Deref for ComponentVTable<T> {

impl<T: ComponentValue> ComponentVTable<T> {
/// Creates a new *typed* vtable of `T`
pub const fn new(name: &'static str, meta: &'static LazyComponentBuffer) -> Self {
pub const fn new(name: &'static str, meta: LazyComponentBuffer) -> Self {
Self {
inner: UntypedVTable::new::<T>(name, meta),
marker: PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl World {
}

let id = desc.key().id;
let mut meta = desc.get_meta();
let mut meta = desc.create_meta();
meta.set(component_info(), desc);
meta.set(name(), desc.name().into());

Expand Down Expand Up @@ -917,7 +917,7 @@ impl World {

let desc = component.desc();

let mut meta = desc.get_meta();
let mut meta = desc.create_meta();
meta.set(component_info(), desc);
meta.set(components::name(), desc.name().into());

Expand Down
17 changes: 9 additions & 8 deletions tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ use glam::{vec2, Vec2};
fn custom_component() {
let mut world = World::new();

static META: LazyComponentBuffer = LazyComponentBuffer::new(|desc| {
let mut buf = ComponentBuffer::new();
<Debuggable as Metadata<Vec2>>::attach(desc, &mut buf);
buf
});

static VTABLE: &ComponentVTable<Vec2> = &ComponentVTable::new("position", &META);
static VTABLE: ComponentVTable<Vec2> = ComponentVTable::new(
"position",
LazyComponentBuffer::new(|desc| {
let mut buf = ComponentBuffer::new();
<Debuggable as Metadata<Vec2>>::attach(desc, &mut buf);
buf
}),
);

let position = world.spawn_component(VTABLE);
let position = world.spawn_component(&VTABLE);

let id = Entity::builder()
.set(position, vec2(1.0, 6.4))
Expand Down

0 comments on commit adb0ff8

Please sign in to comment.