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
Updated an estate_agent office
I went to my nieces' birthday party yesterday.
The theme was pink, and that included all the food, mostly died with beet root.
Shock and horror this morning when doing number two. Not only was my turd dark red, it was also glittering at me. Apparently the carrot cake had edible glitter...
So now I know what's worse than glitter.
😂 ✨ 💩 🟣Long-Tailed Tit on a Branch with Lichen
I've been spending some time in random London local nature reserves.
Sitting and listening, and in fifteen minutes you spot countless species.
This one was in Ham Lands Local Nature Reserve near Teddington.
#london #BirdPhotogaphy #BirdsOfMastodon #Birds #LichenSubscribe
A Colourful Mandarin
In The Long Water in Kensington Palace Gardens, London.
Created 7 benches
Created 2 benches
Created a bench
I walked 7.3km in 2h28m39s
Added a note about a duplicate Papersmiths
I walked 4.1km in 49m02s
Fixed website
fix typo
Updated a bench
I walked 1.6km in 20m26s
I walked 1.1km in 11m49s
The Yellow Eye
A blue heron's head, with its very yellow stare-y eye.
#BirdPhotography #Photography #BirdsOfFediverse #BirdsOfMastodon #London
My little Lego box is telling me it really is quite warm outside.
Created a bicycle_parking and a crossing
I walked 3.3km in 41m56s
What the United States and Israel are doing to Iran is undistinguishable from Russia is doing to Ukraine.
Created 8 waste_baskets, 7 benches, and 5 other objects
I walked 2.7km in 42m28s
The Early Bird Catches the Worm







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