Skip to content

Commit

Permalink
some benchmarks and results
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Sep 15, 2023
1 parent b9e18a9 commit 71772cc
Show file tree
Hide file tree
Showing 23 changed files with 58,683 additions and 229 deletions.
4 changes: 3 additions & 1 deletion Assets/Mirage/Mirage.asmdef
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "Mirage",
"rootNamespace": "",
"references": [
"UniTask",
"UniTask.Linq",
"Mirage.SocketLayer"
"Mirage.SocketLayer",
"JamesFrowen.Benchmarker"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
23 changes: 18 additions & 5 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using JamesFrowen.Benchmarker;
using Mirage.Collections;
using Mirage.Logging;
using Mirage.RemoteCalls;
Expand Down Expand Up @@ -275,6 +276,9 @@ private void SyncObject_OnChange()
}
}

private bool _shouldSync;
internal HashSet<NetworkIdentity> _dirtySet;

/// <summary>
/// Call this after updating SyncSettings to update all SyncObjects
/// <para>
Expand All @@ -284,12 +288,12 @@ private void SyncObject_OnChange()
/// </summary>
public void UpdateSyncObjectShouldSync()
{
var shouldSync = SyncSettings.ShouldSyncFrom(Identity);
_shouldSync = SyncSettings.ShouldSyncFrom(Identity);

if (logger.LogEnabled()) logger.Log($"Settings SyncObject sync on to {shouldSync} for {this}");
if (logger.LogEnabled()) logger.Log($"Settings SyncObject sync on to {_shouldSync} for {this}");
for (var i = 0; i < syncObjects.Count; i++)
{
syncObjects[i].SetShouldSyncFrom(shouldSync);
syncObjects[i].SetShouldSyncFrom(_shouldSync);
}
}

Expand All @@ -304,14 +308,22 @@ protected internal bool SyncVarEqual<T>(T value, T fieldValue)
/// these are masks, not bit numbers, ie. 0x004 not 2
/// </summary>
/// <param name="bitMask">Bit mask to set.</param>
// [BenchmarkMethod]
public void SetDirtyBit(ulong bitMask)
{
if (logger.LogEnabled()) logger.Log($"Dirty bit set {bitMask} on {Identity}");

var wasZero = _syncVarDirtyBits == 0;

_syncVarDirtyBits |= bitMask;

if (SyncSettings.ShouldSyncFrom(Identity))
Identity.SyncVarSender.AddDirtyObject(Identity);
if (
wasZero &&
_shouldSync
)
{
_dirtySet.Add(Identity);
}
}


