Skip to content

Commit

Permalink
ReadOnlySpan optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Nov 11, 2023
1 parent 334810c commit 77749da
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
15 changes: 8 additions & 7 deletions Solver/ActionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public struct ActionSet
{
private uint bits;

public static readonly ActionType[] AcceptedActions = new[]
internal static ReadOnlySpan<ActionType> AcceptedActions => new[]
{
ActionType.StandardTouchCombo,
ActionType.AdvancedTouchCombo,
Expand Down Expand Up @@ -42,22 +42,23 @@ public struct ActionSet
ActionType.BasicTouch,
};

public static readonly int[] AcceptedActionsLUT;
internal static ReadOnlyMemory<int> AcceptedActionsLUT { get; }

static ActionSet()
{
AcceptedActionsLUT = new int[Enum.GetValues<ActionType>().Length];
for (var i = 0; i < AcceptedActionsLUT.Length; i++)
AcceptedActionsLUT[i] = -1;
var lut = new int[Enum.GetValues<ActionType>().Length];
for (var i = 0; i < lut.Length; i++)
lut[i] = -1;
for (var i = 0; i < AcceptedActions.Length; i++)
AcceptedActionsLUT[(byte)AcceptedActions[i]] = i;
lut[(byte)AcceptedActions[i]] = i;
AcceptedActionsLUT = lut;
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FromAction(ActionType action)
{
var ret = AcceptedActionsLUT[(byte)action];
var ret = AcceptedActionsLUT.Span[(byte)action];
if (ret == -1)
throw new ArgumentOutOfRangeException(nameof(action), action, $"Action {action} is unsupported in {nameof(ActionSet)}.");
return ret;
Expand Down
6 changes: 6 additions & 0 deletions Solver/Craftimizer.Solver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<ProjectReference Include="..\Simulator\Craftimizer.Simulator.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Craftimizer.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<PropertyGroup Condition="'$(IS_BENCH)'=='1'">
<DefineConstants>$(DefineConstants);IS_DETERMINISTIC</DefineConstants>
</PropertyGroup>
Expand Down
4 changes: 1 addition & 3 deletions Test/Solver/ActionSet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Craftimizer.Simulator.Actions;

namespace Craftimizer.Test.Solver;

[TestClass]
Expand All @@ -9,7 +7,7 @@ public class ActionSetTests
public void TestAcceptedActions()
{
var actions = ActionSet.AcceptedActions;
var lut = ActionSet.AcceptedActionsLUT;
var lut = ActionSet.AcceptedActionsLUT.Span;

Assert.IsTrue(actions.Length <= 32);
foreach (var i in Enum.GetValues<ActionType>())
Expand Down
1 change: 1 addition & 0 deletions Test/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using Craftimizer.Solver;
global using Craftimizer.Simulator;
global using Craftimizer.Simulator.Actions;

1 comment on commit 77749da

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 77749da Previous: a8c3f34 Ratio
Craftimizer.Benchmark.Bench.Solve(State: SimulationState { Input = SimulationInput { Stats = CharacterStats { Craftsmanship = 3290, Control = 3541, CP = 649, Level = 90, CanUseManipulation = True, HasSplendorousBuff = False, IsSpecialist = False, CLvl = 560 }, Recipe = RecipeInfo { IsExpert = False, ClassJobLevel = 90, RLvl = 580, ConditionsFlag = 15, MaxDurability = 70, MaxQuality = 10920, MaxProgress = 3900, QualityModifier = 70, QualityDivider = 115, ProgressModifier = 80, ProgressDivider = 130 }, Random = System.Random, StartingQuality = 0, BaseProgressGain = 204, BaseQualityGain = 240 }, ActionCount = 0, StepCount = 0, Progress = 0, Quality = 0, Durability = 70, CP = 649, Condition = Normal, ActiveEffects = Effects { InnerQuiet = 0, WasteNot = 0, Veneration = 0, GreatStrides = 0, Innovation = 0, FinalAppraisal = 0, WasteNot2 = 0, MuscleMemory = 0, Manipulation = 0, HeartAndSoul = False }, ActionStates = ActionStates { TouchComboIdx = 0, CarefulObservationCount = 0, UsedHeartAndSoul = False, Observed = False }, HQPercent = 1, Collectability = 1, MaxCollectability = 1092, IsFirstStep = True }, Config: SolverConfig { Iterations = 30000, ScoreStorageThreshold = 1, MaxScoreWeightingConstant = 0.1, ExplorationConstant = 4, MaxStepCount = 30, MaxRolloutStepCount = 99, MaxThreadCount = 3, ForkCount = 32, FurcatedActionCount = 16, StrictActions = True, ScoreProgress = 0.2, ScoreQuality = 0.65, ScoreDurability = 0.05, ScoreCP = 0.05, ScoreSteps = 0.05, Algorithm = Stepwise }) 1516255225 ns (± 1294457.9425381112) 1350316860 ns (± 4838774.167198016) 1.12
Craftimizer.Benchmark.Bench.Solve(State: SimulationState { Input = SimulationInput { Stats = CharacterStats { Craftsmanship = 3304, Control = 3374, CP = 575, Level = 90, CanUseManipulation = True, HasSplendorousBuff = False, IsSpecialist = False, CLvl = 560 }, Recipe = RecipeInfo { IsExpert = False, ClassJobLevel = 90, RLvl = 560, ConditionsFlag = 15, MaxDurability = 80, MaxQuality = 7200, MaxProgress = 3500, QualityModifier = 80, QualityDivider = 115, ProgressModifier = 90, ProgressDivider = 130 }, Random = System.Random, StartingQuality = 0, BaseProgressGain = 230, BaseQualityGain = 262 }, ActionCount = 0, StepCount = 0, Progress = 0, Quality = 0, Durability = 80, CP = 575, Condition = Normal, ActiveEffects = Effects { InnerQuiet = 0, WasteNot = 0, Veneration = 0, GreatStrides = 0, Innovation = 0, FinalAppraisal = 0, WasteNot2 = 0, MuscleMemory = 0, Manipulation = 0, HeartAndSoul = False }, ActionStates = ActionStates { TouchComboIdx = 0, CarefulObservationCount = 0, UsedHeartAndSoul = False, Observed = False }, HQPercent = 1, Collectability = 1, MaxCollectability = 720, IsFirstStep = True }, Config: SolverConfig { Iterations = 30000, ScoreStorageThreshold = 1, MaxScoreWeightingConstant = 0.1, ExplorationConstant = 4, MaxStepCount = 30, MaxRolloutStepCount = 99, MaxThreadCount = 3, ForkCount = 32, FurcatedActionCount = 16, StrictActions = True, ScoreProgress = 0.2, ScoreQuality = 0.65, ScoreDurability = 0.05, ScoreCP = 0.05, ScoreSteps = 0.05, Algorithm = Stepwise }) 1561184490 ns (± 7903809.932410234) 1714389753.3333333 ns (± 7147584.623480124) 0.91

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.