Skip to content

Commit

Permalink
Improve capacity handling for ColliderSet, RigidBodySet. (#726)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
waywardmonkeys authored Sep 16, 2024
1 parent c714ff8 commit e7e196d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- Fix `BroadphaseMultiSap` not being able to serialize correctly with serde_json.

### Added

- `RigidBodySet` and `ColliderSet` have a new constructor `with_capacity`.

### Modified

- `InteractionGroups` default value for `memberships` is now `GROUP_1` (#706)
Expand Down
8 changes: 8 additions & 0 deletions src/dynamics/rigid_body_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
std::mem::take(&mut self.modified_bodies)
}
Expand Down
11 changes: 11 additions & 0 deletions src/geometry/collider_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
std::mem::take(&mut self.modified_colliders)
}
Expand Down

0 comments on commit e7e196d

Please sign in to comment.