Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: remove NetworkBehaviour flags indirection overhead #3778

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Assets/Mirror/Core/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ public abstract class NetworkBehaviour : MonoBehaviour
/// <summary>True if this object is on the server and has been spawned.</summary>
// This is different from NetworkServer.active, which is true if the
// server itself is active rather than this object being active.
public bool isServer => netIdentity.isServer;
public bool isServer; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection!

/// <summary>True if this object is on the client and has been spawned by the server.</summary>
public bool isClient => netIdentity.isClient;
public bool isClient; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection!

/// <summary>True if this object is the the client's own local player.</summary>
public bool isLocalPlayer => netIdentity.isLocalPlayer;
public bool isLocalPlayer; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection!

/// <summary>True if this object is on the server-only, not host.</summary>
public bool isServerOnly => netIdentity.isServerOnly;
public bool isServerOnly => isServer && !isClient;

/// <summary>True if this object is on the client-only, not host.</summary>
public bool isClientOnly => netIdentity.isClientOnly;
public bool isClientOnly => isClient && !isServer;

/// <summary>isOwned is true on the client if this NetworkIdentity is one of the .owned entities of our connection on the server.</summary>
// for example: main player & pets are owned. monsters & npcs aren't.
public bool isOwned => netIdentity.isOwned;
public bool isOwned; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection!

/// <summary>authority is true if we are allowed to modify this component's state. On server, it's true if SyncDirection is ServerToClient. On client, it's true if SyncDirection is ClientToServer and(!) if this object is owned by the client.</summary>
// on the client: if Client->Server SyncDirection and owned
Expand Down Expand Up @@ -98,7 +98,7 @@ public bool authority
}

/// <summary>The unique network Id of this object (unique at runtime).</summary>
public uint netId => netIdentity.netId;
public uint netId; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection!

/// <summary>Client's network connection to the server. This is only valid for player objects on the client.</summary>
// TODO change to NetworkConnectionToServer, but might cause some breaking
Expand All @@ -118,7 +118,7 @@ public bool authority
// component index from in here by searching all NetworkComponents.

/// <summary>Returns the NetworkIdentity of this object</summary>
public NetworkIdentity netIdentity { get; internal set; }
public NetworkIdentity netIdentity; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection!

/// <summary>Returns the index of the component on this object</summary>
public byte ComponentIndex { get; internal set; }
Expand Down
100 changes: 93 additions & 7 deletions Assets/Mirror/Core/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,21 @@ public sealed class NetworkIdentity : MonoBehaviour
// we set it, then never change it. that's the user's expectation too.
//
// => fixes https://github.com/vis2k/Mirror/issues/1475
public bool isClient { get; internal set; }
bool _isClient;
public bool isClient
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _isClient;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set
{
// avoid NetworkBehaviour netIdentity=>flag indirection by
// manually assigning an inlined flag when changed
_isClient = value;
foreach (NetworkBehaviour comp in NetworkBehaviours)
comp.isClient = value;
}
}

/// <summary>Returns true if NetworkServer.active and server is not stopped.</summary>
//
Expand All @@ -67,7 +81,21 @@ public sealed class NetworkIdentity : MonoBehaviour
//
// => fixes https://github.com/vis2k/Mirror/issues/1484
// => fixes https://github.com/vis2k/Mirror/issues/2533
public bool isServer { get; internal set; }
bool _isServer;
public bool isServer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _isServer;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set
{
// avoid NetworkBehaviour netIdentity=>flag indirection by
// manually assigning an inlined flag when changed
_isServer = value;
foreach (NetworkBehaviour comp in NetworkBehaviours)
comp.isServer = value;
}
}

/// <summary>Return true if this object represents the player on the local machine.</summary>
//
Expand All @@ -81,7 +109,21 @@ public sealed class NetworkIdentity : MonoBehaviour
// we set it, then never change it. that's the user's expectation too.
//
// => fixes https://github.com/vis2k/Mirror/issues/2615
public bool isLocalPlayer { get; internal set; }
bool _isLocalPlayer;
public bool isLocalPlayer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _isLocalPlayer;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set
{
// avoid NetworkBehaviour netIdentity=>flag indirection by
// manually assigning an inlined flag when changed
_isLocalPlayer = value;
foreach (NetworkBehaviour comp in NetworkBehaviours)
comp.isLocalPlayer = value;
}
}

/// <summary>True if this object only exists on the server</summary>
public bool isServerOnly => isServer && !isClient;
Expand All @@ -91,7 +133,21 @@ public sealed class NetworkIdentity : MonoBehaviour

/// <summary>isOwned is true on the client if this NetworkIdentity is one of the .owned entities of our connection on the server.</summary>
// for example: main player & pets are owned. monsters & npcs aren't.
public bool isOwned { get; internal set; }
bool _isOwned;
public bool isOwned
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _isOwned;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set
{
// avoid NetworkBehaviour netIdentity=>flag indirection by
// manually assigning an inlined flag when changed
_isOwned = value;
foreach (NetworkBehaviour comp in NetworkBehaviours)
comp.isOwned = value;
}
}

// internal so NetworkManager can reset it from StopClient.
internal bool clientStarted;
Expand All @@ -101,7 +157,21 @@ public sealed class NetworkIdentity : MonoBehaviour
new Dictionary<int, NetworkConnectionToClient>();

/// <summary>The unique network Id of this object (unique at runtime).</summary>
public uint netId { get; internal set; }
uint _netId;
public uint netId
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _netId;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set
{
// avoid NetworkBehaviour netIdentity=>flag indirection by
// manually assigning an inlined flag when changed
_netId = value;
foreach (NetworkBehaviour comp in NetworkBehaviours)
comp.netId = value;
}
}

/// <summary>Unique identifier for NetworkIdentity objects within a scene, used for spawning scene objects.</summary>
// persistent scene id <sceneHash/32,sceneId/32> (see AssignSceneID comments)
Expand Down Expand Up @@ -168,20 +238,28 @@ internal set

/// <summary>Client's network connection to the server. This is only valid for player objects on the client.</summary>
// TODO change to NetworkConnectionToServer, but might cause some breaking
public NetworkConnection connectionToServer { get; internal set; }
public NetworkConnection connectionToServer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set;
}

/// <summary>Server's network connection to the client. This is only valid for client-owned objects (including the Player object) on the server.</summary>
NetworkConnectionToClient _connectionToClient;
public NetworkConnectionToClient connectionToClient
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _connectionToClient;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal set
{
_connectionToClient?.RemoveOwnedObject(this);
_connectionToClient = value;
_connectionToClient?.AddOwnedObject(this);
}
}
NetworkConnectionToClient _connectionToClient;

// get all NetworkBehaviour components
public NetworkBehaviour[] NetworkBehaviours { get; private set; }
Expand Down Expand Up @@ -313,6 +391,14 @@ internal void InitializeNetworkBehaviours()
NetworkBehaviour component = NetworkBehaviours[i];
component.netIdentity = this;
component.ComponentIndex = (byte)i;

// set flags immediately instead of having NetworkBehaviours
// go through the netIdentity=>flag indirection. much faster!
component.isServer = isServer;
component.isClient = isClient;
component.isLocalPlayer = isLocalPlayer;
component.isOwned = isOwned;
component.netId = netId;
}
}

Expand Down