Smaller minor optimizations
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Craftimizer.Simulator.Actions;
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
@@ -52,11 +51,9 @@ public struct ActionSet
|
||||
public readonly bool IsEmpty => bits == 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
||||
public readonly ActionType SelectRandom(Random random) => First();
|
||||
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public ActionType? PopRandom(Random random) => PopFirst();
|
||||
public ActionType? PopRandom(Random random)
|
||||
{
|
||||
uint snapshot;
|
||||
@@ -84,22 +81,16 @@ public struct ActionSet
|
||||
uint snapshot;
|
||||
uint newValue;
|
||||
ActionType action;
|
||||
int i = 0;
|
||||
do
|
||||
{
|
||||
++i;
|
||||
snapshot = bits;
|
||||
if (snapshot == 0)
|
||||
return null;
|
||||
|
||||
var index = 0;
|
||||
|
||||
action = ToAction(Intrinsics.NthBitSet(snapshot, index) - 1);
|
||||
action = ToAction(Intrinsics.NthBitSet(snapshot, 0) - 1);
|
||||
newValue = snapshot & ~ToMask(action);
|
||||
}
|
||||
while (Interlocked.CompareExchange(ref bits, newValue, snapshot) != snapshot);
|
||||
//if (i != 1)
|
||||
//Console.WriteLine($"Retried {i-1} times");
|
||||
return action;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user