[glib/wip/baedert/gparam: 3/5] keyfile: Don't allocate parse_buffer if we don't need it




commit f959fbd4204fd76320974b74a9480d5f2b0c36ff
Author: Timm Bäder <mail baedert org>
Date:   Thu Dec 31 11:54:08 2020 +0100

    keyfile: Don't allocate parse_buffer if we don't need it
    
    When loading a GKeyFile, the sequence is usually:
    
    keyfile = g_key_file_new();
    g_key_file_load_xxx(keyfile, ...)
    
    g_key_file_new() calls g_key_file_init(), which allocates a parse_buffer
    for parsing. g_key_file_load_xxx() will then g_key_file_clear() the
    keyfile and call g_key_file_init() again.
    
    Just don't allocate a parse_buffer unless we need it for parsing.

 glib/gkeyfile.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
---
diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
index 8fc0322e8..744c547b9 100644
--- a/glib/gkeyfile.c
+++ b/glib/gkeyfile.c
@@ -633,7 +633,7 @@ g_key_file_init (GKeyFile *key_file)
   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->parse_buffer = NULL;
   key_file->list_separator = ';';
   key_file->flags = 0;
 }
@@ -1473,6 +1473,9 @@ g_key_file_parse_data (GKeyFile     *key_file,
 
   parse_error = NULL;
 
+  if (!key_file->parse_buffer)
+    key_file->parse_buffer = g_string_sized_new (128);
+
   i = 0;
   while (i < length)
     {
@@ -1529,6 +1532,9 @@ g_key_file_flush_parse_buffer (GKeyFile  *key_file,
 
   g_return_if_fail (key_file != NULL);
 
+  if (!key_file->parse_buffer)
+    return;
+
   file_error = NULL;
 
   if (key_file->parse_buffer->len > 0)


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