Skip to content

Commit 5efc96d

Browse files
committed
Refactor: Remove 'in' modifier for small ID structs
- Removed `in` modifier from `B2ShapeId`, `B2BodyId`, and `B2ContactId` parameters in various functions and delegates. - These structs are small (8 bytes or less), so passing them by value is more efficient than passing by reference. - This change avoids potential pointer indirection overhead and aligns with C# performance best practices for small structs.
1 parent 9579bb9 commit 5efc96d

22 files changed

+92
-92
lines changed

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(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
166+
static float CastCallback(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(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, flo
173173
}
174174

175175

176-
static bool OverlapCallback(in B2ShapeId shapeId, object context)
176+
static bool OverlapCallback(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(in B2ShapeId idA, in B2ShapeId idB)
146+
private bool Filter(B2ShapeId idA, B2ShapeId idB)
147147
{
148148
ShapeUserData userData = null;
149149
if (b2Shape_IsSensor(idA))
@@ -163,7 +163,7 @@ private bool Filter(in B2ShapeId idA, in B2ShapeId idB)
163163
return true;
164164
}
165165

166-
private static bool FilterFcn(in B2ShapeId idA, in B2ShapeId idB, object context)
166+
private static bool FilterFcn(B2ShapeId idA, 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(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
96+
private static float CastCallback(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(in B2ShapeId shapeId, ref B2PlaneResult planeResult, object context)
500+
static bool PlaneResultFcn(B2ShapeId shapeId, ref B2PlaneResult planeResult, object context)
501501
{
502502
B2_ASSERT(planeResult.hit == true);
503503

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

524-
static bool Kick(in B2ShapeId shapeId, object context)
524+
static bool Kick(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(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
559+
static float RayCastClosestCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
560560
{
561561
CastContext rayContext = (CastContext)context;
562562

@@ -584,7 +584,7 @@ static float RayCastClosestCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 n
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(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
587+
static float RayCastAnyCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
588588
{
589589
CastContext rayContext = (CastContext)context;
590590

@@ -614,7 +614,7 @@ static float RayCastAnyCallback(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 norma
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(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
617+
static float RayCastMultipleCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
618618
{
619619
CastContext rayContext = (CastContext)context;
620620

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

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

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(in B2ShapeId shapeId, object context)
67+
static bool OverlapResultFcn(B2ShapeId shapeId, object context)
6868
{
6969
ShapeUserData userData = b2Shape_GetUserData(shapeId).GetRef<ShapeUserData>();
7070
if (userData != null && userData.ignore)

src/Box2D.NET.Samples/Samples/Events/Platform.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public Platform(SampleContext context) : base(context)
116116
m_jumping = false;
117117
}
118118

119-
private static bool PreSolveStatic(in B2ShapeId shapeIdA, in B2ShapeId shapeIdB, B2Vec2 point, B2Vec2 normal, object context)
119+
private static bool PreSolveStatic(B2ShapeId shapeIdA, B2ShapeId shapeIdB, B2Vec2 point, B2Vec2 normal, object context)
120120
{
121121
Platform self = context as Platform;
122122
return self.PreSolve(shapeIdA, shapeIdB, point, normal);
@@ -125,7 +125,7 @@ private static bool PreSolveStatic(in B2ShapeId shapeIdA, in B2ShapeId shapeIdB,
125125
// This callback must be thread-safe. It may be called multiple times simultaneously.
126126
// Notice how this method is constant and doesn't change any data. It also
127127
// does not try to access any values in the world that may be changing, such as contact data.
128-
public bool PreSolve(in B2ShapeId shapeIdA, in B2ShapeId shapeIdB, B2Vec2 point, B2Vec2 normal)
128+
public bool PreSolve(B2ShapeId shapeIdA, B2ShapeId shapeIdB, B2Vec2 point, B2Vec2 normal)
129129
{
130130
B2_ASSERT(b2Shape_IsValid(shapeIdA));
131131
B2_ASSERT(b2Shape_IsValid(shapeIdB));

src/Box2D.NET.Samples/Samples/Events/SensorHits.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public override void UpdateGui()
189189
ImGui.End();
190190
}
191191

192-
void CollectTransforms(in B2ShapeId sensorShapeId)
192+
void CollectTransforms(B2ShapeId sensorShapeId)
193193
{
194194
const int capacity = 5;
195195
Span<B2ShapeId> visitorIds = stackalloc B2ShapeId[capacity];

src/Box2D.NET.Samples/Samples/Events/SensorTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public SensorTypes(SampleContext context) : base(context)
139139
}
140140
}
141141

142-
void PrintOverlaps(in B2ShapeId sensorShapeId, string prefix)
142+
void PrintOverlaps(B2ShapeId sensorShapeId, string prefix)
143143
{
144144
// Determine the necessary capacity
145145
int capacity = b2Shape_GetSensorCapacity(sensorShapeId);

src/Box2D.NET.Samples/Samples/Issues/ShapeCastChain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private bool ShapeCastSingle(ref PhysicsHitQueryResult2D outResult, B2Vec2 start
234234
return false;
235235
}
236236

237-
private static float b2CastResult_Closest(in B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object c)
237+
private static float b2CastResult_Closest(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object c)
238238
{
239239
CastContext_Single context = c as CastContext_Single;
240240

src/Box2D.NET.Samples/Samples/Sample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public void ResetText()
251251
m_textLine = m_textIncrement;
252252
}
253253

254-
public bool QueryCallback(in B2ShapeId shapeId, object context)
254+
public bool QueryCallback(B2ShapeId shapeId, object context)
255255
{
256256
QueryContext queryContext = context as QueryContext;
257257

0 commit comments

Comments
 (0)