[Bf-blender-cvs] [883e4c089ad] master: MetaBall: optimize memory allocation for meta-ball tessellation

Campbell Barton noreply at git.blender.org
Wed Dec 15 13:45:39 CET 2021


Commit: 883e4c089ad7c3336f69847af77449cae4aed548
Author: Campbell Barton
Date:   Wed Dec 15 23:39:55 2021 +1100
Branches: master
https://developer.blender.org/rB883e4c089ad7c3336f69847af77449cae4aed548

MetaBall: optimize memory allocation for meta-ball tessellation

Double the allocation size when the limit is reached instead of
increasing by a fixed number.

Also re-allocate to the exact size once complete instead of over
allocating. This gives a minor speedup in my tests ~19% faster
tessellation for ~1million faces.

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

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

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

diff --git a/source/blender/blenkernel/intern/mball_tessellate.c b/source/blender/blenkernel/intern/mball_tessellate.c
index a2590171abd..eebe6efad78 100644
--- a/source/blender/blenkernel/intern/mball_tessellate.c
+++ b/source/blender/blenkernel/intern/mball_tessellate.c
@@ -55,6 +55,8 @@
 /* experimental (faster) normal calculation */
 // #define USE_ACCUM_NORMAL
 
+#define MBALL_ARRAY_LEN_INIT 4096
+
 /* Data types */
 
 typedef struct corner { /* corner of a cube */
@@ -448,7 +450,7 @@ static void make_face(PROCESS *process, int i1, int i2, int i3, int i4)
 #endif
 
   if (UNLIKELY(process->totindex == process->curindex)) {
-    process->totindex += 4096;
+    process->totindex = process->totindex ? (process->totindex * 2) : MBALL_ARRAY_LEN_INIT;
     process->indices = MEM_reallocN(process->indices, sizeof(int[4]) * process->totindex);
   }
 
@@ -946,8 +948,8 @@ static int getedge(EDGELIST *table[], int i1, int j1, int k1, int i2, int j2, in
  */
 static void addtovertices(PROCESS *process, const float v[3], const float no[3])
 {
-  if (process->curvertex == process->totvertex) {
-    process->totvertex += 4096;
+  if (UNLIKELY(process->curvertex == process->totvertex)) {
+    process->totvertex = process->totvertex ? process->totvertex * 2 : MBALL_ARRAY_LEN_INIT;
     process->co = MEM_reallocN(process->co, process->totvertex * sizeof(float[3]));
     process->no = MEM_reallocN(process->no, process->totvertex * sizeof(float[3]));
   }
@@ -1447,6 +1449,16 @@ void BKE_mball_polygonize(Depsgraph *depsgraph, Scene *scene, Object *ob, ListBa
 
       /* add resulting surface to displist */
       if (process.curindex) {
+
+        /* Avoid over-allocation since this is stored in the displist. */
+        if (process.curindex != process.totindex) {
+          process.indices = MEM_reallocN(process.indices, sizeof(int[4]) * process.curindex);
+        }
+        if (process.curvertex != process.totvertex) {
+          process.co = MEM_reallocN(process.co, process.curvertex * sizeof(float[3]));
+          process.no = MEM_reallocN(process.no, process.curvertex * sizeof(float[3]));
+        }
+
         dl = MEM_callocN(sizeof(DispList), "mballdisp");
         BLI_addtail(dispbase, dl);
         dl->type = DL_INDEX4;



More information about the Bf-blender-cvs mailing list