Skip to content

Commit

Permalink
make all RemoteCalls static. Optimized memory use and some performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed Jan 6, 2024
1 parent 0c24e29 commit bc53270
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 193 deletions.
4 changes: 2 additions & 2 deletions LiteEntitySystem/EntityLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ internal static void SetOwner(EntityLogic entity, byte ownerId)
}
}

protected override void RegisterRPC(in RPCRegistrator r)
protected override void RegisterRPC(ref RPCRegistrator r)
{
base.RegisterRPC(in r);
base.RegisterRPC(ref r);
r.BindOnChange(this, ref _parentId, OnParentChange);
r.BindOnChange(this, ref InternalOwnerId, OnOwnerChange);
}
Expand Down
2 changes: 1 addition & 1 deletion LiteEntitySystem/EntityParams.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LiteEntitySystem
{
public readonly struct EntityParams
public readonly ref struct EntityParams
{
public readonly ushort ClassId;
public readonly ushort Id;
Expand Down
4 changes: 2 additions & 2 deletions LiteEntitySystem/Extensions/JsonSyncableField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public T Value
}
}

private RemoteCallSpan<byte> _initAction;
private static RemoteCallSpan<byte> _initAction;

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, Init, ref _initAction);
}
Expand Down
10 changes: 5 additions & 5 deletions LiteEntitySystem/Extensions/SyncArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ private struct SetCallData
}

private T[] _data;
private RemoteCall<SetCallData> _setRpcAction;
private RemoteCall<int> _resizeRpcAction;
private RemoteCallSpan<T> _initArrayAction;
private RemoteCall _clearAction;
private static RemoteCall<SetCallData> _setRpcAction;
private static RemoteCall<int> _resizeRpcAction;
private static RemoteCallSpan<T> _initArrayAction;
private static RemoteCall _clearAction;

public int Length => _data.Length;

Expand Down Expand Up @@ -47,7 +47,7 @@ protected internal override void OnSyncRequested()
ExecuteRPC(_initArrayAction, _data);
}

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, SetValueRPC, ref _setRpcAction);
r.CreateClientAction(this, InitArrayRPC, ref _initArrayAction);
Expand Down
10 changes: 5 additions & 5 deletions LiteEntitySystem/Extensions/SyncDict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public KeyValue(TKey key, TValue value)

private readonly Dictionary<TKey, TValue> _data = new ();

private RemoteCall<KeyValue> _addAction;
private RemoteCall _clearAction;
private RemoteCall<TKey> _removeAction;
private RemoteCallSpan<KeyValue> _initAction;
private static RemoteCall<KeyValue> _addAction;
private static RemoteCall _clearAction;
private static RemoteCall<TKey> _removeAction;
private static RemoteCallSpan<KeyValue> _initAction;

public Dictionary<TKey, TValue>.KeyCollection Keys => _data.Keys;
public Dictionary<TKey, TValue>.ValueCollection Values => _data.Values;
private static KeyValue[] KvCache = new KeyValue[8];

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, AddAction, ref _addAction);
r.CreateClientAction(this, Clear, ref _clearAction);
Expand Down
6 changes: 3 additions & 3 deletions LiteEntitySystem/Extensions/SyncFixedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ private struct SetCallData
}

private readonly T[] _data;
private RemoteCall<SetCallData> _setRpcAction;
private RemoteCallSpan<T> _initArrayAction;
private static RemoteCall<SetCallData> _setRpcAction;
private static RemoteCallSpan<T> _initArrayAction;

public readonly int Length;

Expand All @@ -27,7 +27,7 @@ protected internal override void OnSyncRequested()
ExecuteRPC(_initArrayAction, _data);
}

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, SetValueRPC, ref _setRpcAction);
r.CreateClientAction(this, InitArrayRPC, ref _initArrayAction);
Expand Down
10 changes: 5 additions & 5 deletions LiteEntitySystem/Extensions/SyncList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class SyncList<T> : SyncableField, ICollection<T>, IReadOnlyList<T> where
private T[] _data;
private int _count;

private RemoteCall<T> _addAction;
private RemoteCall _clearAction;
private RemoteCall<int> _removeAtAction;
private RemoteCallSpan<T> _initAction;
private static RemoteCall<T> _addAction;
private static RemoteCall _clearAction;
private static RemoteCall<int> _removeAtAction;
private static RemoteCallSpan<T> _initAction;

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, Add, ref _addAction);
r.CreateClientAction(this, Clear, ref _clearAction);
Expand Down
4 changes: 2 additions & 2 deletions LiteEntitySystem/Extensions/SyncNetSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public T Value
}
}

private RemoteCallSpan<byte> _initAction;
private static RemoteCallSpan<byte> _initAction;

private readonly Func<T> _constructor;

