Repository navigation
Description
With the latest preview 3 (net9) there seems to be a major performance regression on MacOS 14.4 (M2 Pro) with lists. Given that they are one of the most used types, it should receive special treatment.
Benchmark
[Benchmark]
public List<int> ListAdd10000()
{
var list = new List<int>();
for (var i = 0; i < 10_000; i++)
{
list.Add(i);
}
return list;
}
[Benchmark]
public List<int> ListAdd10000PreAlloc()
{
var list = new List<int>(10_000);
for (var i = 0; i < 10_000; i++)
{
list.Add(i);
}
return list;
}Results:
BenchmarkDotNet v0.13.12, macOS Sonoma 14.4.1 (23E224) [Darwin 23.4.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 9.0.100-preview.3.24204.13
[Host] : .NET 9.0.0 (9.0.24.17209), Arm64 RyuJIT AdvSIMD
.NET 8.0 : .NET 8.0.2 (8.0.224.6711), Arm64 RyuJIT AdvSIMD
.NET 9.0 : .NET 9.0.0 (9.0.24.17209), Arm64 RyuJIT AdvSIMD
| Method | Runtime | Mean | Error | StdDev | Ratio |
|--------------------- |--------- |----------:|----------:|----------:|------:|-
| ListAdd10000 | .NET 8.0 | 9.739 us | 0.0236 us | 0.0209 us | 1.00 |
| ListAdd10000 | .NET 9.0 | 25.646 us | 0.1531 us | 0.1432 us | 2.63 |
| | | | | | |
| ListAdd10000PreAlloc | .NET 8.0 | 6.701 us | 0.0175 us | 0.0146 us | 1.00 |
| ListAdd10000PreAlloc | .NET 9.0 | 22.562 us | 0.1429 us | 0.1336 us | 3.37 |
Interestingly even the pre-allocated list is comparably slow.
dotnet --info
Output
dotnet --info .NET SDK: Version: 9.0.100-preview.3.24204.13 Commit: 81f61d8290 Workload version: 9.0.100-manifests.77bb7ba9 MSBuild version: 17.11.0-preview-24178-16+7ca3c98faRuntime Environment:
OS Name: Mac OS X
OS Version: 14.4
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/9.0.100-preview.3.24204.13/
.NET workloads installed:
There are no installed workloads to display.
Host:
Version: 9.0.0-preview.3.24172.9
Architecture: arm64
Commit: 9e6ba1f
.NET SDKs installed:
6.0.417 [/usr/local/share/dotnet/sdk]
7.0.306 [/usr/local/share/dotnet/sdk]
7.0.404 [/usr/local/share/dotnet/sdk]
8.0.100 [/usr/local/share/dotnet/sdk]
8.0.101 [/usr/local/share/dotnet/sdk]
8.0.200 [/usr/local/share/dotnet/sdk]
9.0.100-preview.2.24121.2 [/usr/local/share/dotnet/sdk]
9.0.100-preview.2.24157.14 [/usr/local/share/dotnet/sdk]
9.0.100-preview.3.24204.13 [/usr/local/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.25 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-preview.2.24120.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-preview.2.24128.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-preview.3.24172.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.25 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-preview.2.24120.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-preview.2.24128.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-preview.3.24172.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
x64 [/usr/local/share/dotnet/x64]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
Activity
dotnet-policy-service commentedon Apr 23, 2024
Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.
EgorBo commentedon Apr 23, 2024
Doesn't repro for me on x64-windows:
don't have an macos-arm64 machine to test
vcsjones commentedon Apr 23, 2024
@EgorBo I can repro on my M1.
BenchmarkDotNet v0.13.12, macOS Sonoma 14.4.1 (23E224) [Darwin 23.4.0]
Apple M1 Ultra, 1 CPU, 20 logical and 20 physical cores
.NET SDK 9.0.100-preview.3.24204.13
[Host] : .NET 8.0.3 (8.0.324.11423), Arm64 RyuJIT AdvSIMD
Job-OISOJI : .NET 8.0.3 (8.0.324.11423), Arm64 RyuJIT AdvSIMD
Job-YEXJFM : .NET 9.0.0 (9.0.24.17209), Arm64 RyuJIT AdvSIMD
linkdotnet commentedon Apr 23, 2024
The same applies to
9.0.100-preview.2.24157.14as well - so the regression might be sincepreview.1orpreview.2.EgorBo commentedon Apr 23, 2024
Interesting! Reproduces for me on M2 Max as well (just found it 🙂):
tannergooding commentedon Apr 23, 2024
Didn't we change Arm64 to start using the newer Arm64 barrier intstructions for the GC if they existed around that point?
IIRC, @kunalspathak did some of that work.
tannergooding commentedon Apr 23, 2024
Not finding the barrier PR I was remembering, but there was #97953 too
EgorBo commentedon Apr 23, 2024
Doesn't repro on Linux-arm64
EgorBo commentedon Apr 23, 2024
codegen diff: https://www.diffchecker.com/6tDgYQ8w/ - the only difference is
ldpon net9 vs 2ldron net8. Hard to imagine it being a culprit 🤔tannergooding commentedon Apr 23, 2024
Could be something more subtle has changed and is impacting the general measurements or iteration count?
ldrandldpare both supposed to be 3 cycle, 3 latency. But alternatively perhaps there's some weird data dependency or other issue here causing the delay?I don't think Apple or Arm64 in general has anything like Intel VTune or AMD uProf, so seeing where the stalls are happening isn't as easy, unfortunately.
40 remaining items
linkdotnet commentedon Apr 28, 2024
Don’t get me wrong - one can still lead this discussion but I do feel it should be handled separately (and not only because of performance characteristics)
EgorBo commentedon Apr 28, 2024
Minimal repro for the Apple issue using Clang and inline asm: https://gist.github.com/EgorBo/88196a218559ec93a197a7d1d5600548
ldpmakes this "benchmark" ~4-5x slower. An interesting thing is that if we haveldpandstp- it's still slow (we thought that the problem reproduces only when we mixstrwithldpandstpwithldpwould be fine)mqudsi commentedon Apr 29, 2024
Wouldn't just moving the
_versionwrite as @EgorBo demonstrated just be the least controversial change/fix here (fixes this particular performance regression, avoids arguing about the bigger questions involved with removing version checks altogether, keeps version checks in place even forList.Add())?The performance regressions can be tackled by monitoring macOS benchmark results (the point about Apple Silicon != macOS is well taken, but also somewhat besides the point since Linux (OS) performance is tracked separately and monitoring macOS (OS + hw) performance would have caught this for both newer Macs (OS + hw) and Linux-on-Apple-silicon — you do not need "idealized" performance monitoring where only one factor changes between each monitored configuration) and figuring out where other notable regressions occur?
You probably don't want to blanket disable ldr to ldp optimizations, as this was a pathological case where there was a false data dependency on two adjacent fields but there likely are cases where compiler optimizations can take advantage of the single copy atomicity improvements to net performance gains.
(But I must confess I am confused why this would problem would not manifest on non-Apple silicon aarch64 w/ FEAT_LSE2 present, as reported earlier in this thread, unless the cpu wasn't taking advantage of the available instruction-level parallelism before so there is no regression now?)
Developer-Ecosystem-Engineering commentedon Apr 29, 2024
We are reviewing this issue thanks for raising it!
Developer-Ecosystem-Engineering commentedon May 10, 2024
The use of LDP instructions is generally encouraged. In this specific case, the structure of the loop (that includes a memory dependence between stores and loads), coupled with the use of LDP instructions prevents certain hardware optimizations from engaging. The small and tight nature of the loop also exacerbates the impact.
Please also see section 4.6.11 of the Apple Silicon CPU optimization guide.
JIT: Check for potential store-to-load forwarding before reordering l…
JIT: Check for potential store-to-load forwarding before reordering l…