From 9c1a44cf32ebeee00a0e2303832b9cea26c93b76 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Mon, 16 May 2011 22:27:11 -0400 Subject: [PATCH] keyfile: avoid needless allocations on file load When loading a key file, the keys and values of individual lines are allocated once when copied and trimmed from the parse buffer and allocated/copied again when added to the lookup map. This commit avoids the second pair of allocations by introducing a new function g_key_file_add_key_value_pair that gives the lookup map direct ownership of the key and value copied from the parse buffer. https://bugzilla.gnome.org/show_bug.cgi?id=650211 --- glib/gkeyfile.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c index 24033e776..1f462bed5 100644 --- a/glib/gkeyfile.c +++ b/glib/gkeyfile.c @@ -139,6 +139,9 @@ static void g_key_file_remove_key_value_pair_node (GKeyFile GKeyFileGroup *group, GList *pair_node); +static void g_key_file_add_key_value_pair (GKeyFile *key_file, + GKeyFileGroup *group, + GKeyFileKeyValuePair *pair); static void g_key_file_add_key (GKeyFile *key_file, GKeyFileGroup *group, const gchar *key, @@ -912,11 +915,22 @@ g_key_file_parse_key_value_pair (GKeyFile *key_file, locale = key_get_locale (key); if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale)) - g_key_file_add_key (key_file, key_file->current_group, key, value); + { + GKeyFileKeyValuePair *pair; + + pair = g_slice_new (GKeyFileKeyValuePair); + pair->key = key; + pair->value = value; + + g_key_file_add_key_value_pair (key_file, key_file->current_group, pair); + } + else + { + g_free (key); + g_free (value); + } g_free (locale); - g_free (key); - g_free (value); } static gchar * @@ -3329,6 +3343,17 @@ g_key_file_remove_group (GKeyFile *key_file, return TRUE; } +static void +g_key_file_add_key_value_pair (GKeyFile *key_file, + GKeyFileGroup *group, + GKeyFileKeyValuePair *pair) +{ + g_hash_table_replace (group->lookup_map, pair->key, pair); + group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair); + group->has_trailing_blank_line = FALSE; + key_file->approximate_size += strlen (pair->key) + strlen (pair->value) + 2; +} + static void g_key_file_add_key (GKeyFile *key_file, GKeyFileGroup *group, @@ -3341,10 +3366,7 @@ g_key_file_add_key (GKeyFile *key_file, pair->key = g_strdup (key); pair->value = g_strdup (value); - g_hash_table_replace (group->lookup_map, pair->key, pair); - group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair); - group->has_trailing_blank_line = FALSE; - key_file->approximate_size += strlen (key) + strlen (value) + 2; + g_key_file_add_key_value_pair (key_file, group, pair); } /**