perf: allocation free syncdict foreach, fix #1172 (#1174)

This commit is contained in:
Paul Pacheco 2019-10-23 08:40:55 -05:00 committed by GitHub
parent 13e4e6fe3a
commit 1ec8910575
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,11 +5,11 @@
namespace Mirror
{
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class SyncDictionary<TKey, TValue> : IDictionary<TKey, TValue>, SyncObject
public abstract class SyncIDictionary<TKey, TValue> : IDictionary<TKey, TValue>, SyncObject
{
public delegate void SyncDictionaryChanged(Operation op, TKey key, TValue item);
readonly IDictionary<TKey, TValue> objects;
protected readonly IDictionary<TKey, TValue> objects;
public int Count => objects.Count;
public bool IsReadOnly { get; private set; }
@ -53,17 +53,7 @@ protected virtual void SerializeItem(NetworkWriter writer, TValue item) { }
// this should be called after a successfull sync
public void Flush() => changes.Clear();
protected SyncDictionary()
{
objects = new Dictionary<TKey, TValue>();
}
protected SyncDictionary(IEqualityComparer<TKey> eq)
{
objects = new Dictionary<TKey, TValue>(eq);
}
protected SyncDictionary(IDictionary<TKey, TValue> objects)
protected SyncIDictionary(IDictionary<TKey, TValue> objects)
{
this.objects = objects;
}
@ -301,8 +291,26 @@ public bool Remove(KeyValuePair<TKey, TValue> item)
return result;
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => ((IDictionary<TKey, TValue>)objects).GetEnumerator();
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => objects.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => objects.GetEnumerator();
}
public abstract class SyncDictionary<TKey, TValue>: SyncIDictionary<TKey, TValue>
{
protected SyncDictionary() : base(new Dictionary<TKey, TValue>())
{
}
protected SyncDictionary(IEqualityComparer<TKey> eq) : base(new Dictionary<TKey,TValue>(eq))
{
}
public new Dictionary<TKey, TValue>.ValueCollection Values => ((Dictionary<TKey,TValue>)objects).Values;
public new Dictionary<TKey, TValue>.KeyCollection Keys => ((Dictionary<TKey,TValue>)objects).Keys;
public new Dictionary<TKey, TValue>.Enumerator GetEnumerator() => ((Dictionary<TKey, TValue>)objects).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IDictionary<TKey, TValue>)objects).GetEnumerator();
}
}