Skip to content

ctx.RequestAborted in a trace surfaces many Dictionary.Resize calls #31240

Closed
@NinoFloris

Description

@NinoFloris

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).

if (_features == null)
{
_features = new Dictionary<Type, object>();
}
_features[key] = value;

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Perfarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions