From cb7f9900a120cea609abfcf303b613d0bf9f3c61 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sat, 4 Jan 2025 03:54:47 -0500 Subject: [PATCH] perf(Messages): Use Flags enum for isOwner / IsLocalPlayer (#3958) * fix(Messages): Use Flags enum for isOwner / IsLocalPlayer Reduces SpawnMessage header by one byte Since these are Mirror's internal messages, the Obsoletes may not be necessary, but included for completeness. * fixed test * Revert style change, added comments * style * Update Messages.cs * Update Messages.cs * Update Messages.cs --------- Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com> --- Assets/Mirror/Core/Messages.cs | 51 ++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/Assets/Mirror/Core/Messages.cs b/Assets/Mirror/Core/Messages.cs index 62888c07e62..15cadde8941 100644 --- a/Assets/Mirror/Core/Messages.cs +++ b/Assets/Mirror/Core/Messages.cs @@ -52,13 +52,14 @@ public struct RpcMessage : NetworkMessage public ArraySegment payload; } + [Flags] public enum AuthorityFlags : byte { None, isOwner, isLocalPlayer } + public struct SpawnMessage : NetworkMessage { // netId of new or existing object public uint netId; - public bool isLocalPlayer; - // Sets hasAuthority on the spawned object - public bool isOwner; + // isOwner and isLocalPlayer are merged into one byte via bitwise op + public AuthorityFlags authorityFlags; public ulong sceneId; // If sceneId != 0 then it is used instead of assetId public uint assetId; @@ -71,13 +72,53 @@ public struct SpawnMessage : NetworkMessage // serialized component data // ArraySegment to avoid unnecessary allocations public ArraySegment payload; + + // Backwards compatibility after implementing authorityFlags + public bool isOwner + { + get => authorityFlags.HasFlag(AuthorityFlags.isOwner); + set => authorityFlags = + value + ? authorityFlags | AuthorityFlags.isOwner + : authorityFlags & ~AuthorityFlags.isOwner; + } + + // Backwards compatibility after implementing authorityFlags + public bool isLocalPlayer + { + get => authorityFlags.HasFlag(AuthorityFlags.isLocalPlayer); + set => authorityFlags = + value + ? authorityFlags | AuthorityFlags.isLocalPlayer + : authorityFlags & ~AuthorityFlags.isLocalPlayer; + } } public struct ChangeOwnerMessage : NetworkMessage { public uint netId; - public bool isOwner; - public bool isLocalPlayer; + // isOwner and isLocalPlayer are merged into one byte via bitwise op + public AuthorityFlags authorityFlags; + + // Backwards compatibility after implementing authorityFlags + public bool isOwner + { + get => authorityFlags.HasFlag(AuthorityFlags.isOwner); + set => authorityFlags = + value + ? authorityFlags | AuthorityFlags.isOwner + : authorityFlags & ~AuthorityFlags.isOwner; + } + + // Backwards compatibility after implementing authorityFlags + public bool isLocalPlayer + { + get => authorityFlags.HasFlag(AuthorityFlags.isLocalPlayer); + set => authorityFlags = + value + ? authorityFlags | AuthorityFlags.isLocalPlayer + : authorityFlags & ~AuthorityFlags.isLocalPlayer; + } } public struct ObjectSpawnStartedMessage : NetworkMessage {}