Avoid allocations propagating Activity baggage - #132368
Open
martincostello wants to merge 1 commit into
Open
Conversation
Avoid allocations when enumerating baggage in internal code paths by using a struct-based enumerator. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces allocations in internal baggage-propagation code paths by introducing a struct-based baggage enumerator on Activity and updating propagators to consume it instead of allocating iterator-based enumeration.
Changes:
- Add an internal
Activity.EnumerateBaggage()struct enumerator to traverse baggage across an activity’s ancestor chain without iterator allocations. - Update W3C and legacy/ passthrough propagators to pass the
Activityand enumerate baggage via the new struct enumerator. - Update
Activity.GetBaggageItemto use the new enumeration path.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs | Switch baggage injection to use Activity.EnumerateBaggage() (struct enumerator) to avoid iterator allocations. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/PassThroughPropagator.cs | Thread root Activity through to baggage injection so it can enumerate baggage allocation-free. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LegacyPropagator.cs | Switch baggage injection to pass Activity and enumerate via the struct enumerator. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DistributedContextPropagator.cs | Update internal baggage injection helper to accept Activity? and use struct-based enumeration. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs | Introduce EnumerateBaggage() and BaggageEnumerator; update GetBaggageItem to use it. |
martincostello
marked this pull request as ready for review
August 16, 2026 12:35
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
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 when enumerating baggage in internal code paths by using a struct-based enumerator.
Public API surface is unchanged.
Benchmarks
Before
After