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

前端 未结 4 1120
余生分开走
余生分开走 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

    It isn't related to the C# compiler, the IL is identical. You found a bug in the .NET 4.0 jitter optimizer. You can repro it in Visual Studio. Tools + Options, Debugging, General, untick the "Suppress JIT optimization on module load" option and run the Release build to repro the failure.

    I haven't looked at it closely enough yet to identify the bug. It looks very strange, it inlines the method and completely omits the code for the boxing conversion. The machine code is substantially different from the code generated by the version 2 jitter.

    A clean workaround isn't that easy, you can do it by suppressing inlining. Like this:

        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
        public bool IsDateTime(object o) {
            return o is DateTime;
        }
    

    You can report the bug at connect.microsoft.com. Let me know if you don't want to and I'll take care of it.


    Never mind, that was already done. It wasn't fixed in the maintenance release that was included with VS2010 SP1.


    This bug has been fixed, I can no longer repro it. My current version of clrjit.dll is 4.0.30319.237 dated May 17th 2011. I can't tell exactly what update repaired it. I got a security update on Aug 5th 2011 that updated clrjit.dll to revision 235 with a date of Apr 12, that would be the earliest.

提交回复
热议问题