Skip to content

Commit c58e718

Browse files
committed
Merge pull request #101071 from mihe/jolt/less-query-allocations
Improve performance of certain physics queries when using Jolt Physics
2 parents 427d808 + 5d2a54e commit c58e718

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

modules/jolt_physics/spaces/jolt_physics_direct_space_state_3d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool JoltPhysicsDirectSpaceState3D::_cast_motion_impl(const JPH::Shape &p_jolt_s
7676
aabb_translated.Translate(motion);
7777
aabb.Encapsulate(aabb_translated);
7878

79-
JoltQueryCollectorAnyMulti<JPH::CollideShapeBodyCollector, 2048> aabb_collector;
79+
JoltQueryCollectorAnyMulti<JPH::CollideShapeBodyCollector, 1024> aabb_collector;
8080
space->get_broad_phase_query().CollideAABox(aabb, aabb_collector, p_broad_phase_layer_filter, p_object_layer_filter);
8181

8282
if (!aabb_collector.had_hit()) {

modules/jolt_physics/spaces/jolt_query_collectors.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,26 @@
3434
#include "../jolt_project_settings.h"
3535
#include "jolt_space_3d.h"
3636

37-
#include "core/templates/local_vector.h"
38-
3937
#include "Jolt/Jolt.h"
4038

39+
#include "Jolt/Core/STLLocalAllocator.h"
4140
#include "Jolt/Physics/Collision/InternalEdgeRemovingCollector.h"
4241
#include "Jolt/Physics/Collision/Shape/Shape.h"
4342

4443
template <typename TBase, int TDefaultCapacity>
4544
class JoltQueryCollectorAll final : public TBase {
4645
public:
4746
typedef typename TBase::ResultType Hit;
47+
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
4848

4949
private:
50-
JPH::Array<Hit> hits;
50+
HitArray hits;
5151

5252
public:
53+
JoltQueryCollectorAll() {
54+
hits.reserve(TDefaultCapacity);
55+
}
56+
5357
bool had_hit() const {
5458
return !hits.is_empty();
5559
}
@@ -109,14 +113,17 @@ template <typename TBase, int TDefaultCapacity>
109113
class JoltQueryCollectorAnyMulti final : public TBase {
110114
public:
111115
typedef typename TBase::ResultType Hit;
116+
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity>> HitArray;
112117

113118
private:
114-
JPH::Array<Hit> hits;
119+
HitArray hits;
115120
int max_hits = 0;
116121

117122
public:
118123
explicit JoltQueryCollectorAnyMulti(int p_max_hits = TDefaultCapacity) :
119-
max_hits(p_max_hits) {}
124+
max_hits(p_max_hits) {
125+
hits.reserve(TDefaultCapacity);
126+
}
120127

121128
bool had_hit() const {
122129
return hits.size() > 0;
@@ -189,14 +196,17 @@ template <typename TBase, int TDefaultCapacity>
189196
class JoltQueryCollectorClosestMulti final : public TBase {
190197
public:
191198
typedef typename TBase::ResultType Hit;
199+
typedef JPH::Array<Hit, JPH::STLLocalAllocator<Hit, TDefaultCapacity + 1>> HitArray;
192200

193201
private:
194-
JPH::Array<Hit> hits;
202+
HitArray hits;
195203
int max_hits = 0;
196204

197205
public:
198206
explicit JoltQueryCollectorClosestMulti(int p_max_hits = TDefaultCapacity) :
199-
max_hits(p_max_hits) {}
207+
max_hits(p_max_hits) {
208+
hits.reserve(TDefaultCapacity + 1);
209+
}
200210

201211
bool had_hit() const {
202212
return hits.size() > 0;
@@ -220,7 +230,7 @@ class JoltQueryCollectorClosestMulti final : public TBase {
220230
}
221231

222232
virtual void AddHit(const Hit &p_hit) override {
223-
typename JPH::Array<Hit>::const_iterator E = hits.cbegin();
233+
typename HitArray::const_iterator E = hits.cbegin();
224234
for (; E != hits.cend(); ++E) {
225235
if (p_hit.GetEarlyOutFraction() < E->GetEarlyOutFraction()) {
226236
break;

0 commit comments

Comments
 (0)