From 26137abbdf70a26b2ec65fa5cc76c6b88af4ffd7 Mon Sep 17 00:00:00 2001 From: martincostello Date: Fri, 25 Sep 2026 11:42:39 +0100 Subject: [PATCH 1/2] [perf] Cache validated W3C tracestate per-thread `ValidateTraceState` re-parses and rebuilds the `tracestate` value on every call, even when `Activity.TraceStateString` is typically set once for the lifetime of a span and its children. A single incoming request that fanned out to several outgoing calls would redo the same work work for each. Adds a last-(input,output)-pair cache to skip re-parsing when the value hasn't changed since the last call on the same thread. --- .../src/System/Diagnostics/W3CPropagator.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs index c02c575bbce010..ea44efdda4cf81 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs @@ -152,6 +152,15 @@ internal static bool TryExtractBaggage(string? baggageString, out IEnumerable Date: Fri, 25 Sep 2026 12:25:16 +0100 Subject: [PATCH 2/2] Address feedback Do not cache values larger than the maximum allowed length. --- .../src/System/Diagnostics/W3CPropagator.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs index ea44efdda4cf81..0b074539f1cd56 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/W3CPropagator.cs @@ -168,6 +168,15 @@ internal static bool TryExtractBaggage(string? baggageString, out IEnumerable MaxTraceStateEncodedLength) + { + return ValidateTraceStateCore(traceState); + } + if (ReferenceEquals(traceState, t_lastRawTraceState)) { return t_lastValidatedTraceState;