Skip to content

CodeGen difference when manually extracting element to a local #102518

@MihaZupan

Description

@MihaZupan

SharpLab

public static void Test1(ReadOnlySpan<char> s) => s.StartsWith1('a');

public static void Test2(ReadOnlySpan<char> s) => s.StartsWith2('a');

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith1<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>?
{
    if (span.Length > 0)
    {
        return span[0]?.Equals(value) ?? (object?)value is null;
    }
    return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith2<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>?
{
    if (span.Length > 0)
    {
        T temp = span[0];
        return temp?.Equals(value) ?? (object?)value is null;
    }
    return false;
}
TestClass.Test1(System.ReadOnlySpan`1<Char>)
    L0000: push rax
    L0001: mov rax, [rcx]
    L0004: mov ecx, [rcx+8]
    L0007: xor edx, edx
    L0009: mov [rsp+4], edx
    L000d: test ecx, ecx
    L000f: jle short L0019
    L0011: movzx eax, word ptr [rax]
    L0014: mov [rsp+4], ax
    L0019: add rsp, 8
    L001d: ret

TestClass.Test2(System.ReadOnlySpan`1<Char>)
    L0000: mov rax, [rcx]
    L0003: mov ecx, [rcx+8]
    L0006: test ecx, ecx
    L0008: jle short L000d
    L000a: cmp [rax], ax
    L000d: ret

Wasn't sure what to search for to look if there's an existing issue for this.

Metadata

Metadata

Assignees

Labels

Priority:2Work that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions