Skip to content

Commit

Permalink
Merge pull request #813 from pzgulyas/profiler
Browse files Browse the repository at this point in the history
Refactored garbage generators
  • Loading branch information
pzgulyas committed Jun 14, 2023
2 parents 0914a53 + 7fdad38 commit baefb6b
Show file tree
Hide file tree
Showing 23 changed files with 274 additions and 311 deletions.
128 changes: 51 additions & 77 deletions Source/ORTS.Common/Filter.cs
Expand Up @@ -19,7 +19,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ORTS.Common
{
Expand Down Expand Up @@ -48,6 +47,12 @@ namespace ORTS.Common
/// </summary>
public class IIRFilter
{
int NCoef;
List<float> ACoef;
List<float> BCoef;
float[] x;
float[] y;

public IIRFilter()
{
/**************************************************************
Expand All @@ -73,25 +78,24 @@ public IIRFilter()
z = 0.599839 + j -0.394883
z = 0.599839 + j 0.394883
***************************************************************/
ACoef = new ArrayList();
ACoef.Add(0.00023973435363423468);
ACoef.Add(0.00047946870726846936);
ACoef.Add(0.00023973435363423468);
ACoef = new List<float>
{
0.00023973435363423468f,
0.00047946870726846936f,
0.00023973435363423468f
};

BCoef = new ArrayList();
BCoef.Add(1.00000000000000000000);
BCoef.Add(-1.94607498611971570000);
BCoef.Add(0.94703573071858904000);
BCoef = new List<float>
{
1.00000000000000000000f,
-1.94607498611971570000f,
0.94703573071858904000f
};

NCoef = A.Count - 1;

x = new ArrayList();
y = new ArrayList();
for (int i = 0; i <= NCoef; i++)
{
x.Add(0.0);
y.Add(0.0);
}
x = new float[NCoef + 1];
y = new float[NCoef + 1];

FilterType = FilterTypes.Bessel;
}
Expand All @@ -102,19 +106,14 @@ public IIRFilter()
/// <param name="a">A coefficients of the filter</param>
/// <param name="b">B coefficients of the filter</param>
/// <param name="type">Filter type</param>
public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
public IIRFilter(List<float> a, List<float> b, FilterTypes type)
{
FilterType = type;
NCoef = a.Count - 1;
ACoef = a;
BCoef = b;
x = new ArrayList();
y = new ArrayList();
for (int i = 0; i <= NCoef; i++)
{
x.Add(0.0);
y.Add(0.0);
}
x = new float[NCoef + 1];
y = new float[NCoef + 1];
}

/// <summary>
Expand All @@ -126,11 +125,10 @@ public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
/// <param name="samplingPeriod">Filter sampling period</param>
public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float samplingPeriod)
{
NCoef = order;
A = new ArrayList();
B = new ArrayList();

FilterType = type;
NCoef = order;
A = new List<float>();
B = new List<float>();

switch (type)
{
Expand All @@ -147,39 +145,25 @@ public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float sampl
NCoef = A.Count - 1;
ACoef = A;
BCoef = B;
x = new ArrayList();
y = new ArrayList();
for (int i = 0; i <= NCoef; i++)
{
x.Add(0.0);
y.Add(0.0);
}
x = new float[NCoef + 1];
y = new float[NCoef + 1];
}

int NCoef;
ArrayList ACoef;
ArrayList BCoef;

/// <summary>
/// A coefficients of the filter
/// </summary>
public ArrayList A
public List<float> A
{
set
{
if(NCoef <= 0)
if (NCoef <= 0)
NCoef = value.Count - 1;
x = new ArrayList();
y = new ArrayList();
for (int i = 0; i <= NCoef; i++)
{
x.Add(0.0);
y.Add(0.0);
}
x = new float[NCoef + 1];
y = new float[NCoef + 1];
if (ACoef == null)
ACoef = new ArrayList();
ACoef = new List<float>();
ACoef.Clear();
foreach (object obj in value)
foreach (var obj in value)
{
ACoef.Add(obj);
}
Expand All @@ -193,23 +177,18 @@ public ArrayList A
/// <summary>
/// B coefficients of the filter
/// </summary>
public ArrayList B
public List<float> B
{
set
{
if(NCoef <= 0)
if (NCoef <= 0)
NCoef = value.Count - 1;
x = new ArrayList();
y = new ArrayList();
for (int i = 0; i <= NCoef; i++)
{
x.Add(0.0);
y.Add(0.0);
}
x = new float[NCoef + 1];
y = new float[NCoef + 1];
if (BCoef == null)
BCoef = new ArrayList();
BCoef = new List<float>();
BCoef.Clear();
foreach (object obj in value)
foreach (var obj in value)
{
BCoef.Add(obj);
}
Expand All @@ -220,9 +199,6 @@ public ArrayList B
}
}

ArrayList y;
ArrayList x;

private float cuttoffFreqRadpS;
/// <summary>
/// Filter Cut off frequency in Radians
Expand Down Expand Up @@ -282,18 +258,18 @@ public enum FilterTypes
public float Filter(float NewSample)
{
//shift the old samples
for (int n = NCoef; n > 0; n--)
for (int n = x.Length - 1; n > 0; n--)
{
x[n] = x[n - 1];
y[n] = y[n - 1];
}
//Calculate the new output
x[0] = NewSample;
y[0] = (float)Convert.ToDouble(ACoef[0]) * (float)Convert.ToDouble(x[0]);
y[0] = ACoef[0] * x[0];
for (int n = 1; n <= NCoef; n++)
y[0] = (float)Convert.ToDouble(y[0]) + (float)Convert.ToDouble(ACoef[n]) * (float)Convert.ToDouble(x[n]) - (float)Convert.ToDouble(BCoef[n]) * (float)Convert.ToDouble(y[n]);
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];

return (float)y[0];
return y[0];
}

/// <summary>
Expand Down Expand Up @@ -322,31 +298,29 @@ public float Filter(float NewSample, float samplingPeriod)
throw new NotImplementedException("Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method.");
}
//shift the old samples
for (int n = NCoef; n > 0; n--)
for (int n = x.Length - 1; n > 0; n--)
{
x[n] = x[n - 1];
y[n] = y[n - 1];
}
//Calculate the new output
x[0] = NewSample;
y[0] = (float)ACoef[0] * (float)x[0];
y[0] = ACoef[0] * x[0];
for (int n = 1; n <= NCoef; n++)
{
y[0] = (float)Convert.ToDouble(y[0]) + (float)Convert.ToDouble(ACoef[n]) * (float)Convert.ToDouble(x[n]) - (float)Convert.ToDouble(BCoef[n]) * (float)Convert.ToDouble(y[n]);
}
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];

return (float)y[0];
return y[0];
}

