Skip to content

Commit 5da0aad

Browse files
committed
Reduce allocations in TT pre-run
1 parent 6087701 commit 5da0aad

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

Source/Orts.Simulation/Simulation/AIs/AITrain.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public enum AI_START_MOVEMENT
129129
public static float minStopDistanceM = 3.0f; // minimum clear distance for stopping at signal in station
130130
public static float signalApproachDistanceM = 20.0f; // final approach to signal
131131

132+
private readonly List<ObjectItemInfo> processedList = new List<ObjectItemInfo>(); // internal processing list for CheckSignalObjects()
133+
132134
#if WITH_PATH_DEBUG
133135
// Only for EnhancedActCompatibility
134136
public string currentAIState = "";
@@ -1164,10 +1166,12 @@ public void CheckSignalObjects()
11641166
}
11651167

11661168
float validSpeed = AllowedMaxSpeedMpS;
1167-
List<ObjectItemInfo> processedList = new List<ObjectItemInfo>();
1169+
processedList.Clear();
11681170

1169-
foreach (ObjectItemInfo thisInfo in SignalObjectItems.Where(item => !item.speed_isWarning))
1171+
foreach (ObjectItemInfo thisInfo in SignalObjectItems)
11701172
{
1173+
if (thisInfo.speed_isWarning)
1174+
continue;
11711175

11721176
// check speedlimit
11731177
if (CheckTrain)

Source/Orts.Simulation/Simulation/Physics/Train.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public TrainCar LastCar
9696
}
9797
public Traveller RearTDBTraveller; // positioned at the back of the last car in the train
9898
public Traveller FrontTDBTraveller; // positioned at the front of the train by CalculatePositionOfCars
99+
Traveller CalculatorTraveller; // CalculatePositionOfCars uses it for the calculations
99100
public float Length; // length of train from FrontTDBTraveller to RearTDBTraveller
100101
public float MassKg; // weight of the train
101102
public float SpeedMpS; // meters per second +ve forward, -ve when backing
@@ -420,6 +421,8 @@ public enum END_AUTHORITY
420421

421422
public bool nextRouteReady = false; // indication to activity.cs that a reversal has taken place
422423

424+
readonly List<int[]> sectionList = new List<int[]>(); // internally used in UpdateSectionState()
425+
423426
// Deadlock Info :
424427
// list of sections where deadlock begins
425428
// per section : list with trainno and end section
@@ -4378,7 +4381,9 @@ public void CalculatePositionOfCars(float elapsedTime, float distance)
43784381

43794382
// TODO : check if train moved back into previous section
43804383

4381-
var traveller = new Traveller(RearTDBTraveller);
4384+
CalculatorTraveller = CalculatorTraveller ?? new Traveller(RearTDBTraveller);
4385+
var traveller = CalculatorTraveller;
4386+
traveller.Copy(RearTDBTraveller);
43824387
// The traveller location represents the back of the train.
43834388
var length = 0f;
43844389

@@ -4453,7 +4458,7 @@ public void CalculatePositionOfCars(float elapsedTime, float distance)
44534458
car.UpdateFreightAnimationDiscretePositions();
44544459
}
44554460

