Reduced memory allocations in GetClientId

This commit is contained in:
Albin Corén 2018-04-03 19:54:36 +02:00
parent f2c3662f63
commit f6c41db513

View File

@ -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) public NetId(uint clientId)
{ {
byte[] bytes = BitConverter.GetBytes(clientId); tempUIntBytes = BitConverter.GetBytes(clientId);
HostId = bytes[0]; HostId = tempUIntBytes[0];
ConnectionId = BitConverter.ToUInt16(bytes, 1); ConnectionId = BitConverter.ToUInt16(tempUIntBytes, 1);
Meta = bytes[3]; Meta = tempUIntBytes[3];
} }
public uint GetClientId() public uint GetClientId()
{ {
byte[] bytes = new byte[4]; tempUShortBytes = BitConverter.GetBytes(ConnectionId);
byte[] connIdBytes = BitConverter.GetBytes(ConnectionId); tempUIntBytes[0] = HostId;
bytes[0] = HostId; tempUIntBytes[1] = tempUShortBytes[0];
bytes[1] = connIdBytes[0]; tempUIntBytes[2] = tempUShortBytes[1];
bytes[2] = connIdBytes[1]; tempUIntBytes[3] = Meta;
bytes[3] = Meta; return BitConverter.ToUInt32(tempUIntBytes, 0);
return BitConverter.ToUInt32(bytes, 0);
} }
public override bool Equals (object obj) public override bool Equals (object obj)