Skip to content

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

Merged
merged 2 commits into from
Jan 9, 2019
Merged

Conversation

davidfowl
Copy link
Member

  • Expose FormOptions on DefaultHttpContext
  • Use those options on DefaultHttpContext when the FormFeature is initialized

This is a little hacky but works well for the 98.456% case.

cc @benaadams

@davidfowl davidfowl requested review from halter73 and Tratcher January 9, 2019 02:43
@@ -6,6 +6,6 @@ namespace Microsoft.AspNetCore.Http
{
public interface IHttpContextContainer
{
HttpContext HttpContext { get; }
DefaultHttpContext HttpContext { get; }
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

@benaadams benaadams Jan 9, 2019

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)

Copy link
Member Author

@davidfowl davidfowl Jan 9, 2019

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.

Copy link
Member Author

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
    };
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm asking whether creating a factory is enough to get full equivalence

I don't even know what you're asking for? equivalence of what?

why is HttpProtocol specifically implementing IDefaultHttpContextContainer

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.

Copy link
Member Author

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.

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.

Copy link
Member Author

@davidfowl davidfowl Feb 4, 2019

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.

@davidfowl davidfowl force-pushed the davidfowl/faster-httpcontext branch 2 times, most recently from 26164d0 to 744be8c Compare January 9, 2019 17:58
@davidfowl davidfowl force-pushed the davidfowl/no-form-feature branch from c43c429 to 95f4608 Compare January 9, 2019 18:35
@davidfowl
Copy link
Member Author

🆙 📅

@Tratcher
Copy link
Member

Tratcher commented Jan 9, 2019

[Edit] nevermind

- Expose FormOptions on DefaultHttpContext
- Use those options on DefaultHttpContext when the FormFeature is initialized
@davidfowl davidfowl force-pushed the davidfowl/no-form-feature branch from 95f4608 to a2574fd Compare January 9, 2019 20:09
@davidfowl davidfowl changed the base branch from davidfowl/faster-httpcontext to master January 9, 2019 20:25
@davidfowl
Copy link
Member Author

@Tratcher anymore feedback? It's actually much cleaner now.

Copy link
Member

@Tratcher Tratcher left a 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?

@davidfowl
Copy link
Member Author

Allocates a bit less 😄 . 48% strings now!

image

Old:
1,925,886

New:
1,930,479

@davidfowl davidfowl merged commit ea344bf into master Jan 9, 2019
@natemcmaster natemcmaster deleted the davidfowl/no-form-feature branch January 18, 2019 19:17
@davidfowl davidfowl added the breaking-change This issue / pr will introduce a breaking change, when resolved / merged. label Feb 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This issue / pr will introduce a breaking change, when resolved / merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants