Skip to content

Commit

Permalink
refactor: always use sparse key for getting data in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
LechintanTudor committed Sep 15, 2024
1 parent 5e22eae commit 15ab587
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 36 deletions.
16 changes: 9 additions & 7 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ where
}

fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
<Q as QueryPart>::get(view, entity)
let key = <Q as QueryPart>::get_sparse_key(view, entity)?;
unsafe { Some(<Q as QueryPart>::get_sparse(view, key)) }
}

fn split_sparse<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Sparse<'a>) {
Expand All @@ -247,16 +248,16 @@ where
data: Self::Data<'a>,
entity: Entity,
) -> Option<Self::Item<'a>> {
let key = <Q as QueryPart>::get_sparse_key(sparse, entity)?;
Some(<Q as QueryPart>::get_sparse(data, key))
let key = <Q as QueryPart>::get_sparse_key_raw(sparse, entity)?;
Some(<Q as QueryPart>::get_sparse_raw(data, key))
}

fn split_dense_data<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Data<'a>) {
<Q as QueryPart>::split_dense_data(view)
}

unsafe fn get_dense(data: Self::Data<'_>, index: usize, entity: Entity) -> Self::Item<'_> {
<Q as QueryPart>::get_dense(data, index, entity)
<Q as QueryPart>::get_dense_raw(data, index, entity)
}

unsafe fn slice<'a>(
Expand Down Expand Up @@ -314,7 +315,8 @@ macro_rules! impl_query {
}

fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
Some(($($Ty::get(&mut view.$idx, entity)?,)+))
let key = ($($Ty::get_sparse_key(&view.$idx, entity)?,)+);
unsafe { Some(($($Ty::get_sparse(&mut view.$idx, key.$idx),)+)) }
}

fn split_sparse<'a>(
Expand Down Expand Up @@ -386,8 +388,8 @@ macro_rules! impl_query {
data: Self::Data<'a>,
entity: Entity,
) -> Option<Self::Item<'a>> {
let key = ($($Ty::get_sparse_key(sparse.$idx, entity)?,)+);
Some(($($Ty::get_sparse(data.$idx, key.$idx),)+))
let key = ($($Ty::get_sparse_key_raw(sparse.$idx, entity)?,)+);
Some(($($Ty::get_sparse_raw(data.$idx, key.$idx),)+))
}

