From 2894867195c000b8f165831e1d72c577866bc36d Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Thu, 6 Jul 2023 19:45:59 +0200 Subject: [PATCH] Smaller minor optimizations --- Solver/Crafty/ActionSet.cs | 13 ++----------- Solver/Crafty/ArenaNode.cs | 4 ++-- Solver/Crafty/NodeScores.cs | 8 ++++---- Solver/Crafty/Solver.cs | 9 +++++---- 4 files changed, 13 insertions(+), 21 deletions(-) diff --git a/Solver/Crafty/ActionSet.cs b/Solver/Crafty/ActionSet.cs index 28941e5..9ef9614 100644 --- a/Solver/Crafty/ActionSet.cs +++ b/Solver/Crafty/ActionSet.cs @@ -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; } diff --git a/Solver/Crafty/ArenaNode.cs b/Solver/Crafty/ArenaNode.cs index 68e0f0e..7b21639 100644 --- a/Solver/Crafty/ArenaNode.cs +++ b/Solver/Crafty/ArenaNode.cs @@ -30,9 +30,9 @@ public sealed class ArenaNode where T : struct if (Data == null) Interlocked.CompareExchange(ref Data, new ArenaNode[BatchCount][], null); - var index = Interlocked.Increment(ref this.index) - 1; + var idx = Interlocked.Increment(ref this.index) - 1; - var (arrayIdx, subIdx) = GetArrayIndex(index); + var (arrayIdx, subIdx) = GetArrayIndex(idx); if (Data[arrayIdx] == null) Interlocked.CompareExchange(ref Data[arrayIdx], new ArenaNode[BatchSize], null); diff --git a/Solver/Crafty/NodeScores.cs b/Solver/Crafty/NodeScores.cs index a94008e..b09dc03 100644 --- a/Solver/Crafty/NodeScores.cs +++ b/Solver/Crafty/NodeScores.cs @@ -7,12 +7,12 @@ public struct NodeScores { public float ScoreSum; public float MaxScore; - public float Visits; + public int Visits; public void Visit(float score) { - ScoreSum += score; - MaxScore = Math.Max(MaxScore, score); - Visits++; + Intrinsics.CASAdd(ref ScoreSum, score); + Intrinsics.CASMax(ref MaxScore, score); + Interlocked.Increment(ref Visits); } } diff --git a/Solver/Crafty/Solver.cs b/Solver/Crafty/Solver.cs index bbd9846..24c2584 100644 --- a/Solver/Crafty/Solver.cs +++ b/Solver/Crafty/Solver.cs @@ -96,7 +96,7 @@ public sealed class Solver [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Node? EvalBestChild(float parentVisits, ref Node.ChildBuffer children) + private Node? EvalBestChild(int parentVisits, ref Node.ChildBuffer children) { if (parentVisits == 0) return null; @@ -110,7 +110,7 @@ public sealed class Solver var CVector = new Vector(C); Span scoreSums = stackalloc float[vecLength]; - Span visits = stackalloc float[vecLength]; + Span visits = stackalloc int[vecLength]; Span maxScores = stackalloc float[vecLength]; var max = (0, 0); @@ -130,8 +130,9 @@ public sealed class Solver var s = new Vector(scoreSums); var m = new Vector(maxScores); - var v = new Vector(visits); - v = Vector.Max(v, Vector.One); + var vInt = new Vector(visits); + vInt = Vector.Max(vInt, Vector.One); + var v = Vector.ConvertToSingle(vInt); var exploitation = (W * (s / v)) + (w * m); var exploration = CVector * Intrinsics.ReciprocalSqrt(v); var evalScores = exploitation + exploration;