Merged
Conversation
Member
|
How does this affect StringBuilder.AppendFormat performance? |
Despite the unintuitive behavior of TryCopyTo also calling Dispose, it’s only being used in a single place.
This was referenced Sep 12, 2025
Open
Contributor
Author
|
The benchmark results for
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Configs;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
[DisassemblyDiagnoser]
[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
public class StringBuilderTest
{
object[] formatArgs;
string formatSmall, formatLarge;
StringBuilder sb;
[GlobalSetup]
public void Setup()
{
sb = new StringBuilder();
formatSmall = "Text={0}, DateTime={1:G}, Number={2}, NumberX={2:X}, EnumD={3:D}, EnumF={3:F}";
formatLarge = formatSmall + ", " + string.Join(", ", Enumerable.Repeat("DateTime={1:yyyyMMdd-HH:mm:ss}", 100));
formatArgs = ["Text", new DateTime(2025, 4, 5, 6, 7, 5), long.MinValue, DayOfWeek.Friday];
StringBuilderLarge(); // allocation
}
[Benchmark] public string ValueStringBuilderSmall() => string.Format(CultureInfo.InvariantCulture, formatSmall, formatArgs);
[Benchmark]
public StringBuilder StringBuilderSmall()
{
sb.Clear();
return sb.AppendFormat(CultureInfo.InvariantCulture, formatSmall, formatArgs);
}
[Benchmark] public string ValueStringBuilderLarge() => string.Format(CultureInfo.InvariantCulture, formatLarge, formatArgs);
[Benchmark]
public StringBuilder StringBuilderLarge()
{
sb.Clear();
return sb.AppendFormat(CultureInfo.InvariantCulture, formatLarge, formatArgs);
}
} |
Member
|
@EgorBot -intel using BenchmarkDotNet.Attributes;
using System.Text;
public class Bench
{
StringBuilder sb = new StringBuilder(100);
[IterationSetup]
public void IterationSetup() => sb.Clear();
[Benchmark]
public void AppendFormatSimple() => sb.AppendFormat("{0}{1}{2}", 2, 3, 4);
} |
Member
|
30+% regression in the simple micro-benchmark for StringBuilder.AppendFormat: EgorBot/runtime-utils#484 (comment) |
Contributor
Author
|
@jkotas I reverted the changes related to AppendFormat. I’ll look into whether this can be addressed separately. |
jkotas
reviewed
Sep 15, 2025
src/libraries/Common/src/System/Text/ValueStringBuilder.EnsureTerminated.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/System/Text/ValueStringBuilder.EnsureTerminated.cs
Outdated
Show resolved
Hide resolved
This was referenced Sep 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extracted the logic into
EnsureTerminated()This allowed removing the overloads of
AsSpan(terminate)andGetPinnableReference(terminate).Unified the implementation ofAppendFormatwith that ofStringBuilder.In .NET 9, generics can use ref structs.This is reverted due to observed performance regression.
Remove ValueStringBuilder.TryCopyTo.
Even though TryCopyTo has the unintuitive behavior of also calling Dispose, it’s only used in a single place—Number.BigInteger.cs.