mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-06-05 06:58:56 +00:00
adapter: optimize _take() a little more
When we have already assembled some data before, reuse this data and only copy the part that is new.
This commit is contained in:
parent
fc4caf55c9
commit
961e2029db
1 changed files with 27 additions and 8 deletions
|
@ -545,19 +545,38 @@ static guint8 *
|
||||||
gst_adapter_take_internal (GstAdapter * adapter, guint nbytes)
|
gst_adapter_take_internal (GstAdapter * adapter, guint nbytes)
|
||||||
{
|
{
|
||||||
guint8 *data;
|
guint8 *data;
|
||||||
|
guint toreuse, tocopy;
|
||||||
|
|
||||||
/* we have enough assembled data, take from there */
|
/* see how much data we can reuse from the assembled memory and how much
|
||||||
if (adapter->assembled_len >= nbytes) {
|
* we need to copy */
|
||||||
GST_LOG_OBJECT (adapter, "taking %u bytes already assembled", nbytes);
|
toreuse = MIN (nbytes, adapter->assembled_len);
|
||||||
|
tocopy = nbytes - toreuse;
|
||||||
|
|
||||||
|
/* find memory to return */
|
||||||
|
if (adapter->assembled_size >= nbytes && toreuse > 0) {
|
||||||
|
/* we reuse already allocated memory but only when we're going to reuse
|
||||||
|
* something from it because else we are worse than the malloc and copy
|
||||||
|
* case below */
|
||||||
|
GST_LOG_OBJECT (adapter, "reusing %u bytes of assembled data", toreuse);
|
||||||
|
/* we have enough free space in the assembled array */
|
||||||
data = adapter->assembled_data;
|
data = adapter->assembled_data;
|
||||||
/* allocate new data, assembled_len will be set to 0 in the flush later */
|
/* flush after this function should set the assembled_size to 0 */
|
||||||
adapter->assembled_data = g_malloc (adapter->assembled_size);
|
adapter->assembled_data = g_malloc (adapter->assembled_size);
|
||||||
} else {
|
} else {
|
||||||
/* we need to allocate and copy. We could potentially copy bytes from the
|
GST_LOG_OBJECT (adapter, "allocating %u bytes", nbytes);
|
||||||
* assembled data before doing the copy_into */
|
/* not enough bytes in the assembled array, just allocate new space */
|
||||||
GST_LOG_OBJECT (adapter, "taking %u bytes by collection", nbytes);
|
|
||||||
data = g_malloc (nbytes);
|
data = g_malloc (nbytes);
|
||||||
copy_into_unchecked (adapter, data, adapter->skip, nbytes);
|
/* reuse what we can from the already assembled data */
|
||||||
|
if (toreuse) {
|
||||||
|
GST_LOG_OBJECT (adapter, "reusing %u bytes", toreuse);
|
||||||
|
memcpy (data, adapter->assembled_data, toreuse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tocopy) {
|
||||||
|
/* copy the remaining data */
|
||||||
|
GST_LOG_OBJECT (adapter, "copying %u bytes", tocopy);
|
||||||
|
copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
|
||||||
|
tocopy);
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue