Skip to content

Commit

Permalink
Change static readonly fields to const fields. (#4211)
Browse files Browse the repository at this point in the history
This will at least somewhat improve performance, as confirmed through microbenchmark testing.

const fields are replaced with literal values in the Assembly's IL code, while static readonly fields may be optimized by the JIT, and is not guaranteed to happen. const fields may also lend themselves to stronger optimizations at runtime.
  • Loading branch information
FlaggAC authored Aug 24, 2024
1 parent 51570e1 commit 7f1b58a
Show file tree
Hide file tree
Showing 67 changed files with 199 additions and 199 deletions.
40 changes: 20 additions & 20 deletions Source/ACE.Common/DerethDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ namespace ACE.Common
public class DerethDateTime
{
// Changing the four variables below will result in the Date and Time reported in ACClient no longer matching
private static int hoursInADay = 16; // A Derethian day has 16 hours
private static int daysInAMonth = 30; // A Derethian month has 30 days and does not vary like Earth months.
private static int monthsInAYear = 12; // A Derethian year has 12 months
private static double dayTicks = 7620; // A Derethain day has 7620 ticks per day
private const int hoursInADay = 16; // A Derethian day has 16 hours
private const int daysInAMonth = 30; // A Derethian month has 30 days and does not vary like Earth months.
private const int monthsInAYear = 12; // A Derethian year has 12 months
private const double dayTicks = 7620; // A Derethain day has 7620 ticks per day

private static double hourTicks = dayTicks / hoursInADay;
private static double minuteTicks = hourTicks / 60;
private static double secondTicks = minuteTicks / 60;
private const double hourTicks = dayTicks / hoursInADay;
private const double minuteTicks = hourTicks / 60;
private const double secondTicks = minuteTicks / 60;

private static double monthTicks = dayTicks * daysInAMonth;
private static double yearTicks = monthTicks * monthsInAYear;
private const double monthTicks = dayTicks * daysInAMonth;
private const double yearTicks = monthTicks * monthsInAYear;

private static double dayZeroTicks = 0; // Morningthaw 1, 10 P.Y. - Morntide-and-Half
private static double hourOneTicks = 210; // Morningthaw 1, 10 P.Y. - Midsong
private static double dayOneTicks = dayZeroTicks + hourOneTicks + (hourTicks * 8); // Morningthaw 2, 10 P.Y. - Darktide
private static double yearOneTicks = dayOneTicks + (dayTicks * 359); // Morningthaw 1, 11 P.Y. - Darktide
private static double yearZeroTicks = dayOneTicks + (dayTicks * 269); // Snowreap 1, 10 P.Y. - Darktide
private const double dayZeroTicks = 0; // Morningthaw 1, 10 P.Y. - Morntide-and-Half
private const double hourOneTicks = 210; // Morningthaw 1, 10 P.Y. - Midsong
private const double dayOneTicks = dayZeroTicks + hourOneTicks + (hourTicks * 8); // Morningthaw 2, 10 P.Y. - Darktide
private const double yearOneTicks = dayOneTicks + (dayTicks * 359); // Morningthaw 1, 11 P.Y. - Darktide
private const double yearZeroTicks = dayOneTicks + (dayTicks * 269); // Snowreap 1, 10 P.Y. - Darktide

private static DateTime dayZero_RealWorld = new DateTime(1999, 4, 1, 10, 30, 00);
private static DateTime dayOne_RealWorld = new DateTime(1999, 4, 2, 00, 00, 00);
private static readonly DateTime dayZero_RealWorld = new DateTime(1999, 4, 1, 10, 30, 00);
private static readonly DateTime dayOne_RealWorld = new DateTime(1999, 4, 2, 00, 00, 00);

private static DateTime retailDayOne_RealWorld = new DateTime(1999, 11, 2, 00, 00, 00);
private static DateTime retailDayLast_RealWorld = new DateTime(2017, 1, 31, 12, 00, 00); // Eastern Standard Time
private static readonly DateTime retailDayOne_RealWorld = new DateTime(1999, 11, 2, 00, 00, 00);
private static readonly DateTime retailDayLast_RealWorld = new DateTime(2017, 1, 31, 12, 00, 00); // Eastern Standard Time

/// <summary>
/// <para>A <see cref="DerethDateTime"/> instance set to the Derethian Date, Portal Year and Time when the worlds first opened.</para>
Expand All @@ -55,13 +55,13 @@ public class DerethDateTime
/// <summary>
/// <para>Date: Morningthaw 1, 10 P.Y. | Time: Morntide-and-Half (0)</para>
/// </summary>
public static readonly double MinValue = 0; // Morningthaw 1, 10 P.Y. - Morntide-and-Half
public const double MinValue = 0; // Morningthaw 1, 10 P.Y. - Morntide-and-Half

/// <summary>
/// <para>Any value higher than this results in acclient crashing upon connection to server.</para>
/// <para>Date: Thistledown 2, 401 P.Y. | Time: Morntide-and-Half (1073741828)</para>
/// </summary>
public static readonly double MaxValue = (yearTicks * 391) + (monthTicks * 5) + (dayTicks * 1) + 4; // Thistledown 2, 401 P.Y. - Morntide-and-Half (1073741828)
public const double MaxValue = (yearTicks * 391) + (monthTicks * 5) + (dayTicks * 1) + 4; // Thistledown 2, 401 P.Y. - Morntide-and-Half (1073741828)

/// <summary>
/// Months of the Portal Year
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Common/ThreadSafeRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static double NextInterval(float qualityMod)
/// <summary>
/// The maximum possible double < 1.0
/// </summary>
private static readonly double maxExclusive = 0.9999999999999999;
private const double maxExclusive = 0.9999999999999999;

public static double NextIntervalMax(float qualityMod)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.DatLoader/DatDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DatDatabase
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

private static readonly uint DAT_HEADER_OFFSET = 0x140;
private const uint DAT_HEADER_OFFSET = 0x140;

public string FilePath { get; }

Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.DatLoader/DatDirectoryHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ACE.DatLoader
{
public class DatDirectoryHeader : IUnpackable
{
internal static readonly uint ObjectSize = ((sizeof(uint) * 0x3E) + sizeof(uint) + (DatFile.ObjectSize * 0x3D));
internal const uint ObjectSize = ((sizeof(uint) * 0x3E) + sizeof(uint) + (DatFile.ObjectSize * 0x3D));

public uint[] Branches { get; } = new uint[0x3E];
public uint EntryCount { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.DatLoader/DatFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ACE.DatLoader
{
public class DatFile : IUnpackable
{
internal static readonly uint ObjectSize = (sizeof(uint) * 6);
internal const uint ObjectSize = (sizeof(uint) * 6);


//public uint BitFlags { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Database/WorldDatabaseWithEntityCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ public void CacheAllTreasureMaterialBase()
}
}

private static readonly float NormalizeEpsilon = 0.00001f;
private const float NormalizeEpsilon = 0.00001f;

private void TreasureMaterialBase_Normalize(Dictionary<int, Dictionary<int, List<TreasureMaterialBase>>> materialBase)
{
Expand Down
6 changes: 3 additions & 3 deletions Source/ACE.Entity/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,9 @@ public string ToLOCString()
return $"0x{LandblockId.Raw:X8} [{PositionX:F6} {PositionY:F6} {PositionZ:F6}] {RotationW:F6} {RotationX:F6} {RotationY:F6} {RotationZ:F6}";
}

public static readonly int BlockLength = 192;
public static readonly int CellSide = 8;
public static readonly int CellLength = 24;
public const int BlockLength = 192;
public const int CellSide = 8;
public const int CellLength = 24;

public bool Equals(Position p)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/ACE.Server/Command/Handlers/DeveloperFixCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,10 +1333,10 @@ public static Dictionary<uint, int> GetImbuedEffect()
}

// head / hands / feet
public static readonly int MaxArmorLevel_Extremity = 345;
public const int MaxArmorLevel_Extremity = 345;

// everything else
public static readonly int MaxArmorLevel_NonExtremity = 315;
public const int MaxArmorLevel_NonExtremity = 315;

public static int GetArmorLevel(int armorLevel, EquipMask equipMask, TinkerLog tinkerLog, int numTinkers, int imbuedEffect)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Entity/Chess/ChessMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public void SendGameOver(Player player, int winner)
/// <summary>
/// How quickly the rankings will raise / lower
/// </summary>
public static readonly int RankFactor = 50;
public const int RankFactor = 50;

/// <summary>
/// Adjusts the chess ranks for 2 players after a match
Expand Down
4 changes: 2 additions & 2 deletions Source/ACE.Server/Entity/Cloak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static float MaxProcBase200
}
}

private static readonly float TwoThirds = 2.0f / 3.0f;
private const float TwoThirds = 2.0f / 3.0f;

/// <summary>
/// Rolls for a chance at procing a cloak spell
Expand Down Expand Up @@ -197,7 +197,7 @@ public static bool IsCloak(WorldObject wo)
/// <summary>
/// The amount of damage reduced by a cloak proced with PropertyInt.CloakWeaveProc=2
/// </summary>
public static readonly int DamageReductionAmount = 200;
public const int DamageReductionAmount = 200;

public static int GetDamageReductionAmount(WorldObject source)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Entity/Fellowship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ internal double GetMemberSharePercent()
return 1.0;
}

public static readonly int MaxDistance = 600;
public const int MaxDistance = 600;

/// <summary>
/// Returns the amount to scale the XP for a fellow
Expand Down
8 changes: 4 additions & 4 deletions Source/ACE.Server/Entity/LandblockMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ public class LandblockMesh: Mesh
/// <summary>
/// A landblock has this many cells squared
/// </summary>
public static readonly int CellDim = 8;
public const int CellDim = 8;

/// <summary>
/// A landblock is this unit size squared
/// </summary>
public static readonly int LandblockSize = 192;
public const int LandblockSize = 192;

/// <summary>
/// A landblock cell is this unit size squared
/// </summary>
public static readonly int CellSize = LandblockSize / CellDim;
public const int CellSize = LandblockSize / CellDim;

/// <summary>
/// A landblock has this many vertices squared
/// </summary>
public static readonly int VertexDim = CellDim + 1;
public const int VertexDim = CellDim + 1;

/// <summary>
/// LandHeightTable mapping non-linear heights
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Entity/Mutations/MutationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public static string GetSecondOperator(MutationEffectType effectType)
return null;
}

