diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-12-15 15:39:55 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-12-15 15:40:39 +0300 |
commit | 883e4c089ad7c3336f69847af77449cae4aed548 (patch) | |
tree | 11ff29ddddbd8ce33156fd8f5835f6f219b854db /source/blender/blenkernel/intern/mball_tessellate.c | |
parent | 6a885e5d89599d5acec8792bcb6aab49b6c4773c (diff) |
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.
Diffstat (limited to 'source/blender/blenkernel/intern/mball_tessellate.c')
-rw-r--r-- | source/blender/blenkernel/intern/mball_tessellate.c | 18 |
1 files changed, 15 insertions, 3 deletions
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; |