Skip to content

Suboptimal code generated for even a trivial ValueConverter #85570

@hez2010

Description

@hez2010

Description

I'm seeing suboptimal code generated for ValueConverters even when what the converter does is trivial.

For example,

class Program
{
    private static readonly Foo foo = new();
    private static readonly CultureInfo culture = CultureInfo.CurrentCulture;

    public int Test()
    {
        var x = 3;
        var y = (int)foo.Convert(x, typeof(int), null, culture);
        return y;
    }
}

interface IValueConverter
{
    object Convert(object value, Type targetType, object? parameter, CultureInfo? culture);
}

sealed class Foo : IValueConverter
{
    public object Convert(object value, Type targetType, object? parameter, CultureInfo? culture)
    {
        return (int)value + 1;
    }
}

The codegen:

; Assembly listing for method Program:Test():int:this
; Emitting BLENDED_CODE for X64 CPU with AVX - Windows
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 1 single block inlinees; 0 inlinees without PGO data

G_M000_IG01:                ;; offset=0000H
       push     rdi
       push     rsi
       sub      rsp, 40
 
G_M000_IG02:                ;; offset=0006H
       mov      rsi, 0x7FF7BD400858
       mov      rcx, rsi
       call     CORINFO_HELP_NEWSFAST
       mov      rdi, rax
       test     byte  ptr [(reloc 0x7ff7bd6f9344)], 1
       je       SHORT G_M000_IG05
 
G_M000_IG03:                ;; offset=0024H
       mov      rcx, 0x23591401DA8
       mov      rcx, gword ptr [rcx]
       mov      dword ptr [rdi+08H], 3
       cmp      byte  ptr [rcx], cl
       mov      rcx, rsi
       call     CORINFO_HELP_NEWSFAST
       mov      ecx, dword ptr [rdi+08H]
       inc      ecx
       mov      dword ptr [rax+08H], ecx
       mov      eax, dword ptr [rax+08H]
 
G_M000_IG04:                ;; offset=004DH
       add      rsp, 40
       pop      rsi
       pop      rdi
       ret      
 
G_M000_IG05:                ;; offset=0054H
       mov      rcx, 0x7FF7BD6F9310
       mov      edx, 4
       call     CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
       jmp      SHORT G_M000_IG03
 
; Total bytes of code 106

This is important because ValueConverters are heavily used in Desktop apps that use XAML (WPF, UWP, MAUI and etc.), and we want to minimize the overhead caused by ValueConverters.

Expected codegen:

mov eax, 4
ret

Configuration

.NET 8 preview 3

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 SuperPMItenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions