Smaller minor optimizations

This commit is contained in:
Asriel Camora
2023-07-06 19:45:59 +02:00
parent 1f5da66bc6
commit 2894867195
4 changed files with 13 additions and 21 deletions
+2 -11
View File
@@ -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;
}