Skip to content

Commit 0cec20b

Browse files
committed
perf: Optimize core math, dynamic trees, and delegates using 'in' modifier and Span
- Updated B2MathFunction methods to pass structs (B2Vec2, B2Rot, B2Transform, etc.) by read-only reference ('in') to reduce copying overhead. - Updated B2DynamicTrees methods to pass B2AABB by read-only reference ('in'). - Refactored b2PartitionSAH in B2DynamicTrees to use Span<B2AABB> instead of arrays for improved performance. - Updated delegate definitions in B2Delegates (e.g., b2CustomFilterFcn, b2PreSolveFcn, DrawSolidPolygonFcn) to use 'in' parameters for structs like B2ShapeId, B2Transform, and B2Vec2. - Adjusted related callback invocations and implementations across the project to align with these signature changes.
1 parent cfe4c33 commit 0cec20b

36 files changed

Lines changed: 200 additions & 200 deletions

src/Box2D.NET.Samples/Graphics/Draws.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public static void DrawPolygon(Draw draw, ReadOnlySpan<B2Vec2> vertices, int ver
6060
}
6161
}
6262

63-
public static void DrawSolidPolygon(Draw draw, ref B2Transform transform, ReadOnlySpan<B2Vec2> vertices, int vertexCount, float radius, B2HexColor color)
63+
public static void DrawSolidPolygon(Draw draw, in B2Transform transform, ReadOnlySpan<B2Vec2> vertices, int vertexCount, float radius, B2HexColor color)
6464
{
65-
AddPolygon(ref draw.polygons, ref transform, vertices, vertexCount, radius, color);
65+
AddPolygon(ref draw.polygons, transform, vertices, vertexCount, radius, color);
6666
}
6767

6868
public static void DrawTransform(Draw draw, B2Transform transform, float scale)

src/Box2D.NET.Samples/Graphics/SolidPolygons.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static void DestroyPolygons(GL gl, ref SolidPolygonRender render)
112112
}
113113
}
114114

