24 November 2009 View Comments

Compress your serialize output using igbinary

Igbinary is a  replacement for the standard PHP serializer. While the PHP serializer uses a texual format to represent the data in the serialized version, igbinary uses a binary format which is compact. This helps in brining down the storage size of the serialized data. This helps while trying to store the data in shared memory or memcache, which uses (limited) RAM to provide faster access to data.

Since I couldn’t find any pre-built binaries, I compiled the code which I got from their site. I compiled the 1.02 build.

Here are quick steps to compile this as a PHP extension for your system ( I tried this on Ubuntu, but should work on other distros as well). Uncompress the contents of the file to a directory and head over to the directory and run the following comands one after the other. Make sure there are no errors in each stage.

>phpize
>./configure CFLAGS="-O2 -g" --enable-igbinary
>make
>make install

If all goes well, the file igbinary.so should be present in your default php extension directory. Once you see this file there, head over to your php.ini file and add the following line at the end of it so that the extension is loaded with PHP.

# Load igbinary extension
extension=igbinary.so

Once you do this, restart your apache server to reload the PHP configurations. When you run phpinfo(), you should see the following lines in the output:
igbinary in phpinfo

Once you’ve got that in your output, all you need to do is to substitute serialize with igbinary_serialize and unserialize with igbinary_unserialize in your code.

To see the differences in the two formats and see if the serialized and the subsequent unserialize I use the following script with some dummy data to print out the size of the output string of serialize and igbinary_serialize.

$arrc[3]=1;
$arrc[2]=2;
$arrc[0]=3;
$arrc[1][]="Testing";
$arrc[1][]="another data";
$arrc[1][]="structure";
$arrc[]=1;
$arrc[]=2;
$arrc[]=3;
$a = serialize($arrc);
echo "<br />size of Serialize :". strlen($a);
$b = igbinary_serialize($arrc);
echo "<br />size of igbinary :". strlen($b);

The output of this script came up showing that output from igbinary_unserialize does indeed use less space than the the output of serialize. Here’s the output of this script when I ran it:

size of Serialize :126
size of igbinary :74

If you want the igbinary functions to auto replace the default serialize in the PHP session handler, all you need to do is to add the following lines in your php.ini

# Use igbinary as session serializer
session.serialize_handler=igbinary

If you have any experience in using this extension on your projects, let us know your thoughts and observations. You can get more information on igbinary from the author’s site at: http://opensource.dynamoid.com/

View Comments to “Compress your serialize output using igbinary”

  1. Pritesh Loke 23 December 2009 at 4:07 pm #

    nice article helps me optimized my php projects.

  2. Tom / Ride Earth 8 April 2010 at 11:01 am #

    Looks good, but it would be nice to see an uncompiled PHP class to do this.


Leave a Reply

blog comments powered by Disqus