/// <summary>
/// Resets all buffers of the filter
/// </summary>
public void Reset()
{
for (int i = 0; i < x.Count; i++)
for (int i = 0; i < x.Length; i++)
{
x[i] = 0.0;
y[i] = 0.0;
x[i] = 0.0f;
y[i] = 0.0f;
}
}
/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Source/ORTS.Settings/InputSettings.cs
Expand Up @@ -67,6 +67,7 @@ public class InputSettings : SettingsBase

public static readonly UserCommandInput[] DefaultCommands = new UserCommandInput[Enum.GetNames(typeof(UserCommand)).Length];
public readonly UserCommandInput[] Commands = new UserCommandInput[Enum.GetNames(typeof(UserCommand)).Length];
static readonly UserCommand[] UserCommandValues = (UserCommand[])Enum.GetValues(typeof(UserCommand));

static InputSettings()
{
Expand All @@ -91,7 +92,7 @@ UserCommand GetCommand(string name)

UserCommand[] GetCommands()
{
return (UserCommand[])Enum.GetValues(typeof(UserCommand));
return UserCommandValues;
}

public override object GetDefaultValue(string name)
Expand Down
22 changes: 21 additions & 1 deletion Source/Orts.Formats.Msts/SignalConfigurationFile.cs
Expand Up @@ -43,6 +43,8 @@ public class SignalConfigurationFile
{
/// <summary>Name-indexed list of available signal functions</summary>
public IDictionary<string, SignalFunction> SignalFunctions;
/// <summary>Allocation-free MstsName-indexed list of available signal functions</summary>
public static Dictionary<MstsSignalFunction, SignalFunction> MstsSignalFunctions;
/// <summary>List of OR defined subtypes for Norman signals</summary>
public IList<string> ORTSNormalSubtypes;
/// <summary>Name-indexed list of available light textures</summary>
Expand Down Expand Up @@ -80,6 +82,19 @@ public SignalConfigurationFile(string filenamewithpath, bool ORTSMode)
{ SignalFunction.UNKNOWN.Name, SignalFunction.UNKNOWN }
};

// and the allocation-free version of the above
MstsSignalFunctions = new Dictionary<MstsSignalFunction, SignalFunction>
{
{ MstsSignalFunction.NORMAL, SignalFunction.NORMAL },
{ MstsSignalFunction.DISTANCE, SignalFunction.DISTANCE },
{ MstsSignalFunction.REPEATER, SignalFunction.REPEATER },
{ MstsSignalFunction.SHUNTING, SignalFunction.SHUNTING },
{ MstsSignalFunction.INFO, SignalFunction.INFO },
{ MstsSignalFunction.SPEED, SignalFunction.SPEED },
{ MstsSignalFunction.ALERT, SignalFunction.ALERT },
{ MstsSignalFunction.UNKNOWN, SignalFunction.UNKNOWN }
};

// preset empty OR normal subtypes
ORTSNormalSubtypes = new List<String>();

Expand Down Expand Up @@ -444,17 +459,22 @@ public class SignalFunction : IEquatable<SignalFunction>

public readonly string Name;
public readonly MstsSignalFunction MstsFunction;
readonly int HashCode;

public SignalFunction(MstsSignalFunction mstsFunction)
{
Name = mstsFunction.ToString();
MstsFunction = mstsFunction;

HashCode = Name.GetHashCode() ^ MstsFunction.GetHashCode();
}

public SignalFunction(string name, MstsSignalFunction mstsFunction)
{
Name = name;
MstsFunction = mstsFunction;

HashCode = Name.GetHashCode() ^ MstsFunction.GetHashCode();
}

public override string ToString()
Expand Down Expand Up @@ -505,7 +525,7 @@ public bool Equals(SignalFunction other)

public override int GetHashCode()
{
return Name.GetHashCode() ^ MstsFunction.GetHashCode();
return HashCode;
}
}
#endregion
Expand Down
8 changes: 6 additions & 2 deletions Source/Orts.Simulation/Simulation/AIs/AITrain.cs
Expand Up @@ -129,6 +129,8 @@ public enum AI_START_MOVEMENT
public static float minStopDistanceM = 3.0f; // minimum clear distance for stopping at signal in station
public static float signalApproachDistanceM = 20.0f; // final approach to signal

private readonly List<ObjectItemInfo> processedList = new List<ObjectItemInfo>(); // internal processing list for CheckSignalObjects()

#if WITH_PATH_DEBUG
// Only for EnhancedActCompatibility
public string currentAIState = "";
Expand Down Expand Up @@ -1164,10 +1166,12 @@ public void CheckSignalObjects()
}

float validSpeed = AllowedMaxSpeedMpS;
List<ObjectItemInfo> processedList = new List<ObjectItemInfo>();
processedList.Clear();

foreach (ObjectItemInfo thisInfo in SignalObjectItems.Where(item => !item.speed_isWarning))
foreach (ObjectItemInfo thisInfo in SignalObjectItems)
{
if (thisInfo.speed_isWarning)
continue;

// check speedlimit
if (CheckTrain)
Expand Down

0 comments on commit baefb6b

Please sign in to comment.