fix thread safety of Pools, minor FishTable improvements
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -12,6 +12,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using FisheryLib.FunctionPointers;
|
||||
using FisheryLib.Pools;
|
||||
using FisheryLib.Utility;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
@@ -227,14 +228,14 @@ public class FishSet<T> : ISet<T>, IReadOnlyCollection<T>, ICollection
|
||||
if (IsTail(bucket, bucketIndex))
|
||||
{
|
||||
var previousEntry = bucket;
|
||||
var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
using var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
|
||||
SetParentTail(bucketIndex, Tails.SOLO);
|
||||
SetEntryWithoutTail(bucketIndex, entry);
|
||||
InsertEntry(previousEntry, ReplaceBehaviour.Throw, true);
|
||||
|
||||
if (tailingEntries != null)
|
||||
InsertEntries(tailingEntries, ReplaceBehaviour.Throw, true);
|
||||
if (tailingEntries != default)
|
||||
InsertEntries(tailingEntries.List, ReplaceBehaviour.Throw, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -245,9 +246,9 @@ public class FishSet<T> : ISet<T>, IReadOnlyCollection<T>, ICollection
|
||||
}
|
||||
}
|
||||
|
||||
private void InsertEntries(T[] tailingEntries, ReplaceBehaviour replaceBehaviour, bool shifting = false)
|
||||
private void InsertEntries(List<T> tailingEntries, ReplaceBehaviour replaceBehaviour, bool shifting = false)
|
||||
{
|
||||
for (var i = 0; i < tailingEntries.Length; i++)
|
||||
for (var i = 0; i < tailingEntries.Count; i++)
|
||||
InsertEntry(tailingEntries[i], replaceBehaviour, shifting);
|
||||
}
|
||||
|
||||
@@ -329,32 +330,32 @@ public class FishSet<T> : ISet<T>, IReadOnlyCollection<T>, ICollection
|
||||
}
|
||||
|
||||
var previousEntry = _buckets[bucketIndex];
|
||||
var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
using var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
|
||||
SetParentTail(bucketIndex, Tails.SOLO);
|
||||
SetEntryAsTail(bucketIndex, entry, parentIndex, offset);
|
||||
|
||||
InsertEntry(previousEntry!, ReplaceBehaviour.Throw, true);
|
||||
|
||||
if (tailingEntries != null)
|
||||
InsertEntries(tailingEntries, ReplaceBehaviour.Throw, true);
|
||||
if (tailingEntries != default)
|
||||
InsertEntries(tailingEntries.List, ReplaceBehaviour.Throw, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private T[]? TryGetAndClearTailingEntries(int bucketIndex)
|
||||
private PooledIList<List<T>> TryGetAndClearTailingEntries(int bucketIndex)
|
||||
{
|
||||
if (!HasTail(bucketIndex))
|
||||
return null;
|
||||
return default;
|
||||
|
||||
var tailingEntries = new T[GetTailCount(bucketIndex)];
|
||||
var i = 0;
|
||||
var tailingEntries = new PooledIList<List<T>>();
|
||||
var tailingEntriesList = tailingEntries.List;
|
||||
var tailIndex = GetTailIndex(bucketIndex);
|
||||
|
||||
while (true)
|
||||
{
|
||||
ref var tailingBucket = ref _buckets[tailIndex];
|
||||
tailingEntries[i++] = tailingBucket!;
|
||||
tailingEntriesList.Add(tailingBucket!);
|
||||
tailingBucket = default;
|
||||
|
||||
if (HasTail(tailIndex))
|
||||
|
||||
@@ -172,9 +172,9 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
|
||||
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values;
|
||||
|
||||
object? IDictionary.this[object key]
|
||||
object IDictionary.this[object key]
|
||||
{
|
||||
get => this[AssertKeyType(key)];
|
||||
get => this[AssertKeyType(key)]!;
|
||||
set => this[AssertKeyType(key)] = AssertValueType(value);
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
return default!;
|
||||
}
|
||||
|
||||
public unsafe TValue? this[TKey key]
|
||||
public unsafe TValue this[TKey key]
|
||||
{
|
||||
[CollectionAccess(CollectionAccessType.Read)]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -211,7 +211,7 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
ref var bucket = ref _buckets[bucketIndex];
|
||||
|
||||
if (_keyEqualityComparer(key, bucket.Key))
|
||||
return bucket.Value;
|
||||
return bucket.Value!;
|
||||
|
||||
ContinueWithTailOrThrow(ref bucketIndex, key);
|
||||
}
|
||||
@@ -352,14 +352,14 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
if (IsTail(ref bucket, bucketIndex))
|
||||
{
|
||||
var previousEntry = bucket;
|
||||
var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
using var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
|
||||
SetParentTail(bucketIndex, Tails.SOLO);
|
||||
SetEntryWithoutTail(bucketIndex, entry);
|
||||
InsertEntry(previousEntry, ReplaceBehaviour.Throw, true);
|
||||
|
||||
if (tailingEntries != null)
|
||||
InsertEntries(tailingEntries, ReplaceBehaviour.Throw, true);
|
||||
if (tailingEntries != default)
|
||||
InsertEntries(tailingEntries.List, ReplaceBehaviour.Throw, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -370,9 +370,9 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
}
|
||||
}
|
||||
|
||||
private void InsertEntries(Entry[] tailingEntries, ReplaceBehaviour replaceBehaviour, bool shifting = false)
|
||||
private void InsertEntries(List<Entry> tailingEntries, ReplaceBehaviour replaceBehaviour, bool shifting = false)
|
||||
{
|
||||
for (var i = 0; i < tailingEntries.Length; i++)
|
||||
for (var i = 0; i < tailingEntries.Count; i++)
|
||||
InsertEntry(tailingEntries[i], replaceBehaviour, shifting);
|
||||
}
|
||||
|
||||
@@ -473,31 +473,31 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
}
|
||||
|
||||
var previousEntry = _buckets[bucketIndex];
|
||||
var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
using var tailingEntries = TryGetAndClearTailingEntries(bucketIndex);
|
||||
|
||||
SetParentTail(bucketIndex, Tails.SOLO);
|
||||
SetEntryAsTail(bucketIndex, entry, parentIndex, offset);
|
||||
InsertEntry(previousEntry, ReplaceBehaviour.Throw, true);
|
||||
|
||||
if (tailingEntries != null)
|
||||
InsertEntries(tailingEntries, ReplaceBehaviour.Throw, true);
|
||||
if (tailingEntries != default)
|
||||
InsertEntries(tailingEntries.List, ReplaceBehaviour.Throw, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private Entry[]? TryGetAndClearTailingEntries(int bucketIndex)
|
||||
private PooledIList<List<Entry>> TryGetAndClearTailingEntries(int bucketIndex)
|
||||
{
|
||||
if (!HasTail(bucketIndex))
|
||||
return null;
|
||||
return default;
|
||||
|
||||
var tailingEntries = new Entry[GetTailCount(bucketIndex)];
|
||||
var i = 0;
|
||||
var tailingEntries = new PooledIList<List<Entry>>();
|
||||
var tailingEntriesList = tailingEntries.List;
|
||||
var tailIndex = GetTailIndex(bucketIndex);
|
||||
|
||||
while (true)
|
||||
{
|
||||
ref var tailingBucket = ref _buckets[tailIndex];
|
||||
tailingEntries[i++] = tailingBucket;
|
||||
tailingEntriesList.Add(tailingBucket);
|
||||
tailingBucket = default;
|
||||
|
||||
if (HasTail(tailIndex))
|
||||
@@ -639,7 +639,7 @@ public class FishTable<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, I
|
||||
}
|
||||
|
||||
[CollectionAccess(CollectionAccessType.Read | CollectionAccessType.UpdatedContent)]
|
||||
public unsafe bool TryGetOrAddValue(TKey key, [MaybeNullWhen(false)] out TValue value)
|
||||
public unsafe bool TryGetOrAddValue(TKey key, out TValue value)
|
||||
{
|
||||
var result = true;
|
||||
|
||||
|
||||
@@ -28,11 +28,11 @@ namespace FisheryLib;
|
||||
[PublicAPI]
|
||||
public static class FisheryLib
|
||||
{
|
||||
public const decimal VERSION = 0.39995M;
|
||||
public const decimal VERSION = 0.5M;
|
||||
|
||||
public static decimal CurrentlyLoadedVersion { get; } = VERSION;
|
||||
|
||||
public const string VERSION_STRING = "0.3.999.5";
|
||||
public const string VERSION_STRING = "0.5.0.0";
|
||||
|
||||
// public static string GetVersion()
|
||||
// {
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
</When>
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Lib.Harmony" Version="2.*" ExcludeAssets="runtime" />
|
||||
<PackageReference Include="Lib.Harmony" Version="2.2.*" ExcludeAssets="runtime" />
|
||||
<PackageReference Include="PolySharp" Version="1.*" />
|
||||
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.*" />
|
||||
<PackageReference Include="Krafs.Publicizer" Version="2.*" />
|
||||
|
||||
@@ -9,29 +9,96 @@ namespace FisheryLib.Pools;
|
||||
|
||||
public static class FishPool<T> where T : IFishPoolable
|
||||
{
|
||||
private static T?[] _freeItems = new T[4];
|
||||
private static int _freeItemCount;
|
||||
[ThreadStatic]
|
||||
private static T?[]? _itemsThreadStatic;
|
||||
|
||||
[ThreadStatic]
|
||||
private static int _itemCountThreadStatic;
|
||||
|
||||
private static object _lockObject = new();
|
||||
private static T?[] _itemsGlobal = new T[16];
|
||||
private static int _itemCountGlobal;
|
||||
|
||||
public static T Get()
|
||||
{
|
||||
if (_freeItemCount == 0)
|
||||
return Reflection.New<T>();
|
||||
if (_itemCountThreadStatic == 0)
|
||||
FetchThroughLock();
|
||||
|
||||
var freeItem = _freeItems[--_freeItemCount];
|
||||
_freeItems[_freeItemCount] = default;
|
||||
return freeItem!;
|
||||
ref var itemBucket = ref _itemsThreadStatic![--_itemCountThreadStatic];
|
||||
var item = itemBucket;
|
||||
itemBucket = default;
|
||||
return item!;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void FetchThroughLock()
|
||||
{
|
||||
var itemsThreadStatic = _itemsThreadStatic ??= InitializeItemsThreadStatic();
|
||||
var createNew = false;
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (_itemCountGlobal != 0)
|
||||
{
|
||||
for (var i = 8; i-- > 0;)
|
||||
{
|
||||
itemsThreadStatic[i] = _itemsGlobal[--_itemCountGlobal];
|
||||
_itemsGlobal[_itemCountGlobal] = default;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
createNew = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (createNew)
|
||||
{
|
||||
for (var i = 8; i-- > 0;)
|
||||
itemsThreadStatic[i] = Reflection.New<T>();
|
||||
}
|
||||
|
||||
_itemCountThreadStatic = 8;
|
||||
}
|
||||
|
||||
public static void Return(T item)
|
||||
{
|
||||
item.Reset();
|
||||
|
||||
if (++_freeItemCount >= _freeItems.Length)
|
||||
ExpandFreeItems();
|
||||
var itemsThreadStatic = _itemsThreadStatic ??= InitializeItemsThreadStatic();
|
||||
|
||||
_freeItems[_freeItemCount - 1] = item;
|
||||
if (_itemCountThreadStatic == 16)
|
||||
PushThroughLock();
|
||||
|
||||
itemsThreadStatic[_itemCountThreadStatic++] = item;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void ExpandFreeItems() => Array.Resize(ref _freeItems, _freeItems.Length << 1);
|
||||
private static void PushThroughLock()
|
||||
{
|
||||
var itemsThreadStatic = _itemsThreadStatic!;
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (_itemCountGlobal + 8 > _itemsGlobal.Length)
|
||||
ExpandGlobalItems();
|
||||
|
||||
for (var i = 16; i-- > 8;)
|
||||
{
|
||||
ref var itemBucket = ref itemsThreadStatic[i];
|
||||
var item = itemBucket;
|
||||
itemBucket = default;
|
||||
|
||||
_itemsGlobal[_itemCountGlobal++] = item;
|
||||
}
|
||||
}
|
||||
|
||||
_itemCountThreadStatic = 8;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static T?[] InitializeItemsThreadStatic() => new T?[16];
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void ExpandGlobalItems() => Array.Resize(ref _itemsGlobal, _itemsGlobal.Length << 1);
|
||||
}
|
||||
@@ -12,8 +12,15 @@ public record struct PooledIList<T>() : IDisposable where T : IList, new()
|
||||
{
|
||||
public T List = Get();
|
||||
|
||||
private static T?[] _freeItems = new T[4];
|
||||
private static int _freeItemCount;
|
||||
[ThreadStatic]
|
||||
private static T?[]? _itemsThreadStatic;
|
||||
|
||||
[ThreadStatic]
|
||||
private static int _itemCountThreadStatic;
|
||||
|
||||
private static object _lockObject = new();
|
||||
private static T?[] _itemsGlobal = new T[16];
|
||||
private static int _itemCountGlobal;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -26,24 +33,84 @@ public record struct PooledIList<T>() : IDisposable where T : IList, new()
|
||||
|
||||
public static T Get()
|
||||
{
|
||||
if (_freeItemCount == 0)
|
||||
return Reflection.New<T>();
|
||||
if (_itemCountThreadStatic == 0)
|
||||
FetchThroughLock();
|
||||
|
||||
var freeItem = _freeItems[--_freeItemCount];
|
||||
_freeItems[_freeItemCount] = default;
|
||||
return freeItem!;
|
||||
ref var itemBucket = ref _itemsThreadStatic![--_itemCountThreadStatic];
|
||||
var item = itemBucket;
|
||||
itemBucket = default;
|
||||
return item!;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void FetchThroughLock()
|
||||
{
|
||||
var itemsThreadStatic = _itemsThreadStatic ??= InitializeItemsThreadStatic();
|
||||
var createNew = false;
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (_itemCountGlobal != 0)
|
||||
{
|
||||
for (var i = 8; i-- > 0;)
|
||||
{
|
||||
itemsThreadStatic[i] = _itemsGlobal[--_itemCountGlobal];
|
||||
_itemsGlobal[_itemCountGlobal] = default;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
createNew = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (createNew)
|
||||
{
|
||||
for (var i = 8; i-- > 0;)
|
||||
itemsThreadStatic[i] = Reflection.New<T>();
|
||||
}
|
||||
|
||||
_itemCountThreadStatic = 8;
|
||||
}
|
||||
|
||||
public static void Return(T item)
|
||||
{
|
||||
item.Clear();
|
||||
|
||||
if (++_freeItemCount >= _freeItems.Length)
|
||||
ExpandFreeItems();
|
||||
var itemsThreadStatic = _itemsThreadStatic ??= InitializeItemsThreadStatic();
|
||||
|
||||
_freeItems[_freeItemCount - 1] = item;
|
||||
if (_itemCountThreadStatic == 16)
|
||||
PushThroughLock();
|
||||
|
||||
itemsThreadStatic[_itemCountThreadStatic++] = item;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void ExpandFreeItems() => Array.Resize(ref _freeItems, _freeItems.Length << 1);
|
||||
private static void PushThroughLock()
|
||||
{
|
||||
var itemsThreadStatic = _itemsThreadStatic!;
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
if (_itemCountGlobal + 8 > _itemsGlobal.Length)
|
||||
ExpandGlobalItems();
|
||||
|
||||
for (var i = 16; i-- > 8;)
|
||||
{
|
||||
ref var itemBucket = ref itemsThreadStatic[i];
|
||||
var item = itemBucket;
|
||||
itemBucket = default;
|
||||
|
||||
_itemsGlobal[_itemCountGlobal++] = item;
|
||||
}
|
||||
}
|
||||
|
||||
_itemCountThreadStatic = 8;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static T?[] InitializeItemsThreadStatic() => new T?[16];
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static void ExpandGlobalItems() => Array.Resize(ref _itemsGlobal, _itemsGlobal.Length << 1);
|
||||
}
|
||||
@@ -40,11 +40,11 @@ public static class SpanExtensions
|
||||
=> ref Unsafe.Add(ref span.DangerousGetPinnableReference(), i);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Sort<T>(this Span<T> span, IComparer<T>? comparer)
|
||||
public static void Sort<T>(this Span<T> span, IComparer<T>? comparer = null)
|
||||
=> ArraySortHelper<T>.Default.Sort(span, comparer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Sort<TKey, TValue>(this Span<TValue> values, Span<TKey> keys, IComparer<TKey>? comparer)
|
||||
public static void Sort<TKey, TValue>(this Span<TValue> values, Span<TKey> keys, IComparer<TKey>? comparer = null)
|
||||
=> ArraySortHelper<TKey, TValue>.Default.Sort(keys, values, comparer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
Reference in New Issue
Block a user