[runtime] Don't build temporary array in mono_custom_attrs_get_attr_checked
authorAleksey Kliger <aleksey@xamarin.com>
Wed, 12 Oct 2016 16:02:08 +0000 (12:02 -0400)
committerAleksey Kliger <aleksey@xamarin.com>
Tue, 18 Oct 2016 18:48:25 +0000 (14:48 -0400)
Just return the first matching attribute as soon as it's found, don't
build a temporary array and then index into it.

mono/metadata/custom-attrs.c

index d76c0376adc8620dd1a35a940bf35eb631b93f80..59af797d05e01941aa67e391d8248406e9869f41 100644 (file)
@@ -1453,26 +1453,25 @@ mono_custom_attrs_get_attr (MonoCustomAttrInfo *ainfo, MonoClass *attr_klass)
 MonoObject*
 mono_custom_attrs_get_attr_checked (MonoCustomAttrInfo *ainfo, MonoClass *attr_klass, MonoError *error)
 {
-       int i, attr_index;
-       MonoArray *attrs;
+       int i;
+       MonoCustomAttrEntry *centry = NULL;
+
+       g_assert (attr_klass != NULL);
 
        mono_error_init (error);
 
-       attr_index = -1;
        for (i = 0; i < ainfo->num_attrs; ++i) {
-               MonoClass *klass = ainfo->attrs [i].ctor->klass;
-               if (mono_class_has_parent (klass, attr_klass)) {
-                       attr_index = i;
+               centry = &ainfo->attrs[i];
+               if (centry->ctor == NULL)
+                       continue;
+               MonoClass *klass = centry->ctor->klass;
+               if (mono_class_is_assignable_from (attr_klass, klass))
                        break;
-               }
        }
-       if (attr_index == -1)
+       if (centry == NULL)
                return NULL;
 
-       attrs = mono_custom_attrs_construct_by_type (ainfo, NULL, error);
-       if (!mono_error_ok (error))
-               return NULL;
-       return mono_array_get (attrs, MonoObject*, attr_index);
+       return create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, error);
 }
 
 /*