What happened?
Description
After upgrading an older site to Craft 5, I noticed that a lot of pages were generating significantly more queries, and had a much higher TTFB, than on Craft 4.
The culprit turned out to be Twig statements like this:
{% if entry|default %}
...
{% endif %}
As it turns out, using the |default filter on an element in Craft 5 generates a lot of queries! Importantly, this is a change in behaviour from prior Craft versions, where doing the same thing would generate no extra queries (I tested this on Craft 4.17.13.1).
Something like entry|default is not really code I would write today, but it's certainly something we did a lot back in the day, before the null coalescing operator was a thing in Twig. A fair amount of those older sites are still alive and kicking and now on Craft 5, so this is a bit of an unfortunate performance gotcha.
The fix (in most situations) will be to use the null coalescing operator instead of |default - but actually identifying and fixing all cases where the default filter has been used on element variables won't be fun, or particularly easy 😓 – so it'd be great if it was possible to prevent those queries from being generated on Craft's end.
Steps to reproduce
Expected behavior
Actual behavior
Craft CMS version
5.9.20
PHP version
No response
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
What happened?
Description
After upgrading an older site to Craft 5, I noticed that a lot of pages were generating significantly more queries, and had a much higher TTFB, than on Craft 4.
The culprit turned out to be Twig statements like this:
{% if entry|default %} ... {% endif %}As it turns out, using the
|defaultfilter on an element in Craft 5 generates a lot of queries! Importantly, this is a change in behaviour from prior Craft versions, where doing the same thing would generate no extra queries (I tested this on Craft 4.17.13.1).Something like
entry|defaultis not really code I would write today, but it's certainly something we did a lot back in the day, before the null coalescing operator was a thing in Twig. A fair amount of those older sites are still alive and kicking and now on Craft 5, so this is a bit of an unfortunate performance gotcha.The fix (in most situations) will be to use the null coalescing operator instead of
|default- but actually identifying and fixing all cases where thedefaultfilter has been used on element variables won't be fun, or particularly easy 😓 – so it'd be great if it was possible to prevent those queries from being generated on Craft's end.Steps to reproduce
Expected behavior
Actual behavior
Craft CMS version
5.9.20
PHP version
No response
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions