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
Merged pull request #1029
Reflow some comments
Add comments, add end of file newlines, fix php 8.5 compilation
Benchmark Xdebug performance
Merged pull request #1051
PHP 8.6: printf() is now optimised out if it only uses %s and %d (and…
PHP 8.6: The object IDs of objects changed in tests
PHP 8.6: ZSTR_INIT_LITERAL() no longer takes non-literals, so use zen…
PHP 8.6: WRONG_PARAM_COUNT has been removed in favour of zend_wrong_p…
PHP 8.6: zval_dtor() has been deprecated in favour of zval_ptr_dtor_n…
Update test for version constraints, as well as the error messages
I walked 7.4km in 1h38m15s
I just watched an interesting and lovely documentary on BBC Four: The Magical World of Moss.
It's on the iPlayer for another month:
https://www.bbc.co.uk/programmes/m001hqthI walked 8.2km in 1h26m34s
Merge branch 'xdebug_3_5'
Document to use PIE instead of PECL in the README
I walked 9.2km in 1h45m44s
I walked 10.1km in 1h40m23s
If we must show off a little…
In more info innocent times, the hat riots caused some spankings...
100 Years Ago Men and Boys Fought on the Streets of New York Over Wearing Straw Hats Past Summer | The New York Public Library
https://www.nypl.org/blog/2022/09/23/straw-hat-riots-nyc


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