private static readonly string prefix = "ACE.Server.Entity.Mutations.";
private const string prefix = "ACE.Server.Entity.Mutations.";

private static List<string> ReadScript(string filename)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Entity/ChanceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ACE.Server.Factories.Entity
public class ChanceTable<T> : List<(T result, float chance)>
{
private bool verified;
private static readonly decimal threshold = 0.0000001M;
private const decimal threshold = 0.0000001M;

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Expand Down
8 changes: 4 additions & 4 deletions Source/ACE.Server/Factories/LootGenerationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,8 @@ public static MaterialType RollGemType(int tier)
return gemResult.MaterialType;
}

public static readonly float WeaponBulk = 0.50f;
public static readonly float ArmorBulk = 0.25f;
public const float WeaponBulk = 0.50f;
public const float ArmorBulk = 0.25f;

private static bool MutateBurden(WorldObject wo, TreasureDeath treasureDeath, bool isWeapon)
{
Expand Down Expand Up @@ -839,9 +839,9 @@ private static void MutateValue(WorldObject wo, int tier, TreasureRoll roll)
}

// increase for a wider variance in item value ranges
private static readonly float valueFactor = 1.0f / 3.0f;
private const float valueFactor = 1.0f / 3.0f;

private static readonly float valueNonFactor = 1.0f - valueFactor;
private const float valueNonFactor = 1.0f - valueFactor;

