Offload node score buffers

This commit is contained in:
Asriel Camora
2023-07-07 15:45:42 +02:00
parent 1386f9150c
commit e4d9e3a52e
10 changed files with 188 additions and 97 deletions
+10 -8
View File
@@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
namespace Craftimizer.Solver.Crafty;
// Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs
public struct ArenaBuffer<T>
public struct ArenaBuffer<T> where T : struct
{
// 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.
@@ -17,39 +17,41 @@ public struct ArenaBuffer<T>
private static int BatchCount = MaxSize / BatchSize;
public T[][] Data;
public ArenaNode<T>[][] Data;
private int index; // Unused in single threaded workload
private int count;
public readonly int Count => count;
public void AddConcurrent(T node)
public void AddConcurrent(ArenaNode<T> node)
{
if (Data == null)
Interlocked.CompareExchange(ref Data, new T[BatchCount][], null);
Interlocked.CompareExchange(ref Data, new ArenaNode<T>[BatchCount][], null);
var idx = Interlocked.Increment(ref index) - 1;
var (arrayIdx, subIdx) = GetArrayIndex(idx);
if (Data[arrayIdx] == null)
Interlocked.CompareExchange(ref Data[arrayIdx], new T[BatchSize], null);
Interlocked.CompareExchange(ref Data[arrayIdx], new ArenaNode<T>[BatchSize], null);
node.ChildIdx = (arrayIdx, subIdx);
Data[arrayIdx][subIdx] = node;
Interlocked.Increment(ref count);
}
public void Add(T node)
public void Add(ArenaNode<T> node)
{
Data ??= new T[BatchCount][];
Data ??= new ArenaNode<T>[BatchCount][];
var idx = count++;
var (arrayIdx, subIdx) = GetArrayIndex(idx);
Data[arrayIdx] ??= new T[BatchSize];
Data[arrayIdx] ??= new ArenaNode<T>[BatchSize];
node.ChildIdx = (arrayIdx, subIdx);
Data[arrayIdx][subIdx] = node;
}