Skip to content

Commit 9b090d5

Browse files
andrewlockclaude
andcommitted
Use ValueStringBuilder in AspNetCoreResourceNameHelper on all TFMs
Replace StringBuilderCache fallback with ValueStringBuilder and stackalloc on all TFMs. AppendAsLowerInvariant lowercases each segment inline, avoiding the extra string allocation from the previous .ToLowerInvariant() call on the final result. Add a string? overload to ValueStringBuilder.AppendAsLowerInvariant to avoid needing explicit .AsSpan() at each call site. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5812d47 commit 9b090d5

2 files changed

Lines changed: 9 additions & 36 deletions

File tree

tracer/src/Datadog.Trace/DiagnosticListeners/AspNetCoreResourceNameHelper.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Collections.Generic;
1212
using System.Diagnostics.CodeAnalysis;
1313
using System.Runtime.CompilerServices;
14-
using System.Text;
1514
using Datadog.Trace.DuckTyping;
1615
using Datadog.Trace.SourceGenerators;
1716
using Datadog.Trace.Util;
@@ -128,14 +127,9 @@ internal static string SimplifyRoutePattern(
128127
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName!.Length - 6, 0)) // "action".Length
129128
+ 1; // '/' prefix
130129

131-
#if NETCOREAPP
132130
var sb = maxSize < 512
133131
? new ValueStringBuilder(stackalloc char[512])
134132
: new ValueStringBuilder(); // too big to use stackallocation, so use array builder
135-
#else
136-
// In .NET Core 2.1, the ValueStringBuilder doesn't actually improve anything
137-
var sb = StringBuilderCache.Acquire(maxSize);
138-
#endif
139133

140134
foreach (var pathSegment in routePattern.PathSegments)
141135
{
@@ -227,23 +221,13 @@ internal static string SimplifyRoutePattern(
227221
}
228222
}
229223

230-
#if NETCOREAPP
231224
if (sb.Length <= 1)
232225
{
233226
sb.Dispose();
234227
return "/";
235228
}
236229

237230
return sb.ToString();
238-
#else
239-
if (sb.Length <= 1)
240-
{
241-
StringBuilderCache.Release(sb);
242-
return "/";
243-
}
244-
245-
return StringBuilderCache.GetStringAndRelease(sb).ToLowerInvariant();
246-
#endif
247231
}
248232

249233
internal static string SimplifyRouteTemplate(
@@ -261,14 +245,9 @@ internal static string SimplifyRouteTemplate(
261245
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName!.Length - 6, 0)) // "action".Length
262246
+ 1; // '/' prefix
263247

264-
#if NETCOREAPP
265248
var sb = maxSize < 512
266249
? new ValueStringBuilder(stackalloc char[512])
267250
: new ValueStringBuilder(); // too big to use stackallocation, so use array builder
268-
#else
269-
// In .NET Core 2.1, the ValueStringBuilder doesn't actually improve anything
270-
var sb = StringBuilderCache.Acquire(maxSize);
271-
#endif
272251

273252
// Remove the boxing of the enumerator
274253
// In all versions of .NET, this is implemented as a List<TemplateSegment>
@@ -367,23 +346,13 @@ internal static string SimplifyRouteTemplate(
367346
}
368347

369348
// We never added anything, or we just added the first `/`, no need for explicit ToString()
370-
#if NETCOREAPP
371349
if (sb.Length <= 1)
372350
{
373351
sb.Dispose();
374352
return "/";
375353
}
376354

377355
return sb.ToString();
378-
#else
379-
if (sb.Length <= 1)
380-
{
381-
StringBuilderCache.Release(sb);
382-
return "/";
383-
}
384-
385-
return StringBuilderCache.GetStringAndRelease(sb).ToLowerInvariant();
386-
#endif
387356
}
388357

389358
[TestingAndPrivateOnly]
@@ -412,10 +381,5 @@ internal static bool IsIdentifierSegment(object? value, out string? valueAsStrin
412381

413382
return UriHelpers.IsIdentifierSegment(valueAsString, 0, valueAsString.Length);
414383
}
415-
416-
#if !NETCOREAPP
417-
// .NET Core 2.1 helper which doesn't _actually_ append as lower invariant, and just does it all at the end instead
418-
private static void AppendAsLowerInvariant(this StringBuilder sb, string? value) => sb.Append(value);
419-
#endif
420384
}
421385
#endif

tracer/src/Datadog.Trace/Util/ValueStringBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ public Span<char> AppendSpan(int length)
232232
return _chars.Slice(origPos, length);
233233
}
234234

235+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
236+
public void AppendAsLowerInvariant(string? value)
237+
{
238+
if (value is not null)
239+
{
240+
AppendAsLowerInvariant(value.AsSpan());
241+
}
242+
}
243+
235244
[MethodImpl(MethodImplOptions.AggressiveInlining)]
236245
public void AppendAsLowerInvariant(scoped ReadOnlySpan<char> value)
237246
{

0 commit comments

Comments
 (0)