fix: Optimize OrderedHashSet and introduce more high perf collections (#679)

* Optimizes OrderedHashSet using the learnings here: https://github.com/dotnet/runtime/issues/10050
* Removes ValueCollection since it was a relic from the conversion of the OrderedDictionary
* Centralizes throw strings.
* Adds pooled ordered hash set (but doesn't use it)

Closes #448
This commit is contained in:
Kamron Batman 2021-08-08 21:37:25 -07:00 • committed by GitHub
parent d10e782371
commit b0f8fe0350
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 2495 additions and 481 deletions

View file

@ -14,14 +14,16 @@
*************************************************************************/
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Server.Collections
{
public static class CollectionHelpers
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddNotNull<T>(this ICollection<T> coll, T t) where T : class
{
if (t != default)
if (t != null)
{
coll.Add(t);
}

View file

@ -0,0 +1,45 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: CollectionThrowStrings.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
namespace Server.Collections
{
public static class CollectionThrowStrings
{
public const string ArgumentOutOfRange_Index =
"Index was out of range. Must be non-negative and less than the size of the collection.";
public const string ArgumentOutOfRange_NeedNonNegNum = "Non-negative number required.";
public const string Argument_InvalidOffLen =
"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.";
public const string Argument_AddingDuplicate = "An item with the same value has already been added. Value: {0}";
public const string Arg_ArrayPlusOffTooSmall =
"Destination array is not long enough to copy all the items in the collection. Check array index and length.";
public const string InvalidOperation_ConcurrentOperationsNotSupported =
"Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.";
public const string InvalidOperation_EnumFailedVersion =
"Collection was modified after the enumerator was instantiated.";
public const string InvalidOperation_EmptyQueue = "Queue empty.";
public const string InvalidOperation_EnumNotStarted = "Enumeration has not started. Call MoveNext.";
public const string InvalidOperation_EnumEnded = "Enumeration already finished.";
}
}

View file

@ -1,144 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: OrderedHashSet.ValueCollection.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Server.Collections
{
public partial class OrderedHashSet<TValue>
{
[DebuggerDisplay("Count = {Count}")]
public sealed class ValueCollection : IList<TValue>, IReadOnlyList<TValue>
{
private readonly OrderedHashSet<TValue> _orderedHashSet;
private const string NotSupported_ValueCollectionSet =
"Mutating a key collection derived from a hash set is not allowed.";
public int Count => _orderedHashSet.Count;
public TValue this[int index] => ((IList<TValue>)_orderedHashSet)[index];
TValue IList<TValue>.this[int index]
{
get => this[index];
set => throw new NotSupportedException(NotSupported_ValueCollectionSet);
}
bool ICollection<TValue>.IsReadOnly => true;
internal ValueCollection(OrderedHashSet<TValue> OrderedHashSet) =>
_orderedHashSet = OrderedHashSet;
public Enumerator GetEnumerator() => new Enumerator(_orderedHashSet);
IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
int IList<TValue>.IndexOf(TValue item) => _orderedHashSet.IndexOf(item);
void IList<TValue>.Insert(int index, TValue item) => throw new NotSupportedException(NotSupported_ValueCollectionSet);
void IList<TValue>.RemoveAt(int index) => throw new NotSupportedException(NotSupported_ValueCollectionSet);
void ICollection<TValue>.Add(TValue item) => throw new NotSupportedException(NotSupported_ValueCollectionSet);
void ICollection<TValue>.Clear() => throw new NotSupportedException(NotSupported_ValueCollectionSet);
bool ICollection<TValue>.Contains(TValue item) => _orderedHashSet.Contains(item);
void ICollection<TValue>.CopyTo(TValue[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if ((uint)arrayIndex > (uint)array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), ArgumentOutOfRange_NeedNonNegNum);
}
int count = Count;
if (array.Length - arrayIndex < count)
{
throw new ArgumentException(Arg_ArrayPlusOffTooSmall);
}
Entry[] entries = _orderedHashSet._entries;
for (int i = 0; i < count; ++i)
{
array[i + arrayIndex] = entries[i].Value;
}
}
bool ICollection<TValue>.Remove(TValue item) => throw new NotSupportedException(NotSupported_ValueCollectionSet);
public struct Enumerator : IEnumerator<TValue>
{
private readonly OrderedHashSet<TValue> _orderedHashSet;
private readonly int _version;
private int _index;
private TValue _current;
public TValue Current => _current;
object IEnumerator.Current => _current;
internal Enumerator(OrderedHashSet<TValue> OrderedHashSet)
{
_orderedHashSet = OrderedHashSet;
_version = OrderedHashSet._version;
_index = 0;
_current = default;
}
public void Dispose()
{
}
public bool MoveNext()
{
if (_version != _orderedHashSet._version)
{
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
}
if (_index < _orderedHashSet.Count)
{
_current = _orderedHashSet._entries[_index].Value;
++_index;
return true;
}
_current = default;
return false;
}
void IEnumerator.Reset()
{
if (_version != _orderedHashSet._version)
{
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
}
_index = 0;
_current = default;
}
}
}
}
}

View file

@ -14,7 +14,6 @@
*************************************************************************/
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
@ -24,7 +23,7 @@ using Microsoft.Collections.Extensions;
namespace Server.Collections
{
[DebuggerDisplay("Count = {Count}")]
public partial class OrderedHashSet<TValue> : IList<TValue>, IReadOnlyList<TValue>, ISet<TValue>, IReadOnlySet<TValue>
public class OrderedHashSet<TValue> : IList<TValue>
{
private struct Entry
{
@ -33,38 +32,20 @@ namespace Server.Collections
public int Next; // the index of the next item in the same bucket, -1 if last
}
private const string ArgumentOutOfRange_Index =
"Index was out of range. Must be non-negative and less than the size of the collection.";
private const string ArgumentOutOfRange_NeedNonNegNum =
"Non-negative number required.";
private const string Argument_InvalidOffLen =
"Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.";
private const string Argument_AddingDuplicate = "An item with the same value has already been added. Value: {0}";
private const string Arg_ArrayPlusOffTooSmall =
"Destination array is not long enough to copy all the items in the collection. Check array index and length.";
private const string InvalidOperation_ConcurrentOperationsNotSupported =
"Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.";
private const string InvalidOperation_EnumFailedVersion =
"Collection was modified; enumeration operation may not execute.";
private static readonly Entry[] InitialEntries = new Entry[1];
private int[] _buckets = HashHelpers.SizeOneIntArray;
private Entry[] _entries = InitialEntries;
private ulong _fastModMultiplier;
private int _count;
private int _version;
private readonly IEqualityComparer<TValue> _comparer;
private ValueCollection _values;
#nullable enable
private readonly IEqualityComparer<TValue>? _comparer;
#nullable disable
public int Count => _count;
public IEqualityComparer<TValue> Comparer => _comparer ?? EqualityComparer<TValue>.Default;
public ValueCollection Values => _values ??= new ValueCollection(this);
#nullable enable
public IEqualityComparer<TValue>? Comparer => _comparer;
#nullable disable
public OrderedHashSet()
: this(0)
@ -111,48 +92,8 @@ namespace Server.Collections
}
}
public void ExceptWith(IEnumerable<TValue> other)
{
throw new NotImplementedException();
}
public void IntersectWith(IEnumerable<TValue> other)
{
throw new NotImplementedException();
}
public bool Contains(TValue item) => TryGetValue(item, out var value) && EqualityComparer<TValue>.Default.Equals(value);
// TODO: Implement IReadOnlySet and ISet
bool IReadOnlySet<TValue>.IsProperSubsetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool IReadOnlySet<TValue>.IsProperSupersetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool IReadOnlySet<TValue>.IsSubsetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool IReadOnlySet<TValue>.IsSupersetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool IReadOnlySet<TValue>.Overlaps(IEnumerable<TValue> other) => throw new NotImplementedException();
bool IReadOnlySet<TValue>.SetEquals(IEnumerable<TValue> other) => throw new NotImplementedException();
bool ISet<TValue>.IsProperSubsetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool ISet<TValue>.IsProperSupersetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool ISet<TValue>.IsSubsetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool ISet<TValue>.IsSupersetOf(IEnumerable<TValue> other) => throw new NotImplementedException();
bool ISet<TValue>.Overlaps(IEnumerable<TValue> other) => throw new NotImplementedException();
bool ISet<TValue>.SetEquals(IEnumerable<TValue> other) => throw new NotImplementedException();
public void SymmetricExceptWith(IEnumerable<TValue> other) => throw new NotImplementedException();
public void UnionWith(IEnumerable<TValue> other) => throw new NotImplementedException();
public void Clear()
{
if (_count > 0)
@ -195,114 +136,12 @@ namespace Server.Collections
{
if ((uint)index > (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(index), ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
TryInsert(index, value);
}
public void Move(int fromIndex, int toIndex)
{
if ((uint)fromIndex >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(fromIndex), ArgumentOutOfRange_Index);
}
if ((uint)toIndex >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(toIndex), ArgumentOutOfRange_Index);
}
if (fromIndex == toIndex)
{
return;
}
Entry[] entries = _entries;
Entry temp = entries[fromIndex];
RemoveEntryFromBucket(fromIndex);
int direction = fromIndex < toIndex ? 1 : -1;
for (int i = fromIndex; i != toIndex; i += direction)
{
entries[i] = entries[i + direction];
UpdateBucketIndex(i + direction, -direction);
}
AddEntryToBucket(ref temp, toIndex, _buckets);
entries[toIndex] = temp;
++_version;
}
public void MoveRange(int fromIndex, int toIndex, int count)
{
if (count == 1)
{
Move(fromIndex, toIndex);
return;
}
if ((uint)fromIndex >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(fromIndex), ArgumentOutOfRange_Index);
}
if ((uint)toIndex >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(toIndex), ArgumentOutOfRange_Index);
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), ArgumentOutOfRange_NeedNonNegNum);
}
if (fromIndex + count > Count)
{
throw new ArgumentException(Argument_InvalidOffLen);
}
if (toIndex + count > Count)
{
throw new ArgumentException(Argument_InvalidOffLen);
}
if (fromIndex == toIndex || count == 0)
{
return;
}
Entry[] entries = _entries;
Entry[] entriesToMove = ArrayPool<Entry>.Shared.Rent(count);
for (int i = 0; i < count; ++i)
{
entriesToMove[i] = entries[fromIndex + i];
RemoveEntryFromBucket(fromIndex + i);
}
// Move entries in between
int direction = 1;
int amount = count;
int start = fromIndex;
int end = toIndex;
if (fromIndex > toIndex)
{
direction = -1;
amount = -count;
start = fromIndex + count - 1;
end = toIndex + count - 1;
}
for (int i = start; i != end; i += direction)
{
entries[i] = entries[i + amount];
UpdateBucketIndex(i + amount, -amount);
}
int[] buckets = _buckets;
// Copy entries to destination
for (int i = 0; i < count; ++i)
{
Entry temp = entriesToMove[i];
AddEntryToBucket(ref temp, toIndex + i, buckets);
entries[toIndex + i] = temp;
}
++_version;
ArrayPool<Entry>.Shared.Return(entriesToMove);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ref int GetBucketRef(uint hashCode)
{
@ -327,7 +166,7 @@ namespace Server.Collections
int count = Count;
if ((uint)index >= (uint)count)
{
throw new ArgumentOutOfRangeException(nameof(index), ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
// Remove the entry from the bucket
@ -345,8 +184,6 @@ namespace Server.Collections
++_version;
}
public void TrimExcess() => TrimExcess(Count);
public void TrimExcess(int capacity)
{
if (capacity < Count)
@ -383,7 +220,7 @@ namespace Server.Collections
{
if ((uint)index >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(index), ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
return _entries[index].Value;
@ -392,7 +229,7 @@ namespace Server.Collections
{
if ((uint)index >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(index), ArgumentOutOfRange_Index);
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
TValue v = value;
@ -412,7 +249,7 @@ namespace Server.Collections
}
else
{
throw new ArgumentException(string.Format(Argument_AddingDuplicate, v.ToString()));
throw new ArgumentException(string.Format(CollectionThrowStrings.Argument_AddingDuplicate, v.ToString()));
}
}
}
@ -432,13 +269,13 @@ namespace Server.Collections
if ((uint)arrayIndex > (uint)array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), ArgumentOutOfRange_NeedNonNegNum);
throw new ArgumentOutOfRangeException(nameof(arrayIndex), CollectionThrowStrings.ArgumentOutOfRange_NeedNonNegNum);
}
int count = Count;
if (array.Length - arrayIndex < count)
{
throw new ArgumentException(Arg_ArrayPlusOffTooSmall);
throw new ArgumentException(CollectionThrowStrings.Arg_ArrayPlusOffTooSmall);
}
Entry[] entries = _entries;
@ -470,38 +307,108 @@ namespace Server.Collections
return newEntries;
}
#nullable enable
private int IndexOf(TValue value, out uint hashCode)
{
IEqualityComparer<TValue> comparer = _comparer;
hashCode = (uint)(comparer?.GetHashCode(value) ?? value?.GetHashCode() ?? 0);
ref int bucket = ref GetBucketRef(hashCode);
int i = bucket - 1;
if (i >= 0)
ref int bucket = ref Unsafe.NullRef<int>();
int i;
IEqualityComparer<TValue>? comparer = _comparer;
if (comparer == null)
{
comparer ??= EqualityComparer<TValue>.Default;
Entry[] entries = _entries;
int collisionCount = 0;
do
hashCode = (uint)(value?.GetHashCode() ?? 0);
bucket = ref GetBucketRef(hashCode);
i = bucket - 1;
if (i >= 0)
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && comparer.Equals(entry.Value, value))
if (typeof(TValue).IsValueType)
{
break;
// ValueType: Devirtualize with EqualityComparer<TValue>.Default intrinsic
Entry[] entries = _entries;
int collisionCount = 0;
do
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && EqualityComparer<TValue>.Default.Equals(entry.Value, value))
{
break;
}
i = entry.Next;
if (collisionCount >= entries.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(
CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported
);
}
++collisionCount;
} while (i >= 0);
}
i = entry.Next;
if (collisionCount >= entries.Length)
else
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(InvalidOperation_ConcurrentOperationsNotSupported);
// Object type: Shared Generic, EqualityComparer<TValue>.Default won't devirtualize (https://github.com/dotnet/runtime/issues/10050),
// so cache in a local rather than get EqualityComparer per loop iteration.
var defaultComparer = EqualityComparer<TValue>.Default;
Entry[] entries = _entries;
int collisionCount = 0;
do
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && defaultComparer.Equals(entry.Value, value))
{
break;
}
i = entry.Next;
if (collisionCount >= entries.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(
CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported
);
}
++collisionCount;
} while (i >= 0);
}
++collisionCount;
} while (i >= 0);
}
}
else
{
hashCode = (uint)comparer.GetHashCode(value);
bucket = ref GetBucketRef(hashCode);
i = bucket - 1;
if (i >= 0)
{
Entry[] entries = _entries;
int collisionCount = 0;
do
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && comparer.Equals(entry.Value, value))
{
break;
}
i = entry.Next;
if (collisionCount >= entries.Length)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported);
}
++collisionCount;
} while (i >= 0);
}
}
return i;
}
#nullable enable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int TryInsert(int? index, TValue value)
{
@ -574,7 +481,7 @@ namespace Server.Collections
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(InvalidOperation_ConcurrentOperationsNotSupported);
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported);
}
++collisionCount;
}
@ -609,7 +516,7 @@ namespace Server.Collections
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(InvalidOperation_ConcurrentOperationsNotSupported);
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported);
}
++collisionCount;
}
@ -643,7 +550,7 @@ namespace Server.Collections
{
if (_version != _orderedHashSet._version)
{
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
}
if (_index < _orderedHashSet.Count)
@ -661,7 +568,7 @@ namespace Server.Collections
{
if (_version != _orderedHashSet._version)
{
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
}
_index = 0;

View file

@ -0,0 +1,603 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PooledOrderedHashSet.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Microsoft.Collections.Extensions;
namespace Server.Collections
{
[DebuggerDisplay("Count = {Count}")]
public class PooledOrderedHashSet<TValue> : IList<TValue>, IDisposable
{
private struct Entry
{
public uint HashCode;
public TValue Value;
public int Next; // the index of the next item in the same bucket, -1 if last
}
private static readonly Entry[] InitialEntries = new Entry[1];
private int[] _buckets = HashHelpers.SizeOneIntArray;
private int _bucketsLength = 1;
private Entry[] _entries = InitialEntries;
private int _entriesLength = 1;
private ulong _fastModMultiplier;
private int _count;
private int _version;
#nullable enable
private readonly IEqualityComparer<TValue>? _comparer;
#nullable disable
public int Count => _count;
#nullable enable
public IEqualityComparer<TValue>? Comparer => _comparer;
#nullable disable
public PooledOrderedHashSet()
: this(0)
{
}
public PooledOrderedHashSet(IEqualityComparer<TValue> comparer)
: this(0, comparer)
{
}
public PooledOrderedHashSet(int capacity, IEqualityComparer<TValue> comparer = null)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
if (capacity > 0)
{
int newSize = HashHelpers.GetPrime(capacity);
_buckets = ArrayPool<int>.Shared.Rent(newSize);
_bucketsLength = newSize;
_entries = ArrayPool<Entry>.Shared.Rent(newSize);
_entriesLength = newSize;
_fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)newSize);
}
if (comparer != EqualityComparer<TValue>.Default)
{
_comparer = comparer;
}
}
public PooledOrderedHashSet(IEnumerable<TValue> collection, IEqualityComparer<TValue> comparer = null)
: this((collection as ICollection<TValue>)?.Count ?? 0, comparer)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
foreach (TValue value in collection)
{
Add(value);
}
}
public bool Contains(TValue item) => TryGetValue(item, out var value) && EqualityComparer<TValue>.Default.Equals(value);
public void Clear()
{
if (_count > 0)
{
Array.Clear(_buckets, 0, _bucketsLength);
Array.Clear(_entries, 0, _count);
_count = 0;
++_version;
}
}
public Enumerator GetEnumerator() => new(this);
void ICollection<TValue>.Add(TValue item) => TryAdd(item);
public bool Add(TValue item) => TryAdd(item);
public int GetOrAdd(TValue value) => TryInsert(null, value);
public int IndexOf(TValue value) => IndexOf(value, out _);
public void Insert(int index, TValue value)
{
if ((uint)index > (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
TryInsert(index, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ref int GetBucketRef(uint hashCode)
{
int[] buckets = _buckets!;
return ref buckets[HashHelpers.FastMod(hashCode, (uint)_bucketsLength, _fastModMultiplier)];
}
public bool Remove(TValue value)
{
int index = IndexOf(value);
if (index >= 0)
{
RemoveAt(index);
return true;
}
return false;
}
public void RemoveAt(int index)
{
int count = Count;
if ((uint)index >= (uint)count)
{
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
// Remove the entry from the bucket
RemoveEntryFromBucket(index);
// Decrement the indices > index
Entry[] entries = _entries;
for (int i = index + 1; i < count; ++i)
{
entries[i - 1] = entries[i];
UpdateBucketIndex(i, incrementAmount: -1);
}
--_count;
entries[_count] = default;
++_version;
}
public bool TryAdd(TValue value) => TryInsert(null, value) != _count - 1;
public bool TryGetValue(TValue value, out TValue actualValue)
{
int index = IndexOf(value);
if (index >= 0)
{
actualValue = _entries[index].Value;
return true;
}
actualValue = default;
return false;
}
public TValue this[int index]
{
get
{
if ((uint)index >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
return _entries[index].Value;
}
set
{
if ((uint)index >= (uint)Count)
{
throw new ArgumentOutOfRangeException(nameof(index), CollectionThrowStrings.ArgumentOutOfRange_Index);
}
TValue v = value;
int foundIndex = IndexOf(v, out uint hashCode);
if (foundIndex < 0)
{
RemoveEntryFromBucket(index);
Entry entry = new Entry { HashCode = hashCode, Value = value };
AddEntryToBucket(ref entry, index, _buckets, _bucketsLength);
_entries[index] = entry;
++_version;
}
else if (foundIndex == index)
{
ref Entry entry = ref _entries[index];
entry.Value = value;
}
else
{
throw new ArgumentException(string.Format(CollectionThrowStrings.Argument_AddingDuplicate, v.ToString()));
}
}
}
public bool IsReadOnly => false;
IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void CopyTo(TValue[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if ((uint)arrayIndex > (uint)array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), CollectionThrowStrings.ArgumentOutOfRange_NeedNonNegNum);
}
int count = Count;
if (array.Length - arrayIndex < count)
{
throw new ArgumentException(CollectionThrowStrings.Arg_ArrayPlusOffTooSmall);
}
Entry[] entries = _entries;
for (int i = 0; i < count; ++i)
{
Entry entry = entries[i];
array[i + arrayIndex] = entry.Value;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Entry[] Resize(int newSize)
{
int[] newBuckets = _buckets.Length < newSize ? ArrayPool<int>.Shared.Rent(newSize) : _buckets;
Entry[] newEntries = _entries.Length < newSize ? ArrayPool<Entry>.Shared.Rent(newSize) : _entries;
int count = Count;
Array.Copy(_entries, newEntries, count);
_fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)newSize);
for (int i = 0; i < count; ++i)
{
AddEntryToBucket(ref newEntries[i], i, newBuckets, newSize);
}
var oldBuckets = _buckets;
var oldEntries = _entries;
if (oldBuckets.Length > 1 && oldBuckets != newBuckets)
{
ArrayPool<int>.Shared.Return(oldBuckets, true);
}
if (oldEntries.Length > 1 && oldEntries != newEntries)
{
ArrayPool<Entry>.Shared.Return(oldEntries, true);
}
_buckets = newBuckets;
_bucketsLength = newSize;
_entries = newEntries;
_entriesLength = newSize;
return newEntries;
}
#nullable enable
private int IndexOf(TValue value, out uint hashCode)
{
ref int bucket = ref Unsafe.NullRef<int>();
int i;
IEqualityComparer<TValue>? comparer = _comparer;
if (comparer == null)
{
hashCode = (uint)value.GetHashCode();
bucket = ref GetBucketRef(hashCode);
i = bucket - 1;
if (i >= 0)
{
if (typeof(TValue).IsValueType)
{
// ValueType: Devirtualize with EqualityComparer<TValue>.Default intrinsic
Entry[] entries = _entries;
int collisionCount = 0;
do
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && EqualityComparer<TValue>.Default.Equals(entry.Value, value))
{
break;
}
i = entry.Next;
if (collisionCount >= _entriesLength)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(
CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported
);
}
++collisionCount;
} while (i >= 0);
}
else
{
// Object type: Shared Generic, EqualityComparer<TValue>.Default won't devirtualize (https://github.com/dotnet/runtime/issues/10050),
// so cache in a local rather than get EqualityComparer per loop iteration.
var defaultComparer = EqualityComparer<TValue>.Default;
Entry[] entries = _entries;
int collisionCount = 0;
do
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && defaultComparer.Equals(entry.Value, value))
{
break;
}
i = entry.Next;
if (collisionCount >= _entriesLength)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(
CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported
);
}
++collisionCount;
} while (i >= 0);
}
}
}
else
{
hashCode = (uint)comparer.GetHashCode(value);
bucket = ref GetBucketRef(hashCode);
i = bucket - 1;
if (i >= 0)
{
Entry[] entries = _entries;
int collisionCount = 0;
do
{
Entry entry = entries[i];
if (entry.HashCode == hashCode && comparer.Equals(entry.Value, value))
{
break;
}
i = entry.Next;
if (collisionCount >= _entriesLength)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported);
}
++collisionCount;
} while (i >= 0);
}
}
return i;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int TryInsert(int? index, TValue value)
{
int i = IndexOf(value, out uint hashCode);
return i >= 0 ? i : AddInternal(index, value, hashCode);
}
private int AddInternal(int? index, TValue value, uint hashCode)
{
Entry[] entries = _entries;
// Check if resize is needed
int count = Count;
if (_entriesLength == count || entries.Length == 1)
{
entries = Resize(HashHelpers.ExpandPrime(_entriesLength));
}
// Increment indices >= index;
int actualIndex = index ?? count;
for (int i = count - 1; i >= actualIndex; --i)
{
entries[i + 1] = entries[i];
UpdateBucketIndex(i, incrementAmount: 1);
}
ref Entry entry = ref entries[actualIndex];
entry.HashCode = hashCode;
entry.Value = value;
AddEntryToBucket(ref entry, actualIndex, _buckets, _bucketsLength);
++_count;
++_version;
return actualIndex;
}
#nullable restore
// Returns the index of the next entry in the bucket
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddEntryToBucket(ref Entry entry, int entryIndex, int[] buckets, int bucketsLength)
{
ref int b = ref buckets[(int)(entry.HashCode % (uint)bucketsLength)];
entry.Next = b - 1;
b = entryIndex + 1;
}
private void RemoveEntryFromBucket(int entryIndex)
{
Entry[] entries = _entries;
Entry entry = entries[entryIndex];
ref int bucket = ref GetBucketRef(entry.HashCode);
// Bucket was pointing to removed entry. Update it to point to the next in the chain
if (bucket == entryIndex + 1)
{
bucket = entry.Next + 1;
}
else
{
// Start at the entry the bucket points to, and walk the chain until we find the entry with the index we want to remove, then fix the chain
int i = bucket - 1;
int collisionCount = 0;
while (true)
{
ref Entry e = ref entries[i];
if (e.Next == entryIndex)
{
e.Next = entry.Next;
return;
}
i = e.Next;
if (collisionCount >= _entriesLength)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported);
}
++collisionCount;
}
}
}
private void UpdateBucketIndex(int entryIndex, int incrementAmount)
{
Entry[] entries = _entries;
Entry entry = entries[entryIndex];
ref int bucket = ref GetBucketRef(entry.HashCode);
// Bucket was pointing to entry. Increment the index by incrementAmount.
if (bucket == entryIndex + 1)
{
bucket += incrementAmount;
}
else
{
// Start at the entry the bucket points to, and walk the chain until we find the entry with the index we want to increment.
int i = bucket - 1;
int collisionCount = 0;
while (true)
{
ref Entry e = ref entries[i];
if (e.Next == entryIndex)
{
e.Next += incrementAmount;
return;
}
i = e.Next;
if (collisionCount >= _entriesLength)
{
// The chain of entries forms a loop; which means a concurrent update has happened.
// Break out of the loop and throw, rather than looping forever.
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_ConcurrentOperationsNotSupported);
}
++collisionCount;
}
}
}
public struct Enumerator : IEnumerator<TValue>
{
private readonly PooledOrderedHashSet<TValue> _PooledOrderedHashSet;
private readonly int _version;
private int _index;
private TValue _current;
public TValue Current => _current;
object IEnumerator.Current => _current;
internal Enumerator(PooledOrderedHashSet<TValue> PooledOrderedHashSet)
{
_PooledOrderedHashSet = PooledOrderedHashSet;
_version = PooledOrderedHashSet._version;
_index = 0;
_current = default;
}
public void Dispose()
{
}
public bool MoveNext()
{
if (_version != _PooledOrderedHashSet._version)
{
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
}
if (_index < _PooledOrderedHashSet.Count)
{
Entry entry = _PooledOrderedHashSet._entries[_index];
_current = entry.Value;
++_index;
return true;
}
_current = default;
return false;
}
void IEnumerator.Reset()
{
if (_version != _PooledOrderedHashSet._version)
{
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
}
_index = 0;
_current = default;
}
}
public void Dispose()
{
if (_buckets.Length > 1)
{
ArrayPool<int>.Shared.Return(_buckets, true);
}
if (_entries.Length > 1)
{
ArrayPool<Entry>.Shared.Return(_entries, true);
}
_buckets = HashHelpers.SizeOneIntArray;
_entries = InitialEntries;
_count = 0;
GC.SuppressFinalize(this);
}
~PooledOrderedHashSet()
{
if (_buckets.Length > 1)
{
ArrayPool<int>.Shared.Return(_buckets, true);
}
if (_entries.Length > 1)
{
ArrayPool<Entry>.Shared.Return(_entries, true);
}
_buckets = HashHelpers.SizeOneIntArray;
_entries = InitialEntries;
_count = 0;
}
}
}

View file

@ -1,17 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.Collections.Generic
namespace Server.Collections
{
// A simple Queue of generic objects. Internally it is implemented as a
// circular buffer, so Enqueue can be O(n). Dequeue is O(1).
[DebuggerDisplay("Count = {Count}")]
[Serializable]
[System.Serializable]
public ref struct PooledRefQueue<T>
{
private T[] _array;
@ -29,7 +30,7 @@ namespace System.Collections.Generic
{
_array = capacity switch
{
< 0 => throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "Non-negative number required."),
< 0 => throw new ArgumentOutOfRangeException(nameof(capacity), capacity, CollectionThrowStrings.ArgumentOutOfRange_NeedNonNegNum),
0 => Array.Empty<T>(),
_ => ArrayPool<T>.Shared.Rent(capacity)
};
@ -74,17 +75,17 @@ namespace System.Collections.Generic
{
if (array == null)
{
throw new ArgumentNullException(nameof(array), "Array cannot be null.");
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, "Index was out of range. Must be non-negative and less than the size of the collection.");
throw new ArgumentOutOfRangeException(nameof(arrayIndex), arrayIndex, CollectionThrowStrings.ArgumentOutOfRange_Index);
}
if (array.Length - arrayIndex < _size)
{
throw new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
throw new ArgumentException(CollectionThrowStrings.Argument_InvalidOffLen);
}
int numToCopy = _size;
@ -305,16 +306,7 @@ namespace System.Collections.Generic
private void ThrowForEmptyQueue()
{
Debug.Assert(_size == 0);
throw new InvalidOperationException("Queue empty.");
}
public void TrimExcess()
{
int threshold = (int)(_array.Length * 0.9);
if (_size < threshold)
{
SetCapacity(_size);
}
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EmptyQueue);
}
/// <summary>
@ -325,7 +317,7 @@ namespace System.Collections.Generic
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "Non-negative number required.");
throw new ArgumentOutOfRangeException(nameof(capacity), capacity, CollectionThrowStrings.ArgumentOutOfRange_NeedNonNegNum);
}
if (_array.Length < capacity)
@ -338,10 +330,8 @@ namespace System.Collections.Generic
private void Grow(int capacity)
{
Debug.Assert(_array.Length < capacity);
const int GrowFactor = 2;
const int MinimumGrow = 32;
const int MinimumGrow = 4;
int newcapacity = GrowFactor * _array.Length;
@ -405,7 +395,7 @@ namespace System.Collections.Generic
{
if (_version != _q._version)
{
throw new InvalidOperationException("Collection was modified after the enumerator was instantiated.");
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
}
if (_index == -2)
@ -462,14 +452,14 @@ namespace System.Collections.Generic
private void ThrowEnumerationNotStartedOrEnded()
{
Debug.Assert(_index == -1 || _index == -2);
throw new InvalidOperationException(_index == -1 ? "Enumeration has not started. Call MoveNext." : "Enumeration already finished.");
throw new InvalidOperationException(_index == -1 ? CollectionThrowStrings.InvalidOperation_EnumNotStarted : CollectionThrowStrings.InvalidOperation_EnumEnded);
}
public void Reset()
{
if (_version != _q._version)
{
throw new InvalidOperationException("Collection was modified after the enumerator was instantiated.");
throw new InvalidOperationException(CollectionThrowStrings.InvalidOperation_EnumFailedVersion);
}
_index = -1;

View file

@ -49,9 +49,10 @@ namespace Server.Network
private static readonly byte[] _layoutBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _stringsBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _packBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly OrderedHashSet<string> _stringsList = new(32);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WritePacked(ReadOnlySpan<byte> span, ref SpanWriter writer)
public static void WritePacked(ReadOnlySpan<byte> span, ref SpanWriter writer)
{
var length = span.Length;
@ -127,16 +128,14 @@ namespace Server.Network
layoutWriter.Write(Gump.NoResize);
}
var stringsList = new OrderedHashSet<string>(11);
foreach (var entry in gump.Entries)
{
entry.AppendTo(ref layoutWriter, stringsList, ref entries, ref switches);
entry.AppendTo(ref layoutWriter, _stringsList, ref entries, ref switches);
}
var stringsWriter = new SpanWriter(_stringsBuffer);
foreach (var str in stringsList)
foreach (var str in _stringsList)
{
var s = str ?? "";
stringsWriter.Write((ushort)s.Length);
@ -155,7 +154,7 @@ namespace Server.Network
maxLength = 23 + layoutWriter.BytesWritten + stringsWriter.BytesWritten;
}
var writer = new SpanWriter(stackalloc byte[maxLength]);
var writer = new SpanWriter(maxLength);
writer.Write((byte)(packed ? 0xDD : 0xB0)); // Packet ID
writer.Seek(2, SeekOrigin.Current);
@ -169,7 +168,7 @@ namespace Server.Network
layoutWriter.Write((byte)0); // Layout text terminator
WritePacked(layoutWriter.Span, ref writer);
writer.Write(stringsList.Count);
writer.Write(_stringsList.Count);
WritePacked(stringsWriter.Span, ref writer);
}
else
@ -177,18 +176,26 @@ namespace Server.Network
writer.Write((ushort)layoutWriter.BytesWritten);
writer.Write(layoutWriter.Span);
writer.Write((ushort)stringsList.Count);
writer.Write((ushort)_stringsList.Count);
writer.Write(stringsWriter.Span);
}
writer.WritePacketLength();
ns.Send(writer.Span);
layoutWriter.Dispose(); // Just in case
stringsWriter.Dispose(); // Just in case
if (_stringsList.Count > 0)
{
_stringsList.Clear();
}
}
public static void SendDisplaySignGump(this NetState ns, Serial serial, int gumpId, string unknown, string caption)
{
if (ns == null || !ns.GetSendBuffer(out var buffer))
if (ns == null)
{
return;
}