eekboard: reduce the number of allocs when parsing a config file.

This commit is contained in:
Daiki Ueno
2010-12-06 11:17:47 +09:00
parent f562e8c212
commit 6343e37bc1

View File

@ -1363,7 +1363,7 @@ config_parser_start_element (GMarkupParseContext *pcontext,
config->rec = xkl_config_rec_new (); config->rec = xkl_config_rec_new ();
context->list = g_slist_prepend (context->list, config); context->list = g_slist_prepend (context->list, config);
} else } else
context->text = g_string_sized_new (100); context->text->len = 0;
} }
static void static void
@ -1388,8 +1388,7 @@ config_parser_end_element (GMarkupParseContext *pcontext,
if (!context->text) if (!context->text)
return; return;
text = g_string_free (context->text, FALSE); text = g_strndup (context->text->str, context->text->len);
context->text = NULL;
if (g_strcmp0 (element_name, "name") == 0) if (g_strcmp0 (element_name, "name") == 0)
config->name = text; config->name = text;
@ -1409,8 +1408,7 @@ config_parser_text (GMarkupParseContext *pcontext,
GError **error) GError **error)
{ {
ConfigContext *context = user_data; ConfigContext *context = user_data;
if (context->text) context->text = g_string_append_len (context->text, text, text_len);
context->text = g_string_append_len (context->text, text, text_len);
} }
GMarkupParser config_parser = { GMarkupParser config_parser = {
@ -1531,7 +1529,6 @@ main (int argc, char *argv[])
GSList *head; GSList *head;
gint i; gint i;
memset (&context, 0, sizeof context);
file = g_file_new_for_path (opt_config); file = g_file_new_for_path (opt_config);
error = NULL; error = NULL;
@ -1542,6 +1539,8 @@ main (int argc, char *argv[])
exit (1); exit (1);
} }
context.list = NULL;
context.text = g_string_sized_new (BUFSIZ);
pcontext = g_markup_parse_context_new (&config_parser, pcontext = g_markup_parse_context_new (&config_parser,
0, 0,
&context, &context,
@ -1565,6 +1564,7 @@ main (int argc, char *argv[])
error = NULL; error = NULL;
g_markup_parse_context_end_parse (pcontext, &error); g_markup_parse_context_end_parse (pcontext, &error);
g_markup_parse_context_free (pcontext); g_markup_parse_context_free (pcontext);
g_string_free (context.text, TRUE);
g_object_unref (file); g_object_unref (file);
if (context.list) { if (context.list) {