bufferlist: pre-allocate buffer array in one go with the buffer list

We can now create and free a buffer list with one slice alloc/free
call in most cases, instead of one slice alloc/free for the list,
one slice alloc/free for the GArray, and one malloc/free for the
GArray array. In practice we know the max size of our buffer list
from the start, so can avoid reallocs.

https://bugzilla.gnome.org/show_bug.cgi?id=732284
This commit is contained in:
Tim-Philipp Müller 2014-06-26 14:09:25 +01:00
parent 3fbc3d76dc
commit fbe2477fae

View file

@ -1,6 +1,7 @@
/* GStreamer
* Copyright (C) 2009 Axis Communications <dev-gstreamer at axis dot com>
* @author Jonas Holmberg <jonas dot holmberg at axis dot com>
* Copyright (C) 2014 Tim-Philipp Müller <tim at centricular dot com>
*
* gstbufferlist.c: Buffer list
*
@ -38,9 +39,13 @@
#include "gstbuffer.h"
#include "gstbufferlist.h"
#include "gstutils.h"
#define GST_CAT_DEFAULT GST_CAT_BUFFER_LIST
#define GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY(list) \
((list)->buffers != &(list)->arr[0])
/**
* GstBufferList:
*
@ -50,7 +55,16 @@ struct _GstBufferList
{
GstMiniObject mini_object;
GArray *array;
GstBuffer **buffers;
guint n_buffers;
guint n_allocated;
gsize slice_size;
/* one-item array, in reality more items are pre-allocated
* as part of the GstBufferList structure, and that
* pre-allocated array extends beyond the declared struct */
GstBuffer *arr[1];
};
GType _gst_buffer_list_type = 0;
@ -69,15 +83,15 @@ _gst_buffer_list_copy (GstBufferList * list)
GstBufferList *copy;
guint i, len;
len = list->array->len;
copy = gst_buffer_list_new_sized (len);
len = list->n_buffers;
copy = gst_buffer_list_new_sized (list->n_allocated);
/* add and ref all buffers in the array */
for (i = 0; i < len; i++) {
GstBuffer *buf = g_array_index (list->array, GstBuffer *, i);
buf = gst_buffer_ref (buf);
g_array_append_val (copy->array, buf);
}
for (i = 0; i < len; i++)
copy->buffers[i] = gst_buffer_ref (list->buffers[i]);
copy->n_buffers = len;
return copy;
}
@ -85,25 +99,31 @@ static void
_gst_buffer_list_free (GstBufferList * list)
{
guint i, len;
GST_LOG ("free %p", list);
/* unrefs all buffers too */
len = list->array->len;
len = list->n_buffers;
for (i = 0; i < len; i++)
gst_buffer_unref (g_array_index (list->array, GstBuffer *, i));
g_array_free (list->array, TRUE);
gst_buffer_unref (list->buffers[i]);
g_slice_free1 (sizeof (GstBufferList), list);
if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list))
g_free (list->buffers);
g_slice_free1 (list->slice_size, list);
}
static void
gst_buffer_list_init (GstBufferList * list, guint asize)
gst_buffer_list_init (GstBufferList * list, guint n_allocated, gsize slice_size)
{
gst_mini_object_init (GST_MINI_OBJECT_CAST (list), 0, _gst_buffer_list_type,
(GstMiniObjectCopyFunction) _gst_buffer_list_copy, NULL,
(GstMiniObjectFreeFunction) _gst_buffer_list_free);
list->array = g_array_sized_new (FALSE, FALSE, sizeof (GstBuffer *), asize);
list->buffers = &list->arr[0];
list->n_buffers = 0;
list->n_allocated = n_allocated;
list->slice_size = slice_size;
GST_LOG ("init %p", list);
}
@ -125,12 +145,18 @@ GstBufferList *
gst_buffer_list_new_sized (guint size)
{
GstBufferList *list;
gsize slice_size;
guint n_allocated;
list = g_slice_new0 (GstBufferList);
n_allocated = GST_ROUND_UP_16 (size);
slice_size = sizeof (GstBufferList) + (n_allocated - 1) * sizeof (gpointer);
list = g_slice_alloc0 (slice_size);
GST_LOG ("new %p", list);
gst_buffer_list_init (list, size);
gst_buffer_list_init (list, n_allocated, slice_size);
return list;
}
@ -165,7 +191,26 @@ gst_buffer_list_length (GstBufferList * list)
{
g_return_val_if_fail (GST_IS_BUFFER_LIST (list), 0);
return list->array->len;
return list->n_buffers;
}
static inline void
gst_buffer_list_remove_range_internal (GstBufferList * list, guint idx,
guint length, gboolean unref_old)
{
guint i;
if (unref_old) {
for (i = idx; i < idx + length; ++i)
gst_buffer_unref (list->buffers[i]);
}
if (idx + length != list->n_buffers) {
memmove (&list->buffers[idx], &list->buffers[idx + length],
(list->n_buffers - (idx + length)) * sizeof (void *));
}
list->n_buffers -= length;
}
/**
@ -193,20 +238,20 @@ gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
g_return_val_if_fail (GST_IS_BUFFER_LIST (list), FALSE);
g_return_val_if_fail (func != NULL, FALSE);
len = list->array->len;
len = list->n_buffers;
for (i = 0; i < len;) {
GstBuffer *buf, *buf_ret;
buf = buf_ret = g_array_index (list->array, GstBuffer *, i);
buf = buf_ret = list->buffers[i];
ret = func (&buf_ret, i, user_data);
/* Check if the function changed the buffer */
if (buf != buf_ret) {
if (buf_ret == NULL) {
g_array_remove_index (list->array, i);
len--;
gst_buffer_list_remove_range_internal (list, i, 1, FALSE);
--len;
} else {
g_array_index (list->array, GstBuffer *, i) = buf_ret;
list->buffers[i] = buf_ret;
}
}
@ -234,14 +279,10 @@ gst_buffer_list_foreach (GstBufferList * list, GstBufferListFunc func,
GstBuffer *
gst_buffer_list_get (GstBufferList * list, guint idx)
{
GstBuffer *buf;
g_return_val_if_fail (GST_IS_BUFFER_LIST (list), NULL);
g_return_val_if_fail (idx < list->array->len, NULL);
g_return_val_if_fail (idx < list->n_buffers, NULL);
buf = g_array_index (list->array, GstBuffer *, idx);
return buf;
return list->buffers[idx];
}
/**
@ -265,15 +306,42 @@ gst_buffer_list_get (GstBufferList * list, guint idx)
void
gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
{
guint want_alloc;
g_return_if_fail (GST_IS_BUFFER_LIST (list));
g_return_if_fail (buffer != NULL);
if (idx == -1)
g_array_append_val (list->array, buffer);
else {
g_return_if_fail (idx < list->array->len);
g_array_insert_val (list->array, idx, buffer);
if (idx == -1 && list->n_buffers < list->n_allocated) {
list->buffers[list->n_buffers++] = buffer;
return;
}
if (idx == -1 || idx > list->n_buffers)
idx = list->n_buffers;
want_alloc = list->n_buffers + 1;
if (want_alloc > list->n_allocated) {
want_alloc = MAX (GST_ROUND_UP_16 (want_alloc), list->n_allocated * 2);
if (GST_BUFFER_LIST_IS_USING_DYNAMIC_ARRAY (list)) {
list->buffers = g_renew (GstBuffer *, list->buffers, want_alloc);
} else {
list->buffers = g_new0 (GstBuffer *, want_alloc);
memcpy (list->buffers, &list->arr[0], list->n_buffers * sizeof (void *));
GST_CAT_LOG (GST_CAT_PERFORMANCE, "exceeding pre-alloced array");
}
list->n_allocated = want_alloc;
}
if (idx < list->n_buffers) {
memmove (&list->buffers[idx + 1], &list->buffers[idx],
(list->n_buffers - idx) * sizeof (void *));
}
++list->n_buffers;
list->buffers[idx] = buffer;
}
/**
@ -288,15 +356,9 @@ gst_buffer_list_insert (GstBufferList * list, gint idx, GstBuffer * buffer)
void
gst_buffer_list_remove (GstBufferList * list, guint idx, guint length)
{
GstBuffer *buf;
gint i;
g_return_if_fail (GST_IS_BUFFER_LIST (list));
g_return_if_fail (idx < list->array->len);
g_return_if_fail (idx < list->n_buffers);
g_return_if_fail (idx + length <= list->n_buffers);
for (i = idx; i < idx + length; ++i) {
buf = g_array_index (list->array, GstBuffer *, i);
gst_buffer_unref (buf);
}
g_array_remove_range (list->array, idx, length);
gst_buffer_list_remove_range_internal (list, idx, length, TRUE);
}