From 609171d8b70eb9bf169e598fcf2aa88e5e93b818 Mon Sep 17 00:00:00 2001 From: Erik Abrahamsson Date: Wed, 14 Sep 2022 10:52:25 +0200 Subject: Optimization: Exit early when resizing material slots. When assigning a huge number of materials, like when importing thousands of objects, the function `BKE_objects_materials_test_all` uses quite a lot of resources because of the way it loops through all objects to resize the mat-array. By counting the amount of processed objects and comparing to the number of users of the obdata ID, we can exit early and avoid looping through all objects every time. Reviewed By: mont29 Differential Revision: https://developer.blender.org/D15740 --- source/blender/blenkernel/intern/material.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'source') diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 59a51436b7b..3ea6dd3d735 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -900,9 +900,15 @@ void BKE_objects_materials_test_all(Main *bmain, ID *id) } BKE_main_lock(bmain); + int processed_objects = 0; for (ob = bmain->objects.first; ob; ob = ob->id.next) { if (ob->data == id) { BKE_object_material_resize(bmain, ob, *totcol, false); + processed_objects++; + BLI_assert(processed_objects <= id->us && processed_objects > 0); + if (processed_objects == id->us) { + break; + } } } BKE_main_unlock(bmain); -- cgit v1.2.3