Merge solver code with static interface

- Breaks backwards compat solver code with last version. Concurrent broke backwards compat because of race conditions with rng, but single is now broken too, despite it being 2x faster (!!!!)
- Literally twice as fast as Rust now in single thread
- Concurrent doesn't work yet, deadlocks somewhere..?
This commit is contained in:
Asriel Camora
2023-07-07 09:58:47 +02:00
parent 2894867195
commit d5a8288439
9 changed files with 332 additions and 145 deletions
+43 -6
View File
@@ -17,11 +17,11 @@ public struct ActionSet
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
private static uint ToMask(ActionType action) => 1u << FromAction(action) + 1;
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddAction(ActionType action)
public bool AddActionConcurrent(ActionType action)
{
var mask = ToMask(action);
var old = Interlocked.Or(ref bits, mask);
@@ -30,13 +30,33 @@ public struct ActionSet
// Return true if action was newly removed and not already gone.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAction(ActionType action)
public bool RemoveActionConcurrent(ActionType action)
{
var mask = ToMask(action);
var old = Interlocked.And(ref bits, ~mask);
return (old & mask) != 0;
}
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddAction(ActionType action)
{
var mask = ToMask(action);
var old = bits;
bits |= mask;
return (old & mask) == 0;
}
// Return true if action was newly removed and not already gone.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAction(ActionType action)
{
var mask = ToMask(action);
var old = bits;
bits &= ~mask;
return (old & mask) != 0;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
@@ -51,10 +71,10 @@ 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) => ElementAt(0);// random.Next(Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType? PopRandom(Random random)
public ActionType? PopRandomConcurrent(Random random)
{
uint snapshot;
uint newValue;
@@ -76,7 +96,7 @@ public struct ActionSet
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType? PopFirst()
public ActionType? PopFirstConcurrent()
{
uint snapshot;
uint newValue;
@@ -94,6 +114,23 @@ public struct ActionSet
return action;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(Random random)
{
return PopFirst();
var action = ElementAt(random.Next(Count));
RemoveAction(action);
return action;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopFirst()
{
var action = First();
RemoveAction(action);
return action;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType First() => ElementAt(0);