Skip to content

Commit 6b44003

Browse files
committed
optimize JSIVariableGraph so it doesn't allocate memory
1 parent bae3dd3 commit 6b44003

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

RasterPropMonitor/Handlers/JSIVariableGraph.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*****************************************************************************
1+
/*****************************************************************************
22
* RasterPropMonitor
33
* =================
44
* Plugin for Kerbal Space Program
@@ -54,13 +54,12 @@ public class JSIVariableGraph : InternalModule
5454
private Texture2D backgroundTexture;
5555
// Because KSPField can't handle double. :E
5656
private double xGraphSpan, interval;
57-
private readonly List<Vector2> borderVertices = new List<Vector2>();
57+
private Vector2[] borderVertices;
5858
private bool startupComplete;
5959
private RasterPropMonitorComputer rpmComp;
6060

6161
public void Start()
6262
{
63-
6463
if (HighLogic.LoadedSceneIsEditor)
6564
{
6665
return;
@@ -106,16 +105,10 @@ public void Start()
106105
switch (borders)
107106
{
108107
case 2:
109-
borderVertices.Add(bottomRight);
110-
borderVertices.Add(bottomLeft);
111-
borderVertices.Add(topLeft);
108+
borderVertices = new Vector2[] {bottomRight, bottomLeft, topLeft};
112109
break;
113110
case 4:
114-
borderVertices.Add(bottomLeft);
115-
borderVertices.Add(topLeft);
116-
borderVertices.Add(topRight);
117-
borderVertices.Add(bottomRight);
118-
borderVertices.Add(bottomLeft);
111+
borderVertices = new Vector2[] {bottomLeft, topLeft, topRight, bottomRight, bottomLeft};
119112
break;
120113
}
121114

@@ -161,7 +154,7 @@ public bool RenderGraphs(RenderTexture screen, float cameraAspect)
161154
foreach (GraphLine graph in graphs)
162155
graph.Draw(graphSpace, time);
163156
if (borders > 0)
164-
GraphLine.DrawVector(borderVertices, borderColorValue);
157+
GraphLine.DrawVector(borderVertices, borderVertices.Length, borderColorValue);
165158

166159
GL.PopMatrix();
167160
return true;
@@ -188,17 +181,22 @@ public override void OnUpdate()
188181
private class GraphLine
189182
{
190183
private readonly Color32 lineColor;
191-
private readonly List<Vector2d> points = new List<Vector2d>();
184+
private readonly Vector2d[] points;
185+
private readonly Vector2[] actualXY;
192186
private readonly int maxPoints;
193187
private readonly VariableOrNumber variable;
194188
private readonly double horizontalSpan;
195189
// Analysis disable once FieldCanBeMadeReadOnly.Local
196190
private Vector2 verticalSpan;
197191
private bool floatingMax, floatingMin;
192+
private int nextPoint = 0;
193+
private int pointCount = 0;
198194

199195
public GraphLine(ConfigNode node, RasterPropMonitorComputer rpmComp, double xSpan, Vector2 ySpan, double secondsBetweenSamples)
200196
{
201197
maxPoints = (int)(xSpan / secondsBetweenSamples);
198+
points = new Vector2d[maxPoints];
199+
actualXY = new Vector2[maxPoints];
202200
horizontalSpan = xSpan;
203201
verticalSpan = ySpan;
204202
if (!node.HasData)
@@ -227,32 +225,32 @@ public GraphLine(ConfigNode node, RasterPropMonitorComputer rpmComp, double xSpa
227225
public void Draw(Rect screenRect, double time)
228226
{
229227
double mintime = time - horizontalSpan;
230-
if (floatingMin && points.Count > 0)
228+
if (floatingMin && pointCount > 0)
231229
{
232230
verticalSpan.x = (float)points[0].y;
233-
foreach (Vector2d dataPoint in points)
231+
for (int pointIndex = 0; pointIndex < pointCount; ++pointIndex)
234232
{
235-
verticalSpan.x = (float)Math.Min(dataPoint.y, verticalSpan.x);
233+
verticalSpan.x = (float)Math.Min(points[pointIndex].y, verticalSpan.x);
236234
}
237235
}
238-
if (floatingMax && points.Count > 0)
236+
if (floatingMax && pointCount > 0)
239237
{
240238
verticalSpan.y = (float)points[0].y;
241-
foreach (Vector2d dataPoint in points)
239+
for (int pointIndex = 0; pointIndex < pointCount; ++pointIndex)
242240
{
243-
verticalSpan.y = (float)Math.Max(dataPoint.y, verticalSpan.y);
241+
verticalSpan.y = (float)Math.Max(points[pointIndex].y, verticalSpan.y);
244242
}
245243
}
246-
var actualXY = new List<Vector2>();
247-
foreach (Vector2d dataPoint in points)
244+
245+
for (int pointIndex = 0; pointIndex < pointCount; ++pointIndex)
248246
{
247+
var dataPoint = points[pointIndex];
249248
if (dataPoint.x > mintime)
250-
actualXY.Add(new Vector2(
249+
actualXY[pointIndex] = new Vector2(
251250
(float)JUtil.DualLerp(screenRect.xMin, screenRect.xMax, mintime, time, dataPoint.x),
252-
(float)JUtil.DualLerp(screenRect.yMin, screenRect.yMax, verticalSpan.x, verticalSpan.y, dataPoint.y)
253-
));
251+
(float)JUtil.DualLerp(screenRect.yMin, screenRect.yMax, verticalSpan.x, verticalSpan.y, dataPoint.y));
254252
}
255-
DrawVector(actualXY, lineColor);
253+
DrawVector(actualXY, pointCount, lineColor);
256254
}
257255

258256
public void Update(double time)
@@ -262,24 +260,26 @@ public void Update(double time)
262260
{
263261
return;
264262
}
265-
points.Add(new Vector2d(time, value));
266-
if (points.Count > maxPoints)
263+
points[nextPoint++] = new Vector2d(time, value);
264+
++pointCount;
265+
if (nextPoint == maxPoints)
267266
{
268-
points.RemoveRange(0, points.Count - maxPoints);
267+
nextPoint = 0;
268+
pointCount = maxPoints;
269269
}
270270
}
271271

272-
public static void DrawVector(List<Vector2> points, Color32 lineColor)
272+
public static void DrawVector(Vector2[] points, int numPoints, Color32 lineColor)
273273
{
274-
if (points.Count < 2)
274+
if (numPoints < 2)
275275
return;
276276
GL.Begin(GL.LINES);
277277
lineMaterial.SetPass(0);
278278
GL.Color(lineColor);
279279

280280
Vector2 start, end;
281281
start = points[0];
282-
for (int i = 1; i < points.Count; i++)
282+
for (int i = 1; i < numPoints; i++)
283283
{
284284
end = points[i];
285285
GL.Vertex(start);

0 commit comments

Comments
 (0)