Improve codegen for remove and swap_remove on EntityList

This commit is contained in:
Amanieu d'Antras
2021-03-08 18:20:05 +00:00
parent 8bd1c33fec
commit b2abe74f25

View File

@@ -445,20 +445,8 @@ impl<T: EntityRef + ReservedValue> EntityList<T> {
} }
} }
/// Removes the element at position `index` from the list. Potentially linear complexity. /// Removes the last element from the list.
pub fn remove(&mut self, index: usize, pool: &mut ListPool<T>) { fn remove_last(&mut self, len: usize, pool: &mut ListPool<T>) {
let len;
{
let seq = self.as_mut_slice(pool);
len = seq.len();
debug_assert!(index < len);
// Copy elements down.
for i in index..len - 1 {
seq[i] = seq[i + 1];
}
}
// Check if we deleted the last element. // Check if we deleted the last element.
if len == 1 { if len == 1 {
self.clear(pool); self.clear(pool);
@@ -477,20 +465,34 @@ impl<T: EntityRef + ReservedValue> EntityList<T> {
pool.data[block] = T::new(len - 1); pool.data[block] = T::new(len - 1);
} }
/// Removes the element at position `index` from the list. Potentially linear complexity.
pub fn remove(&mut self, index: usize, pool: &mut ListPool<T>) {
let len;
{
let seq = self.as_mut_slice(pool);
len = seq.len();
debug_assert!(index < len);
// Copy elements down.
for i in index..len - 1 {
seq[i] = seq[i + 1];
}
}
self.remove_last(len, pool);
}
/// Removes the element at `index` in constant time by switching it with the last element of /// Removes the element at `index` in constant time by switching it with the last element of
/// the list. /// the list.
pub fn swap_remove(&mut self, index: usize, pool: &mut ListPool<T>) { pub fn swap_remove(&mut self, index: usize, pool: &mut ListPool<T>) {
let len = self.len(pool);
debug_assert!(index < len);
if index == len - 1 {
self.remove(index, pool);
} else {
{
let seq = self.as_mut_slice(pool); let seq = self.as_mut_slice(pool);
let len = seq.len();
debug_assert!(index < len);
if index != len - 1 {
seq.swap(index, len - 1); seq.swap(index, len - 1);
} }
self.remove(len - 1, pool);
} self.remove_last(len, pool);
} }
/// Grow the list by inserting `count` elements at `index`. /// Grow the list by inserting `count` elements at `index`.