From 5318a038880b623ac9587539e4b79742335bf0a7 Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Tue, 10 Mar 2020 18:14:57 +0100 Subject: [PATCH] gststructure: Optimize pre-allocation of structures For all the structure creation using valist/varargs we calculate the number of fields we will need to store. This ensures all callers will end up with a single allocation. --- gst/gststructure.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/gst/gststructure.c b/gst/gststructure.c index 1f5cbcd7b1..42340e1b5f 100644 --- a/gst/gststructure.c +++ b/gst/gststructure.c @@ -347,8 +347,25 @@ gst_structure_new_valist (const gchar * name, const gchar * firstfield, va_list varargs) { GstStructure *structure; + va_list copy; + guint len = 0; + const gchar *field_copy = firstfield; + GType type_copy; - structure = gst_structure_new_empty (name); + g_return_val_if_fail (gst_structure_validate_name (name), NULL); + + /* Calculate size of varargs */ + va_copy (copy, varargs); + while (field_copy) { + type_copy = va_arg (copy, GType); + G_VALUE_COLLECT_SKIP (type_copy, copy); + field_copy = va_arg (copy, gchar *); + len++; + } + va_end (copy); + + structure = + gst_structure_new_id_empty_with_size (g_quark_from_string (name), len); if (structure) gst_structure_set_valist (structure, firstfield, varargs); @@ -882,13 +899,28 @@ gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...) { GstStructure *s; va_list varargs; + va_list copy; + guint len = 0; + GQuark quark_copy = field_quark; + GType type_copy; g_return_val_if_fail (name_quark != 0, NULL); g_return_val_if_fail (field_quark != 0, NULL); - s = gst_structure_new_id_empty (name_quark); - va_start (varargs, field_quark); + + /* Calculate size of varargs */ + va_copy (copy, varargs); + while (quark_copy) { + type_copy = va_arg (copy, GType); + G_VALUE_COLLECT_SKIP (type_copy, copy); + quark_copy = va_arg (copy, GQuark); + len++; + } + va_end (copy); + + s = gst_structure_new_id_empty_with_size (name_quark, len); + gst_structure_id_set_valist_internal (s, field_quark, varargs); va_end (varargs);