mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
perf: avoid allocation with message structs (#939)
* avoid allocation with message structs * Use ternary operator instead of if * Explain witchcraft
This commit is contained in:
parent
b4077c1112
commit
7c7c910a5e
@ -8,7 +8,11 @@ public struct NetworkMessage
|
|||||||
|
|
||||||
public TMsg ReadMessage<TMsg>() where TMsg : IMessageBase, new()
|
public TMsg ReadMessage<TMsg>() where TMsg : IMessageBase, new()
|
||||||
{
|
{
|
||||||
TMsg msg = new TMsg();
|
// Normally I would just do:
|
||||||
|
// TMsg msg = new TMsg();
|
||||||
|
// but mono calls an expensive method Activator.CreateInstance
|
||||||
|
// For value types this is unnecesary, just use the default value
|
||||||
|
TMsg msg = typeof(TMsg).IsValueType ? default(TMsg) : new TMsg();
|
||||||
msg.Deserialize(reader);
|
msg.Deserialize(reader);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user