Skip to content

Commit 4c87bc3

Browse files
committed
reduce capacity checks & collections marshall calls
1 parent b130d29 commit 4c87bc3

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

‎sources/Input/Input/Implementations/SDL3/Devices/Pointers/SdlPointerDevice.TargetPoints.cs‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ private unsafe ref TargetPoint CreateOrUpdateTargetPoint(IPointerTarget target,
3131
int? pointIndex = null;
3232
int? defaultIndex = null;
3333

34-
for (var i = 0; i < _actualPoints.Count; i++)
34+
var actualPointsSpan = CollectionsMarshal.AsSpan(_actualPoints);
35+
for (var i = 0; i < actualPointsSpan.Length; i++)
3536
{
36-
var candidatePoint = GetPointRef(i);
37+
ref readonly var candidatePoint = ref actualPointsSpan[i];
3738
if (candidatePoint.Target == target && candidatePoint.Id == touchId)
3839
{
3940
pointIndex = i;
@@ -50,14 +51,16 @@ private unsafe ref TargetPoint CreateOrUpdateTargetPoint(IPointerTarget target,
5051
if (pointIndex == null)
5152
{
5253
pointIndex = defaultIndex ?? _actualPoints.Count;
54+
EnsurePointsListCapacity(pointIndex.Value, _actualPoints);
55+
actualPointsSpan = CollectionsMarshal.AsSpan(_actualPoints);
5356
isNewPoint = true;
5457
}
5558
else
5659
{
5760
isNewPoint = false;
5861
}
5962

60-
ref var point = ref GetPointRef(pointIndex.Value);
63+
ref var point = ref actualPointsSpan[pointIndex.Value];
6164

6265
// note: a null oldPoint means this is a new point
6366
// see PointChangedEvent for more info
@@ -201,9 +204,10 @@ protected void AddOrUpdatePoint(uint? touchId, IPointerTarget target, in Vector3
201204
// point was actually removed - after that point changed event, we should remove it
202205
// note - a null newPoint means the point was removed
203206
var previous = point;
204-
for (var i = 0; i < _actualPoints.Count; i++)
207+
var actualPoints = CollectionsMarshal.AsSpan(_actualPoints);
208+
for (var i = 0; i < actualPoints.Length; i++)
205209
{
206-
ref var candidatePoint = ref GetPointRef(i);
210+
ref var candidatePoint = ref actualPoints[i];
207211
if (candidatePoint.Id == previous.Id)
208212
{
209213
candidatePoint = default;
@@ -270,15 +274,13 @@ protected void UpdatePointRay(uint? touchId, IPointerTarget target, float? xTilt
270274
NewPoint: point), sdlTimestamp);
271275
}
272276

273-
private ref TargetPoint GetPointRef(int index)
277+
private static void EnsurePointsListCapacity(int index, List<TargetPoint> actualPoints)
274278
{
275-
_actualPoints.EnsureCapacity(index + 1);
276-
while (index >= _actualPoints.Count)
279+
actualPoints.EnsureCapacity(index + 1);
280+
while (index >= actualPoints.Count)
277281
{
278-
_actualPoints.Add(default);
282+
actualPoints.Add(default);
279283
}
280-
281-
return ref CollectionsMarshal.AsSpan(_actualPoints)[index];
282284
}
283285

284286
public void TargetDestroyed(IPointerTarget target, long timestamp, ulong sdlTimestamp)

0 commit comments

Comments
 (0)