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
This commit is contained in:
Paul Pacheco 2019-04-10 17:01:09 -05:00 committed by vis2k
parent f00c8e5e62
commit 97e9ac2483
4 changed files with 13 additions and 8 deletions

View File

@ -1,3 +1,4 @@
using System;
using Mono.Cecil; using Mono.Cecil;
namespace Mirror.Weaver namespace Mirror.Weaver
@ -40,6 +41,16 @@ public static bool IsDerivedFrom(this TypeDefinition td, TypeReference baseClass
return false; 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) public static bool ImplementsInterface(this TypeDefinition td, TypeReference baseInterface)
{ {
TypeDefinition typedef = td; TypeDefinition typedef = td;

View File

@ -96,7 +96,7 @@ public static MethodReference GetReadFunc(TypeReference variable, int recursionC
} }
else if (td.IsEnum) else if (td.IsEnum)
{ {
return Weaver.NetworkReaderReadInt32; return GetReadFunc(td.GetEnumUnderlyingType(), recursionCount);
} }
else else
{ {

View File

@ -83,9 +83,6 @@ class Weaver
public static TypeReference CmdDelegateReference; public static TypeReference CmdDelegateReference;
public static MethodReference CmdDelegateConstructor; public static MethodReference CmdDelegateConstructor;
public static MethodReference NetworkReaderReadInt32;
public static MethodReference NetworkWriterWriteInt32;
public static MethodReference NetworkWriterWriteInt16; public static MethodReference NetworkWriterWriteInt16;
public static MethodReference NetworkServerGetActive; public static MethodReference NetworkServerGetActive;
@ -307,9 +304,6 @@ static void SetupTargetTypes()
NetworkServerGetLocalClientActive = Resolvers.ResolveMethod(NetworkServerType, CurrentAssembly, "get_localClientActive"); NetworkServerGetLocalClientActive = Resolvers.ResolveMethod(NetworkServerType, CurrentAssembly, "get_localClientActive");
NetworkClientGetActive = Resolvers.ResolveMethod(NetworkClientType, CurrentAssembly, "get_active"); 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); NetworkWriterWriteInt16 = Resolvers.ResolveMethodWithArg(NetworkWriterType, CurrentAssembly, "Write", int16Type);
NetworkReaderReadPackedUInt32 = Resolvers.ResolveMethod(NetworkReaderType, CurrentAssembly, "ReadPackedUInt32"); NetworkReaderReadPackedUInt32 = Resolvers.ResolveMethod(NetworkReaderType, CurrentAssembly, "ReadPackedUInt32");

View File

@ -90,7 +90,7 @@ public static MethodReference GetWriteFunc(TypeReference variable, int recursion
} }
else if (variable.Resolve().IsEnum) else if (variable.Resolve().IsEnum)
{ {
return Weaver.NetworkWriterWriteInt32; return GetWriteFunc(variable.Resolve().GetEnumUnderlyingType(), recursionCount);
} }
else else
{ {