Skip to content

Commit

Permalink
perf: cache component index in network behavior (#550)
Browse files Browse the repository at this point in the history
small performance gain in rpcs for objects with lots of network behaviors (such as cubica :) )

Thanks to Punfish who found it.
  • Loading branch information
paulpach committed Jan 26, 2021
1 parent 1b606c6 commit e566545
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Assets/Mirror/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,26 @@ public NetworkIdentity NetIdentity
}
}

private int componentIndex = -1;
/// <summary>
/// Returns the index of the component on this object
/// </summary>
public int ComponentIndex
{
get
{
if (componentIndex >= 0)
return componentIndex;

// note: FindIndex causes allocations, we search manually instead
for (int i = 0; i < NetIdentity.NetworkBehaviours.Length; i++)
{
NetworkBehaviour component = NetIdentity.NetworkBehaviours[i];
if (component == this)
{
componentIndex = i;
return i;
}
}

// this should never happen
Expand Down

0 comments on commit e566545

Please sign in to comment.