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.
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
Life Line
@jamesholden Have you ever seen the Expanse? One of the main characters shares your name!
I walked 4.4km in 51m58s
I walked 8.2km in 1h40m07s
I walked 2.4km in 38m25s
@Edent With your ActivityPub implementation, have you figured out how to allow quote posts for your bot posts yet?
📷 Brown Cap in the Grass
🚩 Herikhuizerweg, Rheden, Nederland
I walked 0.9km in 11m17s
I walked 2.8km in 25m32s
I walked 4.6km in 1h8m02s
📷 Stalkers Lane
🚩 Graywood Lane, Wealden, United Kingdom
I hiked 23.0km in 4h10m15s
I walked 3.9km in 39m07s
After my PHP 8.5 in Leeds last night, it's now time to head to Rotterdam to give the same talk there tonight!
It's cold out here, but atleast the snow is now gone.
I walked 1.7km in 13m18s
📷 Avenue Gardens
🚩 Princess Road, London Borough of Brent, United Kingdom
📷 Leafy Entrance
🚩 East Heath Road, London Borough of Camden, United Kingdom
📷 From Green to Yellow.
🚩 St John's Wood Road, City of Westminster, United Kingdom
I walked 9.1km in 1h56m24s
The Secret Maps exhibition at the British Library is well worth a visit!
We went last Sunday and it still runs to January.
📷 Mirror
🚩 The Terrace, London Borough of Richmond upon Thames, United Kingdom









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