Improve capacity handling for ColliderSet, RigidBodySet. (#726)

These allow an application to reduce the cost of reallocation when
they know that a large number of colliders or rigid bodies will
be created.
This commit is contained in:
Bruce Mitchener
2024-09-16 21:45:14 +07:00
committed by GitHub
parent c714ff81f2
commit e7e196d9f9
3 changed files with 23 additions and 0 deletions

View File

@@ -5,6 +5,10 @@
- The region key has been replaced by an i64 in the f64 version of rapier, increasing the range before panics occur. - The region key has been replaced by an i64 in the f64 version of rapier, increasing the range before panics occur.
- Fix `BroadphaseMultiSap` not being able to serialize correctly with serde_json. - Fix `BroadphaseMultiSap` not being able to serialize correctly with serde_json.
### Added
- `RigidBodySet` and `ColliderSet` have a new constructor `with_capacity`.
### Modified ### Modified
- `InteractionGroups` default value for `memberships` is now `GROUP_1` (#706) - `InteractionGroups` default value for `memberships` is now `GROUP_1` (#706)

View File

@@ -43,6 +43,14 @@ impl RigidBodySet {
} }
} }
/// Create a new set of rigid bodies, with an initial capacity.
pub fn with_capacity(capacity: usize) -> Self {
RigidBodySet {
bodies: Arena::with_capacity(capacity),
modified_bodies: Vec::with_capacity(capacity),
}
}
pub(crate) fn take_modified(&mut self) -> Vec<RigidBodyHandle> { pub(crate) fn take_modified(&mut self) -> Vec<RigidBodyHandle> {
std::mem::take(&mut self.modified_bodies) std::mem::take(&mut self.modified_bodies)
} }

View File

@@ -23,6 +23,17 @@ impl ColliderSet {
} }
} }
/// Create a new set of colliders, with an initial capacity
/// for the set of colliders as well as the tracking of
/// modified colliders.
pub fn with_capacity(capacity: usize) -> Self {
ColliderSet {
colliders: Arena::with_capacity(capacity),
modified_colliders: Vec::with_capacity(capacity),
removed_colliders: Vec::new(),
}
}
pub(crate) fn take_modified(&mut self) -> Vec<ColliderHandle> { pub(crate) fn take_modified(&mut self) -> Vec<ColliderHandle> {
std::mem::take(&mut self.modified_colliders) std::mem::take(&mut self.modified_colliders)
} }