[jit] Optimize constrained calls to Enum.GetHasCode () to avoid boxing. Fixes #51330...
authorZoltan Varga <vargaz@gmail.com>
Mon, 16 Jan 2017 00:52:46 +0000 (19:52 -0500)
committerGitHub <noreply@github.com>
Mon, 16 Jan 2017 00:52:46 +0000 (19:52 -0500)
mono/mini/method-to-ir.c
mono/mini/objects.cs

index 8c046edac8d3889feea2ece0de6fd32fa69f5bf3..f5ee5fa74e42e9cd038eca80e073927f82445faf 100644 (file)
@@ -8582,6 +8582,15 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                CHECK_CFG_ERROR;
                                        }
                                }
+
+                               if (constrained_class->enumtype && !strcmp (cmethod->name, "GetHashCode")) {
+                                       /* Use the corresponding method from the base type to avoid boxing */
+                                       MonoType *base_type = mono_class_enum_basetype (constrained_class);
+                                       g_assert (base_type);
+                                       constrained_class = mono_class_from_mono_type (base_type);
+                                       cmethod = mono_class_get_method_from_name (constrained_class, cmethod->name, 0);
+                                       g_assert (cmethod);
+                               }
                        }
                                        
                        if (!dont_verify && !cfg->skip_visibility) {
index 32b92286c914855e53e94636f34abd9d6333cf8e..f378cd205975116ef86c5aefde7e4267d99b545b 100644 (file)
@@ -1765,6 +1765,16 @@ ncells ) {
        public static int test_142_byte_enum_arg_zero_extend () {
                return enum_arg_zero_extend (ByteEnum2.High);
        }
+
+       enum Mine { One, Two }
+
+       public static int test_0_enum_gethashcode_opt () {
+               int sum = 0;
+        for (int i = 0; i < 1000000; ++i)
+                       sum += Mine.Two.GetHashCode();
+
+        return 0;
+    }
 }
 
 #if __MOBILE__