From 7bcaeb6f3a6b22e2b49ae3197383276fd8c76e4a Mon Sep 17 00:00:00 2001 From: mischa Date: Mon, 11 Mar 2024 18:58:19 +0800 Subject: [PATCH 1/2] perf: NetworkBehaviour flags inlined to avoid indirection cost --- Assets/Mirror/Core/NetworkBehaviour.cs | 14 ++-- Assets/Mirror/Core/NetworkIdentity.cs | 88 ++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 12 deletions(-) diff --git a/Assets/Mirror/Core/NetworkBehaviour.cs b/Assets/Mirror/Core/NetworkBehaviour.cs index cc81b9423e3..f40dee06b2a 100644 --- a/Assets/Mirror/Core/NetworkBehaviour.cs +++ b/Assets/Mirror/Core/NetworkBehaviour.cs @@ -49,23 +49,23 @@ public abstract class NetworkBehaviour : MonoBehaviour /// True if this object is on the server and has been spawned. // 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! /// True if this object is on the client and has been spawned by the server. - public bool isClient => netIdentity.isClient; + public bool isClient; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection! /// True if this object is the the client's own local player. - public bool isLocalPlayer => netIdentity.isLocalPlayer; + public bool isLocalPlayer; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection! /// True if this object is on the server-only, not host. - public bool isServerOnly => netIdentity.isServerOnly; + public bool isServerOnly => isServer && !isClient; /// True if this object is on the client-only, not host. - public bool isClientOnly => netIdentity.isClientOnly; + public bool isClientOnly => isClient && !isServer; /// isOwned is true on the client if this NetworkIdentity is one of the .owned entities of our connection on the server. // 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! /// 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. // on the client: if Client->Server SyncDirection and owned @@ -98,7 +98,7 @@ public bool authority } /// The unique network Id of this object (unique at runtime). - public uint netId => netIdentity.netId; + public uint netId; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection! /// Client's network connection to the server. This is only valid for player objects on the client. // TODO change to NetworkConnectionToServer, but might cause some breaking diff --git a/Assets/Mirror/Core/NetworkIdentity.cs b/Assets/Mirror/Core/NetworkIdentity.cs index aed8251f6f9..6dd94378d5e 100644 --- a/Assets/Mirror/Core/NetworkIdentity.cs +++ b/Assets/Mirror/Core/NetworkIdentity.cs @@ -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; + } + } /// Returns true if NetworkServer.active and server is not stopped. // @@ -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; + } + } /// Return true if this object represents the player on the local machine. // @@ -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; + } + } /// True if this object only exists on the server public bool isServerOnly => isServer && !isClient; @@ -91,7 +133,21 @@ public sealed class NetworkIdentity : MonoBehaviour /// isOwned is true on the client if this NetworkIdentity is one of the .owned entities of our connection on the server. // 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(); /// The unique network Id of this object (unique at runtime). - 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; + } + } /// Unique identifier for NetworkIdentity objects within a scene, used for spawning scene objects. // persistent scene id (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; } } From 041953d6feab0b98fefe4ee0104a7b31a2c9cdae Mon Sep 17 00:00:00 2001 From: mischa Date: Mon, 11 Mar 2024 18:59:51 +0800 Subject: [PATCH 2/2] perf: inline property calls in hot path --- Assets/Mirror/Core/NetworkBehaviour.cs | 2 +- Assets/Mirror/Core/NetworkIdentity.cs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Assets/Mirror/Core/NetworkBehaviour.cs b/Assets/Mirror/Core/NetworkBehaviour.cs index f40dee06b2a..f4068233f14 100644 --- a/Assets/Mirror/Core/NetworkBehaviour.cs +++ b/Assets/Mirror/Core/NetworkBehaviour.cs @@ -118,7 +118,7 @@ public bool authority // component index from in here by searching all NetworkComponents. /// Returns the NetworkIdentity of this object - public NetworkIdentity netIdentity { get; internal set; } + public NetworkIdentity netIdentity; // set from NetworkIdentity.InitializeNetworkBehaviours to avoid netIdentity=>flag indirection! /// Returns the index of the component on this object public byte ComponentIndex { get; internal set; } diff --git a/Assets/Mirror/Core/NetworkIdentity.cs b/Assets/Mirror/Core/NetworkIdentity.cs index 6dd94378d5e..0c80b05f030 100644 --- a/Assets/Mirror/Core/NetworkIdentity.cs +++ b/Assets/Mirror/Core/NetworkIdentity.cs @@ -238,12 +238,21 @@ internal set /// Client's network connection to the server. This is only valid for player objects on the client. // 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; + } /// Server's network connection to the client. This is only valid for client-owned objects (including the Player object) on the server. + NetworkConnectionToClient _connectionToClient; public NetworkConnectionToClient connectionToClient { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _connectionToClient; + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal set { _connectionToClient?.RemoveOwnedObject(this); @@ -251,7 +260,6 @@ internal set _connectionToClient?.AddOwnedObject(this); } } - NetworkConnectionToClient _connectionToClient; // get all NetworkBehaviour components public NetworkBehaviour[] NetworkBehaviours { get; private set; }