From 8e2e557633ded9f0774e25f73b026ac9a673c037 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 3 Dec 2007 15:43:20 +0000 Subject: [PATCH] Speed up group lookups svn path=/trunk/; revision=6018 --- ChangeLog | 5 +++++ glib/gkeyfile.c | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index f873f04fc..70d0ce128 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-12-03 Matthias Clasen + + * glib/gkeyfile.c: Add a hash table to speed up group lookups, + which GKeyFile does quite a lot. + 2007-12-03 Alexander Larsson * configure.in: diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c index 72c5761c8..ac7e495c0 100644 --- a/glib/gkeyfile.c +++ b/glib/gkeyfile.c @@ -67,6 +67,7 @@ typedef struct _GKeyFileGroup GKeyFileGroup; struct _GKeyFile { GList *groups; + GHashTable *group_hash; GKeyFileGroup *start_group; GKeyFileGroup *current_group; @@ -199,6 +200,7 @@ g_key_file_init (GKeyFile *key_file) { key_file->current_group = g_slice_new0 (GKeyFileGroup); key_file->groups = g_list_prepend (NULL, key_file->current_group); + key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal); key_file->start_group = NULL; key_file->parse_buffer = g_string_sized_new (128); key_file->approximate_size = 0; @@ -2802,8 +2804,8 @@ g_key_file_get_group_comment (GKeyFile *key_file, GList *group_node; GKeyFileGroup *group; - group_node = g_key_file_lookup_group_node (key_file, group_name); - if (!group_node) + group = g_key_file_lookup_group (key_file, group_name); + if (!group) { g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND, @@ -2813,10 +2815,10 @@ g_key_file_get_group_comment (GKeyFile *key_file, return NULL; } - group = (GKeyFileGroup *)group_node->data; if (group->comment) return g_strdup (group->comment->value); + group_node = g_key_file_lookup_group_node (key_file, group_name); group_node = group_node->next; group = (GKeyFileGroup *)group_node->data; return get_group_comment (key_file, group, error); @@ -2923,7 +2925,7 @@ g_key_file_has_group (GKeyFile *key_file, g_return_val_if_fail (key_file != NULL, FALSE); g_return_val_if_fail (group_name != NULL, FALSE); - return g_key_file_lookup_group_node (key_file, group_name) != NULL; + return g_key_file_lookup_group (key_file, group_name) != NULL; } /** @@ -2996,6 +2998,8 @@ g_key_file_add_group (GKeyFile *key_file, if (key_file->start_group == NULL) key_file->start_group = group; + + g_hash_table_insert (key_file->group_hash, group->name, group); } static void @@ -3050,6 +3054,9 @@ g_key_file_remove_group_node (GKeyFile *key_file, group = (GKeyFileGroup *) group_node->data; + if (group->name) + g_hash_table_remove (key_file->group_hash, group->name); + /* If the current group gets deleted make the current group the last * added group. */ @@ -3250,14 +3257,7 @@ static GKeyFileGroup * g_key_file_lookup_group (GKeyFile *key_file, const gchar *group_name) { - GList *group_node; - - group_node = g_key_file_lookup_group_node (key_file, group_name); - - if (group_node != NULL) - return (GKeyFileGroup *) group_node->data; - - return NULL; + return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name); } static GList *