[glib/mcatanzaro/cast-checks] Automatically disable cast checks when building with optimization




commit b8e1ecdd6bfd6ff00b7b70f6177549f3a8d3cba3
Author: Michael Catanzaro <mcatanzaro redhat com>
Date:   Wed Aug 3 15:31:30 2022 -0500

    Automatically disable cast checks when building with optimization
    
    Cast checks are slow. We seem to have some rough consensus that they are
    important for debug builds, but not for release builds. Problem is, very
    few apps define G_DISABLE_CAST_CHECKS for release builds. Worse, it's
    undocumented, so there's no way apps could even be expected to know
    about it.
    
    We can get the right default is almost all situations by making this
    depend on the __OPTIMIZE__ preprocessor definition. This is a GCC-specific
    thing, although Clang supports it too. If the compiler does not define
    __OPTIMIZE__, then this commit does no harm: you can still use
    G_DISABLE_CAST_CHECKS as before. When checking __OPTIMIZE__, we are
    supposed to ensure our code has the same behavior as it would if we do
    not, which will be true except in case the check fails (which is
    programmer error).
    
    Downside: this will not automatically do the right thing with -Og,
    because __OPTIMIZE__ is always defined to 1. We don't want to disable
    cast checks automatically if using -O0 or -Og. There's no way to
    automatically fix this, but we can create an escape hatch by allowing
    you to define G_DISABLE_CAST_CHECKS=0 to force-enable cast checks. In
    practice, I don't think this matters much because -Og kinda failed:
    GCC's man page says it should be a superior debugging experience to -O0,
    but it optimizes variables away so it's definitely not.
    
    Another downside: this is bad if you really *do* want cast checks in
    release builds. The same solution applies: define
    G_DISABLE_CAST_CHECKS=0 and you'll get your cast checks.

 gobject/gtype.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
---
diff --git a/gobject/gtype.h b/gobject/gtype.h
index 3f2afb31e0..75014dcfe7 100644
--- a/gobject/gtype.h
+++ b/gobject/gtype.h
@@ -2500,15 +2500,16 @@ const gchar *    g_type_name_from_class         (GTypeClass     *g_class);
 
 
 /* --- implementation bits --- */
-#ifndef G_DISABLE_CAST_CHECKS
+#if (defined(G_DISABLE_CAST_CHECKS) && G_DISABLE_CAST_CHECKS != 0) || (!defined(G_DISABLE_CAST_CHECKS) && 
defined(__OPTIMIZE__))
+#  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) ip)
+#  define _G_TYPE_CCC(cp, gt, ct)       ((ct*) cp)
+#else
 #  define _G_TYPE_CIC(ip, gt, ct) \
     ((ct*) (void *) g_type_check_instance_cast ((GTypeInstance*) ip, gt))
 #  define _G_TYPE_CCC(cp, gt, ct) \
     ((ct*) (void *) g_type_check_class_cast ((GTypeClass*) cp, gt))
-#else /* G_DISABLE_CAST_CHECKS */
-#  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) ip)
-#  define _G_TYPE_CCC(cp, gt, ct)       ((ct*) cp)
-#endif /* G_DISABLE_CAST_CHECKS */
+#endif
+
 #define _G_TYPE_CHI(ip)                        (g_type_check_instance ((GTypeInstance*) ip))
 #define _G_TYPE_CHV(vl)                        (g_type_check_value ((GValue*) vl))
 #define _G_TYPE_IGC(ip, gt, ct)         ((ct*) (((GTypeInstance*) ip)->g_class))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]