Skip to content

Commit 6087701

Browse files
committed
Removing some more allocations in signalling
1 parent dbddb68 commit 6087701

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Source/Orts.Simulation/Simulation/Signalling/SignalObject.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public enum Permission
140140

141141
private readonly List<int> SectionsWithAlternativePath = new List<int>();
142142
private readonly List<int> SectionsWithAltPathSet = new List<int>();
143+
private readonly static List<int> sectionsInRoute = new List<int>();
143144

144145
public bool enabled
145146
{
@@ -2157,7 +2158,7 @@ public bool requestClearSignal(Train.TCSubpathRoute RoutePart, Train.TrainRouted
21572158
// copy sections upto next normal signal
21582159
// check for loop
21592160

2160-
List<int> sectionsInRoute = new List<int>();
2161+
sectionsInRoute.Clear();
21612162

21622163
for (int iNode = foundFirstSection; iNode < RoutePart.Count && foundLastSection < 0; iNode++)
21632164
{

Source/Orts.Simulation/Simulation/Signalling/TrackCircuitState.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class TrackCircuitState
3636
public int RemoteReserved; // remote reserved (number only) //
3737
public bool Forced; // forced by human dispatcher //
3838

39+
List<Train.TrainRouted> TrainOccupyList; // used for allocation-free returning of the TrainOccuping() result //
40+
3941
public TrackCircuitState()
4042
{
4143
TrainOccupy = new TrainOccupyState();
@@ -253,12 +255,13 @@ public void Save(BinaryWriter outf)
253255
/// </summary>
254256
public List<Train.TrainRouted> TrainsOccupying()
255257
{
256-
List<Train.TrainRouted> reqList = new List<Train.TrainRouted>();
258+
TrainOccupyList = TrainOccupyList ?? new List<Train.TrainRouted>();
259+
TrainOccupyList.Clear();
257260
foreach (KeyValuePair<Train.TrainRouted, int> thisTCT in TrainOccupy)
258261
{
259-
reqList.Add(thisTCT.Key);
262+
TrainOccupyList.Add(thisTCT.Key);
260263
}
261-
return (reqList);
264+
return (TrainOccupyList);
262265
}
263266

264267
/// <summary>
@@ -267,15 +270,16 @@ public void Save(BinaryWriter outf)
267270
/// </summary>
268271
public List<Train.TrainRouted> TrainsOccupying(int reqDirection)
269272
{
270-
List<Train.TrainRouted> reqList = new List<Train.TrainRouted>();
273+
TrainOccupyList = TrainOccupyList ?? new List<Train.TrainRouted>();
274+
TrainOccupyList.Clear();
271275
foreach (KeyValuePair<Train.TrainRouted, int> thisTCT in TrainOccupy)
272276
{
273277
if (thisTCT.Value == reqDirection)
274278
{
275-
reqList.Add(thisTCT.Key);
279+
TrainOccupyList.Add(thisTCT.Key);
276280
}
277281
}
278-
return (reqList);
282+
return (TrainOccupyList);
279283
}
280284

281285
/// <summary>

0 commit comments

Comments
 (0)