Minor microoptimization

This commit is contained in:
Asriel Camora
2024-02-29 03:32:12 -08:00
parent c8d3161eb1
commit a75a9b1be5
2 changed files with 9 additions and 11 deletions
+4 -4
View File
@@ -7,19 +7,19 @@ namespace Craftimizer.Solver;
public struct ActionSet public struct ActionSet
{ {
internal ulong bits; private ulong bits;
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int FromAction(ActionType action) => (byte)action; private static int FromAction(ActionType action) => (byte)action;
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ActionType ToAction(int index) => (ActionType)index; private static ActionType ToAction(int index) => (ActionType)index;
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong ToMask(ActionType action) => 1ul << FromAction(action); private static ulong ToMask(ActionType action) => 1ul << FromAction(action);
// Return true if action was newly added and not there before. // Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
+5 -7
View File
@@ -7,7 +7,7 @@ namespace Craftimizer.Solver;
internal sealed class Simulator : SimulatorNoRandom internal sealed class Simulator : SimulatorNoRandom
{ {
private readonly ActionType[] actionPool; private readonly (BaseAction Data, ActionType Action)[] actionPoolObjects;
private readonly int maxStepCount; private readonly int maxStepCount;
public override CompletionState CompletionState public override CompletionState CompletionState
@@ -23,7 +23,7 @@ internal sealed class Simulator : SimulatorNoRandom
public Simulator(ActionType[] actionPool, int maxStepCount) public Simulator(ActionType[] actionPool, int maxStepCount)
{ {
this.actionPool = actionPool; actionPoolObjects = actionPool.Select(x => (x.Base(), x)).ToArray();
this.maxStepCount = maxStepCount; this.maxStepCount = maxStepCount;
} }
@@ -32,11 +32,9 @@ internal sealed class Simulator : SimulatorNoRandom
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
// It's just a bunch of if statements, I would assume this is actually quite simple to follow // It's just a bunch of if statements, I would assume this is actually quite simple to follow
#pragma warning disable MA0051 // Method is too long #pragma warning disable MA0051 // Method is too long
private bool CanUseAction(ActionType action, bool strict) private bool CanUseAction(ActionType action, BaseAction baseAction, bool strict)
#pragma warning restore MA0051 // Method is too long #pragma warning restore MA0051 // Method is too long
{ {
var baseAction = action.Base();
if (CalculateSuccessRate(baseAction.SuccessRate(this)) != 1) if (CalculateSuccessRate(baseAction.SuccessRate(this)) != 1)
return false; return false;
@@ -135,8 +133,8 @@ internal sealed class Simulator : SimulatorNoRandom
return new(); return new();
var ret = new ActionSet(); var ret = new ActionSet();
foreach (var action in actionPool) foreach (var (data, action) in actionPoolObjects)
if (CanUseAction(action, strict)) if (CanUseAction(action, data, strict))
ret.AddAction(action); ret.AddAction(action);
return ret; return ret;
} }