4456-
FrontTDBTraveller = traveller;
4461+
(FrontTDBTraveller, CalculatorTraveller) = (CalculatorTraveller, FrontTDBTraveller);
44574462
Length = length;
44584463
travelled += distance;
44594464
} // CalculatePositionOfCars
@@ -6521,7 +6526,7 @@ public void ObtainRequiredActions(int backward)
65216526
public void UpdateSectionState(int backward)
65226527
{
65236528

6524-
List<int[]> sectionList = new List<int[]>();
6529+
sectionList.Clear();
65256530

65266531
int lastIndex = PreviousPosition[0].RouteListIndex;
65276532
int presentIndex = PresentPosition[0].RouteListIndex;
@@ -20025,6 +20030,7 @@ public RoughReversalInfo(int subPathIndex, float reverseReversalOffset, int reve
2002520030

2002620031
public class DistanceTravelledActions : LinkedList<DistanceTravelledItem>
2002720032
{
20033+
readonly List<DistanceTravelledItem> itemList = new List<DistanceTravelledItem>();
2002820034

2002920035
//================================================================================================//
2003020036
//
@@ -20111,7 +20117,7 @@ public void InsertClearSection(float distance, int sectionIndex)
2011120117

2011220118
public List<DistanceTravelledItem> GetActions(float distance)
2011320119
{
20114-
List<DistanceTravelledItem> itemList = new List<DistanceTravelledItem>();
20120+
itemList.Clear();
2011520121

2011620122
bool itemsCollected = false;
2011720123
LinkedListNode<DistanceTravelledItem> nextNode = this.First;
@@ -20136,7 +20142,7 @@ public List<DistanceTravelledItem> GetActions(float distance)
2013620142

2013720143
public List<DistanceTravelledItem> GetAuxActions(Train thisTrain, float distance)
2013820144
{
20139-
List<DistanceTravelledItem> itemList = new List<DistanceTravelledItem>();
20145+
itemList.Clear();
2014020146
LinkedListNode<DistanceTravelledItem> nextNode = this.First;
2014120147

2014220148
while (nextNode != null)
@@ -20159,7 +20165,7 @@ public List<DistanceTravelledItem> GetAuxActions(Train thisTrain, float distance
2015920165

2016020166
public List<DistanceTravelledItem> GetActions(float distance, Type reqType)
2016120167
{
20162-
List<DistanceTravelledItem> itemList = new List<DistanceTravelledItem>();
20168+
itemList.Clear();
2016320169

2016420170
bool itemsCollected = false;
2016520171
LinkedListNode<DistanceTravelledItem> nextNode = this.First;

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,7 @@ public override void Update(float elapsedClockSeconds)
18651865

18661866
AuxWagonType = "AuxiliaryTender";
18671867
}
1868-
else
1868+
else if (AuxWagonType == "")
18691869
{
18701870
AuxWagonType = WagonType.ToString();
18711871
}

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/LocomotivePowerSupply.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ public float ElectricTrainSupplyPowerW
5555
{
5656
get
5757
{
58-
return Train.Cars.OfType<MSTSWagon>()
59-
.Where(wagon => wagon.PassengerCarPowerSupply != null)
60-
.Where(wagon => wagon.PassengerCarPowerSupply.ElectricTrainSupplyConnectedLocomotives.Contains(Locomotive))
61-
.Select(wagon => wagon.PassengerCarPowerSupply.ElectricTrainSupplyPowerW / wagon.PassengerCarPowerSupply.ElectricTrainSupplyConnectedLocomotives.Count())
62-
.Sum();
58+
float result = 0;
59+
foreach (var car in Train.Cars)
60+
{
61+
if (car == null) continue;
62+
if (!(car is MSTSWagon wagon)) continue;
63+
if (!(wagon.PassengerCarPowerSupply?.ElectricTrainSupplyConnectedLocomotives.Contains(Locomotive) ?? false)) continue;
64+
result += wagon.PassengerCarPowerSupply.ElectricTrainSupplyPowerW / wagon.PassengerCarPowerSupply.ElectricTrainSupplyConnectedLocomotives.Count();
65+
}
66+
return result;
6367
}
6468
}
6569

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ public enum Permission
138138

139139
public bool CallOnEnabled = false; // set if signal script file uses CallOn functionality
140140

141+
private readonly List<int> passedSections = new List<int>();
141142
private readonly List<int> SectionsWithAlternativePath = new List<int>();
142143
private readonly List<int> SectionsWithAltPathSet = new List<int>();
143144
private readonly static List<int> sectionsInRoute = new List<int>();
145+
private static readonly ObjectSpeedInfo DefaultSpeedInfo = new ObjectSpeedInfo(-1, -1, false, false, 0, false);
144146

145147
public bool enabled
146148
{
@@ -907,11 +909,11 @@ public MstsSignalAspect this_sig_lr(SignalFunction function, ref bool sigfound)
907909
public ObjectSpeedInfo this_sig_speed(SignalFunction function)
908910
{
909911
var sigAsp = MstsSignalAspect.STOP;
910-
var set_speed = new ObjectSpeedInfo(-1, -1, false, false, 0, false);
912+
var set_speed = DefaultSpeedInfo;
911913

912-
foreach (SignalHead sigHead in SignalHeads.Where(sigHead => sigHead.Function == function))
914+
foreach (SignalHead sigHead in SignalHeads)
913915
{
914-
if (sigHead.state >= sigAsp && sigHead.CurrentSpeedInfo != null)
916+
if (sigHead.Function == function && sigHead.state >= sigAsp && sigHead.CurrentSpeedInfo != null)
915917
{
916918
sigAsp = sigHead.state;
917919
set_speed = sigHead.CurrentSpeedInfo;
@@ -1293,7 +1295,7 @@ public bool route_set(int req_mainnode, uint req_jnnode)
12931295
int sectionIndex = -1;
12941296
bool passedTrackJn = false;
12951297

1296-
List<int> passedSections = new List<int>();
1298+
passedSections.Clear();
12971299
passedSections.Add(thisSection.Index);
12981300

12991301
routeset = req_mainnode == thisSection.OriginalIndex;

Source/Orts.Simulation/Simulation/Traveller.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public enum TravellerDirection : byte
4747
// experimentally to match MSTS's 'capture range'.
4848
const float MaximumCenterlineOffset = 2.5f;
4949

50-
readonly TrackSectionsFile TSectionDat;
51-
readonly TrackNode[] TrackNodes;
50+
TrackSectionsFile TSectionDat;
51+
TrackNode[] TrackNodes;
5252
TravellerDirection direction = TravellerDirection.Forward;
5353
float trackOffset; // Offset into track (vector) section; meters for straight sections, radians for curved sections.
5454
TrackNode trackNode;
@@ -333,8 +333,6 @@ public Traveller(TrackSectionsFile tSectionDat, TrackNode[] trackNodes, TrackNod
333333
public Traveller(Traveller copy)
334334
{
335335
if (copy == null) throw new ArgumentNullException("copy");
336-
TSectionDat = copy.TSectionDat;
337-
TrackNodes = copy.TrackNodes;
338336
Copy(copy);
339337
}
340338

@@ -600,8 +598,10 @@ private static bool InitTrackSectionSucceeded(Traveller traveller, WorldLocation
600598

601599

602600

603-
void Copy(Traveller copy)
601+
public void Copy(Traveller copy)
604602
{
603+
TSectionDat = copy.TSectionDat;
604+
TrackNodes = copy.TrackNodes;
605605
locationSet = copy.locationSet;
606606
location.TileX = copy.location.TileX;
607607
location.TileZ = copy.location.TileZ;

0 commit comments

Comments
 (0)