Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink unsafe blocks for the Archetype/World impl #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,23 @@ impl Archetype {
}

fn grow(&mut self, increment: u32) {
unsafe {
let old_count = self.len as usize;
let new_cap = self.entities.len() + increment as usize;
let mut new_entities = vec![!0; new_cap].into_boxed_slice();
new_entities[0..old_count].copy_from_slice(&self.entities[0..old_count]);
self.entities = new_entities;

let old_data_size = mem::replace(&mut self.data_size, 0);
let mut state = HashMap::with_capacity_and_hasher(self.types.len(), Default::default());
for ty in &self.types {
self.data_size = align(self.data_size, ty.layout.align());
state.insert(ty.id, TypeState::new(self.data_size));
self.data_size += ty.layout.size() * new_cap;
}
let new_data = if self.data_size == 0 {
NonNull::dangling()
} else {
let old_count = self.len as usize;
let new_cap = self.entities.len() + increment as usize;
let mut new_entities = vec![!0; new_cap].into_boxed_slice();
new_entities[0..old_count].copy_from_slice(&self.entities[0..old_count]);
self.entities = new_entities;

let old_data_size = mem::replace(&mut self.data_size, 0);
let mut state = HashMap::with_capacity_and_hasher(self.types.len(), Default::default());
for ty in &self.types {
self.data_size = align(self.data_size, ty.layout.align());
state.insert(ty.id, TypeState::new(self.data_size));
self.data_size += ty.layout.size() * new_cap;
}
let new_data = if self.data_size == 0 {
NonNull::dangling()
} else {
unsafe {
NonNull::new(alloc(
Layout::from_size_align(
self.data_size,
Expand All @@ -266,17 +266,21 @@ impl Archetype {
.unwrap(),
))
.unwrap()
};
if old_data_size != 0 {
for ty in &self.types {
let old_off = self.state.get(&ty.id).unwrap().offset;
let new_off = state.get(&ty.id).unwrap().offset;
}
};
if old_data_size != 0 {
for ty in &self.types {
let old_off = self.state.get(&ty.id).unwrap().offset;
let new_off = state.get(&ty.id).unwrap().offset;
unsafe {
ptr::copy_nonoverlapping(
(*self.data.get()).as_ptr().add(old_off),
new_data.as_ptr().add(new_off),
ty.layout.size() * old_count,
);
}
}
unsafe {
dealloc(
(*self.data.get()).as_ptr().cast(),
Layout::from_size_align_unchecked(
Expand All @@ -285,10 +289,10 @@ impl Archetype {
),
);
}

self.data = UnsafeCell::new(new_data);
self.state = state;
}

self.data = UnsafeCell::new(new_data);
self.state = state;
}

/// Returns the ID of the entity moved into `index`, if any
Expand Down Expand Up @@ -396,7 +400,7 @@ impl Archetype {
.find(|typ| typ.id == component_type)
.map(|info| info.layout)
}

/// Add components from another archetype with identical components
///
/// # Safety
Expand Down
6 changes: 3 additions & 3 deletions src/query_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ impl<'a, Q: Query> QueryOne<'a, Q> {
if self.borrowed {
panic!("called QueryOnce::get twice; construct a new query instead");
}
let fetch = Q::Fetch::new(self.archetype)?;
self.borrowed = true;
Q::Fetch::borrow(self.archetype);
unsafe {
let fetch = Q::Fetch::new(self.archetype)?;
self.borrowed = true;
Q::Fetch::borrow(self.archetype);
Some(fetch.get(self.index as usize))
}
}
Expand Down
38 changes: 20 additions & 18 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ impl World {
});

let archetype = &mut self.archetypes[archetype_id as usize];
let index = unsafe { archetype.allocate(entity.id) };
unsafe {
let index = archetype.allocate(entity.id);
components.put(|ptr, ty| {
archetype.put_dynamic(ptr, ty.id(), ty.layout().size(), index);
});
self.entities.meta[entity.id as usize].location = Location {
archetype: archetype_id,
index,
};
}
self.entities.meta[entity.id as usize].location = Location {
archetype: archetype_id,
index,
};
}

/// Efficiently spawn a large number of entities with the same components
Expand Down Expand Up @@ -542,13 +542,12 @@ impl World {

self.flush();
let loc = self.entities.get_mut(entity)?;
unsafe {
// Assemble Vec<TypeInfo> for the final entity
let arch = &mut self.archetypes[loc.archetype as usize];
let mut info = arch.types().to_vec();
for ty in components.type_info() {
if let Some(ptr) = arch.get_dynamic(ty.id(), ty.layout().size(), loc.index) {
ty.drop(ptr.as_ptr());
if let Some(ptr) = unsafe { arch.get_dynamic(ty.id(), ty.layout().size(), loc.index) }{
unsafe { ty.drop(ptr.as_ptr()) };
} else {
info.push(ty);
}
Expand All @@ -571,9 +570,11 @@ impl World {
if target == loc.archetype {
// Update components in the current archetype
let arch = &mut self.archetypes[loc.archetype as usize];
components.put(|ptr, ty| {
arch.put_dynamic(ptr, ty.id(), ty.layout().size(), loc.index);
});
unsafe {
components.put(|ptr, ty| {
arch.put_dynamic(ptr, ty.id(), ty.layout().size(), loc.index);
});
}
return Ok(());
}

Expand All @@ -583,18 +584,19 @@ impl World {
loc.archetype as usize,
target as usize,
);
let target_index = target_arch.allocate(entity.id);
let target_index = unsafe { target_arch.allocate(entity.id) };
loc.archetype = target;
let old_index = mem::replace(&mut loc.index, target_index);
if let Some(moved) = source_arch.move_to(old_index, |ptr, ty, size| {
if let Some(moved) = unsafe { source_arch.move_to(old_index, |ptr, ty, size| {
target_arch.put_dynamic(ptr, ty, size, target_index);
}) {
}) }{
self.entities.meta[moved as usize].location.index = old_index;
}
components.put(|ptr, ty| {
target_arch.put_dynamic(ptr, ty.id(), ty.layout().size(), target_index);
});
}
unsafe {
components.put(|ptr, ty| {
target_arch.put_dynamic(ptr, ty.id(), ty.layout().size(), target_index);
});
}
Ok(())
}

Expand Down