From ff02295bddb8c972e4f89be0c77f638920d58b7d Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 16 Aug 2026 15:31:19 +0100 Subject: [PATCH] Make LegacyPropagator list sort linear Replace O(n^2) behaviour with O(n) to reverse the list of keys parsed from the baggage header by `LegacyPropagator`, matching the implementation in `W3CPropagator`. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/System/Diagnostics/LegacyPropagator.cs | 6 ++++-- .../tests/PropagatorTests.cs | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LegacyPropagator.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LegacyPropagator.cs index 04a478fde94c40..c2d04607e43ab2 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LegacyPropagator.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LegacyPropagator.cs @@ -167,8 +167,7 @@ internal static bool TryExtractBaggage(string baggageString, out IEnumerable>(); - // Insert in reverse order for asp.net compatibility. - baggageList.Insert(0, new KeyValuePair( + baggageList.Add(new KeyValuePair( WebUtility.UrlDecode(baggageString.Substring(keyStart, keyEnd - keyStart)).Trim(s_trimmingSpaceCharacters), WebUtility.UrlDecode(baggageString.Substring(valueStart, currentIndex - valueStart)).Trim(s_trimmingSpaceCharacters))); } @@ -182,6 +181,9 @@ internal static bool TryExtractBaggage(string baggageString, out IEnumerable>() { new KeyValuePair(" LegacyKey1 ", " LegacyValue1 ") }); + new List>() { + new KeyValuePair(" LegacyKey1 ", " LegacyValue1 "), + new KeyValuePair("LegacyKey1b", "LegacyValue1b"), + new KeyValuePair("LegacyKey1c", "LegacyValue1c") }); TestLegacyPropagatorUsingHierarchicalActivity( DistributedContextPropagator.Current, "Legacy2=true", - new List>() { new KeyValuePair("LegacyKey2", "LegacyValue2") }); + new List>() { + new KeyValuePair("LegacyKey2", "LegacyValue2"), + new KeyValuePair("LegacyKey2b", "LegacyValue2b"), + new KeyValuePair("LegacyKey2c", "LegacyValue2c") }); TestFields(DistributedContextPropagator.Current);