115-
public static void AddPolygon(ref SolidPolygonRender render, ref B2Transform transform, ReadOnlySpan<B2Vec2> points, int count, float radius, B2HexColor color)
115+
public static void AddPolygon(ref SolidPolygonRender render, in B2Transform transform, ReadOnlySpan<B2Vec2> points, int count, float radius, B2HexColor color)
116116
{
117117
PolygonData data = new PolygonData();
118118
data.transform = transform;

src/Box2D.NET.Samples/SampleContext.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,49 +157,49 @@ public static void DrawPolygonFcn(ReadOnlySpan<B2Vec2> vertices, int vertexCount
157157
DrawPolygon(sampleContext.draw, vertices, vertexCount, color);
158158
}
159159

160-
public static void DrawSolidPolygonFcn(ref B2Transform transform, ReadOnlySpan<B2Vec2> vertices, int vertexCount, float radius, B2HexColor color, object context)
160+
public static void DrawSolidPolygonFcn(in B2Transform transform, ReadOnlySpan<B2Vec2> vertices, int vertexCount, float radius, B2HexColor color, object context)
161161
{
162162
SampleContext sampleContext = (SampleContext)context;
163-
DrawSolidPolygon(sampleContext.draw, ref transform, vertices, vertexCount, radius, color);
163+
DrawSolidPolygon(sampleContext.draw, transform, vertices, vertexCount, radius, color);
164164
}
165165

166-
public static void DrawCircleFcn(B2Vec2 center, float radius, B2HexColor color, object context)
166+
public static void DrawCircleFcn(in B2Vec2 center, float radius, B2HexColor color, object context)
167167
{
168168
SampleContext sampleContext = (SampleContext)context;
169169
DrawCircle(sampleContext.draw, center, radius, color);
170170
}
171171

172-
public static void DrawSolidCircleFcn(ref B2Transform transform, float radius, B2HexColor color, object context)
172+
public static void DrawSolidCircleFcn(in B2Transform transform, float radius, B2HexColor color, object context)
173173
{
174174
SampleContext sampleContext = (SampleContext)(context);
175175
DrawSolidCircle(sampleContext.draw, transform, radius, color);
176176
}
177177

178-
public static void DrawSolidCapsuleFcn(B2Vec2 p1, B2Vec2 p2, float radius, B2HexColor color, object context)
178+
public static void DrawSolidCapsuleFcn(in B2Vec2 p1, in B2Vec2 p2, float radius, B2HexColor color, object context)
179179
{
180180
SampleContext sampleContext = (SampleContext)(context);
181181
DrawSolidCapsule(sampleContext.draw, p1, p2, radius, color);
182182
}
183183

184-
public static void DrawLineFcn(B2Vec2 p1, B2Vec2 p2, B2HexColor color, object context)
184+
public static void DrawLineFcn(in B2Vec2 p1, in B2Vec2 p2, B2HexColor color, object context)
185185
{
186186
SampleContext sampleContext = (SampleContext)(context);
187187
DrawLine(sampleContext.draw, p1, p2, color);
188188
}
189189

190-
public static void DrawTransformFcn(B2Transform transform, object context)
190+
public static void DrawTransformFcn(in B2Transform transform, object context)
191191
{
192192
SampleContext sampleContext = (SampleContext)(context);
193193
DrawTransform(sampleContext.draw, transform, 1.0f);
194194
}
195195

196-
public static void DrawPointFcn(B2Vec2 p, float size, B2HexColor color, object context)
196+
public static void DrawPointFcn(in B2Vec2 p, float size, B2HexColor color, object context)
197197
{
198198
SampleContext sampleContext = (SampleContext)(context);
199199
DrawPoint(sampleContext.draw, p, size, color);
200200
}
201201

202-
public static void DrawStringFcn(B2Vec2 p, string s, B2HexColor color, object context)
202+
public static void DrawStringFcn(in B2Vec2 p, string s, B2HexColor color, object context)
203203
{
204204
SampleContext sampleContext = (SampleContext)(context);
205205
DrawWorldString(sampleContext.draw, sampleContext.camera, p, color, s);

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCast.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void BuildScene()
163163
m_minTime = 1e6f;
164164
}
165165

166-
static float CastCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
166+
static float CastCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
167167
{
168168
CastResult result = context as CastResult;
169169
result.point = point;
@@ -173,7 +173,7 @@ static float CastCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float
173173
}
174174

175175

176-
static bool OverlapCallback(B2ShapeId shapeId, object context)
176+
static bool OverlapCallback(in B2ShapeId shapeId, object context)
177177
{
178178
OverlapResult result = context as OverlapResult;
179179
if (result.count < 32)

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkSensor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void CreateRow(float y)
143143
}
144144
}
145145

146-
private bool Filter(B2ShapeId idA, B2ShapeId idB)
146+
private bool Filter(in B2ShapeId idA, in B2ShapeId idB)
147147
{
148148
ShapeUserData userData = null;
149149
if (b2Shape_IsSensor(idA))
@@ -163,7 +163,7 @@ private bool Filter(B2ShapeId idA, B2ShapeId idB)
163163
return true;
164164
}
165165

166-
private static bool FilterFcn(B2ShapeId idA, B2ShapeId idB, object context)
166+
private static bool FilterFcn(in B2ShapeId idA, in B2ShapeId idB, object context)
167167
{
168168
BenchmarkSensor self = context as BenchmarkSensor;
169169
return self.Filter(idA, idB);

src/Box2D.NET.Samples/Samples/Characters/Mover.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private static Sample Create(SampleContext context)
9393
return new Mover(context);
9494
}
9595

96-
private static float CastCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
96+
private static float CastCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
9797
{
9898
CastResult result = (CastResult)context;
9999
result.point = point;
@@ -497,7 +497,7 @@ public override void UpdateGui()
497497
ImGui.End();
498498
}
499499

