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
RE: https://phpc.social/@phpc_tv/116274041642323081
Now that phpc.tv and phpc.social are part of the same umbrella, I've upped my yearly contributions to their Open Collective: https://opencollective.com/phpcommunity/projects/phpc-social
Merge branch 'xdebug_3_5'
Merged pull request #1070
Fixed issue #2405: Handle minimum path in .xdebug directory discovery
I've published a new blog post: "Human Creations", on the difference in content generation by LLMs, and the creation of text, art and code by humans.
You can find it at https://derickrethans.nl/human-creations.html or at @blog
RE: https://phpc.social/@afilina/116274024588235234
It's good to see that more and more people are realising that the Web can be for-good, without all the enshittification.
That's why I'm happy to see endeavours like phpc.tv springing up, and helping out where I can.
Taking back the control of how the Web is for people, by people, without big tech making it all shit.
Created a waste_basket; Updated 5 crossings and a bicycle_parking
I walked 10.7km in 2h35m10s
If you're in the UK (south, preferably), the International Space Station is going to be visible straight above you in about half an hour!
If you're further north than London, it moves further to the south.
Updated 2 crossings
Created 3 crossings, 2 waste_baskets, and 2 trees; Updated a crossing
Created 2 post_boxes and a crossing; Updated a newsagent shop
Updated a crossing
Updated a post_box
Created a bench; Updated a bench
Created an information; Updated 2 benches
Updated a crossing and a supermarket shop
Confirmed a restaurant
I hiked 18.8km in 3h50m35s
Updated a pub
Updated a restaurant, a pub, and a toilet
Created a butcher shop and a restaurant; Updated a restaurant; Confirmed a bakery shop and a restaurant
I walked 10.8km in 1h44m13s
Merged pull request #1069
Bump versions for CI and release scripts


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