Skip to content

Commit

Permalink
perf: Reduce enum bandwidth (#794)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
paulpach authored and miwarnec committed Apr 10, 2019
1 parent f00c8e5 commit 97e9ac2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Assets/Mirror/Editor/Weaver/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Mono.Cecil;

namespace Mirror.Weaver
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Editor/Weaver/Readers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 0 additions & 6 deletions Assets/Mirror/Editor/Weaver/Weaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Editor/Weaver/Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 97e9ac2

Please sign in to comment.