Performance Degraded after update to twig 3.14.2

Created on 12 November 2024, 9 days ago

Problem/Motivation

This is probably a twig issue and not Drupal, however, there appears to be a significant page load performance issues (especially on admin entity create/edit forms such as node forms) introduced after upgrading from twig/twig 3.14.0 to 3.14.2. This is more noticeable on hosting platforms such as Acquia.

Steps to reproduce

  • Upgrade to twig to 3.14.2
  • Open a node create or edit form
  • or reload an existing page

4.2.

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Active

Version

10.3 โœจ

Component

render system

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States themodularlab

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @themodularlab
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States gravelpot

    @themodularlab, could you elaborate on "This is more noticeable on hosting platforms such as Acquia?"

    We are hosted on Pantheon, and right now our limited testing doesn't show any noticeable difference on loading the node edit form on the same site running Twig 3.14.0 and 3.14.2.

    Can you describe the testing you've done to indicate that it is _more_ noticeable on Acquia, to see if we can replicate on Pantheon?
    Is there something specific you suspect about Acquia's architecture versus other platforms where you haven't seen this performance hit?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States themodularlab

    @gravelpot

    We haven't done any official performance testing but rather this is something observed by myself and my clients. Prior to updating to 3.14.2, page load times were pretty normal. After 3.14.2, our page load for a node edit form can be well over 30 seconds. We are building a new site so there is very little content at this point. As a test, I downgraded back to 3.14.0 and page load times returned to normal.

    There may be some server settings in place on our Acquia env that could be partially contributing to this but also the performance and page load times return to normal when downgrading back to 3.14.0.

    Based on this part of the 3.14.2 patch, it looks like they're recursing through everything even before trying to render it?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States themodularlab
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States themodularlab
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand ericgsmith

    I have not noticed the degradation other than locally. When xdebug is enabled it's unusable in places, pages like the permission page fail to load (timeout). On environments without xdebug I didn't notice a difference but have not profiled or measured it.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States gravelpot

    @themodularlab

    I guess what I was really getting at was this -- since you said that it seems "more noticeable" on Acquia, that implied to me that there were other hosting environments where it seemed less noticeable to you. Is that actually the case?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States themodularlab

    @gravelpot,

    I have only personally observed on a local env and via Acquia. I have seen others reporting similar issues but unsure of what hosting they are using.

  • Indeed the next step is to profile โ†’ to find the exact cause.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    So the difference for twig between these versions is it now loops over any array and checks if any objects inside it are safe to convert to string

    But in Drupal we don't do any object specific 'is safe to convert to string' checks because our sandbox policy permits anything that has a __toString method

    We can't override the method in the sandbox extension because it's final, so I think we need to propose a PR to symfony for an escape hatch

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand ericgsmith

    I checked the logs locally - loading the permissions page on a project with several contribute modules and 8 roles (and with env var XDBEUG_MODE: off) does show a big difference in response time.

    First run on twig 3.14.0

    After a drush cr:

    "duration":4.943, "cpu_user":42.08,"cpu_system":5.26,"cpu_total":47.34,"memory_peak":135180288

    Subsequent load with a warmed cache:

    "duration":1.550,"cpu_user":86.44,"cpu_system":5.81,"cpu_total":92.25,"memory_peak":107917312

    After updating to twig 3.14.2

    After a drush cr:

    "duration":14.603,"cpu_user":83.95,"cpu_system":1.78,"cpu_total":85.73,"memory_peak":135180288

    Subsequent load with a warmed cache:

    "duration":11.857,"cpu_user":97.83,"cpu_system":1.18,"cpu_total":99.01,"memory_peak":107917312

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Charlie ChX Negyesi ๐ŸCanada

    We can't override the method in the sandbox extension because it's final,

    Y'all seriously need to consider adding the composer patches plugin and start patching out the final brainrot from upstream.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    10 second+ response times up from 1+ second is critical. Do we know if there's already an upstream issue to track this?

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    I don't see anything at https://github.com/twigphp/Twig/issues, we would probably need to create sample code that demonstrates the issue.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    We have some big and complex entity forms in our backend systems. Since the lastest security updates, the loading time of a very important form increased from 1.5 seconds to more than 50 seconds. Way more than factor 10.
    Running composer require twig/twig:"3.14.0 as 3.14.2" reduced the loading time 1.5 seconds again!

  • ๐Ÿ‡ช๐Ÿ‡ธSpain fjgarlin

    From https://github.com/twigphp/Twig/compare/v3.14.0..v3.14.2

    I guess the method ensureToStringAllowedForArray might be the culprit (it's a loop and it's recursive) as some Drupal objects are definitely quite big. They tried to fix infinite recursion in 3.14.2, but the fact that objects are traversed might cause the performance issue.

  • Merge request !10177Possible fix โ†’ (Open) created by larowlan
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    I've opened an MR that shows a possible approach. When we're using our default sandbox policy, we don't need to check to string on any objects, they're all allowed, so can sidestep it altogether.

    Haven't tested this and haven't linted either, but see if it helps

  • Pipeline finished with Failed
    8 days ago
    Total: 162s
    #337923
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    Code in the MR is working, site looks as normal and \Twig\Extension\SandboxExtension::ensureToStringAllowed is no longer running. Couple of random fails that I've rerun

  • Pipeline finished with Success
    8 days ago
    Total: 1077s
    #337943
  • Pipeline finished with Failed
    8 days ago
    Total: 823s
    #337958
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand ericgsmith

    ๐Ÿ™Œ ๐Ÿ™Œ ๐Ÿ™Œ

    Quick test with same project I mentioned in #12 - using twig 3.14.2 and the patch from MR10177 at commit 67bbb312 which applied cleanly to 10.3.6

    After drush cr

    "duration":4.683,"cpu_user":47.20,"cpu_system":5.98,"cpu_total":53.18,"memory_peak":137277440

    (original 3.14.0 duration was 4.943 so back to normal)

    Subsequent load with a warmed cache:

    "duration":1.559,"cpu_user":86.62,"cpu_system":7.70,"cpu_total":94.32,"memory_peak":107917312

    (original 3.14.0 duration was 1.550 so back to normal)

    Only did the comparison a couple of times with slight variance in times but certainly appears to resolve the performance issue with no noticeable side effects.

    Have not done a thorough code review but good to me - thanks Lee!

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States themodularlab

    Applying the patch from MR10177 on 10.3.8 is also showing improvements for me. I have only tested locally but will be pushing to a dev environment soon so I can report back later. But so far so good!

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    I suspect the fails in the pipeline are random fails, but I'm not seeing the button to retry.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    Rerunning the fails, the last build was green, so confident they're random

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia larowlan ๐Ÿ‡ฆ๐Ÿ‡บ๐Ÿ.au GMT+10

    MR is green

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland tuutti

    We've seen a degraded performance (~20%) on our testing environment since Twig 3.14.2 update.

    I applied this patch and the performance graphs went back to similar levels as before.

    Without patch:

    /node/{node}/edit took 2.04sec on average, template.render took 482-720ms
    /admin/people/permissions took 9.1sec on average, template.render took 8190-8280ms

    โ†’

    With patch:

    /node/{node}/edit took 982ms on average, template.render took 121-330ms
    /admin/people/permissions took 2.42sec on average, template.render took 1402-1503ms

    โ†’

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    This looks very nifty. I left one small comment on the MR.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance andypost
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany chr.fritsch ๐Ÿ‡ฉ๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany chr.fritsch ๐Ÿ‡ฉ๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ
  • First commit to issue fork.
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    I've addressed the review feedback and created the follow-up ๐Ÿ“Œ Simplify RemoveCheckToStringNodeVisitor when Twig 4.0 is released Active

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sagarmohite0031

    Hello,
    Tested and verified with drupal 11.
    MR applied successfully its working as expected.
    Check attachments

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States rhovland Oregon

    Can confirm this fixes the issue in Drupal 10.3.9. While I wasn't seeing any performance impacts in most areas of my site the permissions page was completely broken and hit the server timeout whenever I tried to load it. Applying this MR as a patch restores the performance.

Production build 0.71.5 2024