[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [13979] trunk/blender/source/blender/ blenloader/intern:

Brecht Van Lommel brechtvanlommel at pandora.be
Wed Mar 5 16:13:41 CET 2008


Revision: 13979
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=13979
Author:   blendix
Date:     2008-03-05 16:13:41 +0100 (Wed, 05 Mar 2008)

Log Message:
-----------

Undo optimization: now big chunks of memory are not written as single
memory blocks anymore, but smaller fixed size blocks, so that diffing
can be more effective. For example helps in sculpt mode when making
only local changes to the mesh, previously it would copy the whole
MVert array for each undo step.

Modified Paths:
--------------
    trunk/blender/source/blender/blenloader/intern/readfile.c
    trunk/blender/source/blender/blenloader/intern/writefile.c

Modified: trunk/blender/source/blender/blenloader/intern/readfile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/readfile.c	2008-03-05 14:58:33 UTC (rev 13978)
+++ trunk/blender/source/blender/blenloader/intern/readfile.c	2008-03-05 15:13:41 UTC (rev 13979)
@@ -858,6 +858,7 @@
 	static unsigned int seek= 1<<30;	/* the current position */
 	static unsigned int offset= 0;		/* size of previous chunks */
 	static MemFileChunk *chunk=NULL;
+	unsigned int chunkoffset, readsize, totread;
 	
 	if(size==0) return 0;
 	
@@ -875,29 +876,39 @@
 	}
 	
 	if(chunk) {
-		/* first check if it's on the end if current chunk */
-		if( seek-offset == chunk->size) {
-			offset+= chunk->size;
-			chunk= chunk->next;
-		}
+		totread= 0;
+
+		do {
+			/* first check if it's on the end if current chunk */
+			if(seek-offset == chunk->size) {
+				offset+= chunk->size;
+				chunk= chunk->next;
+			}
+
+			/* debug, should never happen */
+			if(chunk==NULL) {
+				printf("illegal read, chunk zero\n");
+				return 0;
+			}
+
+			chunkoffset= seek-offset;
+			readsize= size-totread;
+
+			/* data can be spread over multiple chunks, so clamp size
+			 * to within this chunk, and then it will read further in
+			 * the next chunk */
+			if(chunkoffset+readsize > chunk->size)
+				readsize= chunk->size-chunkoffset;
+
+			memcpy(buffer + totread, chunk->buf+chunkoffset, readsize);
+			totread += readsize;
+			filedata->seek += readsize;
+			seek += readsize;
+		} while(totread < size);
 		
-		/* debug, should never happen */
-		if(chunk==NULL) {
-			printf("illegal read, chunk zero\n");
-			return 0;
-		}
-		else if( (seek-offset)+size > chunk->size) {
-			size= chunk->size - (seek-offset);
-			printf("chunk too large, clipped to %d\n", size);
-		}
-		
-		memcpy(buffer, chunk->buf + (seek-offset), size);
-		filedata->seek += size;
-		seek+= size;
-		
-		return (size);
-		
+		return totread;
 	}
+
 	return 0;
 }
 

Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c	2008-03-05 14:58:33 UTC (rev 13978)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c	2008-03-05 15:13:41 UTC (rev 13979)
@@ -187,8 +187,11 @@
 
 #include <errno.h>
 
-/* ********* my write, buffered writing with minimum 50k chunks ************ */
+/* ********* my write, buffered writing with minimum size chunks ************ */
 
+#define MYWRITE_BUFFER_SIZE	100000
+#define MYWRITE_MAX_CHUNK	32768
+
 typedef struct {
 	struct SDNA *sdna;
 
@@ -216,7 +219,7 @@
 
 	wd->file= file;
 
-	wd->buf= MEM_mallocN(100000, "wd->buf");
+	wd->buf= MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
 
 	return wd;
 }
@@ -256,12 +259,13 @@
  * @warning Talks to other functions with global parameters
  */
  
-#define MYWRITE_FLUSH	NULL
+#define MYWRITE_FLUSH		NULL
 
 static void mywrite( WriteData *wd, void *adr, int len)
 {
 	if (wd->error) return;
 
+	/* flush helps compression for undo-save */
 	if(adr==MYWRITE_FLUSH) {
 		if(wd->count) {
 			writedata_do_write(wd, wd->buf, wd->count);
@@ -272,21 +276,33 @@
 
 	wd->tot+= len;
 	
-	if(len>50000) {
+	/* if we have a single big chunk, write existing data in
+	 * buffer and write out big chunk in smaller pieces */
+	if(len>MYWRITE_MAX_CHUNK) {
 		if(wd->count) {
 			writedata_do_write(wd, wd->buf, wd->count);
 			wd->count= 0;
 		}
-		writedata_do_write(wd, adr, len);
+
+		do {
+			int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
+			writedata_do_write(wd, adr, writelen);
+			adr = (char*)adr + writelen;
+			len -= writelen;
+		} while(len > 0);
+
 		return;
 	}
-	if(len+wd->count>99999) {
+
+	/* if data would overflow buffer, write out the buffer */
+	if(len+wd->count>MYWRITE_BUFFER_SIZE-1) {
 		writedata_do_write(wd, wd->buf, wd->count);
 		wd->count= 0;
 	}
+
+	/* append data at end of buffer */
 	memcpy(&wd->buf[wd->count], adr, len);
 	wd->count+= len;
-
 }
 
 /**





More information about the Bf-blender-cvs mailing list