[Bf-blender-cvs] [0708b9a] master: writefile: reuse SDNA between writes

Campbell Barton noreply at git.blender.org
Tue Jul 12 05:00:28 CEST 2016


Commit: 0708b9aba8f7da87c4cf97b96c0bc9229fa9689c
Author: Campbell Barton
Date:   Tue Jul 12 12:53:49 2016 +1000
Branches: master
https://developer.blender.org/rB0708b9aba8f7da87c4cf97b96c0bc9229fa9689c

writefile: reuse SDNA between writes

Avoids decoding the SDNA string every undo step.

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

M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/readfile.h
M	source/blender/blenloader/intern/writefile.c
M	source/blender/makesdna/DNA_genfile.h
M	source/blender/makesdna/intern/dna_genfile.c
M	source/blender/windowmanager/intern/wm_init_exit.c
M	source/creator/creator.c

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

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index eeb8a5d..059dce2 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -1080,13 +1080,9 @@ static FileData *filedata_new(void)
 	
 	fd->filedes = -1;
 	fd->gzfiledes = NULL;
-	
-	/* XXX, this doesn't need to be done all the time,
-	 * but it keeps us re-entrant,  remove once we have
-	 * a lib that provides a nice lock. - zr
-	 */
-	fd->memsdna = DNA_sdna_from_data(DNAstr, DNAlen, false, false, NULL);
-	
+
+	fd->memsdna = DNA_sdna_current_get();
+
 	fd->datamap = oldnewmap_new();
 	fd->globmap = oldnewmap_new();
 	fd->libmap = oldnewmap_new();
@@ -1280,9 +1276,7 @@ void blo_freefiledata(FileData *fd)
 		
 		// Free all BHeadN data blocks
 		BLI_freelistN(&fd->listbase);
-		
-		if (fd->memsdna)
-			DNA_sdna_free(fd->memsdna);
+
 		if (fd->filesdna)
 			DNA_sdna_free(fd->filesdna);
 		if (fd->compflags)
diff --git a/source/blender/blenloader/intern/readfile.h b/source/blender/blenloader/intern/readfile.h
index 42728fd..b054cd0 100644
--- a/source/blender/blenloader/intern/readfile.h
+++ b/source/blender/blenloader/intern/readfile.h
@@ -74,7 +74,7 @@ typedef struct FileData {
 	
 	// general reading variables
 	struct SDNA *filesdna;
-	struct SDNA *memsdna;
+	const struct SDNA *memsdna;
 	char *compflags;        /* array of eSDNA_StructCompare */
 	
 	int fileversion;
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index f898eea..88f1c4d 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -303,7 +303,7 @@ static void ww_handle_init(eWriteWrapType ww_type, WriteWrap *r_ww)
 
 
 typedef struct {
-	struct SDNA *sdna;
+	const struct SDNA *sdna;
 
 	unsigned char *buf;
 	MemFile *compare, *current;
@@ -325,7 +325,7 @@ static WriteData *writedata_new(WriteWrap *ww)
 {
 	WriteData *wd = MEM_callocN(sizeof(*wd), "writedata");
 
-	wd->sdna = DNA_sdna_from_data(DNAstr, DNAlen, false, false, NULL);
+	wd->sdna = DNA_sdna_current_get();
 
 	wd->ww = ww;
 
@@ -357,8 +357,6 @@ static void writedata_do_write(WriteData *wd, const void *mem, int memlen)
 
 static void writedata_free(WriteData *wd)
 {
-	DNA_sdna_free(wd->sdna);
-
 	MEM_freeN(wd->buf);
 	MEM_freeN(wd);
 }
diff --git a/source/blender/makesdna/DNA_genfile.h b/source/blender/makesdna/DNA_genfile.h
index 27a3ff8..bc127ac 100644
--- a/source/blender/makesdna/DNA_genfile.h
+++ b/source/blender/makesdna/DNA_genfile.h
@@ -80,6 +80,12 @@ struct SDNA *DNA_sdna_from_data(
         const char **r_error_message);
 void DNA_sdna_free(struct SDNA *sdna);
 
+/* Access for current Blender versions SDNA*/
+void               DNA_sdna_current_init(void);
+/* borrowed reference */
+const struct SDNA *DNA_sdna_current_get(void);
+void               DNA_sdna_current_free(void);
+
 int DNA_struct_find_nr_ex(const struct SDNA *sdna, const char *str, unsigned int *index_last);
 int DNA_struct_find_nr(const struct SDNA *sdna, const char *str);
 void DNA_struct_switch_endian(const struct SDNA *oldsdna, int oldSDNAnr, char *data);
diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c
index f1d48c0..1e3c91d 100644
--- a/source/blender/makesdna/intern/dna_genfile.c
+++ b/source/blender/makesdna/intern/dna_genfile.c
@@ -605,6 +605,30 @@ SDNA *DNA_sdna_from_data(
 	}
 }
 
+/**
+ * Using globals is acceptable here, the data is read-only and only changes between Blender versions.
+ *
+ * So it is safe to create once and reuse.
+ */
+static SDNA *g_sdna = NULL;
+
+void DNA_sdna_current_init(void)
+{
+	g_sdna = DNA_sdna_from_data(DNAstr, DNAlen, false, false, NULL);
+}
+
+const struct SDNA *DNA_sdna_current_get(void)
+{
+	BLI_assert(g_sdna != NULL);
+	return g_sdna;
+}
+
+void DNA_sdna_current_free(void)
+{
+	DNA_sdna_free(g_sdna);
+	g_sdna = NULL;
+}
+
 /* ******************** END READ DNA ********************** */
 
 /* ******************* HANDLE DNA ***************** */
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index 9d1083b..3022d86 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -40,6 +40,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "DNA_genfile.h"
 #include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
 #include "DNA_windowmanager_types.h"
@@ -584,6 +585,8 @@ void WM_exit_ext(bContext *C, const bool do_python)
 	
 	GHOST_DisposeSystemPaths();
 
+	DNA_sdna_current_free();
+
 	BLI_threadapi_exit();
 
 	BKE_blender_atexit();
diff --git a/source/creator/creator.c b/source/creator/creator.c
index e2761c9..289e68f 100644
--- a/source/creator/creator.c
+++ b/source/creator/creator.c
@@ -42,6 +42,8 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "DNA_genfile.h"
+
 #include "BLI_args.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
@@ -350,6 +352,8 @@ int main(
 
 	BLI_threadapi_init();
 
+	DNA_sdna_current_init();
+
 	BKE_blender_globals_init();  /* blender.c */
 
 	IMB_init();




More information about the Bf-blender-cvs mailing list