perf: NetworkBehaviour flags inlined to avoid indirection cost

This commit is contained in:
mischa 2024-03-11 18:58:19 +08:00
parent 0aa8000454
commit 7bcaeb6f3a
2 changed files with 90 additions and 12 deletions

View File

@ -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
@ -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

View File

@ -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>
//
@ -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>
//
@ -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;
@ -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;
@ -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)
@ -313,6 +383,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;
}
}