fn split_dense_data<'a>(
Expand Down
102 changes: 73 additions & 29 deletions src/query/query_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ pub unsafe trait QueryPart {
#[must_use]
fn contains(view: &Self::View<'_>, entity: Entity) -> bool;

/// Returns the item mapped to `entity`, if any.
/// Returns the sparse key mapped to `entity`, if any.
#[must_use]
fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>>;
fn get_sparse_key(view: &Self::View<'_>, entity: Entity) -> Option<Self::SparseKey>;

/// Returns the item mapped to `key`, if any.
#[must_use]
unsafe fn get_sparse<'a>(view: &'a mut Self::View<'_>, key: Self::SparseKey) -> Self::Item<'a>;

/// Splits the view into its entities and sparse vecs.
#[must_use]
Expand All @@ -62,19 +66,19 @@ pub unsafe trait QueryPart {

/// Returns the sparse key extracted from the sparse vecs.
#[must_use]
fn get_sparse_key(sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey>;
fn get_sparse_key_raw(sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey>;

/// Returns the sparse data that can be accessed with the dense key.
#[must_use]
unsafe fn get_sparse(data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_>;
unsafe fn get_sparse_raw(data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_>;

/// Splits the view into its entities and data.
#[must_use]
fn split_dense_data<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Data<'a>);

/// Returns the item at the given `index` or `entity`.
#[must_use]
unsafe fn get_dense(data: Self::Data<'_>, index: usize, entity: Entity) -> Self::Item<'_>;
unsafe fn get_dense_raw(data: Self::Data<'_>, index: usize, entity: Entity) -> Self::Item<'_>;

/// Slices the data at the given `range`.
#[must_use]
Expand Down Expand Up @@ -109,10 +113,18 @@ unsafe impl QueryPart for Entity {
}

#[inline]
fn get<'a>(_view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
fn get_sparse_key(_view: &Self::View<'_>, entity: Entity) -> Option<Self::SparseKey> {
Some(entity)
}

#[inline]
unsafe fn get_sparse<'a>(
_view: &'a mut Self::View<'_>,
key: Self::SparseKey,
) -> Self::Item<'a> {
key
}

#[inline]
fn split_sparse<'a>(_view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Sparse<'a>) {
(None, ())
Expand All @@ -131,12 +143,15 @@ unsafe impl QueryPart for Entity {
}

#[inline]
fn get_sparse_key<'a>(_sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
fn get_sparse_key_raw<'a>(
_sparse: Self::Sparse<'_>,
entity: Entity,
) -> Option<Self::SparseKey> {
Some(entity)
}

#[inline]
unsafe fn get_sparse(_data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_> {
unsafe fn get_sparse_raw(_data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_> {
key
}

Expand All @@ -146,7 +161,11 @@ unsafe impl QueryPart for Entity {
}

#[inline]
unsafe fn get_dense(_part: Self::Data<'_>, _index: usize, entity: Entity) -> Self::Item<'_> {
unsafe fn get_dense_raw(
_part: Self::Data<'_>,
_index: usize,
entity: Entity,
) -> Self::Item<'_> {
entity
}

Expand Down Expand Up @@ -190,8 +209,12 @@ where
view.contains(entity)
}

fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
view.get(entity)
fn get_sparse_key(view: &Self::View<'_>, entity: Entity) -> Option<Self::SparseKey> {
view.sparse().get(entity).map(|i| i as usize)
}

unsafe fn get_sparse<'a>(view: &'a mut Self::View<'_>, key: Self::SparseKey) -> Self::Item<'a> {
view.as_non_null_ptr().add(key).as_ref()
}

fn split_sparse<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Sparse<'a>) {
Expand All @@ -208,19 +231,19 @@ where
(Some(view.entities()), view.sparse(), view.as_non_null_ptr())
}

fn get_sparse_key<'a>(sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
fn get_sparse_key_raw<'a>(sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
Some(sparse.get_sparse(entity.sparse())? as usize)
}

unsafe fn get_sparse(data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_> {
unsafe fn get_sparse_raw(data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_> {
data.add(key).as_ref()
}

fn split_dense_data<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Data<'a>) {
(Some(view.entities()), view.as_non_null_ptr())
}

unsafe fn get_dense(ptr: Self::Data<'_>, index: usize, _entity: Entity) -> Self::Item<'_> {
unsafe fn get_dense_raw(ptr: Self::Data<'_>, index: usize, _entity: Entity) -> Self::Item<'_> {
ptr.add(index).as_ref()
}

Expand Down Expand Up @@ -263,8 +286,12 @@ where
view.contains(entity)
}

fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
view.get_mut(entity)
fn get_sparse_key(view: &Self::View<'_>, entity: Entity) -> Option<Self::SparseKey> {
view.sparse().get(entity).map(|i| i as usize)
}

unsafe fn get_sparse<'a>(view: &'a mut Self::View<'_>, key: Self::SparseKey) -> Self::Item<'a> {
view.as_non_null_ptr().add(key).as_mut()
}

fn split_sparse<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Sparse<'a>) {
Expand All @@ -281,19 +308,19 @@ where
(Some(view.entities()), view.sparse(), view.as_non_null_ptr())
}

fn get_sparse_key(sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
fn get_sparse_key_raw(sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
Some(sparse.get_sparse(entity.sparse())? as usize)
}

unsafe fn get_sparse(data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_> {
unsafe fn get_sparse_raw(data: Self::Data<'_>, key: Self::SparseKey) -> Self::Item<'_> {
data.add(key).as_mut()
}

fn split_dense_data<'a>(view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Data<'a>) {
(Some(view.entities()), view.as_non_null_ptr())
}

unsafe fn get_dense(ptr: Self::Data<'_>, index: usize, _entity: Entity) -> Self::Item<'_> {
unsafe fn get_dense_raw(ptr: Self::Data<'_>, index: usize, _entity: Entity) -> Self::Item<'_> {
ptr.add(index).as_mut()
}

Expand Down Expand Up @@ -329,8 +356,12 @@ where
true
}

fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
Some(view.get(entity))
fn get_sparse_key(_view: &Self::View<'_>, entity: Entity) -> Option<Self::SparseKey> {
Some(entity)
}

unsafe fn get_sparse<'a>(view: &'a mut Self::View<'_>, key: Self::SparseKey) -> Self::Item<'a> {
view.get(key)
}

fn split_sparse<'a>(_view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Sparse<'a>) {
Expand All @@ -347,11 +378,14 @@ where
(None, (), (view.sparse(), view.as_non_null_ptr()))
}

fn get_sparse_key(_sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
fn get_sparse_key_raw(_sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
Some(entity)
}

unsafe fn get_sparse((sparse, ptr): Self::Data<'_>, entity: Self::SparseKey) -> Self::Item<'_> {
unsafe fn get_sparse_raw(
(sparse, ptr): Self::Data<'_>,
entity: Self::SparseKey,
) -> Self::Item<'_> {
sparse
.get_sparse(entity.sparse())
.map(|dense| ptr.add(dense as usize).as_ref())
Expand All @@ -361,7 +395,7 @@ where
(None, (view.sparse(), view.as_non_null_ptr()))
}

unsafe fn get_dense(
unsafe fn get_dense_raw(
(sparse, ptr): Self::Data<'_>,
_index: usize,
entity: Entity,
Expand Down Expand Up @@ -403,8 +437,12 @@ where
true
}

fn get<'a>(view: &'a mut Self::View<'_>, entity: Entity) -> Option<Self::Item<'a>> {
Some(view.get_mut(entity))
fn get_sparse_key(_view: &Self::View<'_>, entity: Entity) -> Option<Self::SparseKey> {
Some(entity)
}

unsafe fn get_sparse<'a>(view: &'a mut Self::View<'_>, key: Self::SparseKey) -> Self::Item<'a> {
view.get_mut(key)
}

fn split_sparse<'a>(_view: &'a Self::View<'_>) -> (Option<&'a [Entity]>, Self::Sparse<'a>) {
Expand All @@ -421,11 +459,17 @@ where
(None, (), (view.sparse(), view.as_non_null_ptr()))
}

fn get_sparse_key<'a>(_sparse: Self::Sparse<'_>, entity: Entity) -> Option<Self::SparseKey> {
fn get_sparse_key_raw<'a>(
_sparse: Self::Sparse<'_>,
entity: Entity,
) -> Option<Self::SparseKey> {
Some(entity)
}

unsafe fn get_sparse((sparse, ptr): Self::Data<'_>, entity: Self::SparseKey) -> Self::Item<'_> {
unsafe fn get_sparse_raw(
(sparse, ptr): Self::Data<'_>,
entity: Self::SparseKey,
) -> Self::Item<'_> {
sparse
.get_sparse(entity.sparse())
.map(|dense| ptr.add(dense as usize).as_mut())
Expand All @@ -435,7 +479,7 @@ where
(None, (view.sparse(), view.as_non_null_ptr()))
}

unsafe fn get_dense(
unsafe fn get_dense_raw(
(sparse, ptr): Self::Data<'_>,
_index: usize,
entity: Entity,
Expand Down

0 comments on commit 15ab587

Please sign in to comment.