Skip to content

Commit

Permalink
Reduce enum stubs codegen with NativeAOT (#1441)
Browse files Browse the repository at this point in the history
* Remove lambdas in Marshaler<T> for enum types

* Avoid display classes for enum stubs

ff

* Cache enum stubs and further avoid display classes
  • Loading branch information
Sergio0694 authored Jan 24, 2024
1 parent b207988 commit ee362c0
Showing 2 changed files with 27 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
@@ -1707,11 +1707,21 @@ public static T FromAbi<T>(IntPtr nativeDelegate)

internal static class Marshaler
{
internal static Func<object, object> ReturnParameterFunc = (object box) => box;
internal static unsafe Action<object, IntPtr> CopyIntEnumFunc =
(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int));
internal static unsafe Action<object, IntPtr> CopyUIntEnumFunc =
(object value, IntPtr dest) => *(uint*)dest.ToPointer() = (uint)Convert.ChangeType(value, typeof(uint));
internal static readonly Func<object, object> ReturnParameterFunc = ReturnParameter;
internal static readonly Action<object, IntPtr> CopyIntEnumFunc = CopyIntEnum;
internal static readonly Action<object, IntPtr> CopyIntEnumDirectFunc = CopyIntEnumDirect;
internal static readonly Action<object, IntPtr> CopyUIntEnumFunc = CopyUIntEnum;
internal static readonly Action<object, IntPtr> CopyUIntEnumDirectFunc = CopyUIntEnumDirect;

private static object ReturnParameter(object arg) => arg;

private static unsafe void CopyIntEnum(object value, IntPtr dest) => *(int*)dest.ToPointer() = (int)Convert.ChangeType(value, typeof(int));

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
@@ -1900,12 +1910,12 @@ static Marshaler()
if (typeof(T).GetEnumUnderlyingType() == typeof(int))
{
CopyAbi = Marshaler.CopyIntEnumFunc;
CopyManaged = (T value, IntPtr dest) => Marshaler.CopyIntEnumFunc(value, dest);
CopyManaged = Marshaler.CopyIntEnumDirectFunc.WithTypedT1<T>();
}
else
{
CopyAbi = Marshaler.CopyUIntEnumFunc;
CopyManaged = (T value, IntPtr dest) => Marshaler.CopyUIntEnumFunc(value, dest);
CopyManaged = Marshaler.CopyUIntEnumDirectFunc.WithTypedT1<T>();
}
}
CreateMarshalerArray = (T[] array) => MarshalBlittable<T>.CreateMarshalerArray(array);
10 changes: 10 additions & 0 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
@@ -47,6 +47,11 @@ public static Action<object> WithMarshaler2Support(this Action<IObjectReference>
return action.InvokeWithMarshaler2Support;
}

public static Action<T, IntPtr> WithTypedT1<T>(this Action<object, IntPtr> action)
{
return action.InvokeWithTypedT1;
}

public static Func<object, TResult> WithObjectT<T, TResult>(this Func<T, TResult> function)
{
return function.InvokeWithObjectT;
@@ -98,6 +103,11 @@ private static void InvokeWithObjectParams<T>(this Action<T> func, object arg)
{
func.Invoke((T)arg);
}

private static void InvokeWithTypedT1<T>(this Action<object, IntPtr> action, T arg1, IntPtr arg2)
{
action.Invoke(arg1, arg2);
}
}

internal sealed class Platform

0 comments on commit ee362c0

Please sign in to comment.