簡體   English   中英

c# 奇怪的方法內聯行為

[英]c# Weird Method Inlining Behavior

在反編譯我編寫的代碼時,我注意到一個奇怪的內聯行為。
我注意到一個方法不會被內聯,除非它在一個循環中。
奇怪的是,以非泛型方式編寫的這個方法的等價物總是被內聯。

代碼:

using System;
using System.Runtime.CompilerServices;
using SharpLab.Runtime;

[JitGeneric(typeof(int))]
public static class GenericOps<T> where T : unmanaged
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool Less(T left, T right)
    {
        if (typeof(T) == typeof(byte)) return (byte)(object)left < (byte)(object)right;
        if (typeof(T) == typeof(sbyte)) return (sbyte)(object)left < (sbyte)(object)right;
        if (typeof(T) == typeof(ushort)) return (ushort)(object)left < (ushort)(object)right;
        if (typeof(T) == typeof(short)) return (short)(object)left < (short)(object)right;
        if (typeof(T) == typeof(uint)) return (uint)(object)left < (uint)(object)right;
        if (typeof(T) == typeof(int)) return (int)(object)left < (int)(object)right;
        if (typeof(T) == typeof(ulong)) return (ulong)(object)left < (ulong)(object)right;
        if (typeof(T) == typeof(long)) return (long)(object)left < (long)(object)right;
        if (typeof(T) == typeof(float)) return (float)(object)left < (float)(object)right;
        if (typeof(T) == typeof(double)) return (double)(object)left < (double)(object)right;

        throw new NotSupportedException(typeof(T).Name);
    }

    public static T Min(T left, T right)
    {
        return Less(left, right) ? left : right;
    }
}


public static class IntOps
{    
    public static bool Less(int left, int right)
    {
        return left < right;
    }

    public static int Min(int left, int right)
    {
        return Less(left, right) ? left : right;
    }
}

[JitGeneric(typeof(int))]
public static class C<T> where T : unmanaged
{      
    public static T M2(T a, T b)
    {
        return GenericOps<T>.Min(a, b);
    }
    
    public static T M2Loop(Span<T> a, Span<T> b)
    {
        T num = default;
        
        for(int i = 0; i < a.Length; i++)
        {
            num = GenericOps<T>.Min(a[i], b[i]);
        }
        
        return num;
    }
}

我注意到的奇怪行為:

  • GenericOps.Min()內部, GenericOps.Less()不是內聯的,與IntOps.Min()相反,其中IntOps.Less()內聯的。
  • C.M2()中, GenericOps<T>.Min內聯,與C.M2Loop C.M2Loop ,其中GenericOps<T>.Min內聯的。

X64平台上使用 SharpLab 進行反編譯, Core CLR v4.700.20.41105 on amd64

反編譯的 JIT:

Microsoft.CodeAnalysis.EmbeddedAttribute..ctor()
    L0000: ret

System.Runtime.CompilerServices.IsUnmanagedAttribute..ctor()
    L0000: ret

GenericOps`1[[System.Int32, System.Private.CoreLib]].Less(Int32, Int32)
    L0000: cmp ecx, edx
    L0002: setl al
    L0005: movzx eax, al
    L0008: ret

GenericOps`1[[System.Int32, System.Private.CoreLib]].Min(Int32, Int32)
    L0000: push rdi
    L0001: push rsi
    L0002: sub rsp, 0x28
    L0006: mov esi, ecx
    L0008: mov edi, edx
    L000a: mov ecx, esi
    L000c: mov edx, edi
    L000e: call GenericOps`1[[System.Int32, System.Private.CoreLib]].Less(Int32, Int32)
    L0013: test eax, eax
    L0015: jne short L0020
    L0017: mov eax, edi
    L0019: add rsp, 0x28
    L001d: pop rsi
    L001e: pop rdi
    L001f: ret
    L0020: mov eax, esi
    L0022: add rsp, 0x28
    L0026: pop rsi
    L0027: pop rdi
    L0028: ret

IntOps.Less(Int32, Int32)
    L0000: cmp ecx, edx
    L0002: setl al
    L0005: movzx eax, al
    L0008: ret

IntOps.Min(Int32, Int32)
    L0000: cmp ecx, edx
    L0002: jl short L0007
    L0004: mov eax, edx
    L0006: ret
    L0007: mov eax, ecx
    L0009: ret
    
C`1[[System.Int32, System.Private.CoreLib]].M2(Int32, Int32)
    L0000: push rdi
    L0001: push rsi
    L0002: sub rsp, 0x28
    L0006: mov esi, ecx
    L0008: mov edi, edx
    L000a: mov ecx, esi
    L000c: mov edx, edi
    L000e: call GenericOps`1[[System.Int32, System.Private.CoreLib]].Less(Int32, Int32)
    L0013: test eax, eax
    L0015: jne short L001b
    L0017: mov eax, edi
    L0019: jmp short L001d
    L001b: mov eax, esi
    L001d: add rsp, 0x28
    L0021: pop rsi
    L0022: pop rdi
    L0023: ret
    
C`1[[System.Int32, System.Private.CoreLib]].M2Loop(System.Span`1<Int32>, System.Span`1<Int32>)
    L0000: push rsi
    L0001: sub rsp, 0x20
    L0005: mov r8, [rcx]
    L0008: mov ecx, [rcx+8]
    L000b: xor eax, eax
    L000d: xor r9d, r9d
    L0010: test ecx, ecx
    L0012: jle short L0046
    L0014: mov r10d, [rdx+8]
    L0018: mov rdx, [rdx]
    L001b: movsxd rax, r9d
    L001e: mov r11d, [r8+rax*4]
    L0022: cmp r9d, r10d
    L0025: jae short L004c
    L0027: mov eax, [rdx+rax*4]
    L002a: cmp r11d, eax
    L002d: setl sil
    L0031: movzx esi, sil
    L0035: test esi, esi
    L0037: jne short L003b
    L0039: jmp short L003e
    L003b: mov eax, r11d
    L003e: inc r9d
    L0041: cmp r9d, ecx
    L0044: jl short L001b
    L0046: add rsp, 0x20
    L004a: pop rsi
    L004b: ret
    L004c: call 0x00007ffbf05bfc60
    L0051: int3

任何人都可以為這種奇怪的行為提供解釋嗎?
有沒有辦法保證GenericOps.Less()GenericOps.Min()總是內聯?

你的第一點很容易解釋。 throw語句防止內聯。 請參閱 .net 源代碼,了解他們如何使用ThrowHelper class 將拋出異常移出主要方法主體。將 throw 語句移至其他方法以增加內聯的機會。 沒有什么能保證它。

至於另一點,我猜測 jit 決定調用開銷將足夠大,以至於循環體內的內聯將超過抵消增加的代碼大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM