[glib] gobject: Speed up g_type_from_name()



commit d9440687ff13bd36b8c5696ce84327999589d04e
Author: Benjamin Otte <otte redhat com>
Date:   Tue May 17 14:58:39 2011 +0200

    gobject: Speed up g_type_from_name()
    
    The hash table used exclusively for looking up types by name used to map
    quarks => types. But we can easily make it map strings => types, which
    avoids the quark lookup. And that in trun avoids taking a lock and
    consulting another hash table. So this change should make
    g_type_from_name() roughly twice as fast.

 gobject/gtype.c |   18 +++++-------------
 1 files changed, 5 insertions(+), 13 deletions(-)
---
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 9439939..088a86a 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -81,10 +81,6 @@
  * not ->supers[]), as all those memory portions can get realloc()ed during
  * callback invocation.
  *
- * TODO:
- * - g_type_from_name() should do an ordered array lookup after fetching the
- *   the quark, instead of a second hashtable lookup.
- *
  * LOCKING:
  * lock handling issues when calling static functions are indicated by
  * uppercase letter postfixes, all static functions have to have
@@ -492,7 +488,7 @@ type_node_any_new_W (TypeNode             *pnode,
   node->global_gdata = NULL;
   
   g_hash_table_insert (static_type_nodes_ht,
-		       GUINT_TO_POINTER (node->qname),
+		       g_quark_to_string (node->qname),
 		       (gpointer) type);
   return node;
 }
@@ -3328,13 +3324,9 @@ g_type_from_name (const gchar *name)
   
   g_return_val_if_fail (name != NULL, 0);
   
-  quark = g_quark_try_string (name);
-  if (quark)
-    {
-      G_READ_LOCK (&type_rw_lock);
-      type = (GType) g_hash_table_lookup (static_type_nodes_ht, GUINT_TO_POINTER (quark));
-      G_READ_UNLOCK (&type_rw_lock);
-    }
+  G_READ_LOCK (&type_rw_lock);
+  type = (GType) g_hash_table_lookup (static_type_nodes_ht, name);
+  G_READ_UNLOCK (&type_rw_lock);
   
   return type;
 }
@@ -4308,7 +4300,7 @@ g_type_init_with_debug_flags (GTypeDebugFlags debug_flags)
   static_quark_dependants_array = g_quark_from_static_string ("-g-type-private--dependants-array");
   
   /* type qname hash table */
-  static_type_nodes_ht = g_hash_table_new (g_direct_hash, g_direct_equal);
+  static_type_nodes_ht = g_hash_table_new (g_str_hash, g_str_equal);
   
   /* invalid type G_TYPE_INVALID (0)
    */



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