Skip to content

[Api] Optimize BaggagePropagator - #7059

Closed
martincostello wants to merge 1 commit into
open-telemetry:mainfrom
martincostello:optimize-BaggagePropagator
Closed

martincostello wants to merge 1 commit into
open-telemetry:mainfrom
martincostello:optimize-BaggagePropagator

Conversation

@martincostello

@martincostello martincostello commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

Changes

While looking at some profiles for an OTel instrumented application of mine, I noticed that BaggagePropagator.Extract() came up in the top 5 OTel-related samples.

This makes some improvements by:

  • Avoiding LINQ.
  • Use spans where possible.

Benchmark results

TL;DR:

  • The results with the refactoring are a clear improvement for Extract at ItemCount=5 and 20 (~14–32% faster and ~22–30% less allocated).
  • Inject is mostly stable to slightly better, except for ItemCount=1, UseSpecialChars=False - I'm not sure if that's an artifact of the benchmarks or an actual impact of the refactoring.
Expand to see

main + the new benchmarks


BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8117/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i7-13700H 2.90GHz, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.201
  [Host]     : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3


Method ItemCount UseSpecialChars Mean Error StdDev Median Gen0 Gen1 Allocated
Extract 1 False 110.84 ns 1.857 ns 1.737 ns 110.53 ns 0.0381 - 480 B
Inject 1 False 40.41 ns 0.770 ns 0.682 ns 40.45 ns 0.0121 - 152 B
Extract 1 True 190.19 ns 3.588 ns 3.685 ns 191.19 ns 0.0675 - 848 B
Inject 1 True 127.35 ns 1.693 ns 1.414 ns 127.34 ns 0.0458 - 576 B
Extract 5 False 395.27 ns 4.367 ns 3.410 ns 394.98 ns 0.1354 0.0005 1704 B
Inject 5 False 152.97 ns 15.618 ns 45.805 ns 128.16 ns 0.0389 - 488 B
Extract 5 True 812.22 ns 10.819 ns 9.591 ns 812.26 ns 0.3138 0.0019 3944 B
Inject 5 True 431.29 ns 5.359 ns 5.013 ns 430.02 ns 0.1535 - 1928 B
Extract 20 False 1,457.75 ns 11.920 ns 22.965 ns 1,459.33 ns 0.5417 0.0114 6800 B
Inject 20 False 432.86 ns 6.930 ns 6.482 ns 433.55 ns 0.1593 0.0005 2000 B
Extract 20 True 3,092.39 ns 60.032 ns 58.959 ns 3,087.80 ns 1.2589 0.0381 15840 B
Inject 20 True 1,527.28 ns 29.403 ns 39.253 ns 1,533.17 ns 0.5436 0.0038 6832 B

This PR


BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8117/25H2/2025Update/HudsonValley2)
13th Gen Intel Core i7-13700H 2.90GHz, 1 CPU, 20 logical and 14 physical cores
.NET SDK 10.0.201
  [Host]     : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.5 (10.0.5, 10.0.526.15411), X64 RyuJIT x86-64-v3


Method ItemCount UseSpecialChars Mean Error StdDev Median Gen0 Gen1 Allocated
Extract 1 False 140.79 ns 3.944 ns 11.251 ns 140.75 ns 0.0324 - 408 B
Inject 1 False 73.32 ns 4.222 ns 12.383 ns 77.57 ns 0.0121 - 152 B
Extract 1 True 149.19 ns 2.218 ns 1.966 ns 149.55 ns 0.0618 - 776 B
Inject 1 True 117.19 ns 1.700 ns 1.507 ns 116.82 ns 0.0459 - 576 B
Extract 5 False 267.31 ns 5.059 ns 4.485 ns 266.76 ns 0.0954 - 1200 B
Inject 5 False 122.32 ns 2.261 ns 2.005 ns 122.41 ns 0.0389 - 488 B
Extract 5 True 641.27 ns 9.382 ns 8.317 ns 640.24 ns 0.2422 0.0010 3040 B
Inject 5 True 406.21 ns 6.804 ns 6.364 ns 403.91 ns 0.1535 - 1928 B
Extract 20 False 1,018.62 ns 19.930 ns 23.725 ns 1,013.65 ns 0.3853 0.0076 4856 B
Inject 20 False 416.45 ns 7.795 ns 7.291 ns 417.78 ns 0.1593 0.0005 2000 B
Extract 20 True 2,645.72 ns 39.775 ns 37.206 ns 2,633.14 ns 0.9766 0.0229 12296 B
Inject 20 True 1,528.43 ns 7.347 ns 6.135 ns 1,528.92 ns 0.5436 0.0038 6832 B

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@github-actions github-actions Bot added pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package perf Performance related labels Apr 10, 2026
@codecov

codecov Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.36%. Comparing base (d5efe3a) to head (2af5684).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7059      +/-   ##
==========================================
- Coverage   88.41%   88.36%   -0.06%     
==========================================
  Files         270      270              
  Lines       12758    12811      +53     
==========================================
+ Hits        11280    11320      +40     
- Misses       1478     1491      +13     
Flag Coverage Δ
unittests-Project-Experimental 88.14% <100.00%> (-0.14%) ⬇️
unittests-Project-Stable 88.27% <100.00%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...metry.Api/Context/Propagation/BaggagePropagator.cs 87.80% <100.00%> (+1.65%) ⬆️

... and 4 files with indirect coverage changes

@martincostello
martincostello marked this pull request as ready for review April 10, 2026 22:35
@martincostello
martincostello requested a review from a team as a code owner April 10, 2026 22:35
Copilot AI review requested due to automatic review settings April 10, 2026 22:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes OpenTelemetry.Context.Propagation.BaggagePropagator—specifically the Extract() path—by reducing allocations and avoiding LINQ, and adds benchmarks to measure the impact.

Changes:

  • Refactored BaggagePropagator.Extract() / TryExtractBaggage() to avoid LINQ/array materialization and to use span-based parsing on .NET targets.
  • Added new BaggagePropagator benchmarks to track Extract/Inject performance across item counts and encoding scenarios.
  • Minor benchmark cleanup in TraceContextPropagatorBenchmarks by switching to a static expression-bodied getter.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
test/Benchmarks/Context/Propagation/TraceContextPropagatorBenchmarks.cs Simplifies header getter to a static expression-bodied delegate (avoids capturing/extra code).
test/Benchmarks/Context/Propagation/BaggagePropagatorBenchmarks.cs Adds benchmarks for BaggagePropagator.Extract() and Inject() with configurable sizes/encoding.
src/OpenTelemetry.Api/Context/Propagation/BaggagePropagator.cs Removes LINQ/materialization in Extract and implements span-based parsing in TryExtractBaggage for .NET builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@martincostello martincostello changed the title [OpenTelemetry] Optimize BaggagePropagator [Api] Optimize BaggagePropagator Apr 11, 2026
@martincostello martincostello mentioned this pull request Apr 12, 2026
1 of 4 tasks
- Avoid LINQ.
- Use spans where possible.
@martincostello
martincostello force-pushed the optimize-BaggagePropagator branch from 9d2892f to 2af5684 Compare April 13, 2026 09:21
@martincostello

Copy link
Copy Markdown
Member Author

Folded into #7061.

@martincostello
martincostello deleted the optimize-BaggagePropagator branch April 13, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance related pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants