Twig extension

A while ago, Fabien asked me to have a look at porting one of Twig's slowest methods, TwigTemplate::getAttribute(), into a PHP extension. It is a complex method that does a lot of different checks and look-ups. Fabien's benchmarks showed that this method was responsible for quite a large amount of time. On top of that, it didn't seem that it could be optimised any further as PHP code itself. Seeing that it was very likely that this method would be a lot faster when written in C, SensioLabs decided to sponser me for the development of a port of this method into a PHP extension.

Over the next few months I've worked on re-implementing just this method as a C-function in twig-ext: twig_template_get_attributes(). From initial benchmarks it looks like this function as implemented in the extension gives about a 15% speed increase while rendering templates. Twig's compiled templates directly contain a call to this function (as opposed to marshal it through the original TwigTemplate::getAttribute() method) for additional performance. This probably means that you'll have to remove your compiled templates cache while upgrading.

The extension at http://github.com/derickr/twig-ext has now been merged, via http://github.com/derickr/Twig, into http://github.com/fabpot/Twig. The plan is that this new improvement is going to be part of Twig 1.4.

If you are also interested in having some of your PHP code ported into a PHP extension in C, please feel free to contact me.

Shortlink

This article has a short URL available: http://drck.me/twig-ext-8un

Comments

This is a great news for all of us PHP-people out there! Thank you for doing this!

Awesome, thanks derick!

Cool, thanks Derick! Twig is the best.

I'm glad to see the sponsorship, hopefully you get more!

Great to hear. Good to know Sensio sponsored for it. So you get some penny also.

Great partnering! Was opcode caching (APC/Xcache etc) active when you were doing the 15% benchmark? I have always wondered about the C ext vs opcode-cached PHP performance in general.

@Halil: The benchmarks were being run from the command line, so no APC or other cache had any effect.

I've yet to find information on how to actually use/install the extension though. I assume that I must compile my own version of PHP with the necessary compiler flags to include the extension? I assume it's not at the point where you can add an twig-ext.so to your extensions dir?

@Jonathan: No need to recompile PHP. You can easily install the extension by checking out from github and running some commands (as long as you have a proper PHP development environment set-up):

git clone https://github.com/fabpot/Twig.git
cd ext/twig
phpize && ./configure && make install

And then add to your php.ini:

extension=twig.so

In the future you can hopefully install the extension by running:

pecl install twig



Hi Derick,

Thanks for the extension it really works well. Here is a benchmark I made about it:

http://www.strangebuzz.com/post/2013/02/21/Twig-C-extension-benchmark

See you. COil

Add Comment

Name:
Email:

Will not be posted. Please leave empty instead of filling in garbage though!
Comment:

Please follow the reStructured Text format. Do not use the comment form to report issues in software, use the relevant issue tracker. I will not answer them here.


All comments are moderated

Life Line