Description
Feature collection initializes the inner dictionary without a capacity, after the first insert this would mean it grows to 3 (due to prime based capacity growth).
aspnetcore/src/Http/Http.Features/src/FeatureCollection.cs
Lines 74 to 78 in 1c8da41
What we're seeing in traces is that we have a lot of pointless resizing as the framework and users use certain features on every request leaving no capacity to grow without resizing, for us this is:
- HttpRequestIdentifierFeature (when scopes are enabled this gets logged by the framework)
- RequestServicesFeature (we set our own on every request due to multi-tenancy, but framework would too either way)
- ItemsFeature (there is some state passing here)
Which means we're already at 3 without really doing anything special.
Now when we access ctx.RequestAborted, which we do only on certain requests, this triggers a resize to fit the HttpRequestLifetimeFeature. (this could just as easily have happened by doing auth, which we do on other requests, or accessing session or some other _features
backed HttpContext properties)
If we can agree on the initial set not doing anything particularly uncommon, would it then make sense to initialize the dictionary with a capacity slightly larger than 3? For Dictionary this would mean it would resolve to capacity 7 which I believe is much more comfortable for most aspnet core uses.