From f6c41db513dbdb14781bacb0b26db72e295f001c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Tue, 3 Apr 2018 19:54:36 +0200 Subject: [PATCH] Reduced memory allocations in GetClientId --- MLAPI/Data/NetId.cs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/MLAPI/Data/NetId.cs b/MLAPI/Data/NetId.cs index 0b97fed..f2e5502 100644 --- a/MLAPI/Data/NetId.cs +++ b/MLAPI/Data/NetId.cs @@ -40,23 +40,24 @@ namespace MLAPI.Data } + private static byte[] tempUIntBytes = new byte[4]; + private static byte[] tempUShortBytes = new byte[2]; public NetId(uint clientId) { - byte[] bytes = BitConverter.GetBytes(clientId); - HostId = bytes[0]; - ConnectionId = BitConverter.ToUInt16(bytes, 1); - Meta = bytes[3]; + tempUIntBytes = BitConverter.GetBytes(clientId); + HostId = tempUIntBytes[0]; + ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1); + Meta = tempUIntBytes[3]; } public uint GetClientId() { - byte[] bytes = new byte[4]; - byte[] connIdBytes = BitConverter.GetBytes(ConnectionId); - bytes[0] = HostId; - bytes[1] = connIdBytes[0]; - bytes[2] = connIdBytes[1]; - bytes[3] = Meta; - return BitConverter.ToUInt32(bytes, 0); + tempUShortBytes = BitConverter.GetBytes(ConnectionId); + tempUIntBytes[0] = HostId; + tempUIntBytes[1] = tempUShortBytes[0]; + tempUIntBytes[2] = tempUShortBytes[1]; + tempUIntBytes[3] = Meta; + return BitConverter.ToUInt32(tempUIntBytes, 0); } public override bool Equals (object obj)