From a75a9b1be57e34a43d62c12de549ab416a058e90 Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Thu, 29 Feb 2024 03:32:12 -0800 Subject: [PATCH] Minor microoptimization --- Solver/ActionSet.cs | 8 ++++---- Solver/Simulator.cs | 12 +++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Solver/ActionSet.cs b/Solver/ActionSet.cs index 860975d..796657e 100644 --- a/Solver/ActionSet.cs +++ b/Solver/ActionSet.cs @@ -7,19 +7,19 @@ namespace Craftimizer.Solver; public struct ActionSet { - internal ulong bits; + private ulong bits; [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static int FromAction(ActionType action) => (byte)action; + private static int FromAction(ActionType action) => (byte)action; [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static ActionType ToAction(int index) => (ActionType)index; + private static ActionType ToAction(int index) => (ActionType)index; [Pure] [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. [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Solver/Simulator.cs b/Solver/Simulator.cs index eb2176e..8ddea8f 100644 --- a/Solver/Simulator.cs +++ b/Solver/Simulator.cs @@ -7,7 +7,7 @@ namespace Craftimizer.Solver; internal sealed class Simulator : SimulatorNoRandom { - private readonly ActionType[] actionPool; + private readonly (BaseAction Data, ActionType Action)[] actionPoolObjects; private readonly int maxStepCount; public override CompletionState CompletionState @@ -23,7 +23,7 @@ internal sealed class Simulator : SimulatorNoRandom public Simulator(ActionType[] actionPool, int maxStepCount) { - this.actionPool = actionPool; + actionPoolObjects = actionPool.Select(x => (x.Base(), x)).ToArray(); this.maxStepCount = maxStepCount; } @@ -32,11 +32,9 @@ internal sealed class Simulator : SimulatorNoRandom [MethodImpl(MethodImplOptions.AggressiveInlining)] // 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 - private bool CanUseAction(ActionType action, bool strict) + private bool CanUseAction(ActionType action, BaseAction baseAction, bool strict) #pragma warning restore MA0051 // Method is too long { - var baseAction = action.Base(); - if (CalculateSuccessRate(baseAction.SuccessRate(this)) != 1) return false; @@ -135,8 +133,8 @@ internal sealed class Simulator : SimulatorNoRandom return new(); var ret = new ActionSet(); - foreach (var action in actionPool) - if (CanUseAction(action, strict)) + foreach (var (data, action) in actionPoolObjects) + if (CanUseAction(action, data, strict)) ret.AddAction(action); return ret; }