Skip to content

Commit 5af6ea5

Browse files
committed
Removed RemoveAll LINQ calls to avoid delegate allocation
1 parent 65cebcd commit 5af6ea5

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

MLAPI/GlobalSuppressions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@
2727
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.NetworkingManagerComponents.Core.InternalMessageHandler.Send(System.String,System.String,System.Byte[],System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
2828
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.NetworkingManagerComponents.Core.InternalMessageHandler.Send(System.String,System.String,System.Byte[],System.UInt32,System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
2929
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0018:Inline variable declaration", Justification = "Not supported in Unity Mono version", Scope = "member", Target = "~M:MLAPI.NetworkingManagerComponents.Core.InternalMessageHandler.Send(System.UInt32[],System.String,System.String,System.Byte[],System.Nullable{System.UInt32},System.Nullable{System.UInt16})")]
30+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0202:Value type to reference type conversion allocation for string concatenation", Justification = "Log Message", Scope = "member", Target = "~M:MLAPI.NetworkingManager.Update")]
31+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0601:Value type to reference type conversion causing boxing allocation", Justification = "Log Message", Scope = "member", Target = "~M:MLAPI.NetworkingManager.Update")]
32+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0202:Value type to reference type conversion allocation for string concatenation", Justification = "Log Message", Scope = "member", Target = "~M:MLAPI.NetworkingManager.ApprovalTimeout(System.UInt32)~System.Collections.IEnumerator")]
33+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0202:Value type to reference type conversion allocation for string concatenation", Justification = "Log Message", Scope = "member", Target = "~M:MLAPI.NetworkingManager.HandleIncomingData(System.UInt32,System.Byte[],System.Int32,System.Int32)")]
34+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0302:Display class allocation to capture closure", Justification = "Creates duplication, it's not a commonly called function", Scope = "member", Target = "~M:MLAPI.NetworkingManager.DisconnectClient(System.UInt32)")]
35+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "No function pointers", Scope = "member", Target = "~M:MLAPI.Internal.InternalMessageHandler.HandleConnectionRequest(System.UInt32,System.IO.Stream,System.Int32)")]
36+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0202:Value type to reference type conversion allocation for string concatenation", Justification = "Log Message", Scope = "member", Target = "~M:MLAPI.Internal.InternalMessageHandler.HandleNetworkedVarDelta(System.UInt32,System.IO.Stream,System.Int32)")]
37+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0202:Value type to reference type conversion allocation for string concatenation", Justification = "Log Message", Scope = "member", Target = "~M:MLAPI.Internal.InternalMessageHandler.HandleNetworkedVarUpdate(System.UInt32,System.IO.Stream,System.Int32)")]
38+

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ private void CacheAttributes()
421421
ulong hash = HashMethodName(methods[i].Name);
422422
if (HashResults.ContainsKey(hash) && HashResults[hash] != methods[i].Name)
423423
{
424-
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError("Hash collision detected for RPC method. The method \"" + methods[i].Name + "\" collides with the method \"" + HashResults[hash] + "\". This can be solved by increasing the amount of bytes to use for hashing in the NetworkConfig or changing the name of one of the conflicting methods.");
424+
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError($"Hash collision detected for RPC method. The method \"{methods[i].Name}\" collides with the method \"{HashResults[hash]}\". This can be solved by increasing the amount of bytes to use for hashing in the NetworkConfig or changing the name of one of the conflicting methods.");
425425
}
426426
else if (!HashResults.ContainsKey(hash))
427427
{
@@ -453,7 +453,7 @@ private void CacheAttributes()
453453
ulong hash = HashMethodName(methods[i].Name);
454454
if (HashResults.ContainsKey(hash) && HashResults[hash] != methods[i].Name)
455455
{
456-
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError("Hash collision detected for RPC method. The method \"" + methods[i].Name + "\" collides with the method \"" + HashResults[hash] + "\". This can be solved by increasing the amount of bytes to use for hashing in the NetworkConfig or changing the name of one of the conflicting methods.");
456+
if (LogHelper.CurrentLogLevel <= LogLevel.Error) LogHelper.LogError($"Hash collision detected for RPC method. The method \"{methods[i].Name}\" collides with the method \"{HashResults[hash]}\". This can be solved by increasing the amount of bytes to use for hashing in the NetworkConfig or changing the name of one of the conflicting methods.");
457457
}
458458
else if (!HashResults.ContainsKey(hash))
459459
{
@@ -774,7 +774,9 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
774774
#endregion
775775

776776
#region SEND METHODS
777+
#pragma warning disable HAA0101 // Array allocation for params parameter
777778
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
779+
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
778780
/// <exclude />
779781
public delegate void Action();
780782
/// <exclude />
@@ -2775,6 +2777,9 @@ public void InvokeServerRpc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T
27752777
SendServerRPCBoxed(HashMethodName(methodName), t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32);
27762778
}
27772779

2780+
#pragma warning restore HAA0101 // Array allocation for params parameter
2781+
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
2782+
27782783
//PERFORMANCE SERVER RPC
27792784
public void InvokeServerRpc(RpcDelegate method, Stream stream)
27802785
{

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using MLAPI.Transports;
1919
using MLAPI.Transports.UNET;
2020
using BitStream = MLAPI.Serialization.BitStream;
21+
using System.Runtime.CompilerServices;
2122

2223
namespace MLAPI
2324
{
@@ -691,7 +692,7 @@ private void Update()
691692
NetworkProfiler.EndEvent();
692693
break;
693694
case NetEventType.Data:
694-
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("Incomming Data From " + clientId + " : " + receivedSize + " bytes");
695+
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo($"Incomming Data From {clientId} : {receivedSize} bytes");
695696

696697
HandleIncomingData(clientId, messageBuffer, channelId, receivedSize);
697698
break;
@@ -846,6 +847,9 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
846847
}
847848
}
848849

850+
#if NET45
851+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
852+
#endif
849853
internal void DisconnectClient(uint clientId)
850854
{
851855
if (!isServer)
@@ -856,8 +860,12 @@ internal void DisconnectClient(uint clientId)
856860

857861
if (ConnectedClients.ContainsKey(clientId))
858862
ConnectedClients.Remove(clientId);
859-
860-
ConnectedClientsList.RemoveAll(x => x.ClientId == clientId); // :(
863+
864+
for (int i = ConnectedClientsList.Count - 1; i > -1; i--)
865+
{
866+
if (ConnectedClientsList[i].ClientId == clientId)
867+
ConnectedClientsList.RemoveAt(i);
868+
}
861869

862870
#if !DISABLE_CRYPTOGRAPHY
863871
if (diffieHellmanPublicKeys.ContainsKey(clientId))
@@ -883,7 +891,15 @@ internal void OnClientDisconnectFromServer(uint clientId)
883891
Destroy(ConnectedClients[clientId].OwnedObjects[i].gameObject);
884892
}
885893
}
886-
ConnectedClientsList.RemoveAll(x => x.ClientId == clientId);
894+
895+
for (int i = 0; i < ConnectedClientsList.Count; i++)
896+
{
897+
if (ConnectedClientsList[i].ClientId == clientId)
898+
{
899+
ConnectedClientsList.RemoveAt(i);
900+
break;
901+
}
902+
}
887903
ConnectedClients.Remove(clientId);
888904
}
889905

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ internal static void RemoveOwnership(uint netId)
6767
return;
6868
}
6969
NetworkedObject netObject = SpawnManager.SpawnedObjects[netId];
70-
NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
70+
for (int i = NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.Count - 1; i > -1; i--)
71+
{
72+
if (NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects[i].NetworkId == netId)
73+
NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAt(i);
74+
}
7175
netObject.OwnerClientId = NetworkingManager.singleton.ServerClientId;
7276