Expand All @@ -31,7 +31,7 @@ public SyncNetSerializable(Func<T> constructor)
_constructor = constructor;
}

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, Init, ref _initAction);
}
Expand Down
10 changes: 5 additions & 5 deletions LiteEntitySystem/Extensions/SyncSquare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ private struct SetCallData
}

private T[] _data;
private RemoteCall<SetCallData> _setRpcAction;
private RemoteCall<int> _resizeRpcAction;
private RemoteCallSpan<T> _initArrayAction;
private RemoteCall _clearAction;
private static RemoteCall<SetCallData> _setRpcAction;
private static RemoteCall<int> _resizeRpcAction;
private static RemoteCallSpan<T> _initArrayAction;
private static RemoteCall _clearAction;

public int Length => _data.Length;

Expand Down Expand Up @@ -47,7 +47,7 @@ public void Clear()
protected internal override void OnSyncRequested()
=> ExecuteRPC(_initArrayAction, _data);

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, SetValueRPC, ref _setRpcAction);
r.CreateClientAction(this, InitArrayRPC, ref _initArrayAction);
Expand Down
4 changes: 2 additions & 2 deletions LiteEntitySystem/Extensions/SyncString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SyncString : SyncableField
private string _string;
private int _size;

private RemoteCallSpan<byte> _setStringClientCall;
private static RemoteCallSpan<byte> _setStringClientCall;

public string Value
{
Expand All @@ -27,7 +27,7 @@ public string Value
}
}

