breaking: perf: change NB.syncVarDirtyBits to field (#3762)

@miwarnec saw the property showing up while profiling. Very much unnecessary, so lets change that to a field.
BREAKING: Changed NetworkBehaviour.syncVarDirtyBits from a "get-only" property to a field
This commit is contained in:
Robin Rolf 2024-02-07 18:04:36 +01:00 committed by MrGadget
parent 7b50cd5197
commit bd7b6fe038
4 changed files with 39 additions and 6 deletions

View File

@ -135,8 +135,9 @@ public bool authority
// -> still supports dynamically sized types
//
// 64 bit mask, tracking up to 64 SyncVars.
protected ulong syncVarDirtyBits { get; private set; }
// 64 bit mask, tracking up to 64 sync collections (internal for tests).
// protected since NB child classes read this field in the weaver generated SerializeSyncVars method
protected ulong syncVarDirtyBits;
// 64 bit mask, tracking up to 64 sync collections.
// internal for tests, field for faster access (instead of property)
// TODO 64 SyncLists are too much. consider smaller mask later.
internal ulong syncObjectDirtyBits;

View File

@ -504,7 +504,7 @@ void GenerateSerialization(ref bool WeavingFailed)
worker.Emit(OpCodes.Ldarg_1);
// base
worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Call, weaverTypes.NetworkBehaviourDirtyBitsReference);
worker.Emit(OpCodes.Ldfld, weaverTypes.NetworkBehaviourDirtyBitsReference);
MethodReference writeUint64Func = writers.GetWriteFunc(weaverTypes.Import<ulong>(), ref WeavingFailed);
worker.Emit(OpCodes.Call, writeUint64Func);
@ -524,7 +524,7 @@ void GenerateSerialization(ref bool WeavingFailed)
// Generates: if ((base.get_syncVarDirtyBits() & 1uL) != 0uL)
// base
worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Call, weaverTypes.NetworkBehaviourDirtyBitsReference);
worker.Emit(OpCodes.Ldfld, weaverTypes.NetworkBehaviourDirtyBitsReference);
// 8 bytes = long
worker.Emit(OpCodes.Ldc_I8, 1L << dirtyBit);
worker.Emit(OpCodes.And);

View File

@ -42,6 +42,38 @@ public static MethodReference ResolveMethod(TypeReference t, AssemblyDefinition
return null;
}
public static FieldReference ResolveField(TypeReference tr, AssemblyDefinition assembly, Logger Log, string name, ref bool WeavingFailed)
{
if (tr == null)
{
Log.Error($"Cannot resolve Field {name} without a class");
WeavingFailed = true;
return null;
}
FieldReference field = ResolveField(tr, assembly, Log, m => m.Name == name, ref WeavingFailed);
if (field == null)
{
Log.Error($"Field not found with name {name} in type {tr.Name}", tr);
WeavingFailed = true;
}
return field;
}
public static FieldReference ResolveField(TypeReference t, AssemblyDefinition assembly, Logger Log, System.Func<FieldDefinition, bool> predicate, ref bool WeavingFailed)
{
foreach (FieldDefinition fieldRef in t.Resolve().Fields)
{
if (predicate(fieldRef))
{
return assembly.MainModule.ImportReference(fieldRef);
}
}
Log.Error($"Field not found in type {t.Name}", t);
WeavingFailed = true;
return null;
}
public static MethodReference TryResolveMethodInParents(TypeReference tr, AssemblyDefinition assembly, string name)
{
if (tr == null)

View File

@ -10,7 +10,7 @@ public class WeaverTypes
{
public MethodReference ScriptableObjectCreateInstanceMethod;
public MethodReference NetworkBehaviourDirtyBitsReference;
public FieldReference NetworkBehaviourDirtyBitsReference;
public MethodReference GetWriterReference;
public MethodReference ReturnWriterReference;
@ -90,7 +90,7 @@ public WeaverTypes(AssemblyDefinition assembly, Logger Log, ref bool WeavingFail
TypeReference NetworkBehaviourType = Import<NetworkBehaviour>();
NetworkBehaviourDirtyBitsReference = Resolvers.ResolveProperty(NetworkBehaviourType, assembly, "syncVarDirtyBits");
NetworkBehaviourDirtyBitsReference = Resolvers.ResolveField(NetworkBehaviourType, assembly, Log, "syncVarDirtyBits", ref WeavingFailed);
generatedSyncVarSetter = Resolvers.ResolveMethod(NetworkBehaviourType, assembly, Log, "GeneratedSyncVarSetter", ref WeavingFailed);
generatedSyncVarSetter_GameObject = Resolvers.ResolveMethod(NetworkBehaviourType, assembly, Log, "GeneratedSyncVarSetter_GameObject", ref WeavingFailed);