From 27a328af3b05d3ee4d40a452609d03ac4198806c Mon Sep 17 00:00:00 2001 From: Katelyn Gadd Date: Tue, 23 Apr 2024 08:48:23 -0700 Subject: [PATCH] [mono] Miscellaneous startup optimizations (#101263) * Optimize out an unnecessary memset under g_slist_prepend * Don't inline constructors of Exception or its descendants into interpreter method bodies --- src/mono/mono/eglib/gslist.c | 3 ++- src/mono/mono/metadata/class-getters.h | 1 + src/mono/mono/metadata/class-init.c | 12 ++++++++++++ src/mono/mono/metadata/class-private-definition.h | 5 +++-- src/mono/mono/mini/interp/transform.c | 3 +++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/eglib/gslist.c b/src/mono/mono/eglib/gslist.c index 71cd6dfd972e97..aa03c4be8d1536 100644 --- a/src/mono/mono/eglib/gslist.c +++ b/src/mono/mono/eglib/gslist.c @@ -52,7 +52,8 @@ g_slist_append (GSList *list, gpointer data) GSList* g_slist_prepend (GSList *list, gpointer data) { - GSList *head = g_slist_alloc (); + // We don't need to zero the allocation using g_slist_alloc since we fully initialize the node + GSList *head = g_new (GSList, 1); head->data = data; head->next = list; diff --git a/src/mono/mono/metadata/class-getters.h b/src/mono/mono/metadata/class-getters.h index 8e9804ba45aa8b..30cc66fcf7c176 100644 --- a/src/mono/mono/metadata/class-getters.h +++ b/src/mono/mono/metadata/class-getters.h @@ -48,6 +48,7 @@ MONO_CLASS_GETTER(m_class_is_interfaces_inited, gboolean, , MonoClass, interface MONO_CLASS_GETTER(m_class_is_simd_type, gboolean, , MonoClass, simd_type) MONO_CLASS_GETTER(m_class_is_has_finalize_inited, gboolean, , MonoClass, has_finalize_inited) MONO_CLASS_GETTER(m_class_is_fields_inited, gboolean, , MonoClass, fields_inited) +MONO_CLASS_GETTER(m_class_is_exception_class, gboolean, , MonoClass, is_exception_class) MONO_CLASS_GETTER(m_class_has_failure, gboolean, , MonoClass, has_failure) MONO_CLASS_GETTER(m_class_has_deferred_failure, gboolean, , MonoClass, has_deferred_failure) MONO_CLASS_GETTER(m_class_has_weak_fields, gboolean, , MonoClass, has_weak_fields) diff --git a/src/mono/mono/metadata/class-init.c b/src/mono/mono/metadata/class-init.c index c2210a0e41765c..13cf1fa612b359 100644 --- a/src/mono/mono/metadata/class-init.c +++ b/src/mono/mono/metadata/class-init.c @@ -735,6 +735,17 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError } } + // compute is_exception_class, used by interp to avoid inlining exception handling code + if ( + klass->parent && !m_class_is_valuetype (klass) && + !m_class_is_interface (klass) + ) { + if (m_class_is_exception_class (klass->parent)) + klass->is_exception_class = 1; + else if (!strcmp (klass->name, "Exception") && !strcmp(klass->name_space, "System")) + klass->is_exception_class = 1; + } + mono_loader_unlock (); MONO_PROFILER_RAISE (class_loaded, (klass)); @@ -926,6 +937,7 @@ mono_class_create_generic_inst (MonoGenericClass *gclass) klass->this_arg.byref__ = TRUE; klass->is_inlinearray = gklass->is_inlinearray; klass->inlinearray_value = gklass->inlinearray_value; + klass->is_exception_class = gklass->is_exception_class; klass->enumtype = gklass->enumtype; klass->valuetype = gklass->valuetype; diff --git a/src/mono/mono/metadata/class-private-definition.h b/src/mono/mono/metadata/class-private-definition.h index ff33e6dd8a78e6..1e0c01ab336b3b 100644 --- a/src/mono/mono/metadata/class-private-definition.h +++ b/src/mono/mono/metadata/class-private-definition.h @@ -68,9 +68,8 @@ struct _MonoClass { guint no_special_static_fields : 1; /* has no thread/context static fields */ guint nested_classes_inited : 1; /* Whenever nested_class is initialized */ - - /* next byte*/ guint interfaces_inited : 1; /* interfaces is initialized */ + /* next byte*/ guint simd_type : 1; /* class is a simd intrinsic type */ guint has_finalize_inited : 1; /* has_finalize is initialized */ guint fields_inited : 1; /* setup_fields () has finished */ @@ -79,6 +78,8 @@ struct _MonoClass { guint has_dim_conflicts : 1; /* Class has conflicting default interface methods */ guint any_field_has_auto_layout : 1; /* a field in this type's layout uses auto-layout */ guint has_deferred_failure : 1; + /* next byte*/ + guint is_exception_class : 1; /* is System.Exception or derived from it */ MonoClass *parent; MonoClass *nested_in; diff --git a/src/mono/mono/mini/interp/transform.c b/src/mono/mono/mini/interp/transform.c index 88844dae725374..be1d722f146408 100644 --- a/src/mono/mono/mini/interp/transform.c +++ b/src/mono/mono/mini/interp/transform.c @@ -2907,6 +2907,9 @@ interp_method_check_inlining (TransformData *td, MonoMethod *method, MonoMethodS if (g_list_find (td->dont_inline, method)) return FALSE; + if (m_class_is_exception_class (method->klass) && !strcmp (method->name, ".ctor")) + return FALSE; + return TRUE; }