[Bf-blender-cvs] [609171d8b70] master: Optimization: Exit early when resizing material slots.

Erik Abrahamsson noreply at git.blender.org
Wed Sep 14 11:22:59 CEST 2022


Commit: 609171d8b70eb9bf169e598fcf2aa88e5e93b818
Author: Erik Abrahamsson
Date:   Wed Sep 14 10:52:25 2022 +0200
Branches: master
https://developer.blender.org/rB609171d8b70eb9bf169e598fcf2aa88e5e93b818

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

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

M	source/blender/blenkernel/intern/material.c

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

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);



More information about the Bf-blender-cvs mailing list