7377
using (PooledBitStream stream = PooledBitStream.Get())
@@ -88,7 +92,11 @@ internal static void ChangeOwnership(uint netId, uint clientId)
8892
return;
8993
}
9094
NetworkedObject netObject = SpawnManager.SpawnedObjects[netId];
91-
NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == netId);
95+
for (int i = NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.Count - 1; i > -1; i--)
96+
{
97+
if (NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects[i].NetworkId == netId)
98+
NetworkingManager.singleton.ConnectedClients[netObject.OwnerClientId].OwnedObjects.RemoveAt(i);
99+
}
92100
NetworkingManager.singleton.ConnectedClients[clientId].OwnedObjects.Add(netObject);
93101
netObject.OwnerClientId = clientId;
94102

@@ -342,7 +350,11 @@ internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
342350
netManager.ConnectedClients.ContainsKey(SpawnedObjects[networkId].OwnerClientId))
343351
{
344352
//Someone owns it.
345-
netManager.ConnectedClients[SpawnedObjects[networkId].OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == networkId);
353+
for (int i = NetworkingManager.singleton.ConnectedClients[SpawnedObjects[networkId].OwnerClientId].OwnedObjects.Count - 1; i > -1; i--)
354+
{
355+
if (NetworkingManager.singleton.ConnectedClients[SpawnedObjects[networkId].OwnerClientId].OwnedObjects[i].NetworkId == networkId)
356+
NetworkingManager.singleton.ConnectedClients[SpawnedObjects[networkId].OwnerClientId].OwnedObjects.RemoveAt(i);
357+
}
346358
}
347359
SpawnedObjects[networkId].isSpawned = false;
348360

@@ -365,7 +377,11 @@ internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
365377
if (destroyGameObject && go != null)
366378
MonoBehaviour.Destroy(go);
367379
SpawnedObjects.Remove(networkId);
368-
SpawnedObjectsList.RemoveAll(x => x.NetworkId == networkId);
380+
for (int i = SpawnedObjectsList.Count - 1; i > -1; i--)
381+
{
382+
if (SpawnedObjectsList[i].NetworkId == networkId)
383+
SpawnedObjectsList.RemoveAt(i);
384+
}
369385
}
370386
}
371387
}

0 commit comments

Comments
 (0)