500-
static bool PlaneResultFcn(B2ShapeId shapeId, ref B2PlaneResult planeResult, object context)
500+
static bool PlaneResultFcn(in B2ShapeId shapeId, ref B2PlaneResult planeResult, object context)
501501
{
502502
B2_ASSERT(planeResult.hit == true);
503503

@@ -521,7 +521,7 @@ static bool PlaneResultFcn(B2ShapeId shapeId, ref B2PlaneResult planeResult, obj
521521
return true;
522522
}
523523

524-
static bool Kick(B2ShapeId shapeId, object context)
524+
static bool Kick(in B2ShapeId shapeId, object context)
525525
{
526526
Mover self = (Mover)context;
527527
B2BodyId bodyId = b2Shape_GetBody(shapeId);

src/Box2D.NET.Samples/Samples/Collisions/CastWorld.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ public override void Draw()
556556

557557

558558
// This callback finds the closest hit. This is the most common callback used in games.
559-
static float RayCastClosestCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
559+
static float RayCastClosestCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
560560
{
561561
CastContext rayContext = (CastContext)context;
562562

@@ -584,7 +584,7 @@ static float RayCastClosestCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 norm
584584
// This callback finds any hit. For this type of query we are usually just checking for obstruction,
585585
// so the hit data is not relevant.
586586
// NOTE: shape hits are not ordered, so this may not return the closest hit
587-
static float RayCastAnyCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
587+
static float RayCastAnyCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
588588
{
589589
CastContext rayContext = (CastContext)context;
590590

@@ -614,7 +614,7 @@ static float RayCastAnyCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal,
614614
// NOTE: shape hits are not ordered, so this may return hits in any order. This means that
615615
// if you limit the number of results, you may discard the closest hit. You can see this
616616
// behavior in the sample.
617-
static float RayCastMultipleCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
617+
static float RayCastMultipleCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
618618
{
619619
CastContext rayContext = (CastContext)context;
620620

@@ -648,7 +648,7 @@ static float RayCastMultipleCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 nor
648648
}
649649

650650
// This ray cast collects multiple hits along the ray and sorts them.
651-
static float RayCastSortedCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
651+
static float RayCastSortedCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
652652
{
653653
CastContext rayContext = (CastContext)context;
654654

src/Box2D.NET.Samples/Samples/Collisions/DynamicTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static bool QueryCallback(int proxyId, ulong userData, ref DynamicTreeContext co
5858
return true;
5959
}
6060

61-
static float RayCallback(ref B2RayCastInput input, int proxyId, ulong userData, ref DynamicTreeContext context)
61+
static float RayCallback(in B2RayCastInput input, int proxyId, ulong userData, ref DynamicTreeContext context)
6262
{
6363
DynamicTree sample = context.tree;
6464
Proxy proxy = sample.m_proxies[userData];

src/Box2D.NET.Samples/Samples/Collisions/OverlapWorld.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static Sample Create(SampleContext context)
6464
}
6565

6666

67-
static bool OverlapResultFcn(B2ShapeId shapeId, object context)
67+
static bool OverlapResultFcn(in B2ShapeId shapeId, object context)
6868
{
6969
ShapeUserData userData = (ShapeUserData)b2Shape_GetUserData(shapeId);
7070
if (userData != null && userData.ignore)

src/Box2D.NET.Samples/Samples/Collisions/RayCast.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public override void Step()
237237
B2Vec2 translation = b2InvRotateVector(transform.q, b2Sub(m_rayEnd, m_rayStart));
238238
B2RayCastInput input = new B2RayCastInput(start, translation, maxFraction);
239239

240-
B2CastOutput localOutput = b2RayCastCircle(ref m_circle, ref input);
240+
B2CastOutput localOutput = b2RayCastCircle(m_circle, ref input);
241241
if (localOutput.hit)
242242
{
243243
output = localOutput;

0 commit comments

Comments
 (0)