Skip to content

Different codegen for pinning GetPinnableReference vs GetReference after a length check #110710

@MihaZupan

Description

@MihaZupan
public static void Foo(ReadOnlySpan<char> span)
{
    if (!span.IsEmpty)
    {
        fixed (char* ptr = span)
            Consume(ptr);
    }
}

public static void Bar(ReadOnlySpan<char> span)
{
    if (!span.IsEmpty)
    {
        fixed (char* ptr = &MemoryMarshal.GetReference(span))
            Consume(ptr);
    }
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Consume(char* ptr) { }

https://www.diffchecker.com/cAaIxNr9/

-mov       ecx,[rcx+8]
-test      ecx,ecx
+cmp       dword ptr [rcx+8],0

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions