From 923f29cf3cea4fbfa6d08002da92f2f2209f1882 Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Sun, 3 Mar 2024 19:00:39 -0800 Subject: [PATCH] Increase arena node size 24 -> 32 --- Solver/ArenaBuffer.cs | 23 +++++++++++++---------- Solver/NodeScoresBuffer.cs | 23 +++++++---------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/Solver/ArenaBuffer.cs b/Solver/ArenaBuffer.cs index 6405a85..dda988a 100644 --- a/Solver/ArenaBuffer.cs +++ b/Solver/ArenaBuffer.cs @@ -4,31 +4,34 @@ using System.Runtime.CompilerServices; namespace Craftimizer.Solver; -// Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs -public struct ArenaBuffer where T : struct +public struct ArenaBuffer { // Technically 25, but it's very unlikely to actually get to there. // The benchmark reaches 20 at most, but here we have a little leeway just in case. - private const int MaxSize = 24; + internal const int MaxSize = 32; - private static readonly int BatchSize = Vector.Count; - private static readonly int BatchSizeBits = int.Log2(BatchSize); - private static readonly int BatchSizeMask = BatchSize - 1; + internal static readonly int BatchSize = Vector.Count; + internal static readonly int BatchSizeBits = int.Log2(BatchSize); + internal static readonly int BatchSizeMask = BatchSize - 1; - private static readonly int BatchCount = MaxSize / BatchSize; + internal static readonly int BatchCount = MaxSize / BatchSize; +} +// Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs +public struct ArenaBuffer where T : struct +{ public ArenaNode[][] Data; public int Count { get; private set; } public void Add(ArenaNode node) { - Data ??= new ArenaNode[BatchCount][]; + Data ??= new ArenaNode[ArenaBuffer.BatchCount][]; var idx = Count++; var (arrayIdx, subIdx) = GetArrayIndex(idx); - Data[arrayIdx] ??= new ArenaNode[BatchSize]; + Data[arrayIdx] ??= new ArenaNode[ArenaBuffer.BatchSize]; node.ChildIdx = (arrayIdx, subIdx); Data[arrayIdx][subIdx] = node; @@ -37,5 +40,5 @@ public struct ArenaBuffer where T : struct [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] private static (int arrayIdx, int subIdx) GetArrayIndex(int idx) => - (idx >> BatchSizeBits, idx & BatchSizeMask); + (idx >> ArenaBuffer.BatchSizeBits, idx & ArenaBuffer.BatchSizeMask); } diff --git a/Solver/NodeScoresBuffer.cs b/Solver/NodeScoresBuffer.cs index 5d45e59..1178267 100644 --- a/Solver/NodeScoresBuffer.cs +++ b/Solver/NodeScoresBuffer.cs @@ -1,12 +1,13 @@ using System.Diagnostics.Contracts; -using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace Craftimizer.Solver; // Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs public struct NodeScoresBuffer { + [StructLayout(LayoutKind.Auto)] public readonly struct ScoresBatch { public readonly Memory ScoreSum; @@ -15,28 +16,18 @@ public struct NodeScoresBuffer public ScoresBatch() { - ScoreSum = new float[BatchSize]; - MaxScore = new float[BatchSize]; - Visits = new int[BatchSize]; + ScoreSum = new float[ArenaBuffer.BatchSize]; + MaxScore = new float[ArenaBuffer.BatchSize]; + Visits = new int[ArenaBuffer.BatchSize]; } } - // Technically 25, but it's very unlikely to actually get to there. - // The benchmark reaches 20 at most, but here we have a little leeway just in case. - private const int MaxSize = 24; - - private static readonly int BatchSize = Vector.Count; - private static readonly int BatchSizeBits = int.Log2(BatchSize); - private static readonly int BatchSizeMask = BatchSize - 1; - - private static readonly int BatchCount = MaxSize / BatchSize; - public ScoresBatch[] Data; public int Count { get; private set; } public void Add() { - Data ??= new ScoresBatch[BatchCount]; + Data ??= new ScoresBatch[ArenaBuffer.BatchCount]; var idx = Count++; @@ -59,5 +50,5 @@ public struct NodeScoresBuffer [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] private static (int arrayIdx, int subIdx) GetArrayIndex(int idx) => - (idx >> BatchSizeBits, idx & BatchSizeMask); + (idx >> ArenaBuffer.BatchSizeBits, idx & ArenaBuffer.BatchSizeMask); }