Skip to content

Commit

Permalink
perf: avoid allocation with message structs (#939)
Browse files Browse the repository at this point in the history
* avoid allocation with message structs

* Use ternary operator instead of if

* Explain witchcraft
  • Loading branch information
paulpach authored and miwarnec committed Jul 29, 2019
1 parent b4077c1 commit 7c7c910
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Assets/Mirror/Runtime/NetworkMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public struct NetworkMessage

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);
return msg;
}
Expand Down

0 comments on commit 7c7c910

Please sign in to comment.