> The specific case I had was serializing an array containing a whole
> bunch of arrays representing the data from tables in a database.  I
> don't recall how many rows there were, but the serialized data was
> around 5 MB.  This was on windows, and feels like realloc doing
> over-time, so a fix could be something as simple as tweaking the
> smart_str chunking code; start with a fair initial size and perhaps
> having it double the size on each realloc.

    The default values you find inside php_smart_str.h are tuned
    for efficient usage of the engine's allocator.  They are not
    tuned for huge real-world usage.

    The attached patch should change that for var.c.
    
    - Sascha
        
Index: php_smart_str.h
===================================================================
RCS file: /repository/php-src/ext/standard/php_smart_str.h,v
retrieving revision 1.28
diff -u -r1.28 php_smart_str.h
--- php_smart_str.h	8 Jan 2004 17:32:51 -0000	1.28
+++ php_smart_str.h	24 Oct 2004 11:09:23 -0000
@@ -40,6 +40,10 @@
 #define SMART_STR_START_SIZE 78
 #endif
 
+#ifndef SMART_STR_DOUBLE_ALLOC
+#define SMART_STR_DOUBLE_ALLOC 0
+#endif
+
 #ifdef SMART_STR_USE_REALLOC
 #define SMART_STR_REALLOC(a,b,c) realloc((a),(b))
 #else
@@ -49,6 +53,21 @@
 #define SMART_STR_DO_REALLOC(d, what) \
 	(d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 1, (what))
 
+#if SMART_STR_DOUBLE_ALLOC == 1
+
+#define SMART_STR_NEW_SIZE(d, newlen) do {							\
+	do {															\
+		(d)->a <<= 1;												\
+	} while (newlen >= (d)->a);										\
+} while (0)
+#else
+
+#define SMART_STR_NEW_SIZE(d, newlen) do {							\
+	(d)->a = newlen + SMART_STR_PREALLOC;							\
+} while (0)
+
+#endif
+
 #define smart_str_alloc4(d, n, what, newlen) do {					\
 	if (!(d)->c) {													\
 		(d)->len = 0;												\
@@ -60,7 +79,7 @@
 	} else {														\
 		newlen = (d)->len + (n);									\
 		if (newlen >= (d)->a) {										\
-			(d)->a = newlen + SMART_STR_PREALLOC;					\
+			SMART_STR_NEW_SIZE(d, newlen);							\
 			SMART_STR_DO_REALLOC(d, what);							\
 		}															\
 	}																\
Index: var.c
===================================================================
RCS file: /repository/php-src/ext/standard/var.c,v
retrieving revision 1.196
diff -u -r1.196 var.c
--- var.c	8 Oct 2004 19:02:00 -0000	1.196
+++ var.c	24 Oct 2004 11:09:23 -0000
@@ -30,6 +30,11 @@
 #include "php.h"
 #include "php_string.h"
 #include "php_var.h"
+
+#define SMART_STR_PREALLOC 1
+#define SMART_STR_START_SIZE 2048
+#define SMART_STR_DOUBLE_ALLOC 1
+
 #include "php_smart_str.h"
 #include "basic_functions.h"
 #include "php_incomplete_class.h"
-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to