Skip to content

Commit 8e31dbe

Browse files
committed
Removed allocations from NetId
1 parent 503964c commit 8e31dbe

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

MLAPI/Data/NetId.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using MLAPI.MonoBehaviours.Core;
2-
using System;
32

43
namespace MLAPI.Data
54
{
@@ -66,33 +65,23 @@ public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
6665
else
6766
Meta = 0;
6867
}
69-
70-
71-
private static byte[] tempUIntBytes = new byte[4];
72-
private static byte[] tempUShortBytes = new byte[2];
7368
/// <summary>
7469
/// Initializes a new instance of the netId struct from a clientId
7570
/// </summary>
7671
/// <param name="clientId">Client identifier.</param>
7772
public NetId(uint clientId)
7873
{
79-
tempUIntBytes = BitConverter.GetBytes(clientId);
80-
HostId = tempUIntBytes[0];
81-
ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1);
82-
Meta = tempUIntBytes[3];
74+
HostId = (byte)(clientId & 0xFF);
75+
ConnectionId = (ushort)((byte)((clientId >> 8) & 0xFF) | (ushort)(((clientId >> 16) & 0xFF) << 8));
76+
Meta = (byte)((clientId >> 24) & 0xFF);
8377
}
8478
/// <summary>
8579
/// Gets the clientId.
8680
/// </summary>
8781
/// <returns>The client identifier.</returns>
8882
public uint GetClientId()
8983
{
90-
tempUShortBytes = BitConverter.GetBytes(ConnectionId);
91-
tempUIntBytes[0] = HostId;
92-
tempUIntBytes[1] = tempUShortBytes[0];
93-
tempUIntBytes[2] = tempUShortBytes[1];
94-
tempUIntBytes[3] = Meta;
95-
return BitConverter.ToUInt32(tempUIntBytes, 0);
84+
return (uint)HostId | (ushort)((uint)(byte)(ConnectionId & 0xFF) << 8) | (ushort)((uint)(byte)((ConnectionId >> 8) & 0xFF) << 16) | (ushort)((uint)Meta << 24);
9685
}
9786
// Rider generated vvv
9887
/// <summary>

0 commit comments

Comments
 (0)