php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32727 Slow serialize on FreeBSD
Submitted: 2005-04-16 13:08 UTC Modified: 2016-12-30 22:45 UTC
Votes:7
Avg. Score:5.0 ± 0.0
Reproduced:6 of 6 (100.0%)
Same Version:5 (83.3%)
Same OS:6 (100.0%)
From: cws at miraclenet dot co dot th Assigned:
Status: Open Package: Variables related
PHP Version: 5.*, 4.* OS: FreeBSD
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-04-16 13:08 UTC] cws at miraclenet dot co dot th
Description:
------------
serialize use a lot of realloc which are very slow on FreeBSD

From - http://rgarciasuarez.free.fr/p5p/p5p-200307-1.pod -

Dan Kogai explained that FreeBSD comes with an implementation of malloc()
that is optimized for paged memory, and safe from duplicate free() calls.
But the downside is that realloc() is very slow. That's usually not a big
deal, because most programs don't use realloc() very often -- but perl
does. (The default configuration of perl on FreeBSD is to use perl's
internal malloc, that hasn't this realloc limitation.)

Serialize use a fix incremental value (SMART_STR_PREALLOC in php_smart_ptr.h). It better for serialize to use exponential incremential value to reduce number of realloc. 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-16 13:39 UTC] cws at miraclenet dot co dot th
This is a patch 

t42# diff -u ext/standard/php_smart_str.h /home/cws/php_smart_str.h
--- ext/standard/php_smart_str.h        Wed Apr 16 16:12:37 2003
+++ /home/cws/php_smart_str.h   Sat Apr 16 18:27:30 2005
@@ -32,6 +32,10 @@
 #define SMART_STR_PREALLOC 128
 #endif

+#ifndef SMART_PTR_MAX_PREALLOC
+#define SMART_PTR_MAX_PREALLOC 1048576
+#endif
+
 #ifdef SMART_STR_USE_REALLOC
 #define SMART_STR_REALLOC(a,b,c) realloc((a),(b))
 #else
@@ -42,8 +46,11 @@
        if (!d->c) d->len = d->a = 0; \
        newlen = d->len + n; \
        if (newlen >= d->a) {\
-               d->c = SMART_STR_REALLOC(d->c, newlen + SMART_STR_PREALLOC + 1, what); \
-               d->a = newlen + SMART_STR_PREALLOC; \
+               size_t pre_alloc = newlen *2;\
+               if ( pre_alloc > SMART_PTR_MAX_PREALLOC ) { pre_alloc = SMART_PTR_MAX_PREALLOC; }\
+               if ( pre_alloc < SMART_STR_PREALLOC) { pre_alloc = SMART_STR_PREALLOC; }\
+               d->c = SMART_STR_REALLOC(d->c, newlen + pre_alloc + 1, what); \
+               d->a = newlen + pre_alloc; \
        }\
 }
 [2005-04-16 16:38 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip

PHP 5.1-dev has got some optimizing done in this part, see if it's any better (without your patch of course)

 [2005-04-19 02:36 UTC] cws at miraclenet dot co dot th
No it don't help me to speed up my test code.
This is the result

cws@t42$ ./php test.php
Content-type: text/html
X-Powered-By: PHP/5.1.0-dev

<PRE>version:5.1.0-dev
Length: 2798041
Serialize time:  elapse(2.136879)

cws@t42$ /usr/local/bin/php test.php
<PRE>version:4.3.11
Length: 2798041
Serialize time:  elapse(0.200818)

The test code and data can be download at
http://freebie.miraclenet.co.th/tmp/test_serialize.tgz

The test data may seem too extreme (an array of 2.8M) but it is the one that our customer really use.
 [2005-04-19 02:48 UTC] cws at miraclenet dot co dot th
I forget to mension that the previous result is 
from my patched version.
The result of unpatch one is

cws@t42# ./php test_serialize.php
version:4.3.11
Length: 2798041
Serialize time:  elapse(2.554760)
 [2007-06-05 08:37 UTC] runner at protom dot org
I hope this patch to be taken in main
 [2009-07-14 12:13 UTC] ale at FreeBSD dot org
Can you try the following patch and tell me the results and which FreeBSD version you are using?

--- ext/standard/php_smart_str.h.orig    2009-07-14 14:08:07.000000000 +0200
+++ ext/standard/php_smart_str.h 2009-07-14 14:08:12.000000000 +0200
@@ -30,12 +30,14 @@

 #define smart_str_0(x) do {                                                                                     \
         if ((x)->c) {                                                                                                   \
+                (x)->a = (x)->len; \
+                SMART_STR_DO_REALLOC(x, 0); \
                 (x)->c[(x)->len] = '\0';                                                                        \
         }                                                                                                                               \
 } while (0)

 #ifndef SMART_STR_PREALLOC
-#define SMART_STR_PREALLOC 128
+#define SMART_STR_PREALLOC 1048576
 #endif

 #ifndef SMART_STR_START_SIZE
 [2016-12-30 22:45 UTC] cmb@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2016-12-30 22:45 UTC] cmb@php.net
-Package: *General Issues +Package: Variables related
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Nov 15 20:01:39 2019 UTC