Skip to content

Unroll SequenceEqual for u8 literals #82474

@EgorBo

Description

@EgorBo
bool IsValue(ReadOnlySpan<byte> a) => a.SequenceEqual("value"u8);
; Method P:IsValue(System.ReadOnlySpan`1[ubyte]):bool:this
       sub      rsp, 40
       mov      r8, 0xD1FFAB1E ;; address of RVA field
       mov      rcx, bword ptr [rdx]
       mov      edx, dword ptr [rdx+08H]
       mov      bword ptr [rsp+20H], r8
       cmp      edx, 5
       jne      SHORT G_M29601_IG04
       mov      edx, 5
       mov      r8, rdx
       mov      rdx, bword ptr [rsp+20H]
       call     [System.SpanHelpers:SequenceEqual(byref,byref,ulong):bool]
       jmp      SHORT G_M29601_IG05
G_M29601_IG04:
       xor      eax, eax
       add      rsp, 40
       ret      
; Total bytes of code: 58

Should be unrolled similar to UTF-16:

bool IsValue(ReadOnlySpan<char> a) => a.SequenceEqual("value");
; Method P:IsValue(System.ReadOnlySpan`1[ushort]):bool:this
       mov      rax, bword ptr [rdx]
       cmp      dword ptr [rdx+08H], 5
       jne      SHORT G_M10905_IG04
       mov      rdx, 0x75006C00610076
       xor      rdx, qword ptr [rax]
       mov      eax, dword ptr [rax+06H]
       xor      eax, 0x650075
       or       rax, rdx
       sete     al
       movzx    rax, al
       jmp      SHORT G_M10905_IG05
G_M10905_IG04:
       xor      eax, eax
G_M10905_IG05:
       ret      
; Total bytes of code: 44

Motivation

To clean up various places like:

  1. JsonSerializer.Read.HandleMetadata.cs
  2. aspnetcore's HttpHeaders.Generated.cs
  3. ^ similar to that in runtime: HeaderDescriptor.cs - this currently picks a candidate and then does non-optimized comparison while it could do comparison against u8 literal in each case
  4. Should optimize TE benchmarks as well e.g. here BenchmarkApplication.cs

Work items

  • SequenceEqual
  • OrdinalIgnoreCase
  • StartsWith

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

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions