Fabien Potencier dd0bd0ffbc minor #4904 Release destructuring temporaries after assignment (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Release destructuring temporaries after assignment

The compiled destructuring code kept the right-hand side alive in a hidden local until the end of rendering, wasting memory on large arrays and iterators. The temporary is now cleared as part of the compiled expression.

The way it works:

For this Twig expression:

```twig
{% do {name, email: address} = user %}
```

The previous generated expression was conceptually:

```php
[$context["name"], $context["address"]] = [
    getAttribute($_v0 = $context["user"], "name"),
    getAttribute($_v0, "email"),
];
```

The branch now generates:

```php
[
    [$context["name"], $context["address"]] = [
        getAttribute($_v0 = $context["user"], "name"),
        getAttribute($_v0, "email"),
    ],
    $_v0 = null,
][0]
```

The important wrapper is:

```php
[$originalExpression, $_v0 = null][0]
```

PHP evaluates array elements from left to right:

1. The original destructuring assignment runs.
2. The temporary variable is set to `null`, releasing its reference.
3. `[0]` returns the result of the original expression.

This preserves Twig’s rule that a destructuring expression returns its right-hand value.

Commits
-------

c459ef0bdd Release destructuring temporaries after assignment
2026-08-29 00:15:46 +02:00

Twig, the flexible, fast, and secure template language for PHP
==============================================================

Twig is a template language for PHP.

Twig uses a syntax similar to the Django and Jinja template languages which
inspired the Twig runtime environment.

Sponsors
--------

.. raw:: html

    <a href="https://docs.blackfire.io/introduction?utm_source=twig&utm_medium=github_readme&utm_campaign=logo">
        <img src="https://static.blackfire.io/assets/intemporals/logo/png/blackfire-io_secondary_horizontal_transparent.png?1" width="255px" alt="Blackfire.io">
    </a>

More Information
----------------

Read the `documentation`_ for more information.

.. _documentation: https://twig.symfony.com/documentation
Languages
PHP 99.9%