From 8aa3b1c2be375801c0a4fdc93c61ee24862883af Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Fri, 14 Oct 2022 21:26:14 +0200 Subject: Improve performance. --- source/blender/blenkernel/intern/collection.c | 26 +++++++++++++++------- .../blender/python/intern/bpy_rna_id_collection.c | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) 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); -- cgit v1.2.3