Speed up group lookups

svn path=/trunk/; revision=6018
This commit is contained in:
Matthias Clasen 2007-12-03 15:43:20 +00:00
parent 7561ec5765
commit 8e2e557633
2 changed files with 17 additions and 12 deletions

View File

@ -1,3 +1,8 @@
2007-12-03 Matthias Clasen <mclasen@redhat.com>
* glib/gkeyfile.c: Add a hash table to speed up group lookups,
which GKeyFile does quite a lot.
2007-12-03 Alexander Larsson <alexl@redhat.com> 2007-12-03 Alexander Larsson <alexl@redhat.com>
* configure.in: * configure.in:

View File

@ -67,6 +67,7 @@ typedef struct _GKeyFileGroup GKeyFileGroup;
struct _GKeyFile struct _GKeyFile
{ {
GList *groups; GList *groups;
GHashTable *group_hash;
GKeyFileGroup *start_group; GKeyFileGroup *start_group;
GKeyFileGroup *current_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->current_group = g_slice_new0 (GKeyFileGroup);
key_file->groups = g_list_prepend (NULL, key_file->current_group); 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->start_group = NULL;
key_file->parse_buffer = g_string_sized_new (128); key_file->parse_buffer = g_string_sized_new (128);
key_file->approximate_size = 0; key_file->approximate_size = 0;
@ -2802,8 +2804,8 @@ g_key_file_get_group_comment (GKeyFile *key_file,
GList *group_node; GList *group_node;
GKeyFileGroup *group; GKeyFileGroup *group;
group_node = g_key_file_lookup_group_node (key_file, group_name); group = g_key_file_lookup_group (key_file, group_name);
if (!group_node) if (!group)
{ {
g_set_error (error, G_KEY_FILE_ERROR, g_set_error (error, G_KEY_FILE_ERROR,
G_KEY_FILE_ERROR_GROUP_NOT_FOUND, G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
@ -2813,10 +2815,10 @@ g_key_file_get_group_comment (GKeyFile *key_file,
return NULL; return NULL;
} }
group = (GKeyFileGroup *)group_node->data;
if (group->comment) if (group->comment)
return g_strdup (group->comment->value); return g_strdup (group->comment->value);
group_node = g_key_file_lookup_group_node (key_file, group_name);
group_node = group_node->next; group_node = group_node->next;
group = (GKeyFileGroup *)group_node->data; group = (GKeyFileGroup *)group_node->data;
return get_group_comment (key_file, group, error); 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 (key_file != NULL, FALSE);
g_return_val_if_fail (group_name != 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) if (key_file->start_group == NULL)
key_file->start_group = group; key_file->start_group = group;
g_hash_table_insert (key_file->group_hash, group->name, group);
} }
static void static void
@ -3050,6 +3054,9 @@ g_key_file_remove_group_node (GKeyFile *key_file,
group = (GKeyFileGroup *) group_node->data; 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 /* If the current group gets deleted make the current group the last
* added group. * added group.
*/ */
@ -3250,14 +3257,7 @@ static GKeyFileGroup *
g_key_file_lookup_group (GKeyFile *key_file, g_key_file_lookup_group (GKeyFile *key_file,
const gchar *group_name) const gchar *group_name)
{ {
GList *group_node; return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
group_node = g_key_file_lookup_group_node (key_file, group_name);
if (group_node != NULL)
return (GKeyFileGroup *) group_node->data;
return NULL;
} }
static GList * static GList *