Skip to content

Unroll SequenceEqual for u8 literals #82474

Closed

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

Activity

  1. ghost added
    untriaged
    on Feb 22, 2023
  2. self-assigned this
    on Feb 22, 2023
  3. added
    area-CodeGen-coreclr
    and removed
    untriaged
    on Feb 22, 2023
  4. added this to the Future milestone on Feb 22, 2023
  5. stephentoub commented on Feb 22, 2023

    @stephentoub
    Member

    I thought we already did this :( Thanks for opening the issue. We don't do any such handling for bytes today, just chars?

  6. EgorBo commented on Feb 22, 2023

    @EgorBo
    MemberAuthor

    I thought we already did this :( Thanks for opening the issue. We don't do any such handling for bytes today, just chars?

    Yep, chars only at the moment. RVA didn't fit well into that importer-level optimization, but I have a quick prototype to do this expansion later in ValueNumbering (or Early Prop). We also can then expand it for other patterns like "false"u8.CopyTo(span)

    ulong fals_val = BitConverter.IsLittleEndian ? 0x73006C00610046ul : 0x460061006C0073ul; // "Fals"
    MemoryMarshal.Write<ulong>(MemoryMarshal.AsBytes(destination), ref fals_val);
    destination[4] = 'e';

  7. modified the milestones: Future, 8.0.0 on Apr 1, 2023
  8. EgorBo commented on Apr 1, 2023

    @EgorBo
    MemberAuthor

    Closed via #83945

    bool IsValue(ReadOnlySpan<byte> a) => a.SequenceEqual("value"u8);
    ; Method Prog:IsValue(System.ReadOnlySpan`1[ubyte]):bool:this
    G_M18139_IG01:
           sub      rsp, 40
    						;; size=4 bbWeight=1 PerfScore 0.25
    
    G_M18139_IG02:
           mov      rax, 0xD1FFAB1E      ; data for <PrivateImplementationDetails>:2AC3E771679DFE9EC82B8748F6F7718F3AD0E4EAAFABC9F68F4BC25187A5F230
           mov      rcx, bword ptr [rdx]
           mov      edx, dword ptr [rdx+08H]
           cmp      edx, 5
           jne      SHORT G_M18139_IG04
    						;; size=21 bbWeight=1 PerfScore 5.50
    
    G_M18139_IG03:
           mov      edx, dword ptr [rcx]
           mov      r8d, dword ptr [rax]
           mov      ecx, dword ptr [rcx+01H]
           mov      eax, dword ptr [rax+01H]
           xor      edx, r8d
           xor      eax, ecx
           or       eax, edx
           sete     al
           movzx    rax, al
           jmp      SHORT G_M18139_IG05
    						;; size=26 bbWeight=0.50 PerfScore 6.00
    
    G_M18139_IG04:
           xor      eax, eax
    						;; size=2 bbWeight=0.50 PerfScore 0.12
    
    G_M18139_IG05:
           add      rsp, 40
           ret      
    						;; size=5 bbWeight=1 PerfScore 1.25
    ; Total bytes of code: 58
  9. gfoidl commented on Apr 2, 2023

    @gfoidl
    Member

    @EgorBo for char the xor is done against two constants, saving two memory loads. The JIT is able to do this when written manually.

    Is it possible to have this codegen here too? I.e. avoid the memory loads for the constant parts, and use only constant values.

  10. EgorBo commented on Apr 2, 2023

    @EgorBo
    MemberAuthor

    @EgorBo for char the xor is done against two constants, saving two memory loads. The JIT is able to do this when written manually.

    Is it possible to have this codegen here too? I.e. avoid the memory loads for the constant parts, and use only constant values.

    A bit more complicated fix since I'll have to do that in an earlier phase. I just wanted to land a quick fix and see impact from it, so far not much

  11. ghost locked as resolved and limited conversation to collaborators on May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions