Can C# 'is' operator suffer under release mode optimization on .NET 4?

前端 未结 4 1123
余生分开走
余生分开走 2021-01-31 15:03

Below is a simple test fixture. It succeeds in Debug builds and fails in Release builds (VS2010, .NET4 solution, x64):

[TestFixture]
public sealed class Test
{
          


        
4条回答
  •  庸人自扰
    2021-01-31 15:41

    For reference I checked with mono

    • Mono JIT compiler version 2.6.7 (Debian 2.6.7-3ubuntu1)
    • Mono JIT compiler version 2.8.2 (sehe/d1c74ad Fri Feb 18 21:46:52 CET 2011)

    Both presented no problems whatsoever. Here is the IL with optimization in 2.8.2

    .method public hidebysig 
           instance default bool IsDateTime (object o)  cil managed 
    {
        // Method begins at RVA 0x2130
        // Code size 10 (0xa)
        .maxstack 8
        IL_0000:  ldarg.1 
        IL_0001:  isinst [mscorlib]System.DateTime
        IL_0006:  ldnull 
        IL_0007:  cgt.un 
        IL_0009:  ret 
    } // end of method Checker::IsDateTime
    

    Without optimizations is exactly the same

    Here is the result of mono's jitted code for this IL:

    00000130 :
         130:       55                      push   %ebp
         131:       8b ec                   mov    %esp,%ebp
         133:       53                      push   %ebx
         134:       56                      push   %esi
         135:       83 ec 10                sub    $0x10,%esp
         138:       e8 00 00 00 00          call   13d 
         13d:       5b                      pop    %ebx
         13e:       81 c3 03 00 00 00       add    $0x3,%ebx
         144:       8b 45 0c                mov    0xc(%ebp),%eax
         147:       89 45 f4                mov    %eax,-0xc(%ebp)
         14a:       8b 75 0c                mov    0xc(%ebp),%esi
         14d:       83 7d 0c 00             cmpl   $0x0,0xc(%ebp)
         151:       74 1a                   je     16d 
         153:       8b 45 f4                mov    -0xc(%ebp),%eax
         156:       8b 00                   mov    (%eax),%eax
         158:       8b 00                   mov    (%eax),%eax
         15a:       8b 40 08                mov    0x8(%eax),%eax
         15d:       8b 48 08                mov    0x8(%eax),%ecx
         160:       8b 93 10 00 00 00       mov    0x10(%ebx),%edx
         166:       33 c0                   xor    %eax,%eax
         168:       3b ca                   cmp    %edx,%ecx
         16a:       0f 45 f0                cmovne %eax,%esi
         16d:       85 f6                   test   %esi,%esi
         16f:       0f 97 c0                seta   %al
         172:       0f b6 c0                movzbl %al,%eax
         175:       8d 65 f8                lea    -0x8(%ebp),%esp
         178:       5e                      pop    %esi
         179:       5b                      pop    %ebx
         17a:       c9                      leave  
         17b:       c3                      ret    
         17c:       8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
    

提交回复
热议问题