Repository navigation
Conversation
#70161 avoided allocating HttpContext.Items in hosting RequestEnd by reading IItemsFeature from the feature collection directly. That bypassed DefaultHttpContext's cached feature reference, so requests that had already used Items paid for an uncached Features.Get on every lookup. Add an internal DefaultHttpContext.ItemsFeatureIfExists that returns the feature cached by Items when it is still current and otherwise reads it from the collection, without creating or adding the feature. Extend the hosting benchmark with Items unused/used/server-provided rows. Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The production friend-assembly dependency lets Hosting access Http internals across a prohibited assembly boundary and must be replaced with a supported contract.
1 open issue
What changed in this PR
This follow-up to #70161 reduces hosting diagnostics lookup overhead when HttpContext.Items is already populated, while preserving non-allocating lookups when it is unused.
Changes:
- Adds a revision-aware cached items-feature accessor and uses it from hosting diagnostics.
- Adds tests for existing, custom, replaced, removed, and reinitialized items features.
- Extends hosting benchmarks with three items-usage scenarios.
| File | Description |
|---|---|
| src/Http/Http/test/DefaultHttpContextTests.cs | Tests accessor behavior and feature lifecycle handling. |
| src/Http/Http/src/Microsoft.AspNetCore.Http.csproj | Grants Hosting access to Http internals. |
| src/Http/Http/src/DefaultHttpContext.cs | Adds the non-creating cached feature accessor. |
| src/Hosting/Hosting/test/HostingMetricsTests.cs | Tests metric tags and feature replacement/removal behavior. |
| src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs | Uses the cached accessor for items lookups. |
| src/Hosting/Hosting/perf/Microbenchmarks/HostingApplicationBenchmark.cs | Benchmarks unused, populated, and server-provided items. |
Review effort: Balanced
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <InternalsVisibleTo Include="Microsoft.AspNetCore.Hosting" /> |
There was a problem hiding this comment.
Agreed, the rule applies. .github/copilot-instructions.md ("Framework assembly boundaries", added in #68493) bans new production InternalsVisibleTo between framework assemblies and says existing uses aren't precedent. Those existing uses include Http.Abstractions → Http.Extensions/Http.Results (src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj), Authentication.Abstractions → Authentication.Core, and Kestrel.Core → Kestrel. They don't justify this edge.
I couldn't find a clean replacement that keeps the fix without public API:
- Shared-source internal interface: this doesn't work. Each assembly gets its own copy of the type, so
DefaultHttpContextcan't implement Hosting's copy. [UnsafeAccessor]: also banned by the same rule.- Public-only rewrite: I measured one
Features.Get<IItemsFeature>()perRequestEnd, with reads throughHttpContext.Itemsonce the feature exists. Results below. - Fetch the feature once and reuse it for both lookups: this never costs more than main, but it only removes the second
Geton unmatched requests. Matched endpoints with auth or CORS, the case raised in review, stay at main's cost.
The public-only rewrite, compared with main: mean ns, metrics enabled, 3 interleaved rounds, same benchmark as the PR description:
| Endpoint | Items | main | public-only | this PR (IVT)* |
|---|---|---|---|---|
| no | Used | 353 / 367 / 368 | 346 / 357 / 355 | 322 / 322 / 345 |
| yes | Used | 387 / 412 / 420 | 394 / 406 / 397 | 381 / 386 / 409 |
| no | server IItemsFeature |
244 / 248 / 261 | 256 / 265 / 251 | 249 / 241 / 270 |
| yes | server IItemsFeature |
291 / 310 / 311 | 320 / 331 / 312 | 311 / 298 / 327 |
| no / yes | Unused | 198–213 / 265–280 | 192–195 / 262–276 | 199–222 / 268–299 |
*From a separate 3-round run against main on the same machine.
The public-only rewrite gains only about 10 ns on the unmatched Used row. It is within noise on the matched Used row and 10–20 ns slower with a server-provided feature. In that case, HttpContext.Items misses DefaultHttpContext's empty cache and calls Get again.
So restoring the cached path for matched endpoints needs one of two things: this IVT, which the rule forbids, or a small public non-allocating lookup on DefaultHttpContext, which needs API review. Otherwise this PR shouldn't go in. I'm leaving the thread open for that decision and haven't pushed changes.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)
|
/review |
|
Closing this PR without merging.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |

Fixes the slowdown #70161 introduced for requests that already use
HttpContext.Items.Description
#70161 stopped hosting's
RequestEndfrom allocatingHttpContext.Itemsby readingIItemsFeaturestraight from the feature collection. That skipsDefaultHttpContext's cached feature reference, so requests whereItemswas already populated (authorization and CORS do this on matched endpoints) paid for an uncachedFeatures.Get<IItemsFeature>()per lookup, twice on unmatched requests. @halter73 asked about this case in his review, and the follow-up measurement showed it was 5–15 ns slower.This adds an internal
DefaultHttpContext.ItemsFeatureIfExists:Itemsis still current (same collection revision), it returns it. This is the same checkFeatureReferences.Fetchdoes.Features.Get<IItemsFeature>(), which is what main does today.Fetchwith a null factory isn't used because it callsIFeatureCollection.Set, which bumps Kestrel's revision.Hosting's
TryGetItemuses it forDefaultHttpContext.Microsoft.AspNetCore.Httpgets anInternalsVisibleToforMicrosoft.AspNetCore.Hosting. No public API or behavior changes. A replaced or removedIItemsFeatureis still picked up through the revision check.Benchmark
The existing
HostingApplicationBenchmarkgets anItemsparameter:Unused: nothing touchesItems.Used: middleware writesItems, like auth and CORS do.CustomFeature: the server provides its ownIItemsFeature, and the pipeline doesn't touch it.There are 3 interleaved rounds (order rotated each round). Each op is one full hosting request lifecycle. Mean ns, metrics enabled:
Get. The first-round PR values are the outliers."pre-#70161" is main with
HostingApplicationDiagnostics.csreverted to 3ef88ec's parent. All three versions use the same benchmark code.Machine: BenchmarkDotNet v0.13.0 (
--config perflab, in-process), AMD EPYC 9V74 (2 cores), Ubuntu 24.04 container, .NET SDK 12.0.100-alpha.1.26531.102, Server GC.Tests
HostingMetricsTestsadds 4 tests:IItemsFeatureIItemsFeaturereplaced afterItemswas used, which changes the revisionIItemsFeatureremoved after use, which must not be recreatedAll 4 pass on main and on this PR.
DefaultHttpContextTestsadds 5 tests forItemsFeatureIfExists:Microsoft.AspNetCore.Hosting.Tests: 479/479 pass.Microsoft.AspNetCore.Http.Tests: 287 pass, 7 skipped.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)