Skip to content

Commit

Permalink
Improved performance of System.Reflection.Emit API (dotnet/coreclr#23833
Browse files Browse the repository at this point in the history
)

* Improved performance of System.Reflection.Emit API

* Moved heap allocation to stack

* Eliminating conditional branch by casting sbyte to byte

* Reduced heap allocation of ScopeTree

* Removed redundant `unsafe` from function signature


Commit migrated from dotnet/coreclr@77a5f9c
  • Loading branch information
bbartels authored and jkotas committed Apr 15, 2019
1 parent 7c7ee2c commit 0f9a001
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
===========================================================*/


using System;
using System.Reflection;
using System.Buffers.Binary;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Diagnostics;

namespace System.Reflection.Emit
Expand Down Expand Up @@ -305,7 +302,7 @@ private static void VerifyTypeAndPassedObjectType(Type type, Type passedType, st
}
}

private void EmitType(BinaryWriter writer, Type type)
private static void EmitType(BinaryWriter writer, Type type)
{
if (type.IsPrimitive)
{
Expand Down Expand Up @@ -377,7 +374,7 @@ private void EmitType(BinaryWriter writer, Type type)
}
}

private void EmitString(BinaryWriter writer, string str)
private static void EmitString(BinaryWriter writer, string str)
{
// Strings are emitted with a length prefix in a compressed format (1, 2 or 4 bytes) as used internally by metadata.
byte[] utf8Str = Encoding.UTF8.GetBytes(str);
Expand All @@ -388,20 +385,16 @@ private void EmitString(BinaryWriter writer, string str)
}
else if (length <= 0x3fff)
{
writer.Write((byte)((length >> 8) | 0x80));
writer.Write((byte)(length & 0xff));
writer.Write(BinaryPrimitives.ReverseEndianness((short)(length | 0x80_00)));
}
else
{
writer.Write((byte)((length >> 24) | 0xc0));
writer.Write((byte)((length >> 16) & 0xff));
writer.Write((byte)((length >> 8) & 0xff));
writer.Write((byte)(length & 0xff));
writer.Write(BinaryPrimitives.ReverseEndianness(length | 0xC0_00_00_00));
}
writer.Write(utf8Str);
}

private void EmitValue(BinaryWriter writer, Type type, object value)
private static void EmitValue(BinaryWriter writer, Type type, object value)
{
if (type.IsEnum)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace System.Reflection.Emit
{
using System;
using System.Buffers.Binary;
using System.Globalization;
using System.Diagnostics.SymbolStore;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -723,10 +724,7 @@ internal override byte[] GetCodeInfo(

if ((header & 0x40) != 0) // Fat
{
byte[] size = new byte[4];
for (int q = 0; q < 3; q++)
size[q] = m_exceptionHeader[q + 1];
EHCount = (BitConverter.ToInt32(size, 0) - 4) / 24;
EHCount = (BinaryPrimitives.ReadInt32LittleEndian(m_exceptionHeader.AsSpan(1)) - 4) / 24;
}
else
EHCount = (m_exceptionHeader[1] - 2) / 12;
Expand Down
Loading

0 comments on commit 0f9a001

Please sign in to comment.