NetworkIdentity.observers returns m_Observers directly without creating a new ReadOnlyCollection every time. This was a performance nightmare because we looped through observers all the time in NetworkServer.SendToObservers/SendToReady via observers[i], observers.Count, etc. Furthermore we don't even worry about ReadOnlyCollections in any other place. The user could clear and break all other collections all the time too.

This commit is contained in:
vis2k 2018-10-16 14:08:14 +02:00
parent a754eaac4f
commit f2d4fac170
4 changed files with 14 additions and 21 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
@ -80,12 +81,12 @@ public override void OnInspectorGUI()
if (m_ShowObservers)
{
EditorGUI.indentLevel += 1;
foreach (NetworkConnection observer in m_NetworkIdentity.observers)
foreach (KeyValuePair<int, NetworkConnection> kvp in m_NetworkIdentity.observers)
{
if (observer.playerController != null)
EditorGUILayout.ObjectField("Connection " + observer.connectionId, observer.playerController.gameObject, typeof(GameObject), false);
if (kvp.Value.playerController != null)
EditorGUILayout.ObjectField("Connection " + kvp.Value.connectionId, kvp.Value.playerController.gameObject, typeof(GameObject), false);
else
EditorGUILayout.TextField("Connection " + observer.connectionId);
EditorGUILayout.TextField("Connection " + kvp.Value.connectionId);
}
EditorGUI.indentLevel -= 1;
}

View File

@ -161,9 +161,9 @@ public override void OnPreviewGUI(Rect r, GUIStyle background)
observerRect.x += 20; // indent names
observerRect.y += observerRect.height;
foreach (var info in m_Identity.observers)
foreach (KeyValuePair<int, NetworkConnection> kvp in m_Identity.observers)
{
GUI.Label(observerRect, info.address + ":" + info.connectionId, m_Styles.componentName);
GUI.Label(observerRect, kvp.Value.address + ":" + kvp.Value.connectionId, m_Styles.componentName);
observerRect.y += observerRect.height;
lastY = observerRect.y;
}

View File

@ -114,13 +114,7 @@ internal void ForceAuthority(bool authority)
public NetworkConnection connectionToServer { get { return m_ConnectionToServer; } }
public NetworkConnection connectionToClient { get { return m_ConnectionToClient; } }
public ReadOnlyCollection<NetworkConnection> observers
{
get
{
return m_Observers != null ? new ReadOnlyCollection<NetworkConnection>(m_Observers.Values.ToList()) : null;
}
}
public Dictionary<int, NetworkConnection> observers { get { return m_Observers; } }
static uint s_NextNetworkId = 1;
internal static NetworkInstanceId GetNextNetworkId()

View File

@ -224,14 +224,13 @@ static bool SendToObservers(GameObject contextObj, short msgType, MessageBase ms
{
if (LogFilter.logDev) { Debug.Log("Server.SendToObservers id:" + msgType); }
var uv = contextObj.GetComponent<NetworkIdentity>();
NetworkIdentity uv = contextObj.GetComponent<NetworkIdentity>();
if (uv != null && uv.observers != null)
{
bool result = true;
for (int i = 0; i < uv.observers.Count; ++i)
foreach (KeyValuePair<int, NetworkConnection> kvp in uv.observers)
{
var conn = uv.observers[i];
result &= conn.Send(msgType, msg);
result &= kvp.Value.Send(msgType, msg);
}
return result;
}
@ -273,12 +272,11 @@ public static bool SendToReady(GameObject contextObj, short msgType, MessageBase
if (uv != null && uv.observers != null)
{
bool result = true;
for (int i = 0; i < uv.observers.Count; ++i)
foreach (KeyValuePair<int, NetworkConnection> kvp in uv.observers)
{
NetworkConnection conn = uv.observers[i];
if (conn.isReady)
if (kvp.Value.isReady)
{
result &= conn.Send(msgType, msg);
result &= kvp.Value.Send(msgType, msg);
}
}
return result;