keyfile: fill parse buffer in line sized chunks
authorJohn Lindgren <john.lindgren@tds.net>
Tue, 17 May 2011 03:03:30 +0000 (23:03 -0400)
committerRay Strode <rstrode@redhat.com>
Tue, 17 May 2011 03:41:55 +0000 (23:41 -0400)
When loading a keyfile the incoming bytes are fed
to a line buffer to get parsed each time a new line
is encountered.

The code that fills the line buffer does it inefficiently,
one byte at a time.

This commit changes that code to look ahead at the incoming
bytes for the next '\n' character and then fill the line buffer
all at once.

https://bugzilla.gnome.org/show_bug.cgi?id=650211

glib/gkeyfile.c

index 1f462be..31c2a15 100644 (file)
@@ -963,7 +963,8 @@ g_key_file_parse_data (GKeyFile     *key_file,
 
   parse_error = NULL;
 
-  for (i = 0; i < length; i++)
+  i = 0;
+  while (i < length)
     {
       if (data[i] == '\n')
         {
@@ -988,9 +989,25 @@ g_key_file_parse_data (GKeyFile     *key_file,
               g_propagate_error (error, parse_error);
               return;
             }
+          i++;
         }
       else
-        g_string_append_c (key_file->parse_buffer, data[i]);
+        {
+          const gchar *start_of_line;
+          const gchar *end_of_line;
+          gsize line_length;
+
+          start_of_line = data + i;
+          end_of_line = memchr (start_of_line, '\n', length - i);
+
+          if (end_of_line == NULL)
+            end_of_line = data + length;
+
+          line_length = end_of_line - start_of_line;
+
+          g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
+          i += line_length;
+        }
     }
 
   key_file->approximate_size += length;