Module: Mesa
Branch: main
Commit: f9477770d84becad51f34eb149b8333a73bedd0f
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=f9477770d84becad51f34eb149b8333a73bedd0f

Author: Paulo Zanoni <[email protected]>
Date:   Thu Jan 12 14:27:23 2023 -0800

anv: use vk_realloc for the anv_execbuf arrays

Three reasons for that:

0. The operation we're doing here is actually a reallocation.

1. The newer code is, IMHO, easier to read.

2. Realloc has this property where sometimes, when possible, it will
   expand your array without moving it somewhere else, so it doesn't
   need to copy the memory contents, returning the original pointer
   back to you. I did some analysis and while that case is not common,
   it does happen sometimes in real world applications (I could see it
   happening in Shootergame and Aztec Ruins, but not Dota 2), so we're
   able to save a few CPU cycles.

v2: Rebase.

Reviewed-by: Ivan Briano <[email protected]>
Reviewed-by: Lionel Landwerlin <[email protected]>
Signed-off-by: Paulo Zanoni <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20703>

---

 src/intel/vulkan/i915/anv_batch_chain.c | 42 +++++++++++----------------------
 1 file changed, 14 insertions(+), 28 deletions(-)

diff --git a/src/intel/vulkan/i915/anv_batch_chain.c 
b/src/intel/vulkan/i915/anv_batch_chain.c
index ece13cfbc05..43fa371b698 100644
--- a/src/intel/vulkan/i915/anv_batch_chain.c
+++ b/src/intel/vulkan/i915/anv_batch_chain.c
@@ -109,28 +109,19 @@ anv_execbuf_add_bo(struct anv_device *device,
          uint32_t new_len = exec->objects ? exec->bo_array_length * 2 : 64;
 
          struct drm_i915_gem_exec_object2 *new_objects =
-            vk_alloc(exec->alloc, new_len * sizeof(*new_objects), 8, 
exec->alloc_scope);
+            vk_realloc(exec->alloc, exec->objects,
+                       new_len * sizeof(*new_objects), 8, exec->alloc_scope);
          if (new_objects == NULL)
             return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+         exec->objects = new_objects;
+
          struct anv_bo **new_bos =
-            vk_alloc(exec->alloc, new_len * sizeof(*new_bos), 8, 
exec->alloc_scope);
-         if (new_bos == NULL) {
-            vk_free(exec->alloc, new_objects);
+            vk_realloc(exec->alloc, exec->bos, new_len * sizeof(*new_bos), 8,
+                       exec->alloc_scope);
+         if (new_bos == NULL)
             return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
-         }
-
-         if (exec->objects) {
-            memcpy(new_objects, exec->objects,
-                   exec->bo_count * sizeof(*new_objects));
-            memcpy(new_bos, exec->bos,
-                   exec->bo_count * sizeof(*new_bos));
-         }
 
-         vk_free(exec->alloc, exec->objects);
-         vk_free(exec->alloc, exec->bos);
-
-         exec->objects = new_objects;
          exec->bos = new_bos;
          exec->bo_array_length = new_len;
       }
@@ -200,26 +191,21 @@ anv_execbuf_add_syncobj(struct anv_device *device,
       uint32_t new_len = MAX2(exec->syncobj_array_length * 2, 16);
 
       struct drm_i915_gem_exec_fence *new_syncobjs =
-         vk_alloc(exec->alloc, new_len * sizeof(*new_syncobjs),
-                  8, exec->alloc_scope);
-      if (!new_syncobjs)
+         vk_realloc(exec->alloc, exec->syncobjs,
+                    new_len * sizeof(*new_syncobjs), 8, exec->alloc_scope);
+      if (new_syncobjs == NULL)
          return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
 
-      if (exec->syncobjs)
-         typed_memcpy(new_syncobjs, exec->syncobjs, exec->syncobj_count);
-
       exec->syncobjs = new_syncobjs;
 
       if (exec->syncobj_values) {
          uint64_t *new_syncobj_values =
-            vk_alloc(exec->alloc, new_len * sizeof(*new_syncobj_values),
-                     8, exec->alloc_scope);
-         if (!new_syncobj_values)
+            vk_realloc(exec->alloc, exec->syncobj_values,
+                       new_len * sizeof(*new_syncobj_values), 8,
+                       exec->alloc_scope);
+         if (new_syncobj_values == NULL)
             return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
 
-         typed_memcpy(new_syncobj_values, exec->syncobj_values,
-                      exec->syncobj_count);
-
          exec->syncobj_values = new_syncobj_values;
       }
 

Reply via email to