Table of contents:

AMFEXT

AMFEXT is a PHP extension written in C that implements encoding and decoding the AMF and AMF3 messages for the PHP language. THe native encoding and decoding is extremely ast and memory efficient in respect to the php class based implementation. Additionaly flexability has been provided using callbacks to the php code.

  • AMF and AMF3 (Flex 2) messages
  • memory optimization for large data sets
  • special encoding of recordsets
  • callbacks for custom encoding and decoding of objects, xml and bytearray

For additional information and ways to help this project please visit the AMFEXT project site. Thank you Emanuele Ruffaldi for your amazing extension!

Current status: Encoding works in AMF0 and AMF3 mode

Do I have to install this?

No, the module is just used to offload the serialization process. The PHP C extension is not a replacement for amfphp; it will never be required to use amfphp, but if amfphp detects it in the php installation, it will be used instead of the equivalent php code.

Installation

Simply "pecl install amfext" and then add "extension=amf.so" int he php.ini as you would any other pecl module. Restart the webserver and now you have the speed of C!

If you would like to compile from source or use the linux or windows bindary rather than PECL download the release.

As fast as C

AMF encoding and decoding at the PHP level is not optimal, since PHP is a dynamic language, and encoding and decoding things is a repetitive, CPU and memory intensive operation. PHP, at its heart, is a thin scripting language which is powered and extended by C.

Emanuele Ruffaldi, who had previously created a Remoting implementation for C++, created a C extension for PHP which does AMF encoding and decoding. We've tested it through and through, it is very stable, and let me tell you, it is ridiculously fast. On sample data varying in size and complexity, it's 50 to 200 times faster on pure encoding and decoding than the previous PHP solution. Of course, as I've explained earlier, encoding and decoding AMF is only one of the many things that amfphp does, and the bottleneck may or may not be there depending on the size of the data. This extension will really help when sending tons of complicated data over the wire, data like large recordsets and deeply nested object trees. As users create more and more complicated apps with Flex, this will ensure that amfphp won't fall behind, and that you can continue to concentrate on your UI instead of encoding/decoding matters.

I want to stress that you don't need the extension installed to run the new version of amfphp. The extension is an add-on to PHP that the new version of amfphp will detect, load and use if it finds it, and revert to the original PHP-based encoding/decoding if it doesn't.

Here are some real world, before and after tests of amfphp's speed with this new extension:

test Hello world Big RecordSet (4x1800) Huge Recordset (20x2500) Mike 100 Mike 1000 Mike 10000 Mike 100,000
total php 28 140 1378 45 188 2228 timeout
total native 21 55 165 20 26 99 988
diff (%) 25% 61% 88% 56% 86% 96%
encode php 1 89 1303 18 157 2164 timeout
encode native 0 13 102 0 3 35 558
diff (%) 100% 85% 92% 100% 98% 98%
size 482 bytes 78.4 KB 961.5 KB 7.2 KB 69.6 KB 720 KB 7.3 MBs

The tests are:

  • A simple Hello world test
  • Sending a recordset with all the cities in Quebec with their coordinates (1 integer column, 1 string column, 2 float columns)
  • Sending a recordset with 2500 user profiles in a dating site (10 string columns and 10 int columns)
  • Mike Potter's test, adjusted so it doesn't use AMF3 repeated strings, with 100, 1000, 10,000 and 100,000 elements.

The top half of the table shows the pingback time for every test, over localhost, with the top row showing the speed of amfphp without the extension and the bottom one with the extension. The bottom half of the table shows only the amount of time spent in the encoder.

At the extreme end of the table, you should notice that it takes amfphp with the C extension less than a second to send 7.3 MBs of data consisting of 100,000 array elements each containing an object with three fields. That is absolutely insanely fast, my friends. It's so fast in fact, that it takes Flash more time to decode the AMF data than it does for amfphp to create it, and it's such a ludicrously large amount of data that Flash 9 will time out when using ObjectUtil.toString on that dataset. Note also that the regular amfphp doesn't fare bad at all in these tests, as in the large recordset example.

I should note that the serializer times shown above include all the time spent in the serializer, not just the time calling amf_encode or writeData. Therefore in the case of the large recordset, the speed improvement is not as dramatic as in the other cases because most of the time is actually being spent in the mysql recordset adapter when using the C extension, which obscures the real speed improvement. Furthermore, these tests don't use deeply nested data, but with the limited testing I've done, I can assert that the speed improvements are even better when serializing large trees.

So here's how to enable this extension: download it from Emanuele's site. On Windows, place the dll in php's ext directory and add a php_extension=php_amf.dll line in php.ini; restart Apache. On *NIX, compile php --with-amf from the source. Once that's done, load gateway.php in a browser, and you should see "AMF C Extension is loaded and enabled" on the second line. The new extension should also appear in phpinfo(). You don't have to change a setting in amfphp to enable it, if it sees it, it'll use it, if it doesn't, it'll revert to the default PHP encoding/decoding.


© amfphp.org | Disclaimer | Conditions of use