-
Notifications
You must be signed in to change notification settings - Fork 307
[Perf] HttpContextFactory should only wrap ReadOnly feature collections #310
Comments
Nevermind, FeatureCollection has made significant improvements in this scenario. |
I did have a look at Taking a step back there are FeatureCollection improvements coming in Kestrel; which regardless of metrics, have the distinct allocation advantage of already being created, and this wrapping would prevent it being utilised passed the point the context is created. However, I don't have the holistic view on what circumstances; if any, return new DefaultHttpContext(new FeatureCollection(featureCollection)); to what @Tratcher originally suggested (in preferred line break formatting) return new DefaultHttpContext(
featureCollection.IsReadOnly
? new FeatureCollection(featureCollection)
: featureCollection); which would work and allow the changes in Kestrel's collection to flow through. So that would be an easy fix? Then open question on when would the |
Allowing the IFeatureCollection to be read-only can make for simpler server implementations. FeatureCollection makes up the difference. Or we cut that and just ask the simple servers to use FeatureCollection directly. |
Are there any existing implementations? Doing an if check/ternary operator here is about as lightweight as you can get, so if you want to support that scenario I don't think there is any disadvantage. If there aren't any existing implementations, pushing out the readonly might make sense as its sort of a hidden intent coercion (e.g. "I made this readonly; with So is definitely a design call. Though I'd favour changing it to wrap conditionally, this instant as you've suggested; as it would be more performant and otherwise will cause issues with Kestrel's new implementation - then pick up readonly in an internal discussion? (even if its only 5 secs 😝) However... as I said at the start; the |
In fact just made a PR for it... |
I think we should keep IFeatureCollection and do the optimization. |
@davidfowl wasn't suggesting removing |
I was suggesting that Hosting doesn't need to wrap at all since it's trivial for servers to generate a mutable feature collection. See |
Yes, we can just make that part of the contract. |
https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/Builder/HttpContextFactory.cs#L14
IFeatureCollections can be marked as IsReadOnly or not. HttpContextFactory currently assumes they are all read only and wraps them in a mutable FeatureCollection. This has some perf overhead where it creates extra Dictionaries, etc, and is redundant if the server already provided a mutable collection.
Recommendation: Only wrap the given feature collection if is marked as IsReadOnly.
The text was updated successfully, but these errors were encountered: