Core: use span<T> to cut down on array copys

This commit is contained in:
hondacrx
2018-12-24 01:02:49 -05:00
parent 581d0fd1ff
commit 40564a73ce
6 changed files with 32 additions and 24 deletions
@@ -634,9 +634,10 @@ namespace Game.Movement
uint ns = 0;
while (ns < nsteerPath)
{
Span<float> span = steerPath;
// Stop at Off-Mesh link or when point is further than slop away.
if ((steerPathFlags[ns].HasAnyFlag((byte)Detour.dtStraightPathFlags.DT_STRAIGHTPATH_OFFMESH_CONNECTION) ||
!InRangeYZX(steerPath.Skip((int)ns * 3).ToArray(), startPos, minTargetDist, 1000.0f)))
!InRangeYZX(span.Slice((int)ns * 3).ToArray(), startPos, minTargetDist, 1000.0f)))
break;
ns++;
}
@@ -45,9 +45,9 @@ namespace Game.Movement
_msToNext = info.TimeToNext;
}
uint SendPathSpline(Unit me, List<Vector3> wp)
uint SendPathSpline(Unit me, Span<Vector3> wp)
{
int numWp = wp.Count;
int numWp = wp.Length;
Cypher.Assert(numWp > 1, "Every path must have source & destination");
MoveSplineInit init = new MoveSplineInit(me);
if (numWp > 2)
@@ -64,7 +64,7 @@ namespace Game.Movement
Log.outDebug(LogFilter.Movement, "{0}: Sending spline for {1}.", me.GetGUID().ToString(), index);
SplineChainLink thisLink = _chain[index];
uint actualDuration = SendPathSpline(me, thisLink.Points);
uint actualDuration = SendPathSpline(me, new Span<Vector3>(thisLink.Points.ToArray()));
if (actualDuration != thisLink.ExpectedDuration)
{
Log.outDebug(LogFilter.Movement, "{0}: Sent spline for {1}, duration is {2} ms. Expected was {3} ms (delta {4} ms). Adjusting.", me.GetGUID().ToString(), index, actualDuration, thisLink.ExpectedDuration, actualDuration - thisLink.ExpectedDuration);
@@ -90,9 +90,8 @@ namespace Game.Movement
Log.outError(LogFilter.Movement, "{0}: Attempted to resume spline chain from invalid resume state ({1}, {2}).", me.GetGUID().ToString(), _nextIndex, _nextFirstWP);
_nextFirstWP = (byte)(thisLink.Points.Count - 1);
}
List<Vector3> partial = new List<Vector3>();
partial.AddRange(thisLink.Points.Skip(_nextFirstWP - 1).ToArray());
SendPathSpline(me, partial);
Span<Vector3> span = thisLink.Points.ToArray();
SendPathSpline(me, span.Slice(_nextFirstWP - 1));
Log.outDebug(LogFilter.Movement, "{0}: Resumed spline chain generator from resume state.", me.GetGUID().ToString());
++_nextIndex;
if (_msToNext == 0)