protected override void RegisterRPC(in SyncableRPCRegistrator r)
protected internal override void RegisterRPC(ref SyncableRPCRegistrator r)
{
r.CreateClientAction(this, SetNewString, ref _setStringClientCall);
}
Expand Down
7 changes: 7 additions & 0 deletions LiteEntitySystem/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public static unsafe int ReadStruct<T>(this ReadOnlySpan<byte> data, out T value
value = *(T*)rawData;
return sizeof(T);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe T ReadStruct<T>(this ReadOnlySpan<byte> data) where T : unmanaged
{
fixed (byte* rawData = data)
return *(T*)rawData;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int SizeOfStruct<T>() where T : unmanaged
Expand Down
6 changes: 3 additions & 3 deletions LiteEntitySystem/HumanControllerLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ServerResponse(ushort requestId, bool success)
public const int StringSizeLimit = 1024;
private readonly NetPacketProcessor _packetProcessor = new(StringSizeLimit);
private readonly NetDataWriter _requestWriter = new();
private RemoteCall<ServerResponse> _serverResponseRpc;
private static RemoteCall<ServerResponse> _serverResponseRpc;
private ushort _requestId;
private readonly Queue<(ushort,Action<bool>)> _awaitingRequests;

Expand All @@ -53,9 +53,9 @@ public NetPlayer GetAssignedPlayer()
return ServerManager.GetPlayer(InternalOwnerId);
}

protected override void RegisterRPC(in RPCRegistrator r)
protected override void RegisterRPC(ref RPCRegistrator r)
{
base.RegisterRPC(in r);
base.RegisterRPC(ref r);
r.CreateRPCAction(this, OnServerResponse, ref _serverResponseRpc, ExecuteFlags.SendToOwner);
}

Expand Down
18 changes: 9 additions & 9 deletions LiteEntitySystem/Internal/EntityClassData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ public SyncableFieldInfo(int offset)
internal readonly struct RpcOffset
{
public readonly int SyncableOffset;
public readonly int Offset;
public readonly SyncFlags Flags;

public RpcOffset(int syncableOffset, int offset, SyncFlags executeFlags)
public RpcOffset(int syncableOffset,SyncFlags executeFlags)
{
SyncableOffset = syncableOffset;
Offset = offset;
Flags = executeFlags;
}
}
Expand Down Expand Up @@ -53,7 +51,6 @@ internal struct EntityClassData
public readonly bool IsLocalOnly;
public readonly EntityConstructor<InternalEntity> EntityConstructor;
public readonly MethodCallDelegate[] RemoteCallsClient;
public readonly Delegate[] RemoteCallsServer;

private readonly bool _isCreated;
private readonly Type[] _baseTypes;
Expand Down Expand Up @@ -132,7 +129,8 @@ public EntityClassData(ushort filterId, Type entType, RegisteredTypeInfo typeInf
const BindingFlags bindingFlags = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
BindingFlags.DeclaredOnly |
BindingFlags.Static;

foreach (var baseType in baseTypes)
{
Expand All @@ -150,7 +148,9 @@ public EntityClassData(ushort filterId, Type entType, RegisteredTypeInfo typeInf

if(IsRemoteCallType(ft))
{
remoteCallOffsets.Add(new RpcOffset(-1, offset, syncFlags));
if (!field.IsStatic)
throw new Exception($"RemoteCalls should be static! (Class: {entType} Field: {field.Name})");
remoteCallOffsets.Add(new RpcOffset(-1, syncFlags));
}
//syncvars
else if (ft.IsValueType && ft.IsGenericType && !ft.IsArray)
Expand Down Expand Up @@ -208,8 +208,9 @@ public EntityClassData(ushort filterId, Type entType, RegisteredTypeInfo typeInf
var syncableFieldType = syncableField.FieldType;
if(IsRemoteCallType(syncableFieldType))
{
int rpcOffset = Utils.GetFieldOffset(syncableField);
remoteCallOffsets.Add(new RpcOffset(offset, rpcOffset, syncFlags));
if (!syncableField.IsStatic)
throw new Exception($"RemoteCalls should be static! (Class: {entType} Field: {field.Name})");
remoteCallOffsets.Add(new RpcOffset(offset, syncFlags));
continue;
}
if (!syncableFieldType.IsValueType || !syncableFieldType.IsGenericType || syncableFieldType.GetGenericTypeDefinition() != typeof(SyncVar<>))
Expand Down Expand Up @@ -251,7 +252,6 @@ public EntityClassData(ushort filterId, Type entType, RegisteredTypeInfo typeInf
LagCompensatedFields = lagCompensatedFields.ToArray();
LagCompensatedCount = LagCompensatedFields.Length;
RemoteCallsClient = new MethodCallDelegate[RpcOffsets.Length];
RemoteCallsServer = new Delegate[RpcOffsets.Length];

int fixedOffset = 0;
int predictedOffset = 0;
Expand Down
26 changes: 11 additions & 15 deletions LiteEntitySystem/Internal/InternalEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,13 @@ protected internal virtual void OnConstructed()
internal void RegisterRpcInternal()
{
ref var classData = ref GetClassData();
//load cache and/or init RpcIds
//load cache and/or init syncable rpc flags
for (ushort i = 0; i < classData.RpcOffsets.Length; i++)
{
var rpcOffset = classData.RpcOffsets[i];
if (rpcOffset.SyncableOffset == -1)
{
ref var remoteCall = ref RefMagic.RefFieldValue<RemoteCall>(this, rpcOffset.Offset);
remoteCall = new RemoteCall(i, classData.RemoteCallsServer[i]);
}
else
if (rpcOffset.SyncableOffset >= 0)
{
var syncable = RefMagic.RefFieldValue<SyncableField>(this, rpcOffset.SyncableOffset);
ref var remoteCall = ref RefMagic.RefFieldValue<RemoteCall>(syncable, rpcOffset.Offset);
remoteCall = new RemoteCall(i, classData.RemoteCallsServer[i]);
syncable.ParentEntityId = Id;
if (rpcOffset.Flags.HasFlagFast(SyncFlags.OnlyForOwner))
syncable.Flags = ExecuteFlags.SendToOwner;
else if (rpcOffset.Flags.HasFlagFast(SyncFlags.OnlyForOtherPlayers))
Expand All @@ -154,6 +146,7 @@ internal void RegisterRpcInternal()
}
if(!classData.IsRpcBound)
{
//setup field ids for BindOnChange
for (int i = 0; i < classData.FieldsCount; i++)
{
ref var field = ref classData.Fields[i];
Expand All @@ -163,13 +156,16 @@ internal void RegisterRpcInternal()
id = (byte)i;
}
}

var syncablesRegistrator = new SyncableRPCRegistrator(classData.RemoteCallsClient);
for (int i = 0; i < classData.SyncableFields.Length; i++)
{
var syncField = RefMagic.RefFieldValue<SyncableField>(this, classData.SyncableFields[i].Offset);
syncField.ParentEntityId = Id;
syncField.InternalInit(new SyncableRPCRegistrator(this));
syncField.ParentEntity = this;
syncField.RegisterRPC(ref syncablesRegistrator);
}
RegisterRPC(new RPCRegistrator());
var rpcRegistrator = new RPCRegistrator(syncablesRegistrator.RpcId);
RegisterRPC(ref rpcRegistrator);
//Logger.Log($"RegisterRPCs for class: {classData.ClassId}");
classData.IsRpcBound = true;
}
Expand All @@ -179,7 +175,7 @@ internal void RegisterRpcInternal()
for (int i = 0; i < classData.SyncableFields.Length; i++)
{
var syncField = RefMagic.RefFieldValue<SyncableField>(this, classData.SyncableFields[i].Offset);
syncField.ParentEntityId = Id;
syncField.ParentEntity = this;
}
}
}
Expand All @@ -206,7 +202,7 @@ protected void ExecuteRPC<T>(in RemoteCallSpan<T> rpc, ReadOnlySpan<T> value) wh
/// Method for registering RPCs and OnChange notifications
/// </summary>
/// <param name="r"></param>
protected virtual void RegisterRPC(in RPCRegistrator r)
protected virtual void RegisterRPC(ref RPCRegistrator r)
{
r.BindOnChange(this, ref _isDestroyed, OnDestroyChange);
}
Expand Down
Loading

0 comments on commit bc53270

Please sign in to comment.