[development] Re: [drupal-devel] performance improvements - avoiding big unserialize()

Richard Archer drupal.org at juggernaut.com.au
Wed Nov 30 06:00:36 UTC 2005


At 3:10 PM +0200 21/10/05, Moshe Weitzman wrote:

>In speaking with Rasmus last night, he discouraged us from using
>unserialize() on large arrays when possible. This eats memory and speed. We
>do this at lkeast twice on evrry page view: the menu cache and the variables
>cache (i.e. $conf).
>
>Rasmus' suggestion was to store these arrays without serialization in a
>shared memory system like APC.

I have set up and run some tests comparing various ways of
retrieving an array from storage.

The storage mechanisms I have tested are:
Storing serialized data in a file,
Storing var_export output in a file,
Storing the array in APC.

I haven't tested storing the serialized array in a database
because that would just be testing the database performance,
whereas I'm primarily interested in testing unserialize.

With each test I verified how much memory was available to
the script by allocating memory to exhaustion as the last step.
This confirmed the summary results below.

As I understand it, Rasmus said at DrupalCon that unserialize
allocates and does not release a certain amount of memory.
I don't see any evidence of this.

I do however see a massive memory leak in apc_fetch!
It seems to lose about 7 kB of memory each time it's called.
I wouldn't like to roll out a system which uses APC in its
current state, and I have disabled it on my server.

It is interesting to note that the only way to benefit from
using APC is to store the array in a file and include it.
Not worth the security risk, IMHO.

My conclusion is that using unserialize is quite OK and there
is no need or benefit to changing the way arrays are stored.

The test script can be downloaded from:
http://mel01.juggernaut.com.au/arraytest.php.gz


Here are the results of my tests.

Reading serialized data from a file with fread and then
unserializing it:
time: 0.5022668838501 seconds elapsed for 100 iterations.
memory: 120808 bytes consumed.

Reading serialized data from a file with implode('', file())
and then unserializing it:
time: 0.52363586425781 seconds elapsed for 100 iterations.
memory: 118408 bytes consumed.

Including var_export output from a file, APC disabled:
time: 1.392637014389 seconds elapsed for 100 iterations.
memory: 122096 bytes consumed.

Including var_export output from a file, APC enabled:
time: 0.33631801605225 seconds elapsed for 100 iterations.
memory: 121640 bytes consumed.

Fetching data from APC:
time: 0.48792600631714 seconds elapsed for 100 iterations.
memory: 836016 bytes consumed.

Test system is:
Apache/2.0.53 (Fedora)
PHP Version 4.3.11
APC Version 3.0.8

 ...Richard.


More information about the development mailing list