[Bf-blender-cvs] [ffcf39e3b5d] master: BMesh: reduce memory usage existing edit-mode

Campbell Barton noreply at git.blender.org
Thu Nov 28 08:17:14 CET 2019


Commit: ffcf39e3b5dbe7f12d04a350c629055ad21d40ce
Author: Campbell Barton
Date:   Thu Nov 28 18:14:08 2019 +1100
Branches: master
https://developer.blender.org/rBffcf39e3b5dbe7f12d04a350c629055ad21d40ce

BMesh: reduce memory usage existing edit-mode

- Don't duplicate the original vertices.
- Free old geometry before allocating the new geometry.

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

M	source/blender/bmesh/intern/bmesh_mesh_conv.c

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

diff --git a/source/blender/bmesh/intern/bmesh_mesh_conv.c b/source/blender/bmesh/intern/bmesh_mesh_conv.c
index 3942cdd27fb..1ecbfccab74 100644
--- a/source/blender/bmesh/intern/bmesh_mesh_conv.c
+++ b/source/blender/bmesh/intern/bmesh_mesh_conv.c
@@ -561,58 +561,34 @@ BLI_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e)
  */
 void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMeshParams *params)
 {
-  MLoop *mloop;
-  MPoly *mpoly;
-  MVert *mvert, *oldverts;
-  MEdge *med, *medge;
+  MEdge *med;
   BMVert *v, *eve;
   BMEdge *e;
   BMFace *f;
   BMIter iter;
-  int i, j, ototvert;
+  int i, j;
 
   const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
   const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
   const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
   const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX);
 
-  ototvert = me->totvert;
-
-  /* New vertex block. */
-  if (bm->totvert == 0) {
-    mvert = NULL;
-  }
-  else {
-    mvert = MEM_callocN(bm->totvert * sizeof(MVert), "loadeditbMesh vert");
-  }
+  MVert *oldverts = NULL;
+  const int ototvert = me->totvert;
 
-  /* New edge block. */
-  if (bm->totedge == 0) {
-    medge = NULL;
-  }
-  else {
-    medge = MEM_callocN(bm->totedge * sizeof(MEdge), "loadeditbMesh edge");
-  }
+  if (me->key && (cd_shape_keyindex_offset != -1)) {
+    /* Keep the old verts in case we are working on* a key, which is done at the end. */
 
-  /* New ngon face block. */
-  if (bm->totface == 0) {
-    mpoly = NULL;
-  }
-  else {
-    mpoly = MEM_callocN(bm->totface * sizeof(MPoly), "loadeditbMesh poly");
-  }
-
-  /* New loop block. */
-  if (bm->totloop == 0) {
-    mloop = NULL;
-  }
-  else {
-    mloop = MEM_callocN(bm->totloop * sizeof(MLoop), "loadeditbMesh loop");
-  }
-
-  /* Lets save the old verts just in case we are actually working on
-   * a key ... we now do processing of the keys at the end. */
+    /* Use the array in-place instead of duplicating the array. */
+#if 0
   oldverts = MEM_dupallocN(me->mvert);
+#else
+    oldverts = me->mvert;
+    me->mvert = NULL;
+    CustomData_update_typemap(&me->vdata);
+    CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
+#endif
+  }
 
   /* Free custom data. */
   CustomData_free(&me->vdata, me->totvert);
@@ -640,6 +616,11 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh
     CustomData_copy(&bm->pdata, &me->pdata, mask.pmask, CD_CALLOC, me->totpoly);
   }
 
+  MVert *mvert = bm->totvert ? MEM_callocN(sizeof(MVert) * bm->totvert, "bm_to_me.vert") : NULL;
+  MEdge *medge = bm->totedge ? MEM_callocN(sizeof(MEdge) * bm->totedge, "bm_to_me.edge") : NULL;
+  MLoop *mloop = bm->totloop ? MEM_callocN(sizeof(MLoop) * bm->totloop, "bm_to_me.loop") : NULL;
+  MPoly *mpoly = bm->totface ? MEM_callocN(sizeof(MPoly) * bm->totface, "bm_to_me.poly") : NULL;
+
   CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
   CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
   CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
@@ -868,12 +849,15 @@ void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMesh
         (actkey != NULL) &&
 
         /* Not used here, but 'oldverts' is used later for applying 'ofs'. */
-        (oldverts != NULL)) {
+        (oldverts != NULL) &&
+
+        /* Needed for referencing oldverts. */
+        (cd_shape_keyindex_offset != -1)) {
 
       const bool act_is_basis = BKE_keyblock_is_basis(me->key, bm->shapenr - 1);
 
       /* Active key is a base. */
-      if (act_is_basis && (cd_shape_keyindex_offset != -1)) {
+      if (act_is_basis) {
         const float(*fp)[3] = actkey->data;
 
         ofs = MEM_callocN(sizeof(float) * 3 * bm->totvert, "currkey->data");



More information about the Bf-blender-cvs mailing list