Skip to content

Simplify and optimize async parameters in RequestDelegateFactory #42588

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

Closed
wants to merge 2 commits into from

Conversation

halter73
Copy link
Member

@halter73 halter73 commented Jul 6, 2022

  • Avoid object[] allocation for single BindAsync call
  • Remove redundant state and logic
  • Cleanup some names and nullability annotations
  • Split RequestDelegateFactory.Log class into its own RequestDelegateFactory.Log.cs file

- Avoid object[] for single BindAsync
- Remove redundant state and logic
- Cleanup some names and nullability annotations
@halter73 halter73 requested a review from Tratcher as a code owner July 6, 2022 01:17
@ghost ghost added the area-runtime label Jul 6, 2022
Copy link
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

No deep insight into the code but I noticed this 😄

var arguments = new Expression[factoryContext.ArgumentExpressions.Length + 1];
arguments[0] = HttpContextExpr;
factoryContext.ArgumentExpressions.CopyTo(arguments, 1);
for (int i = 0; i < methodParameters.Length; i++)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (int i = 0; i < methodParameters.Length; i++)
for (var i = 0; i < methodParameters.Length; i++)

return Expression.New(constructor, arguments);
// For AOT platforms it's not possible to support the closed generic arguments that are based on the
// parameter arguments dynamically (for value types). In that case, fallback to boxing the argument list.
// new RouteHandlerInvocionContext(httpContext, (object)name_local, (object)int_local);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// new RouteHandlerInvocionContext(httpContext, (object)name_local, (object)int_local);
// new RouteHandlerInvocationContext(httpContext, (object)name_local, (object)int_local);

else
{
methodArgumentTypes = new Type[methodArguments.Length];
var boxedArgs = new Expression[methodArguments.Length];
Copy link
Member

Choose a reason for hiding this comment

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

super nit: this is only used in the if (!RuntimeFeature.IsDynamicCodeCompiled || !constructorType.IsGenericType) case, we could slightly restructure to avoid this startup cost

Copy link
Member

Choose a reason for hiding this comment

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

That’s what the code did before

Copy link
Member

Choose a reason for hiding this comment

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

Hence "super nit"

}

// new RouteHandlerInvocationContext(httpContext, (object)name_local, (object)int_local);
return fallbackConstruction;
var constructor = constructorType.MakeGenericType(methodArgumentTypes).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).Single();
Copy link
Member

Choose a reason for hiding this comment

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

Debug.Assert(constructor is not null)?

if (!successful)
var boundValues = new object?[count];

// Looping over arrays is faster
Copy link
Member

Choose a reason for hiding this comment

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

Comment was more for when we called .ToArray() on the binders collection, can remove

{
return valueExpression;
return Expression.Condition(Expression.NotEqual(valueExpression, Expression.Default(parameter.ParameterType)),
Copy link
Member

Choose a reason for hiding this comment

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

Show code in a comment?

args[i] = CreateArgument(parameters[i], factoryContext);
var parameter = parameters[i];

factoryContext.ParametersAndPropertiesAsParameters.Add(parameter);
Copy link
Member

@brunolins16 brunolins16 Jul 6, 2022

Choose a reason for hiding this comment

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

It will change the order the metadata (for parameters from AsParameters) will be added, right? I don't know if that is a problem and, to be honest, it looks more correct with this change.

@martincostello
Copy link
Member

As you're taking the big refactor-hammer to things as it is, I wonder if maybe it's also worth moving the other nested types into their own partial files too to get the main file down to under 2k LoC?

@ghost ghost added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 25, 2023
@@ -466,24 +468,22 @@ private static Expression CreateRouteHandlerInvocationContextBase(FactoryContext
_ => typeof(DefaultRouteHandlerInvocationContext)
};

if (constructorType.IsGenericType)
if (!RuntimeFeature.IsDynamicCodeCompiled || !constructorType.IsGenericType)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should remove all of these checks now that we're going full source generator.

Copy link
Member

Choose a reason for hiding this comment

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

@halter73 Sounds like this needs to be updated after the introduction of RDG. @captainsafia might have thoughts?

@captainsafia
Copy link
Member

@halter73 Thoughts on this PR? If I recall, it still needs a hearty rebase to get it in an up-to-date state.

There's also some goodness in here that is probably better to split up into a separate PR like:

Cleanup some names and nullability annotations
Split RequestDelegateFactory.Log class into its own RequestDelegateFactory.Log.cs file

@halter73
Copy link
Member Author

Thanks for the reminder. I'll work on rebasing this after I update my MapIdentityApi() PR (#47414) in response to API review.

@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
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment Feb 13, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 20, 2024
@halter73 halter73 closed this Apr 16, 2024
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-preview4 milestone Apr 16, 2024
@halter73 halter73 deleted the halter73/less-rdf-combos branch November 19, 2024 04:46
@halter73 halter73 restored the halter73/less-rdf-combos branch November 19, 2024 04:46
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 pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants