[Xfce4-commits] <libxfce4util:master> Optimize strcmp in xfce-rc-simple.

Nick Schermer noreply at xfce.org
Tue Feb 9 13:16:02 CET 2010


Updating branch refs/heads/master
         to 7d95bf8ad827dd5d7089fd9923771658e706c7ab (commit)
       from 2d211eb0873dbcb496f4457f8b4c193028791cc9 (commit)

commit 7d95bf8ad827dd5d7089fd9923771658e706c7ab
Author: Nick Schermer <nick at xfce.org>
Date:   Tue Feb 9 13:12:25 2010 +0100

    Optimize strcmp in xfce-rc-simple.
    
    Because we heavily rely on strcmp in this code, comparing
    the first character gives a nice speedup.
    
    Test app read 100 groups, with each 200 keys. When the
    group and key names started with the same character there
    was a 3% slowdown, when the first character was 'unique'
    (in a 0-9 range) a 35% speedup when looking at the total
    function cost of callgrind.

 libxfce4util/xfce-rc-simple.c |   36 +++++++++++++++++++++---------------
 1 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/libxfce4util/xfce-rc-simple.c b/libxfce4util/xfce-rc-simple.c
index 2f83703..1382218 100644
--- a/libxfce4util/xfce-rc-simple.c
+++ b/libxfce4util/xfce-rc-simple.c
@@ -128,6 +128,12 @@ struct _Group
 
 
 
+/* because strcmp is by far the most called function in this code, 
+ * we inline the comparison of the first character */
+#define simple_strcmp(a,b) (*(a) != *(b) ? *(a) - *(b) : strcmp ((a), (b)))
+
+
+
 static Group*
 simple_add_group (XfceRcSimple *simple,
                   const gchar  *name)
@@ -135,7 +141,7 @@ simple_add_group (XfceRcSimple *simple,
   Group *group;
 
   for (group = simple->gfirst; group != NULL; group = group->next)
-    if (strcmp (group->name, name) == 0)
+    if (simple_strcmp (group->name, name) == 0)
       return group;
 
   group         = _xfce_slice_new (Group);
@@ -173,7 +179,7 @@ simple_add_entry (XfceRcSimple *simple,
   gint    result;
 
   for (entry = simple->group->efirst; entry != NULL; entry = entry->next)
-    if (strcmp (entry->key, key) == 0)
+    if (simple_strcmp (entry->key, key) == 0)
       break;
 
   if (G_UNLIKELY (entry == NULL))
@@ -226,7 +232,7 @@ simple_add_entry (XfceRcSimple *simple,
   if (G_UNLIKELY (locale == NULL))
     {
       /* overwrite existing value */
-      if (G_LIKELY (strcmp (entry->value, value) != 0))
+      if (G_LIKELY (simple_strcmp (entry->value, value) != 0))
         entry->value = g_string_chunk_insert (simple->string_chunk, value);
     }
   else
@@ -243,7 +249,7 @@ simple_add_entry (XfceRcSimple *simple,
       lentry_before = NULL;
       for (lentry = entry->llast; lentry != NULL; lentry = lentry->prev)
         {
-          result = strcmp (lentry->locale, locale);
+          result = simple_strcmp (lentry->locale, locale);
 
           if (result == 0)
             break;
@@ -288,7 +294,7 @@ simple_add_entry (XfceRcSimple *simple,
       else
         {
           /* overwrite value in existing localized entry */
-          if (G_LIKELY (strcmp (lentry->value, value) != 0))
+          if (G_LIKELY (simple_strcmp (lentry->value, value) != 0))
             lentry->value = g_string_chunk_insert (simple->string_chunk, value);
         }
     }
@@ -517,7 +523,7 @@ simple_write (XfceRcSimple *simple, const gchar *filename)
         continue;
 
       /* NULL_GROUP has no header */
-      if (strcmp (group->name, NULL_GROUP) != 0)
+      if (simple_strcmp (group->name, NULL_GROUP) != 0)
         fprintf (fp, "[%s]\n", group->name);
 
       for (entry = group->efirst; entry != NULL; entry = entry->next)
@@ -837,7 +843,7 @@ _xfce_rc_simple_get_entries (const XfceRc *rc,
     name = NULL_GROUP;
 
   for (group = simple->gfirst; group != NULL; group = group->next)
-    if (strcmp (group->name, name) == 0)
+    if (simple_strcmp (group->name, name) == 0)
       break;
 
   if (group == NULL)
@@ -880,9 +886,9 @@ _xfce_rc_simple_delete_group (XfceRc      *rc,
 
   for (group = simple->gfirst; group != NULL; group = group->next)
     {
-      if (strcmp (group->name, name) == 0)
+      if (simple_strcmp (group->name, name) == 0)
         {
-          if (simple->group == group || strcmp (name, NULL_GROUP) == 0)
+          if (simple->group == group || simple_strcmp (name, NULL_GROUP) == 0)
             {
               /* don't delete current group or the default group, just clear them */
               for (entry = group->efirst; entry != NULL; entry = next)
@@ -921,7 +927,7 @@ _xfce_rc_simple_get_group (const XfceRc *rc)
 {
   const XfceRcSimple *simple = XFCE_RC_SIMPLE_CONST (rc);
 
-  if (strcmp (simple->group->name, NULL_GROUP) == 0)
+  if (simple_strcmp (simple->group->name, NULL_GROUP) == 0)
     return NULL;
   else
     return simple->group->name;
@@ -941,7 +947,7 @@ _xfce_rc_simple_has_group (const XfceRc *rc,
     return TRUE;
 
   for (group = simple->gfirst; group != NULL; group = group->next)
-    if (strcmp (group->name, name) == 0)
+    if (simple_strcmp (group->name, name) == 0)
       break;
 
   return group != NULL;
@@ -958,7 +964,7 @@ _xfce_rc_simple_set_group (XfceRc      *rc,
   if (name == NULL)
     name = NULL_GROUP;
 
-  if (strcmp (simple->group->name, name) != 0)
+  if (simple_strcmp (simple->group->name, name) != 0)
     simple->group = simple_add_group (simple, name);
 }
 
@@ -974,7 +980,7 @@ _xfce_rc_simple_delete_entry (XfceRc      *rc,
 
   for (entry = simple->group->efirst; entry != NULL; entry = entry->next)
     {
-      if (strcmp (entry->key, key) == 0)
+      if (simple_strcmp (entry->key, key) == 0)
         {
           if (entry->prev != NULL)
             entry->prev->next = entry->next;
@@ -1005,7 +1011,7 @@ _xfce_rc_simple_has_entry (const XfceRc *rc,
   const Entry        *entry;
 
   for (entry = simple->group->efirst; entry != NULL; entry = entry->next)
-    if (strcmp (entry->key, key) == 0)
+    if (simple_strcmp (entry->key, key) == 0)
       break;
 
   return entry != NULL;
@@ -1026,7 +1032,7 @@ _xfce_rc_simple_read_entry (const XfceRc *rc,
   guint               match;
 
   for (entry = simple->group->efirst; entry != NULL; entry = entry->next)
-    if (strcmp (entry->key, key) == 0)
+    if (simple_strcmp (entry->key, key) == 0)
       break;
 
   if (G_LIKELY (entry != NULL))



More information about the Xfce4-commits mailing list