[Bf-blender-cvs] [8aa3b1c2be3] temp-collection-objects-link-multiple: Improve performance.

Jeroen Bakker noreply at git.blender.org
Fri Oct 14 21:26:19 CEST 2022


Commit: 8aa3b1c2be375801c0a4fdc93c61ee24862883af
Author: Jeroen Bakker
Date:   Fri Oct 14 21:26:14 2022 +0200
Branches: temp-collection-objects-link-multiple
https://developer.blender.org/rB8aa3b1c2be375801c0a4fdc93c61ee24862883af

Improve performance.

===================================================================

M	source/blender/blenkernel/intern/collection.c
M	source/blender/python/intern/bpy_rna_id_collection.c

===================================================================

diff --git a/source/blender/blenkernel/intern/collection.c b/source/blender/blenkernel/intern/collection.c
index 42b709104f3..faec986dded 100644
--- a/source/blender/blenkernel/intern/collection.c
+++ b/source/blender/blenkernel/intern/collection.c
@@ -1095,16 +1095,26 @@ static bool collection_object_add(Main *bmain,
       }
     }
 
-    if (collection_objects == NULL) {
-      collection_objects = BLI_gset_ptr_new(__func__);
-      LISTBASE_FOREACH (CollectionObject *, collection_object, &collection->gobject) {
-        BLI_gset_insert(collection_objects, collection_object->ob);
+    if (objects_len <= 10) {
+      /*
+       * Building a GSet adds overhead. When objects_len is small BLI_findptr is faster.
+       */
+      if (BLI_findptr(&collection->gobject, ob, offsetof(CollectionObject, ob))) {
+        continue;
       }
     }
-
-    if (!BLI_gset_add(collection_objects, ob)) {
-      result = false;
-      continue;
+    else {
+      if (collection_objects == NULL) {
+        int gobject_len = BLI_listbase_count(&collection->gobject);
+        collection_objects = BLI_gset_ptr_new_ex(__func__, gobject_len + objects_len);
+        LISTBASE_FOREACH (CollectionObject *, collection_object, &collection->gobject) {
+          BLI_gset_insert(collection_objects, collection_object->ob);
+        }
+      }
+      if (!BLI_gset_add(collection_objects, ob)) {
+        result = false;
+        continue;
+      }
     }
 
     CollectionObject *cob = MEM_callocN(sizeof(CollectionObject), __func__);
diff --git a/source/blender/python/intern/bpy_rna_id_collection.c b/source/blender/python/intern/bpy_rna_id_collection.c
index 10b7ba50988..bd2058394d8 100644
--- a/source/blender/python/intern/bpy_rna_id_collection.c
+++ b/source/blender/python/intern/bpy_rna_id_collection.c
@@ -479,8 +479,9 @@ static PyObject *bpy_link_multiple(PyObject *self, PyObject *args, PyObject *kwd
       objs[i] = obj;
     }
     Py_DECREF(objects_fast);
+
+    BKE_collection_object_add_multiple(bmain, collection, objs, objects_len);
     for (int j = 0; j < objects_len; j++) {
-      BKE_collection_object_add(bmain, collection, objs[j]);
       WM_main_add_notifier(NC_OBJECT | ND_DRAW, &objs[j]->id);
     }
     MEM_freeN(objs);



More information about the Bf-blender-cvs mailing list