Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AOT] Add middleware reflection fallback #45890

Merged
merged 1 commit into from
Jan 6, 2023
Merged

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Jan 5, 2023

Addresses the warning part of #45528

Adds a reflection fallback to invoking middleware. The goal is to avoid native AOT warnings on publish.

There is also a small performance benefit to using reflection instead of an expression with AOT. Avoids allocating and running expression interpreter. The ideal solution to performance here is using source generation to invoke middleware. That is still to be done before #45528 is complete.

Copy link
Member

@davidfowl davidfowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JamesNK
Copy link
Member Author

JamesNK commented Jan 5, 2023

@eerhardt After making these changes locally, updating the assembly in the SDK folder, and republishing, I still see expressions related warnings.

/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/ArrayOperations.cs(23): AOT analysis warning IL3050: System.Linq.Expressions.Interpreter.NewArrayInitInstruction.Run(InterpretedFrame): Using member 'System.Array.CreateInstance(Type,Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. [C:\Users\james\source\repos\WebApplication25\WebApplication25\WebApplication25.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/ArrayOperations.cs(52): AOT analysis warning IL3050: System.Linq.Expressions.Interpreter.NewArrayInstruction.Run(InterpretedFrame): Using member 'System.Array.CreateInstance(Type,Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. [C:\Users\james\source\repos\WebApplication25\WebApplication25\WebApplication25.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/ArrayOperations.cs(87): AOT analysis warning IL3050: System.Linq.Expressions.Interpreter.NewArrayBoundsInstruction.Run(InterpretedFrame): Using member 'System.Array.CreateInstance(Type,Int32[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specifiedtype might not be available. [C:\Users\james\source\repos\WebApplication25\WebApplication25\WebApplication25.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/TypeUtils.cs(28): AOT analysis warning IL3050: System.Dynamic.Utils.TypeUtils.GetNullableType(Type): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\Users\james\source\repos\WebApplication25\WebApplication25\WebApplication25.csproj]
/_/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/MethodCallExpression.cs(1379): AOT analysis warning IL3050: System.Linq.Expressions.Expression.ApplyTypeArgs(MethodInfo,Type[]): Using member 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\Users\james\source\repos\WebApplication25\WebApplication25\WebApplication25.csproj]

I mentioned on the original issue that it's odd the warnings are coming from System.Linq.Expressions files. Is the problem that System.Linq.Expressions is not correctly annotated?

Or am I not testing correctly?
Or does the PR not correctly fix the cause of the warnings?

@davidfowl
Copy link
Member

I thought this was the issue.

@JamesNK
Copy link
Member Author

JamesNK commented Jan 6, 2023

This doesn't fix the expressions AOT warnings, but IMO this change is still worthwhile. Invoking via raw reflection is better than interpreting compiled expressions. And this change is pretty modest, self-contained, and is on the critical path (called potentially multiple times per request). Source gen is the best solution, but this is a good improvement for existing libraries who call UseMiddleware<T>().

@davidfowl
Copy link
Member

Yes this is fine. The source gen will also be usable in libraries.

@JamesNK JamesNK merged commit f88238f into main Jan 6, 2023
@JamesNK JamesNK deleted the jamesnk/aot-middleware branch January 6, 2023 13:59
@ghost ghost added this to the 8.0-preview1 milestone Jan 6, 2023
@@ -107,7 +109,9 @@ public static class UseMiddlewareExtensions
return (RequestDelegate)invokeMethod.CreateDelegate(typeof(RequestDelegate), instance);
}

var factory = Compile<object>(invokeMethod, parameters);
var factory = RuntimeFeature.IsDynamicCodeSupported
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants