[Flatbuffer] Remove memcpy in converter
authorGichan Jang <gichan2.jang@samsung.com>
Fri, 26 Mar 2021 05:44:17 +0000 (14:44 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 30 Mar 2021 04:24:48 +0000 (13:24 +0900)
Remove memory copy in converter.
Memory allocation occurs two times on g_memdup and gst_memory_new_wrapped.
Changed to use gst_memory_share.
No memory copy is performed and the memory region is simply shared.

Signed-off-by: Gichan Jang <gichan2.jang@samsung.com>
ext/nnstreamer/tensor_converter/tensor_converter_flatbuf.cc
ext/nnstreamer/tensor_converter/tensor_converter_flexbuf.cc
gst/nnstreamer/tensor_converter/tensor_converter.c

index 9724073..b24dbf0 100644 (file)
@@ -100,10 +100,12 @@ fbc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensorsC
   GstMemory *in_mem, *out_mem;
   GstMapInfo in_info;
   guint mem_size;
-  gpointer mem_data;
 
   in_mem = gst_buffer_peek_memory (in_buf, 0);
-  g_assert (gst_memory_map (in_mem, &in_info, GST_MAP_READ));
+  if (FALSE == gst_memory_map (in_mem, &in_info, GST_MAP_READ)) {
+    nns_loge ("Cannot map input memory / tensor_converter::flatbuf");
+    return NULL;
+  }
 
   tensors = GetTensors (in_info.data);
   g_assert (tensors);
@@ -121,8 +123,9 @@ fbc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensorsC
   *frame_size = 0;
   *frames_in = 1;
 
-
   for (guint i = 0; i < config->info.num_tensors; i++) {
+    gsize offset;
+
     config->info.info[i].name = g_strdup (tensor->Get (i)->name ()->str ().c_str ());
     config->info.info[i].type = (tensor_type)tensor->Get (i)->type ();
     tensor_data = tensor->Get (i)->data ();
@@ -133,10 +136,9 @@ fbc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensorsC
     mem_size = VectorLength (tensor_data);
     *frame_size += mem_size;
 
-    mem_data = g_memdup (tensor_data->data (), mem_size);
+    offset = tensor_data->data () - in_info.data;
 
-    out_mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, mem_data,
-        mem_size, 0, mem_size, mem_data, g_free);
+    out_mem = gst_memory_share (in_mem, offset, mem_size);
 
     gst_buffer_append_memory (out_buf, out_mem);
   }
index 761e74d..9a4dd30 100644 (file)
@@ -116,9 +116,9 @@ flxc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensors
   GstMemory *in_mem, *out_mem;
   GstMapInfo in_info;
   guint mem_size;
-  gpointer mem_data;
 
   in_mem = gst_buffer_peek_memory (in_buf, 0);
+
   if (gst_memory_map (in_mem, &in_info, GST_MAP_READ) == FALSE) {
     ml_loge ("Cannot map input memory / tensor_converter::flexbuf.\n");
     return NULL;
@@ -139,6 +139,7 @@ flxc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensors
 
   for (guint i = 0; i < config->info.num_tensors; i++) {
     gchar * tensor_key = g_strdup_printf ("tensor_%d", i);
+    gsize offset;
     flexbuffers::Vector tensor = tensors[tensor_key].AsVector ();
     config->info.info[i].name = g_strdup (tensor[0].AsString ().c_str ());
     config->info.info[i].type = (tensor_type) tensor[1].AsInt32 ();
@@ -151,11 +152,9 @@ flxc_convert (GstBuffer *in_buf, gsize *frame_size, guint *frames_in, GstTensors
     mem_size = tensor_data.size ();
     *frame_size += mem_size;
 
-    /** @todo Consider removing memory copies for better performance. */
-    mem_data = g_memdup (tensor_data.data (), mem_size);
+    offset = tensor_data.data () - in_info.data;
 
-    out_mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, mem_data,
-        mem_size, 0, mem_size, mem_data, g_free);
+    out_mem = gst_memory_share (in_mem, offset, mem_size);
 
     gst_buffer_append_memory (out_buf, out_mem);
     g_free (tensor_key);
index 50a4c0d..d093993 100644 (file)
@@ -808,7 +808,6 @@ _gst_tensor_converter_chain_push (GstTensorConverter * self, GstBuffer * buf)
     if (self->tensors_info.num_tensors > 1) {
       GstMemory *mem;
       GstMapInfo info;
-      gpointer data;
       gsize idx, size;
       guint i;
 
@@ -826,11 +825,8 @@ _gst_tensor_converter_chain_push (GstTensorConverter * self, GstBuffer * buf)
 
       for (i = 0; i < self->tensors_info.num_tensors; ++i) {
         size = gst_tensors_info_get_size (&self->tensors_info, i);
-        data = g_memdup (info.data + idx, size);
+        gst_buffer_append_memory (buffer, gst_memory_share (mem, idx, size));
         idx += size;
-
-        gst_buffer_append_memory (buffer,
-            gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
       }
 
       gst_memory_unmap (mem, &info);