[glib/wip/matthiasc/try-inlining-magic] Add an inline version of g_strcmp0




commit 7d218c9222da96be8acb62234c1df4eb01065e7b
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Oct 2 17:02:21 2022 -0400

    Add an inline version of g_strcmp0
    
    This can be a significant optimization, since
    compilers know how to optimize strcmp, and
    inlining g_strcmp0 will make the strcmp call
    visible to the compiler at the call site.

 glib/gtestutils.c |  5 +++++
 glib/gtestutils.h | 26 +++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)
---
diff --git a/glib/gtestutils.c b/glib/gtestutils.c
index de70e921ad..350b20e87f 100644
--- a/glib/gtestutils.c
+++ b/glib/gtestutils.c
@@ -19,8 +19,11 @@
  */
 
 #include "config.h"
+#include "gversionmacros.h"
 
+GLIB_AVAILABLE_IN_ALL int g_strcmp0 (const char *str1, const char *str2);
 #include "gtestutils.h"
+
 #include "gfileutils.h"
 
 #include <sys/types.h>
@@ -3397,6 +3400,7 @@ g_assertion_message_error (const char     *domain,
   g_string_free (gstring, TRUE);
 }
 
+#if 0
 /**
  * g_strcmp0:
  * @str1: (nullable): a C string or %NULL
@@ -3420,6 +3424,7 @@ g_strcmp0 (const char     *str1,
     return str1 != str2;
   return strcmp (str1, str2);
 }
+#endif
 
 static void
 test_trap_clear (void)
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 4e38eb4146..056a2031ea 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -238,9 +238,29 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
                                         } G_STMT_END
 #endif /* !G_DISABLE_ASSERT */
 
-GLIB_AVAILABLE_IN_ALL
-int     g_strcmp0                       (const char     *str1,
-                                         const char     *str2);
+/**
+ * g_strcmp0:
+ * @str1: (nullable): a C string or %NULL
+ * @str2: (nullable): another C string or %NULL
+ *
+ * Compares @str1 and @str2 like strcmp(). Handles %NULL
+ * gracefully by sorting it before non-%NULL strings.
+ * Comparing two %NULL pointers returns 0.
+ *
+ * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2.
+ *
+ * Since: 2.16
+ */
+inline int
+g_strcmp0 (const char *str1,
+           const char *str2)
+{
+  if (!str1)
+    return -(str1 != str2);
+  if (!str2)
+    return str1 != str2;
+  return strcmp (str1, str2);
+}
 
 /* report performance results */
 GLIB_AVAILABLE_IN_ALL


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