diff --git a/PerfectNumbers.Core/ConcurrentFixedCapacityStack.cs b/PerfectNumbers.Core/ConcurrentFixedCapacityStack.cs index dcc983e..745112d 100644 --- a/PerfectNumbers.Core/ConcurrentFixedCapacityStack.cs +++ b/PerfectNumbers.Core/ConcurrentFixedCapacityStack.cs @@ -3,14 +3,23 @@ using System.Runtime.CompilerServices; namespace PerfectNumbers.Core; -public sealed class ConcurrentFixedCapacityStack(int capacity) +public sealed class ConcurrentFixedCapacityStack { - private static readonly FixedCapacityArrayPool _arrayPool = FixedCapacityPools.ExclusiveArray; + [ThreadStatic] + private static FixedCapacityArrayPool? _arrayPool; - private readonly T[] _items = _arrayPool.Rent(capacity); - private readonly int _capacity = capacity; + private readonly T[] _items; + private readonly int _capacity; private int _count = 0; + + public ConcurrentFixedCapacityStack(int capacity) + { + _arrayPool ??= FixedCapacityPools.ExclusiveArray; + _items = _arrayPool.Value.Rent(capacity); + _capacity = capacity; + } + public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] @@ -68,13 +77,13 @@ public sealed class ConcurrentFixedCapacityStack(int capacity) } [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static void Return(in T[] items) => _arrayPool.Return(items); + public static void Return(in T[] items) => _arrayPool!.Value.Return(items); [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public T[] ToArray() { int count = Volatile.Read(ref _count); - T[] result = _arrayPool.Rent(count); + T[] result = _arrayPool!.Value.Rent(count); Array.Copy(_items, result, count); return result; } diff --git a/PerfectNumbers.Core/FixedCapacityStack.cs b/PerfectNumbers.Core/FixedCapacityStack.cs index e5c0440..ce9783c 100644 --- a/PerfectNumbers.Core/FixedCapacityStack.cs +++ b/PerfectNumbers.Core/FixedCapacityStack.cs @@ -2,14 +2,23 @@ using System.Runtime.CompilerServices; namespace PerfectNumbers.Core; -public sealed class FixedCapacityStack(int capacity) +public sealed class FixedCapacityStack { - private static readonly FixedCapacityArrayPool _arrayPool = FixedCapacityPools.ExclusiveArray; + [ThreadStatic] + private static FixedCapacityArrayPool? _arrayPool; public int Count = 0; - private T[] _items = _arrayPool.Rent(capacity); + private T[] _items; + + private int _capacity; + + public FixedCapacityStack(int capacity) + { + _arrayPool ??= new(PerfectNumberConstants.DefaultPoolCapacity); + _items = _arrayPool.Value.Rent(capacity); + _capacity = capacity; + } - private int _capacity = capacity; public int Capacity { [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] @@ -20,8 +29,8 @@ public sealed class FixedCapacityStack(int capacity) { if (value < _capacity) { - _arrayPool.Return(_items); - _items = _arrayPool.Rent(value); + _arrayPool!.Value.Return(_items); + _items = _arrayPool.Value.Rent(value); _capacity = value; } }