Expand Down Expand Up @@ -390,6 +402,7 @@ public bool AnyDirtyBits()
/// <param name="writer">Writer to use to write to the stream.</param>
/// <param name="initialState">If this is being called to send initial state.</param>
/// <returns>True if data was written.</returns>
[BenchmarkMethod("Behaviour.OnSerialize")]
public virtual bool OnSerialize(NetworkWriter writer, bool initialState)
{
var objectWritten = SerializeObjects(writer, initialState);
Expand Down
15 changes: 14 additions & 1 deletion Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using JamesFrowen.Benchmarker;
using Mirage.Events;
using Mirage.Logging;
using Mirage.RemoteCalls;
Expand Down Expand Up @@ -118,7 +119,7 @@ public sealed class NetworkIdentity : MonoBehaviour
/// <summary>
/// Returns true if NetworkServer.active and server is not stopped.
/// </summary>
public bool IsServer => IsSpawned && Server != null && Server.Active;
public bool IsServer { get; private set; }

/// <summary>
/// Returns true if we're on host mode.
Expand Down Expand Up @@ -648,6 +649,7 @@ internal void StopClient()
/// </description></item>
/// </list>
/// </remarks>
[BenchmarkMethod("Behaviour.OnSerialize wrapper")]
private void OnSerialize(int i, NetworkBehaviour comp, NetworkWriter writer, bool initialState)
{
// write index as byte [0..255]
Expand All @@ -668,6 +670,7 @@ private void OnSerialize(int i, NetworkBehaviour comp, NetworkWriter writer, boo
/// <param name="initialState"></param>
/// <param name="ownerWriter"></param>
/// <param name="observersWriter"></param>
[BenchmarkMethod("Identity.OnSerializeAll")]
internal (int ownerWritten, int observersWritten) OnSerializeAll(bool initialState, NetworkWriter ownerWriter, NetworkWriter observersWriter)
{
// how many times it written to (NOT BYTES)
Expand Down Expand Up @@ -880,13 +883,18 @@ private void ForwardToObservers(NetworkWriter writer)
internal void SetServerValues(NetworkServer networkServer, ServerObjectManager serverObjectManager)
{
Server = networkServer;
IsServer = true;
ServerObjectManager = serverObjectManager;
World = networkServer.World;
SyncVarSender = networkServer.SyncVarSender;
Client = networkServer.LocalClient;

foreach (var behaviour in NetworkBehaviours)
{
behaviour.InitializeSyncObjects();
behaviour._dirtySet = SyncVarSender?._dirtyObjects;
behaviour.UpdateSyncObjectShouldSync();
}
}

internal void SetClientValues(ClientObjectManager clientObjectManager, SpawnMessage msg)
Expand All @@ -910,7 +918,11 @@ internal void SetClientValues(ClientObjectManager clientObjectManager, SpawnMess
}

foreach (var behaviour in NetworkBehaviours)
{
behaviour.InitializeSyncObjects();
behaviour._dirtySet = SyncVarSender?._dirtyObjects;
behaviour.UpdateSyncObjectShouldSync();
}
}

/// <summary>
Expand Down Expand Up @@ -1112,6 +1124,7 @@ internal void NetworkReset()
_localPlayerStarted = false;
NetId = 0;
Server = null;
IsServer = false;
Client = null;
ServerObjectManager = null;
ClientObjectManager = null;
Expand Down
7 changes: 7 additions & 0 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Cysharp.Threading.Tasks;
using JamesFrowen.Benchmarker;
using Mirage.Authentication;
using Mirage.Events;
using Mirage.Logging;
Expand Down Expand Up @@ -325,6 +326,12 @@ internal void Update()
public void UpdateSent()
{
SyncVarSender?.Update();
PeerUpdate();
}

[BenchmarkMethod("UpdateTransport")]
private void PeerUpdate()
{
_peer?.UpdateSent();
}

Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirage/Runtime/Sockets/Udp/Mirage.Sockets.Udp.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "Mirage.Sockets.Udp",
"rootNamespace": "",
"references": [
"GUID:c0b2064c294eb174c9f3f7da398eb677"
"GUID:c0b2064c294eb174c9f3f7da398eb677",
"GUID:2353400444788674ab8945effbb8b282"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
3 changes: 3 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoSocket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// windows, linux or standalone c#, unless EXCLUDE_NANOSOCKETS is defined
#if !EXCLUDE_NANOSOCKETS && (UNITY_EDITOR_WIN || UNITY_EDITOR_LINUX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || NETCOREAPP || NET_5_0_OR_GREATER)
using System;
using JamesFrowen.Benchmarker;
using Mirage.SocketLayer;
using NanoSockets;

Expand Down Expand Up @@ -38,6 +39,7 @@ public void Bind(IEndPoint endPoint)
receiveEndPoint = (NanoEndPoint)endPoint;

CreateSocket();

var result = UDP.Bind(socket, ref receiveEndPoint.address);
if (result != 0)
{
Expand Down Expand Up @@ -82,6 +84,7 @@ public int Receive(byte[] buffer, out IEndPoint endPoint)
return count;
}

[BenchmarkMethod]
public void Send(IEndPoint endPoint, byte[] packet, int length)
{
var nanoEndPoint = (NanoEndPoint)endPoint;
Expand Down
2 changes: 2 additions & 0 deletions Assets/Mirage/Runtime/Sockets/Udp/UdpSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public void Bind(IEndPoint endPoint)
Endpoint = (EndPointWrapper)endPoint;

socket = CreateSocket(Endpoint.inner);
// todo add option to disable DualMode
// this is needing to run on platforms like nintendo switch
socket.DualMode = true;
socket.Bind(Endpoint.inner);
}
Expand Down
17 changes: 17 additions & 0 deletions Assets/Mirage/Runtime/SyncSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static void UpdateTime(float interval, SyncTiming timing, ref float nextS
}
}

// public bool ShouldSyncFrom(NetworkIdentity identity, bool initialState)
// {
// // when initial, we want to sync even if "from.Server" is false
// if ((initialState && AlwaysSendInitialToOwner) || ((From & SyncFrom.Server) != 0 && identity.IsServer))
// {
public bool ShouldSyncFrom(NetworkIdentity identity)
{
if ((From & SyncFrom.Server) != 0 && identity.IsServer)
Expand Down Expand Up @@ -83,6 +88,8 @@ public bool ShouldSyncFrom(NetworkIdentity identity)
return false;
}

// public bool ToObserverWriterOnly(NetworkIdentity identity, bool initialState)
// {
public bool ToObserverWriterOnly(NetworkIdentity identity)
{
// if not to observer, then we can return early (saves performance on IsServer check)
Expand All @@ -94,6 +101,8 @@ public bool ToObserverWriterOnly(NetworkIdentity identity)
return false;

// if not to.Owner, then use ObserverWriter
// OR if initial
// if ((To & (SyncTo.Owner)) == 0 || (initialState && AlwaysSendInitialToOwner))
if ((To & (SyncTo.Owner)) == 0)
return true;

Expand Down Expand Up @@ -246,6 +255,14 @@ public enum SyncTo : byte
Owner = 1,
ObserversOnly = 2,
Server = 4,
/// <summary>
/// Should Server send initial state to owner on spawn
/// </summary>
OwnerSpawn = 8,
/// <summary>
/// Should Server send initial state to Observers on spawn
/// </summary>
ObserversSpawn = 16,

OwnerAndObservers = Owner | ObserversOnly,
}
Expand Down
Loading

0 comments on commit 71772cc

Please sign in to comment.