Skip to content

Commit

Permalink
perf: allocation free syncdict foreach, fix #1172 (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach committed Oct 23, 2019
1 parent 13e4e6f commit 1ec8910
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions Assets/Mirror/Runtime/SyncDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -53,17 +53,7 @@ struct Change
// 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;
}
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 1ec8910

Please sign in to comment.