Skip to content

Suboptimal codegen with StartsWith(literal) #110019

Open
@MihaZupan

Description

@MihaZupan

When switching from the BitConverter.ToUInt64 hack to the more readable StartsWith(literal), the codegen slightly regresses (doesn't matter for actual perf in this case).

private static readonly ulong s_http10Bytes = BitConverter.ToUInt64("HTTP/1.0"u8);
private static readonly ulong s_http11Bytes = BitConverter.ToUInt64("HTTP/1.1"u8);

private static int Parse1(ReadOnlySpan<byte> line)
{
    if (line.Length < 12 || line[8] != ' ') return 1;

    ulong first8Bytes = BitConverter.ToUInt64(line);
    if (first8Bytes == s_http11Bytes) return 2;
    if (first8Bytes == s_http10Bytes) return 3;

    return 4;
}

private static int Parse2(ReadOnlySpan<byte> line)
{
    if (line.Length < 12 || line[8] != ' ') return 1;

    if (line.StartsWith("HTTP/1.1"u8)) return 2;
    if (line.StartsWith("HTTP/1.0"u8)) return 3;

    return 4;
}

https://www.diffchecker.com/X4PhzKVx/

Patterns like

-cmp       rcx,rax
-jne       short M01_L00
+cmp       rcx,[rax]
+sete      al
+movzx     eax,al
+test      eax,eax
+je        short M01_L00

Metadata

Metadata

Assignees

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