-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add support for RouteHandlerInvocationContext<> overloads #41406
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
Conversation
Where are the object[] allocations coming from? |
The ~1000 allocations before the change are from the object's array initialized in the invocation pipeline when we do the boxing. The rest are more dispersed.
|
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextOfT.cs
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextOfT.tt
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextOfT.cs
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContext.cs
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextBase.cs
Outdated
Show resolved
Hide resolved
public void ProhibitsActionsThatModifyListSize() | ||
{ | ||
var context = new RouteHandlerInvocationContext<string, int, bool>(new DefaultHttpContext(), "This is a test", 42, false); | ||
Assert.Throws<NotSupportedException>(() => context.Add("string")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this, I think we should change the type of RouteHandlerInvocationContext.Arguments
to IReadOnlyList<object?>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so.
The type we use for Arguments
will be used in both the generic and non-generic overloads. For the non-generic overload, we have to use IList
to support in-place modification of items in the list.
One option is that we could change the behavior of the implementation so that if you're using the non-generic variant (more than 10 arguments) you're not able to make modifications to parameters. For scenarios where you're using ten arguments or less, you'll be able to modify in-place using the custom setter in the generic overloads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, nevermind. We cannot do this anyway. The IReadOnlyList
implementation does not support a setter on this[index]
so we would be able to use it for generic variants anways.
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextOfT.tt
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextOfT.tt
Outdated
Show resolved
Hide resolved
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can include this in the global directory.props
cc @dougbu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean notice a project contains a .tt file and add the <Service />
element @davidfowl❔ That may work but VS would rewrite it the next time someone opens the project IIRC.
We only have 3 projects containing this <Service />
after this PR. How big an issue is this❔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not an issue, I would just like to clean it up as we plan to code generate more things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's up to you @davidfowl whether to centralize things in this PR. I recommend testing if the code in our root Directory.Build.props prevents VS rewriting the project to list the <Service/>
. If "Save All" is fine after adding a new .tt file, you're good to go. Otherwise, the extra code will become redundant and something we'll end up fighting.
The specific approach would be something like
<ItemGroup>
<_GeneratorSources Include="$(MSBuildProjectDirectory)\**\*.tt" />
</ItemGroup>
<ItemGroup>
<Service Condition=" '@(_GeneratorSources->Count()) ' != '0' " Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
@@ -0,0 +1,129 @@ | |||
<#@ template language="C#" #> | |||
<#@ output extension=".cs" #> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @mhutch I'm trying to resurrect t4
src/Http/Http.Abstractions/test/RouteHandlerInvocationContextOfTTests.cs
Show resolved
Hide resolved
🆙 📅 : Can I get another look at this? |
src/Http/Http.Abstractions/src/DefaultRouteHandlerInvocationContext.cs
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContext.cs
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/RouteHandlerInvocationContextOfT.Generated.tt
Outdated
Show resolved
Hide resolved
/// Provides a default implementation for wrapping the <see cref="HttpContext"/> and parameters | ||
/// provided to a route handler. | ||
/// </summary> | ||
public sealed class DefaultRouteHandlerInvocationContext : RouteHandlerInvocationContext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this name discussed/approved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
Closes #40514.
RouteHandlerInvocationContext<>
overloads of various arity (supports up to 10 arguments)GetParameter<T>(int index)
API toRouteHandlerInvocationContext
HttpContext
andParameters
properties onRouteHandlerInvocationContext
virtualAPI Changes
Perf Impact
Before:
Allocations for
System.Object[]
in non-generic RHICAfter: