[Bf-blender-cvs] [55546b4] master: writefile: replace most struct lookups /w constants

Campbell Barton noreply at git.blender.org
Tue Jun 28 12:29:43 CEST 2016


Commit: 55546b4e135283f2ec4b5715536e59722c55f11c
Author: Campbell Barton
Date:   Tue Jun 28 20:05:42 2016 +1000
Branches: master
https://developer.blender.org/rB55546b4e135283f2ec4b5715536e59722c55f11c

writefile: replace most struct lookups /w constants

Removes many hash lookups per file-save and undo-step.

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

M	source/blender/blenloader/CMakeLists.txt
M	source/blender/blenloader/intern/writefile.c
M	source/blender/makesdna/intern/CMakeLists.txt
M	source/blender/makesdna/intern/makesdna.c

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

diff --git a/source/blender/blenloader/CMakeLists.txt b/source/blender/blenloader/CMakeLists.txt
index 8364df3..479d3a1 100644
--- a/source/blender/blenloader/CMakeLists.txt
+++ b/source/blender/blenloader/CMakeLists.txt
@@ -35,6 +35,10 @@ set(INC
 	../nodes
 	../render/extern/include
 	../../../intern/guardedalloc
+
+	# for writefile.c: dna_type_offsets.h
+	${CMAKE_BINARY_DIR}/source/blender/makesdna/intern
+
 )
 
 set(INC_SYS
@@ -74,3 +78,6 @@ if(WITH_CODEC_FFMPEG)
 endif()
 
 blender_add_lib(bf_blenloader "${SRC}" "${INC}" "${INC_SYS}")
+
+# needed so writefile.c can use dna_type_offsets.h
+add_dependencies(bf_blenloader bf_dna)
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 5a7b2a7..9cf32e1 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -184,6 +184,9 @@
 
 #include "readfile.h"
 
+/* for SDNA_TYPE_FROM_STRUCT() macro */
+#include "dna_type_offsets.h"
+
 #include <errno.h>
 
 /* ********* my write, buffered writing with minimum size chunks ************ */
@@ -367,7 +370,7 @@ static void writedata_free(WriteData *wd)
  * \param len Length of new chunk of data
  * \warning Talks to other functions with global parameters
  */
- 
+
 #define MYWRITE_FLUSH		NULL
 
 static void mywrite(WriteData *wd, const void *adr, int len)
@@ -462,13 +465,15 @@ static int endwrite(WriteData *wd)
 
 /* ********** WRITE FILE ****************** */
 
-static void writestruct_at_address(
-        WriteData *wd, int filecode, const char *structname, int nr,
+static void writestruct_at_address_nr(
+        WriteData *wd, int filecode, const int struct_nr, int nr,
         const void *adr, const void *data)
 {
 	BHead bh;
 	const short *sp;
 
+	BLI_assert(struct_nr > 0 && struct_nr < SDNA_TYPE_MAX);
+
 	if (adr == NULL || data == NULL || nr == 0) {
 		return;
 	}
@@ -478,11 +483,7 @@ static void writestruct_at_address(
 	bh.old = adr;
 	bh.nr = nr;
 
-	bh.SDNAnr = DNA_struct_find_nr(wd->sdna, structname);
-	if (bh.SDNAnr == -1) {
-		printf("error: can't find SDNA code <%s>\n", structname);
-		return;
-	}
+	bh.SDNAnr = struct_nr;
 	sp = wd->sdna->structs[bh.SDNAnr];
 
 	bh.len = nr * wd->sdna->typelens[sp[0]];
@@ -495,11 +496,35 @@ static void writestruct_at_address(
 	mywrite(wd, data, bh.len);
 }
 
-static void writestruct(
+static void writestruct_at_address_id(
+        WriteData *wd, int filecode, const char *structname, int nr,
+        const void *adr, const void *data)
+{
+	if (adr == NULL || data == NULL || nr == 0) {
+		return;
+	}
+
+	const int SDNAnr = DNA_struct_find_nr(wd->sdna, structname);
+	if (UNLIKELY(SDNAnr == -1)) {
+		printf("error: can't find SDNA code <%s>\n", structname);
+		return;
+	}
+
+	writestruct_at_address_nr(wd, filecode, SDNAnr, nr, adr, data);
+}
+
+static void writestruct_nr(
+        WriteData *wd, int filecode, const int struct_nr, int nr,
+	const void *adr)
+{
+	writestruct_at_address_nr(wd, filecode, struct_nr, nr, adr, adr);
+}
+
+static void writestruct_id(
         WriteData *wd, int filecode, const char *structname, int nr,
         const void *adr)
 {
-	writestruct_at_address(wd, filecode, structname, nr, adr, adr);
+	writestruct_at_address_id(wd, filecode, structname, nr, adr, adr);
 }
 
 static void writedata(WriteData *wd, int filecode, int len, const void *adr)  /* do not use for structs */
@@ -525,16 +550,45 @@ static void writedata(WriteData *wd, int filecode, int len, const void *adr)  /*
 }
 
 /* use this to force writing of lists in same order as reading (using link_list) */
-static void writelist(WriteData *wd, int filecode, const char *structname, const ListBase *lb)
+static void writelist_nr(WriteData *wd, int filecode, const int struct_nr, const ListBase *lb)
 {
 	const Link *link = lb->first;
 
 	while (link) {
-		writestruct(wd, filecode, structname, 1, link);
+		writestruct_nr(wd, filecode, struct_nr, 1, link);
 		link = link->next;
 	}
 }
 
+#if 0
+static void writelist_id(WriteData *wd, int filecode, const char *structname, const ListBase *lb)
+{
+	const Link *link = lb->first;
+	if (link) {
+
+		const int struct_nr = DNA_struct_find_nr(wd->sdna, structname);
+		if (struct_nr == -1) {
+			printf("error: can't find SDNA code <%s>\n", structname);
+			return;
+		}
+
+		while (link) {
+			writestruct_nr(wd, filecode, struct_nr, 1, link);
+			link = link->next;
+		}
+	}
+}
+#endif
+
+#define writestruct_at_address(wd, filecode, struct_id, nr, adr, data) \
+	writestruct_at_address_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr, data)
+
+#define writestruct(wd, filecode, struct_id, nr, adr) \
+	writestruct_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), nr, adr)
+
+#define writelist(wd, filecode, struct_id, lb) \
+	writelist_nr(wd, filecode, SDNA_TYPE_FROM_STRUCT(struct_id), lb)
+
 /* *************** writing some direct data structs used in more code parts **************** */
 /*These functions are used by blender's .blend system for file saving/loading.*/
 void IDP_WriteProperty_OnlyData(const IDProperty *prop, void *wd);
@@ -564,7 +618,7 @@ static void IDP_WriteIDPArray(const IDProperty *prop, void *wd)
 		const IDProperty *array = prop->data.pointer;
 		int a;
 
-		writestruct(wd, DATA, "IDProperty", prop->len, array);
+		writestruct(wd, DATA, IDProperty, prop->len, array);
 
 		for (a = 0; a < prop->len; a++) {
 			IDP_WriteProperty_OnlyData(&array[a], wd);
@@ -608,7 +662,7 @@ void IDP_WriteProperty_OnlyData(const IDProperty *prop, void *wd)
 
 void IDP_WriteProperty(const IDProperty *prop, void *wd)
 {
-	writestruct(wd, DATA, "IDProperty", 1, prop);
+	writestruct(wd, DATA, IDProperty, 1, prop);
 	IDP_WriteProperty_OnlyData(prop, wd);
 }
 
@@ -632,7 +686,7 @@ static void write_previews(WriteData *wd, const PreviewImage *prv_orig)
 			prv.h[1] = 0;
 			prv.rect[1] = NULL;
 		}
-		writestruct_at_address(wd, DATA, "PreviewImage", 1, prv_orig, &prv);
+		writestruct_at_address(wd, DATA, PreviewImage, 1, prv_orig, &prv);
 		if (prv.rect[0]) {
 			writedata(wd, DATA, prv.w[0] * prv.h[0] * sizeof(unsigned int), prv.rect[0]);
 		}
@@ -647,7 +701,7 @@ static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
 	FModifier *fcm;
 
 	/* Write all modifiers first (for faster reloading) */
-	writelist(wd, DATA, "FModifier", fmodifiers);
+	writelist(wd, DATA, FModifier, fmodifiers);
 
 	/* Modifiers */
 	for (fcm = fmodifiers->first; fcm; fcm = fcm->next) {
@@ -656,7 +710,7 @@ static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
 		/* Write the specific data */
 		if (fmi && fcm->data) {
 			/* firstly, just write the plain fmi->data struct */
-			writestruct(wd, DATA, fmi->structName, 1, fcm->data);
+			writestruct_id(wd, DATA, fmi->structName, 1, fcm->data);
 
 			/* do any modifier specific stuff */
 			switch (fcm->type) {
@@ -677,7 +731,7 @@ static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
 
 					/* write envelope data */
 					if (data->data) {
-						writestruct(wd, DATA, "FCM_EnvelopeData", data->totvert, data->data);
+						writestruct(wd, DATA, FCM_EnvelopeData, data->totvert, data->data);
 					}
 
 					break;
@@ -701,14 +755,14 @@ static void write_fcurves(WriteData *wd, ListBase *fcurves)
 {
 	FCurve *fcu;
 
-	writelist(wd, DATA, "FCurve", fcurves);
+	writelist(wd, DATA, FCurve, fcurves);
 	for (fcu = fcurves->first; fcu; fcu = fcu->next) {
 		/* curve data */
 		if (fcu->bezt) {
-			writestruct(wd, DATA, "BezTriple", fcu->totvert, fcu->bezt);
+			writestruct(wd, DATA, BezTriple, fcu->totvert, fcu->bezt);
 		}
 		if (fcu->fpt) {
-			writestruct(wd, DATA, "FPoint", fcu->totvert, fcu->fpt);
+			writestruct(wd, DATA, FPoint, fcu->totvert, fcu->fpt);
 		}
 
 		if (fcu->rna_path) {
@@ -720,10 +774,10 @@ static void write_fcurves(WriteData *wd, ListBase *fcurves)
 			ChannelDriver *driver = fcu->driver;
 			DriverVar *dvar;
 
-			writestruct(wd, DATA, "ChannelDriver", 1, driver);
+			writestruct(wd, DATA, ChannelDriver, 1, driver);
 
 			/* variables */
-			writelist(wd, DATA, "DriverVar", &driver->variables);
+			writelist(wd, DATA, DriverVar, &driver->variables);
 			for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
 				DRIVER_TARGETS_USED_LOOPER(dvar)
 				{
@@ -748,17 +802,17 @@ static void write_actions(WriteData *wd, ListBase *idbase)
 
 	for (act = idbase->first; act; act = act->id.next) {
 		if (act->id.us > 0 || wd->current) {
-			writestruct(wd, ID_AC, "bAction", 1, act);
+			writestruct(wd, ID_AC, bAction, 1, act);
 			write_iddata(wd, &act->id);
 
 			write_fcurves(wd, &act->curves);
 
 			for (grp = act->groups.first; grp; grp = grp->next) {
-				writestruct(wd, DATA, "bActionGroup", 1, grp);
+				writestruct(wd, DATA, bActionGroup, 1, grp);
 			}
 
 			for (marker = act->markers.first; marker; marker = marker->next) {
-				writestruct(wd, DATA, "TimeMarker", 1, marker);
+				writestruct(wd, DATA, TimeMarker, 1, marker);
 			}
 		}
 	}
@@ -774,12 +828,12 @@ static void write_keyingsets(WriteData *wd, ListBase *list)
 
 	for (ks = list->first; ks; ks = ks->next) {
 		/* KeyingSet */
-		writestruct(wd, DATA, "KeyingSet", 1, ks);
+		writestruct(wd, DATA, KeyingSet, 1, ks);
 
 		/* Paths */
 		for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
 			/* Path */
-			writestruct(wd, DATA, "KS_Path", 1, ksp);
+			writestruct(wd, DATA, KS_Path, 1, ksp);
 
 			if (ksp->rna_path) {
 				writedata(wd, DATA, strlen(ksp->rna_path) + 1, ksp->rna_path);
@@ -792,7 +846,7 @@ static void write_nlastrips(WriteData *wd, ListBase *strips)
 {
 	NlaStrip *strip;
 
-	writelist(wd, DATA, "NlaStrip", strips);
+	writelist(wd, DATA, NlaStrip, strips);
 	for (strip = strips->first; strip; strip = strip->next) {
 		/* write the strip's F-Curves and modifiers */
 		write_fcurves(wd, &strip->fcurves);
@@ -810,7 +864,7 @@ static void write_nladata(WriteData *wd, ListBase *nlabase)
 	/* write all the tracks */
 	for (nlt = nlabase->first; nlt; nlt = nlt->next) {
 		/* write the track first */
-		writestruct(wd, DATA, "NlaTrack", 1, nlt);
+		writestruct(wd, DATA, NlaTrack, 1, nlt);
 
 		/* write the track's strips */
 		write_nlastrips(wd, &nlt->strips);
@@ -822,7 +876,7 @@ static void write_animdata(WriteData *wd, AnimData *adt)
 	AnimOverride *aor

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list