From 47e43327864de3c5e85cae390f0c3ee3ef6b5889 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 13 Jan 2024 17:46:16 +0100 Subject: [PATCH] Cache enum stubs and further avoid display classes --- src/WinRT.Runtime/Marshalers.cs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/WinRT.Runtime/Marshalers.cs b/src/WinRT.Runtime/Marshalers.cs index 7d0989fce..c1f392d57 100644 --- a/src/WinRT.Runtime/Marshalers.cs +++ b/src/WinRT.Runtime/Marshalers.cs @@ -1672,16 +1672,24 @@ public static T FromAbi(IntPtr nativeDelegate) internal static class Marshaler { - internal static Action EmptyFunc = (object box) => { }; - internal static Func ReturnParameterFunc = (object box) => box; + internal static readonly Action EmptyFunc = Empty; + internal static readonly Func ReturnParameterFunc = ReturnParameter; + internal static readonly Action CopyIntEnumFunc = CopyIntEnum; + internal static readonly Action CopyIntEnumDirectFunc = CopyIntEnumDirect; + internal static readonly Action CopyUIntEnumFunc = CopyUIntEnum; + internal static readonly Action CopyUIntEnumDirectFunc = CopyUIntEnumDirect; - internal static unsafe void CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int)); + private static void Empty(object arg) { } - internal static unsafe void CopyIntEnumDirect(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)value; + private static object ReturnParameter(object arg) => arg; - internal static unsafe void CopyUIntEnum(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint)); + private static unsafe void CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int)); - internal static unsafe void CopyUIntEnumDirect(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)value; + private static unsafe void CopyIntEnumDirect(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)value; + + private static unsafe void CopyUIntEnum(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint)); + + private static unsafe void CopyUIntEnumDirect(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)value; } #if EMBED @@ -1842,13 +1850,13 @@ static Marshaler() // For marshaling non-blittable enum arrays via MarshalNonBlittable if (typeof(T).GetEnumUnderlyingType() == typeof(int)) { - CopyAbi = Marshaler.CopyIntEnum; - CopyManaged = ((Action)Marshaler.CopyIntEnumDirect).WithTypedEnumT1(); + CopyAbi = Marshaler.CopyIntEnumFunc; + CopyManaged = Marshaler.CopyIntEnumDirectFunc.WithTypedEnumT1(); } else { - CopyAbi = Marshaler.CopyUIntEnum; - CopyManaged = ((Action)Marshaler.CopyUIntEnumDirect).WithTypedEnumT1(); + CopyAbi = Marshaler.CopyUIntEnumFunc; + CopyManaged = Marshaler.CopyUIntEnumDirectFunc.WithTypedEnumT1(); } } CreateMarshalerArray = (T[] array) => MarshalBlittable.CreateMarshalerArray(array);