private static void MutateValue_Generic(WorldObject wo, int tier)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static class ArmorCantrips
SpellId.CANTRIPPIERCINGBANE1,
};

private static readonly int NumLevels = 4;
private const int NumLevels = 4;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static class JewelryCantrips
SpellId.CANTRIPPIERCINGWARD1,
};

private static readonly int NumLevels = 4;
private const int NumLevels = 4;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class MeleeCantrips
SpellId.CantripSneakAttackProwess1,
};

private static readonly int NumLevels = 4;
private const int NumLevels = 4;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class MissileCantrips
SpellId.CantripSneakAttackProwess1,
};

private static readonly int NumLevels = 4;
private const int NumLevels = 4;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static class WandCantrips
SpellId.CantripSpiritThirst1,
};

private static readonly int NumLevels = 4;
private const int NumLevels = 4;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Tables/Spells/ArmorSpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static class ArmorSpells
SpellId.LightningBane1,
};

private static readonly int NumTiers = 8;
private const int NumTiers = 8;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Tables/Spells/GemSpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static class GemSpells
SpellId.LightningProtectionSelf1,
};

private static readonly int NumTiers = 8;
private const int NumTiers = 8;

// original api
public static readonly SpellId[][] GemCreatureSpellMatrix = new SpellId[NumTiers][];
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Tables/Spells/JewelrySpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static class JewelrySpells
SpellId.LightningProtectionSelf1,
};

