[Hosting] Avoid allocations for OpenTelemetry#67440
Open
martincostello wants to merge 2 commits into
Open
Conversation
- Pre-box common/expected `server.port` values. - Cache activity display names for endpoints.
Contributor
|
Thanks for your PR, @martincostello. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Contributor
There was a problem hiding this comment.
Pull request overview
Reduces per-request allocations in the Hosting OpenTelemetry path by reusing boxed server.port values and caching Activity display names derived from HTTP method + route.
Changes:
- Reuse boxed instances for common ports and add a configurable pre-boxing mechanism for server-bound ports.
- Cache
Activity.DisplayNamestrings for(method, route)to avoid per-request string construction. - Register/unregister pre-boxed server ports during
GenericWebHostServicestart/stop to avoid cross-host accumulation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Hosting/Hosting/src/Internal/HostingTelemetryHelpers.cs | Adds boxed-port reuse + registration APIs and introduces a display name cache for method/route pairs. |
| src/Hosting/Hosting/src/GenericHost/GenericWebHostService.cs | Collects bound/configured ports on startup and registers them for boxing reuse; unregisters on stop. |
- Use a `ConditionalWeakTable` to avoid keeping the display strings around forever. - Update comment.
landsharkiest
approved these changes
Jun 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Avoid allocations for OpenTelemetry
Reduce allocations for traces generated for HTTP requests.
Description
I was investigating why I wasn't getting the improvements I would expect with ASP.NET Core 11 preview.5 and OpenTelemetry.Instrumentation.AspNetCore 1.16.0 when the
Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryDataAppContext switch is set tofalsewith Claude, which identified a number of possible ways to avoid allocations when OpenTelemetry is being generated.The changes in this PR are essentially all the possible ways to avoid some of the allocations, whether they are worth doing and/or should be done in the ways suggested are for the team to decide.
The display name cache is probably the least controversial. The port boxing one could potentially be slimmed down to not care about 5000/5001/8080 and user-configured ports, and just keep the last-seen value for anything not 80 or 443, or just drop that part entirely.
Specific changes:
server.portvalues.I've proposed some similar changes for the OTel instrumentation for ASP.NET Core itself as well: open-telemetry/opentelemetry-dotnet-contrib#4600