Twig will have some overhead. Currently the overhead of having everything in twig templates seems to be around 0.165 ms per theme() call.
While this is not twig specific, we can maybe add better caching to the theme layer by gathering semantic information on the input,
It would be too slow and probably not wise to gather information about all passed variables and creating an md5 of that. (shown in autoescape initial patch that deep array traversing is slow).
However what we can do is add a "cache key" to the hook_theme() definition like for example for theme_username:
'username' => array(
'variables' => array(
'account' => NULL,
),
'cache key' => array(
'account' => array('uid'),
),
);
This would allow a theme engine like twig or even the overall theme engine to cache this based on the cache_key_information. For more complex objects, the cache key could be also more complex by just adding more data to it.
This would:
a) be not too difficult to implement
b) easily extensible
c) make Drupal at least in cached state faster
To prevent that this might be slower for sites using DB cache backend, that would be a new option like "cache blocks" (if that still exists then).
It could be extensible also with cache tags, by providing them in the array as well:
'username' => array(
'variables' => array(
'account' => NULL,
),
'cache key' => array(
'account' => array('uid'),
),
'cache tags' => array(
'user',
),
);
That would make clearing natural as well at least for objects or where this is rendered in a certain context.
h3. TODO
* Patch
* Benchmarks
* Tests
* Documentation