gobject/performance: fix "type-check" test to not optimize out test code

Despite assigning the function to a variable, gcc can still detect that
the function never changes and most of the test code is optimized out.
Initialize it somewhere, where the compiler cannot prove that this
function pointer is always set to the same value.

We could also make the pointer volatile, but this approach seems
preferable to me.
This commit is contained in:
Thomas Haller 2025-02-26 08:12:37 +01:00
parent 2cee7809d1
commit 6a231008e4

View File

@ -712,6 +712,11 @@ test_finalization_print_result (PerformanceTest *test,
#define NUM_KILO_CHECKS_PER_ROUND 50 #define NUM_KILO_CHECKS_PER_ROUND 50
/* Work around g_type_check_instance_is_a being marked "pure",
* and thus only called once for the loop. */
static gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
GType iface_type);
struct TypeCheckTest { struct TypeCheckTest {
GObject *object; GObject *object;
unsigned int n_checks; unsigned int n_checks;
@ -722,6 +727,8 @@ test_type_check_setup (PerformanceTest *test)
{ {
struct TypeCheckTest *data; struct TypeCheckTest *data;
my_type_check_instance_is_a = &g_type_check_instance_is_a;
data = g_new0 (struct TypeCheckTest, 1); data = g_new0 (struct TypeCheckTest, 1);
data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL); data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
@ -738,12 +745,6 @@ test_type_check_init (PerformanceTest *test,
data->n_checks = (unsigned int) (factor * NUM_KILO_CHECKS_PER_ROUND); data->n_checks = (unsigned int) (factor * NUM_KILO_CHECKS_PER_ROUND);
} }
/* Work around g_type_check_instance_is_a being marked "pure",
and thus only called once for the loop. */
gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
GType iface_type) = &g_type_check_instance_is_a;
static void static void
test_type_check_run (PerformanceTest *test, test_type_check_run (PerformanceTest *test,
gpointer _data) gpointer _data)