Repository navigation
Missed optimization; potential compiler regression involving StringBuilder.Append(IFormatProvider, $"") #136181
Replies: 3 comments 13 replies
|
When creating benchmarks, you need to be using BenchmarkDotNet, not raw |
|
Running correct benchmark with BenchmarkDotNet: [MemoryDiagnoser]
public class Program
{
static void Main(string[] args)
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.Run(args);
}
decimal value = (decimal)17.1994;
[Benchmark]
public StringBuilder Append1()
{
var sb = new StringBuilder();
sb.Append(CultureInfo.InvariantCulture, $"{value}");
return sb;
}
[Benchmark]
public StringBuilder Append2()
{
var sb = new StringBuilder();
sb.Append(value);
return sb;
}
}
It's clear that the interpolation handler allocates less memory. The allocated number contains the overhead of |
|
Okay; so I have come to the end of the search; it's a missed optimization in a .NET 7 compiler. I duped myself; because -c Debug and -c Release didn't matter on .NET 7 I never got around to trying it on .NET 10. And the only reason I was using NET 7 was for parity with LinqPad. In production we are using .NET 10. With release: (global.json doesn't matter; I tried it the first time without...) We're done here. I don't know why LinqPad is able to pick up optimizations from newer .NET versions than it builds against; but it does. And that sent me down a wrong path. |
Uh oh!
There was an error while loading. Please reload this page.
In looking at a system benchmark I ran into this troublesome result. This resists bisection and I can't account for it at all.
If I run this in LinqPad I get something that seems reasonable:
But if I run this with dotnet run I get much worse:
Dotnet version makes no meaningful difference
Something's gone very wrong with the actual release build; as far as I can tell LinqPad is doing it right and dotnet run is doing it wrong. My bisection skills failed me; it won't bisect to anything useful.
We got here on some debate on setting InvariantCulture vs the funky overload to StringBuilder.Append(). And now the benchmark results are wonky and it doesn't look like a problem in the benchmark code.
All reactions