private static readonly int NumTiers = 8;
private const int NumTiers = 8;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Tables/Spells/MeleeSpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class MeleeSpells
SpellId.SneakAttackMasterySelf1,
};

private static readonly int NumTiers = 8;
private const int NumTiers = 8;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Tables/Spells/MissileSpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class MissileSpells
SpellId.SneakAttackMasterySelf1,
};

private static readonly int NumTiers = 8;
private const int NumTiers = 8;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
4 changes: 2 additions & 2 deletions Source/ACE.Server/Factories/Tables/Spells/ScrollSpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ public static class ScrollSpells
SpellId.CurseWeakness1,
};

private static readonly int NumLevels = 7;
private const int NumLevels = 7;

private static readonly int MaxLevels = 8;
private const int MaxLevels = 8;

private static readonly int NumSpells = creatureSpells.Count + lifeSpells.Count + itemSpells.Count + warSpells.Count + voidSpells.Count;

Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Factories/Tables/Spells/WandSpells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class WandSpells
SpellId.SneakAttackMasterySelf1,
};

private static readonly int NumTiers = 8;
private const int NumTiers = 8;

// original api
public static readonly SpellId[][] Table = new SpellId[spells.Count][];
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/Managers/RecipeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ public static void ModifyDataID(Player player, RecipeModsDID didMod, WorldObject
/// <summary>
/// flag to use c# logic instead of mutate script logic
/// </summary>
private static readonly bool useMutateNative = false;
private const bool useMutateNative = false;

public static bool TryMutate(Player player, WorldObject source, WorldObject target, Recipe recipe, uint dataId, HashSet<uint> modified)
{
Expand Down
26 changes: 13 additions & 13 deletions Source/ACE.Server/Network/GameAction/Actions/GameActionQueryAge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public static void Handle(ClientMessage message, Session session)
session.Network.EnqueueSend(new GameEventQueryAgeResponse(session, string.Empty, ageMsg));
}

private static readonly int SecondsPerMinute = 60;
private static readonly int MinutesPerHour = 60;
private static readonly int HoursPerDay = 24;

private static readonly int DaysPerWeek = 7;
private static readonly int DaysPerMonth = 30;
private static readonly int DaysPerYear = 365;

private static readonly int SecondsPerHour = SecondsPerMinute * MinutesPerHour; // 3600
private static readonly int SecondsPerDay = SecondsPerHour * HoursPerDay; // 86400
private static readonly int SecondsPerWeek = SecondsPerDay * DaysPerWeek; // 604800
private static readonly int SecondsPerMonth = SecondsPerDay * DaysPerMonth; // 2592000
private static readonly int SecondsPerYear = SecondsPerDay * DaysPerYear; // 31536000
private const int SecondsPerMinute = 60;
private const int MinutesPerHour = 60;
private const int HoursPerDay = 24;

private const int DaysPerWeek = 7;
private const int DaysPerMonth = 30;
private const int DaysPerYear = 365;

private const int SecondsPerHour = SecondsPerMinute * MinutesPerHour; // 3600
private const int SecondsPerDay = SecondsPerHour * HoursPerDay; // 86400
private const int SecondsPerWeek = SecondsPerDay * DaysPerWeek; // 604800
private const int SecondsPerMonth = SecondsPerDay * DaysPerMonth; // 2592000
private const int SecondsPerYear = SecondsPerDay * DaysPerYear; // 31536000

public static string CalculateAgeMessage(int ageSeconds)
{
Expand Down
Loading

0 comments on commit 7f1b58a

Please sign in to comment.