-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Don't allocate the FormFeature eagerly #6511
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
@@ -6,6 +6,6 @@ namespace Microsoft.AspNetCore.Http | |||
{ | |||
public interface IHttpContextContainer | |||
{ | |||
HttpContext HttpContext { get; } | |||
DefaultHttpContext HttpContext { get; } |
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.
Is this going to be a problem if you seal DefaultHttpContext
?
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.
Ehhhh, maybe? For that 1% that want to make a derived version. This was to avoid the casting in the default HttpContextFactory
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.
Ah, so HttpContext
can still be overriden and flow, it isn't bonded to DefaultHttpContext
?
(Otherwise just rename DefaultHttpContext
to HttpContext
)
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.
The scenario here is you want to implement a server with a custom lifetime management of the HttpContext that isn't a DefaultHttpContext. This change ties you to DefaultHttpContext. The alternative would be to cast detect the cast.
The another approach would be to only assign this field once since it can't change per request anyways (that would likely mitigate the cost of the downcast) but there's no way to know if this is the first time.
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.
This is what it would look like.
private HttpContext CreateHttpContext(IFeatureCollection featureCollection)
{
if (featureCollection is IHttpContextContainer container)
{
var context = container.HttpContext;
if (context is DefaultHttpContext defaultHttpContext)
{
defaultHttpContext.FormOptions = _formOptions;
}
else
{
var formFeature = new FormFeature(context.Request, _formOptions);
featureCollection.Set<IFormFeature>(formFeature);
}
}
return new DefaultHttpContext(featureCollection)
{
FormOptions = _formOptions
};
}
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.
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 even know what you're asking for? equivalence of what?
My ask is simple: What if I want equivalent reusability semantics to DefaultHttpContext, but have reasons to create my own HttpContext fields.
Now DefaultHttpContext is sealed, what to do?
If it's possible — without having to create my own Kestrel protocol too — then there's full equivalence between DefaultHttpContext and my own HttpContext.
If it's not possible then there's only equivalence that comes at a performance penalty.
I think that's an important aspect to highlight with respect to these changes.
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.
My ask is simple: What if I want equivalent reusability semantics to DefaultHttpContext, but have reasons to create my own HttpContext fields.
Now DefaultHttpContext is sealed, what to do?
I think it's fine to then file a bug and request it be unsealed. Until then it's sealed because nobody has done that (or not enough that I think it matters). When the masses complain about this change then we'll have some data we can use to unseal the type. Thus far my theory is that 0.01% of people do this so it won't affect anymore in real life.
I think that's an important aspect to highlight with respect to these changes.
The change was marked as breaking and the relevant parties know and hopefully, people try out the previews and give feedback with concrete scenarios, not hypothetical ones.
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.
But wasn't there a straightforward fix for HttpContextContainer to support HttpContext instead?
All I'm saying, if it's low effort/trade-off to keep the full equivalence then that would be my preferred option.
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.
But wasn't there a straightforward fix for HttpContextContainer to support HttpContext instead?
All I'm saying, if it's low effort/trade-off to keep the full equivalence then that would be my preferred option.
More complicated code that i'm not willing to write because of the narrow-ness of the scenario. This is a new extensibility point introduced in 3.0 that only one of our servers supports. If you have a concrete scenario, file an issue with a concrete use case and we'll evaluate it.
26164d0
to
744be8c
Compare
c43c429
to
95f4608
Compare
🆙 📅 |
[Edit] nevermind |
- Expose FormOptions on DefaultHttpContext - Use those options on DefaultHttpContext when the FormFeature is initialized
95f4608
to
a2574fd
Compare
@Tratcher anymore feedback? It's actually much cleaner now. |
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.
Any measurable perf improvement?
This is a little hacky but works well for the 98.456% case.
cc @benaadams