Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions TUnit.Core/Services/TestNameFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Text;
using TUnit.Core.Helpers;
using TUnit.Core.Interfaces;

namespace TUnit.Core.Services;
Expand Down Expand Up @@ -68,11 +69,23 @@ public string BuildTestId(
int classDataIndex = 0,
int methodDataIndex = 0)
{
return template
.Replace("{TestIndex}", testIndex.ToString())
.Replace("{RepeatIndex}", repeatIndex.ToString())
.Replace("{ClassDataIndex}", classDataIndex.ToString())
.Replace("{MethodDataIndex}", methodDataIndex.ToString());
// Mutate a pooled StringBuilder in place then materialize once, instead of
// allocating a new string per Replace call. Only the final string allocates.
var builder = StringBuilderPool.Get();
try
{
return builder
.Append(template)
.Replace("{TestIndex}", testIndex.ToString())
.Replace("{RepeatIndex}", repeatIndex.ToString())
.Replace("{ClassDataIndex}", classDataIndex.ToString())
.Replace("{MethodDataIndex}", methodDataIndex.ToString())
.ToString();
}
finally
{
StringBuilderPool.Return(builder);
}
}

private string FormatArguments(object?[] args)
Expand All @@ -87,20 +100,30 @@ private string FormatArguments(object?[] args)

private string FormatEnumerable(IEnumerable enumerable)
{
var sb = new StringBuilder("[");
var first = true;

foreach (var item in enumerable)
// Pool the builder like BuildTestId. Reentrant-safe: a nested enumerable draws a
// distinct instance from the pool, and each Get is balanced by a Return.
var sb = StringBuilderPool.Get();
try
{
if (!first)
sb.Append('[');
var first = true;

foreach (var item in enumerable)
{
sb.Append(", ");
if (!first)
{
sb.Append(", ");
}
first = false;
sb.Append(FormatArgumentValue(item));
}
first = false;
sb.Append(FormatArgumentValue(item));
}

sb.Append(']');
return sb.ToString();
sb.Append(']');
return sb.ToString();
}
finally
{
StringBuilderPool.Return(sb);
}
}
}
Loading