Technically, this is postponed on #1757550: [Meta] Convert core theme functions to Twig templates, but hopefully we could get a good start well before then.

But we need to understand the performance implications of Twig, and hopefully mitigate some of that prior to merging it in.

I'm currently aware of the following performance-related Twig issues, but there may be more:

- #1922304: Remove TwigReference objects in favor of a high speed implementation by using NodeVisitors more cleverly
- #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code
- #1938430: Don't add a default theme hook class in template_preprocess()
- #1979290: Add static caching to Twig_Environment::getTemplateClass()

There's also some really nice profiling data at #1818266: [meta] A secure theme system (with twig), but that deals with the auto-escape system and this issue's about more "raw" data just in template/theme function conversions before we start adding in more doo-dads.


webchick’s picture

Component:rest.module» theme system


Cottser’s picture

Nice. Thanks for opening this, added a couple issues to the summary. More to come :)

Fabianx’s picture

What is important is to no longer test against the base overhead of Twig itself (that was fine according to all involved), but to the overhead when twig is already loaded to isolate things.

Of course we can also again test the "raw" overhead, but that is dependent on class loading, etc.

That means compare:

* one node in core (rendered via twig) and 200 calls to views-view-field.tpl.php


* one node in core (rendered via twig) and 200 calls to views-view-field.html.twig

Also make sure to activate APC and APC class loader.


Shameless plug: And make sure to checkout for easy branch testing, which allows to upload run data for all others to see and such full disclosure of tested things.

If you need an API key, please contact me via contact form or in IRC.


Fabianx’s picture

Assigned:Unassigned» Fabianx

Taking a stab at this.

Fabianx’s picture

Assigned:Fabianx» Unassigned

Preliminary Results

Here is a first stab at the results (using the twig-in-core sandbox on github rebased on 8.x):

From theme_ to template_ the overhead seems to be 0.168 ms per call.

=> for 300 theme calls we end up with:

0.168 ms * 300 theme calls ~= around 50 ms

=> around 10% slower

Therefore with optimizations for template_preprocess and drupal_html_class we end up with the following overhead currently:

=== head-stark-21-nodes-debug2..tic--gtc--nn--optimized--debug2 compared (517dc23f63001..517dc41118628):

ct  : 130,183|143,923|13,740|10.6%
wt  : 494,774|546,516|51,742|10.5%
cpu : 492,031|544,035|52,004|10.6%
mu  : 22,341,736|23,808,008|1,466,272|6.6%
pmu : 22,597,616|23,985,944|1,388,328|6.1%

This is a special debug build both for core and twig that enables us to compare twig and theme functions directly, for example like for breadcrumb:

In general one can always look at the: profiler_debug_ to find the difference, however recursive rendering makes this still difficult to compare for more complex theme / tpl.php vs. twig functions.

Here is the patch for making profiling fun:

Replace theme2_core with theme2_twig in theme() for the twig branch.

Fabianx’s picture

404 page with and html_tag conversion patches removed and adding:

* Get Template Class static caching:
* drupal_html_class static caching:
* Lazy load Attribute():

using the "make profiling fun" patch in both HEAD and Twig.

=== head-404-debug3..twig-404-gtc-lla-debug3--optimized compared (5185c8620c8e8..5185cca3dd8d9):

ct  : 17,224|18,153|929|5.4%
wt  : 80,836|85,527|4,691|5.8%
cpu : 76,005|84,005|8,000|10.5%
mu  : 11,260,584|12,179,296|918,712|8.2%
pmu : 11,457,088|12,369,560|912,472|8.0%

I think that is pretty good. While 6% still looks much, it is really just 5 ms and considering that all classes need to be loaded, that is really good.

Around: 0.6 ms is the slowdown for example for conversion of theme_breadcrumb, but that is called only once usually.

catch’s picture

Status:Postponed» Fixed

This has been done in terms of phptemplate -> twig and individual theme functions are being profiled, so I think we can close this. There are issues open for specific Twig performance issues.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary:View changes

Add some more performance-related issues