From 97e9ac24830b1e0e0aec28c8608ad630ed024f5c Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Wed, 10 Apr 2019 17:01:09 -0500 Subject: [PATCH] perf: Reduce enum bandwidth (#794) enums are serialized according to their size. if enum extend byte, they use 1 byte if enum extend short, they use 2 bytes if enum extend int, they are varinted if enum extend long, they are varinted So on average, most enums will take 1 byte. Previously they always required 4 bytes --- Assets/Mirror/Editor/Weaver/Extensions.cs | 11 +++++++++++ Assets/Mirror/Editor/Weaver/Readers.cs | 2 +- Assets/Mirror/Editor/Weaver/Weaver.cs | 6 ------ Assets/Mirror/Editor/Weaver/Writers.cs | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Editor/Weaver/Extensions.cs b/Assets/Mirror/Editor/Weaver/Extensions.cs index e49354140..94c37453d 100644 --- a/Assets/Mirror/Editor/Weaver/Extensions.cs +++ b/Assets/Mirror/Editor/Weaver/Extensions.cs @@ -1,3 +1,4 @@ +using System; using Mono.Cecil; namespace Mirror.Weaver @@ -40,6 +41,16 @@ public static bool IsDerivedFrom(this TypeDefinition td, TypeReference baseClass return false; } + public static TypeReference GetEnumUnderlyingType(this TypeDefinition td) + { + foreach (FieldDefinition field in td.Fields) + { + if (!field.IsStatic) + return field.FieldType; + } + throw new ArgumentException($"Invalid enum {td.FullName}"); + } + public static bool ImplementsInterface(this TypeDefinition td, TypeReference baseInterface) { TypeDefinition typedef = td; diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index e796cf807..17dd5bae9 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -96,7 +96,7 @@ public static MethodReference GetReadFunc(TypeReference variable, int recursionC } else if (td.IsEnum) { - return Weaver.NetworkReaderReadInt32; + return GetReadFunc(td.GetEnumUnderlyingType(), recursionCount); } else { diff --git a/Assets/Mirror/Editor/Weaver/Weaver.cs b/Assets/Mirror/Editor/Weaver/Weaver.cs index 50ff8100f..1a028489c 100644 --- a/Assets/Mirror/Editor/Weaver/Weaver.cs +++ b/Assets/Mirror/Editor/Weaver/Weaver.cs @@ -83,9 +83,6 @@ class Weaver public static TypeReference CmdDelegateReference; public static MethodReference CmdDelegateConstructor; - public static MethodReference NetworkReaderReadInt32; - - public static MethodReference NetworkWriterWriteInt32; public static MethodReference NetworkWriterWriteInt16; public static MethodReference NetworkServerGetActive; @@ -307,9 +304,6 @@ static void SetupTargetTypes() NetworkServerGetLocalClientActive = Resolvers.ResolveMethod(NetworkServerType, CurrentAssembly, "get_localClientActive"); NetworkClientGetActive = Resolvers.ResolveMethod(NetworkClientType, CurrentAssembly, "get_active"); - NetworkReaderReadInt32 = Resolvers.ResolveMethod(NetworkReaderType, CurrentAssembly, "ReadInt32"); - - NetworkWriterWriteInt32 = Resolvers.ResolveMethodWithArg(NetworkWriterType, CurrentAssembly, "Write", int32Type); NetworkWriterWriteInt16 = Resolvers.ResolveMethodWithArg(NetworkWriterType, CurrentAssembly, "Write", int16Type); NetworkReaderReadPackedUInt32 = Resolvers.ResolveMethod(NetworkReaderType, CurrentAssembly, "ReadPackedUInt32"); diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index 11241591d..7f5e6acae 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -90,7 +90,7 @@ public static MethodReference GetWriteFunc(TypeReference variable, int recursion } else if (variable.Resolve().IsEnum) { - return Weaver.NetworkWriterWriteInt32; + return GetWriteFunc(variable.Resolve().GetEnumUnderlyingType(), recursionCount); } else {