Skip to content

Jit should avoid allocations for T1 is T2 pattern in generics #58554

@EgorBo

Description

@EgorBo

Minimal repro from #58535

using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool Test<T1, T2>(T1 source) where T1 : struct where T2 : struct 
    {
        return source is T2; // box, issinst, ldnull
    }

    static void Main()
    {
        Test<(int, string), (int, string)>((42, "42"));
    }
}

Test is compiled as:

G_M19373_IG01:
       push     rdi
       push     rsi
       push     rbp
       push     rbx
       sub      rsp, 40
       mov      qword ptr [rsp+20H], rcx
       mov      rbx, rcx
       mov      rsi, rdx
						;; bbWeight=1    PerfScore 5.75
G_M19373_IG02:
       mov      rcx, qword ptr [rbx+56]
       mov      rcx, qword ptr [rcx]
       call     CORINFO_HELP_NEWSFAST
       mov      rbp, rax
       lea      rdi, bword ptr [rbp+8]
       call     CORINFO_HELP_ASSIGN_BYREF
       movsq    
       mov      rax, qword ptr [rbp]
       mov      rdx, qword ptr [rbx+56]
       cmp      rax, qword ptr [rdx+8]
       je       SHORT G_M19373_IG04
						;; bbWeight=1    PerfScore 14.75
G_M19373_IG03:
       xor      rbp, rbp
						;; bbWeight=0.12 PerfScore 0.03
G_M19373_IG04:
       test     rbp, rbp
       setne    al
       movzx    rax, al
						;; bbWeight=1    PerfScore 1.50
G_M19373_IG05:
       add      rsp, 40
       pop      rbx
       pop      rbp
       pop      rsi
       pop      rdi
       ret      
						;; bbWeight=1    PerfScore 3.25

while we could avoid the boxing here and compare generic contexts?

category:performance
theme:generics
skill-level:intermediate
cost:small
impact:small

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions