adapter: minor optimisation for gst_adapter_take_buffer_list()

Try to allocate buffer list with a suitable size from the
beginning to avoid having to re-alloc the buffer list array.
This commit is contained in:
Tim-Philipp Müller 2015-03-14 17:39:39 +00:00
parent b1b718c6d0
commit 2b4d066355

View file

@ -132,6 +132,7 @@ struct _GstAdapter
GSList *buflist_end; GSList *buflist_end;
gsize size; gsize size;
gsize skip; gsize skip;
guint count;
/* we keep state of assembled pieces */ /* we keep state of assembled pieces */
gpointer assembled_data; gpointer assembled_data;
@ -233,6 +234,7 @@ gst_adapter_clear (GstAdapter * adapter)
g_slist_free (adapter->buflist); g_slist_free (adapter->buflist);
adapter->buflist = NULL; adapter->buflist = NULL;
adapter->buflist_end = NULL; adapter->buflist_end = NULL;
adapter->count = 0;
adapter->size = 0; adapter->size = 0;
adapter->skip = 0; adapter->skip = 0;
adapter->assembled_len = 0; adapter->assembled_len = 0;
@ -346,6 +348,7 @@ gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
adapter->buflist_end = g_slist_append (adapter->buflist_end, buf); adapter->buflist_end = g_slist_append (adapter->buflist_end, buf);
adapter->buflist_end = g_slist_next (adapter->buflist_end); adapter->buflist_end = g_slist_next (adapter->buflist_end);
} }
++adapter->count;
} }
#if 0 #if 0
@ -607,6 +610,7 @@ gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
gst_buffer_unref (cur); gst_buffer_unref (cur);
g = g_slist_delete_link (g, g); g = g_slist_delete_link (g, g);
--adapter->count;
if (G_UNLIKELY (g == NULL)) { if (G_UNLIKELY (g == NULL)) {
GST_LOG_OBJECT (adapter, "adapter empty now"); GST_LOG_OBJECT (adapter, "adapter empty now");
@ -972,6 +976,7 @@ gst_adapter_take_buffer_list (GstAdapter * adapter, gsize nbytes)
GstBufferList *buffer_list; GstBufferList *buffer_list;
GstBuffer *cur; GstBuffer *cur;
gsize hsize, skip, cur_size; gsize hsize, skip, cur_size;
guint n_bufs;
g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL); g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
@ -980,7 +985,14 @@ gst_adapter_take_buffer_list (GstAdapter * adapter, gsize nbytes)
GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes); GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
buffer_list = gst_buffer_list_new (); /* try to create buffer list with sufficient size, so no resize is done later */
if (adapter->count < 64)
n_bufs = adapter->count;
else
n_bufs = (adapter->count * nbytes * 1.2 / adapter->size) + 1;
buffer_list = gst_buffer_list_new_sized (n_bufs);
while (nbytes > 0) { while (nbytes > 0) {
cur = adapter->buflist->data; cur = adapter->buflist->data;
skip = adapter->skip; skip = adapter->skip;