Skip to content

Suboptimal codegen for [value] vs new ReadOnlySpan<byte>(&value, 1) #116112

Open
@EgorBo

Description

@EgorBo

I expect Test1 and Test2 to have exactly the same codegen in the following code snippet:

static unsafe void Test1(byte value) => Consume(new ReadOnlySpan<byte>(&value, 1));
static unsafe void Test2(byte value) => Consume([value]);

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Consume(ReadOnlySpan<byte> span) {}

win-x64 codegen:

; Assembly listing for method Bench:Test1(ubyte) (FullOpts)
       sub      rsp, 56
       xor      eax, eax
       mov      qword ptr [rsp+0x28], rax
       mov      dword ptr [rsp+0x40], ecx
       lea      rcx, [rsp+0x40]
       mov      bword ptr [rsp+0x28], rcx
       mov      dword ptr [rsp+0x30], 1
       lea      rcx, [rsp+0x28]
       call     [Bench:Consume(System.ReadOnlySpan`1[ubyte])]
       nop      
       add      rsp, 56
       ret      
; Total bytes of code 50


; Assembly listing for method Bench:Test2(ubyte) (FullOpts)
       sub      rsp, 56
       xor      eax, eax
       mov      qword ptr [rsp+0x20], rax
       mov      qword ptr [rsp+0x28], rax
       movzx    rcx, cl
       mov      dword ptr [rsp+0x34], ecx
       lea      rcx, bword ptr [rsp+0x34]
       mov      bword ptr [rsp+0x20], rcx
       mov      dword ptr [rsp+0x28], 1
       lea      rcx, [rsp+0x20]
       call     [Bench:Consume(System.ReadOnlySpan`1[ubyte])]
       nop      
       add      rsp, 56
       ret      
; Total bytes of code 58

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIreduce-unsafe

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions