Skip to content

stackalloc long[3] is slower than [0,0,0] #121248

Closed

Description

@EgorBo

Noticed while working on #121225

Here is the minimal repro:

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

public class Benchmarks
{
    [Benchmark]
    public long Bench_stackalloc() => ParseNonCanonical_stackalloc("11");

    [Benchmark]
    public long Bench_InlineArray() => ParseNonCanonical_InlineArray("11");


    [MethodImpl(MethodImplOptions.NoInlining)]
    int ParseNonCanonical_stackalloc(ReadOnlySpan<char> name)
    {
        Span<long> parts = stackalloc long[3];
        Consume(parts);
        return name[1];
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    int ParseNonCanonical_InlineArray(ReadOnlySpan<char> name)
    {
        Span<long> parts = [0, 0, 0];
        Consume(parts);
        return name[1];
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Span<long> parts) { }
}

Benchmarks results on Linux-x64:

| Method            | Mean     | Error     | StdDev    |
|------------------ |---------:|----------:|----------:|
| Bench_stackalloc  | 6.967 ns | 0.1560 ns | 0.2135 ns |
| Bench_InlineArray | 1.608 ns | 0.0043 ns | 0.0034 ns |

Presumably, the perf penalty comes from Store Forwarding:

       vmovdqu  xmm0, xmmword ptr [rsp+0x30]
       vmovdqu  xmmword ptr [rsp+0x20], xmm0

I haven't looked into JitDump yet to tell why.

Activity

  1. added
    untriaged
    on Oct 31, 2025
  2. added
    area-CodeGen-coreclr
    on Oct 31, 2025
  3. dotnet-policy-service commented on Oct 31, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. EgorBo commented on Oct 31, 2025

    @EgorBo
    MemberAuthor

    cc @dotnet/jit-contrib

  5. gfoidl commented on Nov 1, 2025

    @gfoidl
    Member

    Presumably, the perf penalty comes from Store Forwarding...

    Another difference is that stack cookie that isn't emitted for the InlineArray variant (cf. godbolt).
    So another memory read / write.

  6. EgorBo commented on Nov 1, 2025

    @EgorBo
    MemberAuthor

    Presumably, the perf penalty comes from Store Forwarding...

    Another difference is that stack cookie that isn't emitted for the InlineArray variant

    Right, but I already checked that it doesn't play the key part here (if I add it for InlineArray it doesn't change the results)

  7. AndyAyersMS commented on Nov 7, 2025

    @AndyAyersMS
    Member

    llvm-mca analysis of a snipped from the slow case:

    Timeline view:
                        0
    Index     0123456789
    
    [0,0]     DR   .    .   xor     eax, eax
    [0,1]     DeER .    .   mov     qword ptr [rbp - 0x60], rax
    [0,2]     D=eER.    .   mov     qword ptr [rbp - 0x28], 0x17a37d
    [0,3]     D==eER    .   mov     qword ptr [rbp - 0x50], rdi
    [0,4]     D===eER   .   mov     qword ptr [rbp - 0x48], rsi
    [0,5]     DeeeeeeER .   vmovdqu xmm0, xmmword ptr [rbp - 0x50]
    [0,6]     .D=====eER.   vmovdqu xmmword ptr [rbp - 0x60], xmm0
    [0,7]     .DeeeeeE-R.   mov     rax, qword ptr [rbp - 0x60]
    [0,8]     .D=eeeeeER.   mov     r9d, dword ptr [rbp - 0x58]
    [0,9]     .D======eER   mov     dword ptr [rdx], r9d
    

    After further review llvm-mca does not model store forwarding, so it's analysis here is a pure latency guess.

  8. AndyAyersMS commented on Nov 9, 2025

    @AndyAyersMS
    Member

    Further profiling strongly implicates store forwarding, the issue being here

    [0,3]     D==eER    .   mov     qword ptr [rbp - 0x50], rdi
    [0,4]     D===eER   .   mov     qword ptr [rbp - 0x48], rsi
    [0,5]     DeeeeeeER .   vmovdqu xmm0, xmmword ptr [rbp - 0x50]
    

    where there are narrow stores and then a wide load.

    We likely need smarter struct copy logic to avoid creating this sort of thing. Might be a simple within-block tracking of how we last accessed a struct would be sufficient? Eg if we "just" wrote to a field then copying field-wise is possibly better for perf.

  9. JulieLeeMSFT commented on Dec 4, 2025

    @JulieLeeMSFT
    Member
  10. removed
    untriaged
    on Dec 4, 2025
  11. added this to the 11.0.0 milestone on Dec 4, 2025
  12. AndyAyersMS commented on May 8, 2026

    @AndyAyersMS
    Member

    Fixed by #127487

  13. locked and limited conversation to collaborators on Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions