Skip to content

Commit f6c41db

Browse files
committed
Reduced memory allocations in GetClientId
1 parent f2c3662 commit f6c41db

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

MLAPI/Data/NetId.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,24 @@ public NetId(byte hostId, ushort connectionId, bool isHost, bool isInvalid)
4040
}
4141

4242

43+
private static byte[] tempUIntBytes = new byte[4];
44+
private static byte[] tempUShortBytes = new byte[2];
4345
public NetId(uint clientId)
4446
{
45-
byte[] bytes = BitConverter.GetBytes(clientId);
46-
HostId = bytes[0];
47-
ConnectionId = BitConverter.ToUInt16(bytes, 1);
48-
Meta = bytes[3];
47+
tempUIntBytes = BitConverter.GetBytes(clientId);
48+
HostId = tempUIntBytes[0];
49+
ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1);
50+
Meta = tempUIntBytes[3];
4951
}
5052

5153
public uint GetClientId()
5254
{
53-
byte[] bytes = new byte[4];
54-
byte[] connIdBytes = BitConverter.GetBytes(ConnectionId);
55-
bytes[0] = HostId;
56-
bytes[1] = connIdBytes[0];
57-
bytes[2] = connIdBytes[1];
58-
bytes[3] = Meta;
59-
return BitConverter.ToUInt32(bytes, 0);
55+
tempUShortBytes = BitConverter.GetBytes(ConnectionId);
56+
tempUIntBytes[0] = HostId;
57+
tempUIntBytes[1] = tempUShortBytes[0];
58+
tempUIntBytes[2] = tempUShortBytes[1];
59+
tempUIntBytes[3] = Meta;
60+
return BitConverter.ToUInt32(tempUIntBytes, 0);
6061
}
6162

6263
public override bool Equals (object obj)

0 commit comments

Comments
 (0)