gstobject: more default name generation more efficient
authorStefan Kost <ensonic@users.sf.net>
Tue, 19 Oct 2010 11:27:20 +0000 (14:27 +0300)
committerStefan Kost <ensonic@users.sf.net>
Fri, 3 Dec 2010 07:50:31 +0000 (09:50 +0200)
Save ~2000 malloc/memcpy/free pairs at startup by running to_lower in-place.
Also skip the numbers as we can.

gst/gstobject.c

index 02e7b45..5424eb4 100644 (file)
@@ -617,8 +617,9 @@ gst_object_set_name_default (GstObject * object)
 {
   const gchar *type_name;
   gint count;
-  gchar *name, *tmp;
+  gchar *name;
   GQuark q;
+  guint i, l;
 
   /* to ensure guaranteed uniqueness across threads, only one thread
    * may ever assign a name */
@@ -634,17 +635,20 @@ gst_object_set_name_default (GstObject * object)
 
   G_UNLOCK (object_name_mutex);
 
-  /* GstFooSink -> foosinkN */
+  /* GstFooSink -> foosink<N> */
   type_name = g_quark_to_string (q);
   if (strncmp (type_name, "Gst", 3) == 0)
     type_name += 3;
-  tmp = g_strdup_printf ("%s%d", type_name, count);
-  name = g_ascii_strdown (tmp, -1);
-  g_free (tmp);
+  l = strlen (type_name);
+  name = g_malloc (l + 6 + 1);
+  for (i = 0; i < l; i++)
+    name[i] = g_ascii_tolower (type_name[i]);
+  snprintf (&name[i], 6, "%d", count);
 
   GST_OBJECT_LOCK (object);
   if (G_UNLIKELY (object->parent != NULL))
     goto had_parent;
+
   g_free (object->name);
   object->name = name;