Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Remove FeatureReference indirection #420

Merged
merged 1 commit into from
Oct 15, 2015
Merged

Conversation

benaadams
Copy link
Contributor

Is more code Is greatly improved in recent incarnation.

Doesn't cause any extra casting; shouldn't be extra instructions executed; cuts CreateHttpContext allocation by 17.5%

From 1824 kB per 4k requests

logging-scope

To 1504 kB per 4k requests

logging-scope-prev

So down by 320 kB per 4k requests; as similar saving to the AsyncLocal change in coreclr

@dnfclas
Copy link

dnfclas commented Sep 27, 2015

Hi @benaadams, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by .NET Foundation and real humans are currently evaluating your PR.

TTYL, DNFBOT;

@benaadams benaadams changed the title Remove FeatureReference indirection Proposal: Remove FeatureReference indirection Sep 27, 2015
@davidfowl
Copy link
Member

@lodejard please review.

@davidfowl davidfowl added this to the 1.0.0-rc1 milestone Sep 30, 2015
@lodejard
Copy link
Contributor

This is removing a few ints, doesn't reduce the number of objects allocated, and has much more manual code in several objects that reference features. I don't think it's worth the change...

@benaadams
Copy link
Contributor Author

Will come back with some more analysis; but agreed its a lot more messy

@benaadams
Copy link
Contributor Author

Its down by 80 bytes a request which if it were just ints (4 bytes) would be 20 ints; which its clearly not; so not entirely sure what's happening.

Perhaps on 64bit the structs (IntPtr 8 bytes + Int 4 bytes) 8 byte align with a 4 byte padding; doubling the effect? Still seems high though.

@benaadams
Copy link
Contributor Author

Ok, is a memory layout thing, exacerbated on x64, with the 4 memory layouts for DefaultHttpContext looking like the following

memory layout

As the elements can't be rearranged out of the struct for tighter packing and the pointers need to start on their boundry

@lodejard
Copy link
Contributor

lodejard commented Oct 7, 2015

Hrmmm - yeah, that does seem like a really unfortunate bloating and a reasonable way to avoid it. Plus it's living in per-request memory pressure. Thanks for digging into it deeper!

And if we want to keep up the manually-maintained programming pattern under control down the road we can use the Xxx.Generated.cs partial class trick for the cached-feature code paths. Don't need to do that now as part of this PR, but it's good to keep in mind if the extra code is considered unfortunate.

:shipit:

Last thought... We might want to keep the FeatureReference class around, if middleware ever have per-request contexts that query for particular features at multiple points in time. The FR might still be a good halfway point for those cases.

@Tratcher
Copy link
Member

Tratcher commented Oct 7, 2015

Is there at least some way to share this bookkeeping code? This looks like it will cause maintenance problems.

@benaadams
Copy link
Contributor Author

Is there at least some way to share this bookkeeping code?

Could probably do something with extensions and ref params; don't know what the feel on ref is though? Haven't seem much of it about.

@Tratcher
Copy link
Member

Tratcher commented Oct 7, 2015

Sample?

@benaadams
Copy link
Contributor Author

Would have to be static methods, rather than extensions as there are nulls involved; and also a couple variants as some have different paths, but something like (updated PR with new code)

@benaadams
Copy link
Contributor Author

@Tratcher Something more like the PR now? I'm much happier with this design than the earlier one.

@benaadams benaadams changed the title Proposal: Remove FeatureReference indirection Remove FeatureReference indirection Oct 8, 2015
@benaadams benaadams force-pushed the Features-perf branch 2 times, most recently from 25dfcad to fdb3d54 Compare October 8, 2015 12:25
@Tratcher
Copy link
Member

Tratcher commented Oct 8, 2015

Better. Really what you've done is turn FeatureReference from a struct to a static helper class. One more comment inline.

I think Lou still wants to save FeatureReference for use elsewhere.

return FeatureHelpers.GetOrCreateAndCache(
this,
_features,
() => new HttpAuthenticationFeature(),
Copy link
Member

Choose a reason for hiding this comment

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

Does this lamda bother you? Or are you relying on it getting optimized into a static field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be a compile time const I believe? Was being careful not to create any closures which would then cause runtime per-call creation.

Copy link
Member

Choose a reason for hiding this comment

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

It's worth verifying.

Copy link
Member

Choose a reason for hiding this comment

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

It will be optimized. If we want to be more anal, we can declare static delegates.

@benaadams
Copy link
Contributor Author

Really what you've done is turn FeatureReference from a struct to a static helper class.

Pretty much, took a few iterations to get there though :)

@davidfowl
Copy link
Member

This looks like a good change and is much simpler than previous incantations 😄

@@ -36,6 +36,8 @@ public abstract class HttpContext : IDisposable

public abstract ISession Session { get; set; }

public abstract string TraceIdentifier { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

API change. I actually think this should be exposed but this change just went from being an optimization to an API change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to back peddle; but want to put a PR on a PR and my git fu is not strong enough... 😭

Copy link
Member

Choose a reason for hiding this comment

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

Revert the last commit and force push

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted; have branched other changes to different repo

benaadams added a commit to benaadams/HttpAbstractions that referenced this pull request Oct 12, 2015
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNet.Http.Features
Copy link
Member

Choose a reason for hiding this comment

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

@benaadams Bring this type back then I'll be ready to merge this PR. Also squash the commits.

Use FeatureCacheHelpers static methods rather than struct FeatureReference by default for lower allocation
@benaadams
Copy link
Contributor Author

Added FeatureReference back and squashed commits

@davidfowl
Copy link
Member

LGTM

davidfowl added a commit that referenced this pull request Oct 15, 2015
Remove FeatureReference indirection
@davidfowl davidfowl merged commit d565659 into aspnet:dev Oct 15, 2015
@benaadams benaadams deleted the Features-perf branch October 15, 2015 11:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants