Skip to content

Commit

Permalink
Minor microoptimization
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Feb 29, 2024
1 parent c8d3161 commit a75a9b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Solver/ActionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 5 additions & 7 deletions Solver/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,7 @@ public override CompletionState CompletionState

public Simulator(ActionType[] actionPool, int maxStepCount)
{
this.actionPool = actionPool;
actionPoolObjects = actionPool.Select(x => (x.Base(), x)).ToArray();
this.maxStepCount = maxStepCount;
}

Expand All @@ -32,11 +32,9 @@ public Simulator(ActionType[] actionPool, int maxStepCount)
[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;

Expand Down Expand Up @@ -135,8 +133,8 @@ public ActionSet AvailableActionsHeuristic(bool strict)
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;
}
Expand Down

1 comment on commit a75a9b1

@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: a75a9b1 Previous: 337d42f Ratio
Craftimizer.Benchmark.Bench.Solve(State: 8014A87B, Config: 638A4A95) 1497469550 ns (± 9804357.61679861)
Craftimizer.Benchmark.Bench.Solve(State: 8014A87B, Config: 638A4A95) 1172548093.3333333 ns (± 6370339.8951106295)
Craftimizer.Benchmark.Bench.Solve(State: FFD6C57C, Config: 638A4A95) 2073491128.5714285 ns (± 7618370.642823081)
Craftimizer.Benchmark.Bench.Solve(State: FFD6C57C, Config: 638A4A95) 1644050453.3333333 ns (± 7693080.521933485)

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

Please sign in to comment.