[Bf-blender-cvs] [a1499522742] undo-experiments: undoexp: cleanup/simplify handling of WM/SCR/WS IDs.

Bastien Montagne noreply at git.blender.org
Wed Jan 29 11:55:16 CET 2020


Commit: a149952274213176440f6b5045931027e7541d2d
Author: Bastien Montagne
Date:   Wed Jan 29 11:52:55 2020 +0100
Branches: undo-experiments
https://developer.blender.org/rBa149952274213176440f6b5045931027e7541d2d

undoexp: cleanup/simplify handling of WM/SCR/WS IDs.

Re-read from memfile WindowManagers, Screens and WorkSpaces
are never actually used in undo situation. This allows us to simply
early abort on those ID types, even if we do not find them in current
(old) bmain for some reason.

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

M	source/blender/blenkernel/intern/blendfile.c
M	source/blender/blenloader/intern/readfile.c

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

diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c
index 6c48104662c..8c654b001ff 100644
--- a/source/blender/blenkernel/intern/blendfile.c
+++ b/source/blender/blenkernel/intern/blendfile.c
@@ -475,13 +475,13 @@ bool BKE_blendfile_read_from_memfile(bContext *C,
 
   bfd = BLO_read_from_memfile(bmain, BKE_main_blendfile_path(bmain), memfile, params, reports);
   if (bfd) {
-    /* remove the unused screens and wm */
-    while (bfd->main->wm.first) {
-      BKE_id_free(bfd->main, bfd->main->wm.first);
-    }
-    while (bfd->main->screens.first) {
-      BKE_id_free(bfd->main, bfd->main->screens.first);
-    }
+    /* Removing the unused workspaces, screens and wm is useless here, setup_app_data will switch
+     * those lists with the ones from old bmain, which freeing is much more efficient than
+     * individual calls to `BKE_id_free()`.
+     * Further more, those are expected to be empty anyway with new memfile reading code. */
+    BLI_assert(BLI_listbase_is_empty(&bfd->main->wm));
+    BLI_assert(BLI_listbase_is_empty(&bfd->main->workspaces));
+    BLI_assert(BLI_listbase_is_empty(&bfd->main->screens));
 
     setup_app_blend_file_data(C, bfd, "<memory1>", params, reports);
     BLO_blendfiledata_free(bfd);
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index cd65ceb889d..cffba0207cd 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -9450,7 +9450,7 @@ static BHead *read_libblock(FileData *fd,
 
   BHead *id_bhead = bhead;
 
-  if (id) {
+  if (id != NULL) {
     const short idcode = GS(id->name);
 
     if (id_bhead->code != ID_LINK_PLACEHOLDER) {
@@ -9466,9 +9466,7 @@ static BHead *read_libblock(FileData *fd,
       DEBUG_PRINTF(
           "%s: ID %s is unchanged: %d\n", __func__, id->name, fd->are_memchunks_identical);
 
-      if (fd->memfile != NULL && (fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0 &&
-          fd->are_memchunks_identical && !ELEM(idcode, ID_WM, ID_SCR, ID_WS)) {
-        BLI_assert(fd->memfile != NULL);
+      if (fd->memfile != NULL) {
         BLI_assert(fd->old_idmap != NULL);
         /* This code should only ever be reached for local data-blocks. */
         BLI_assert(main->curlib == NULL);
@@ -9476,32 +9474,47 @@ static BHead *read_libblock(FileData *fd,
         /* Find the 'current' existing ID we want to reuse instead of the one we would read from
          * the undo memfile. */
         ID *id_old = BKE_main_idmap_lookup(fd->old_idmap, idcode, id->name + 2, NULL);
-        if (id_old != NULL) {
+        bool can_finalize_and_return = false;
+
+        if (ELEM(idcode, ID_WM, ID_SCR, ID_WS)) {
+          /* Read WindowManager, Screen and WorkSpace IDs are never during undo (see
+           * `setup_app_data()` in `blendfile.c`).
+           * So we can just abort here, just ensuring libmapping is set accordingly. */
+          can_finalize_and_return = true;
+        }
+        else if ((fd->skip_flags & BLO_READ_SKIP_UNDO_OLD_MAIN) == 0 &&
+                 fd->are_memchunks_identical && id_old != NULL) {
           MEM_freeN(id);
-          id = id_old;
 
           /* Do not add LIB_TAG_NEW here, this should not be needed/used in undo case anyway (as
            * this is only for do_version-like code), but for sake of consistency, and also because
            * it will tell us which ID is re-used from old Main, and which one is actually new. */
-          id->tag = tag | LIB_TAG_NEED_LINK | LIB_TAG_UNDO_OLD_ID_REUSED;
-          id->lib = main->curlib;
-          id->us = ID_FAKE_USERS(id);
+          id_old->tag = tag | LIB_TAG_NEED_LINK | LIB_TAG_UNDO_OLD_ID_REUSED;
+          id_old->lib = main->curlib;
+          id_old->us = ID_FAKE_USERS(id_old);
           /* Do not reset id->icon_id here, memory allocated for it remains valid. */
-          id->newid = NULL; /* Needed because .blend may have been saved with crap value here... */
-          id->orig_id = NULL;
-
-          oldnewmap_insert(fd->libmap, id_bhead->old, id, id_bhead->code);
-          oldnewmap_insert(fd->libmap_undo_reused, id, id, id_bhead->code);
+          /* Needed because .blend may have been saved with crap value here... */
+          id_old->newid = NULL;
+          id_old->orig_id = NULL;
 
           Main *old_bmain = fd->old_mainlist->first;
           BLI_assert(old_bmain == BKE_main_idmap_main_get(fd->old_idmap));
           ListBase *old_lb = which_libbase(old_bmain, idcode);
           ListBase *new_lb = which_libbase(main, idcode);
-          BLI_remlink(old_lb, id);
-          BLI_addtail(new_lb, id);
+          BLI_remlink(old_lb, id_old);
+          BLI_addtail(new_lb, id_old);
+
+          can_finalize_and_return = true;
+        }
+
+        if (can_finalize_and_return) {
+          oldnewmap_insert(fd->libmap, id_bhead->old, id_old, id_bhead->code);
+          if (id_old != NULL) {
+            oldnewmap_insert(fd->libmap_undo_reused, id_old, id_old, id_bhead->code);
+          }
 
           if (r_id) {
-            *r_id = id;
+            *r_id = id_old;
           }
 
           oldnewmap_free_unused(fd->datamap);



More information about the Bf-blender-cvs mailing list