Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Resolve constant parent templates once instead of on every lookup - #4924

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:resolve-constant-parent-once
Sep 11, 2026
Merged

Resolve constant parent templates once instead of on every lookup#4924
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:resolve-constant-parent-once

Conversation

@fabpot

@fabpot fabpot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Currently, Template::$parent is only memoized by the compiled doDisplay(). Anything that reaches a template through getParent() without rendering it (TemplateWrapper::hasBlock(), getBlockNames(), renderBlock(), yieldParentBlock(), MacroNamespace::getParent()) re-evaluates doGetParent() and re-runs the sandbox check on every call, even when the parent is a string literal.

doGetParent() now does $this->parent ??= $this->load("name", $line) for a constant parent, which is what doDisplay() already does one line later. Dynamic parents are untouched and still resolve per context.

This also fixes a bug: getParent() loaded a constant parent with line -1, so a missing parent reported through hasBlock()/getBlockNames() lost its line number, while render() reported the {% extends %} line. All three now agree.

@fabpot
fabpot force-pushed the resolve-constant-parent-once branch from 30aec13 to c53b646 Compare September 11, 2026 13:23
@fabpot
fabpot merged commit bf3636c into twigphp:3.x Sep 11, 2026
12 of 13 checks passed
@fabpot
fabpot deleted the resolve-constant-parent-once branch September 11, 2026 13:23
Comment thread src/Node/ModuleNode.php
->raw(')')
;
// a constant parent never depends on the context, so resolve it once
$compiler->raw('$this->parent ??= ');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property should be defined in the template class?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is:

protected $parent;

fabpot added a commit that referenced this pull request Sep 12, 2026
This PR was merged into the 3.x branch.

Discussion
----------

Resolve block chains against the render context

While working on the Symfony PR for #4917, I realize that the performance was worse with the Twig's way. #4924 fixes part of the performance "regression". This one closes the gap.

`BlockChain` currently freezes each template's lineage by cloning it, so the block map and `parent()` both resolve `{% extends %}` against the constructor context.

This PR changes that to resolve the chain against the render context instead. `hasBlock()` and `getBlockNames()` take a context, like `TemplateWrapper` already does, and the third constructor argument becomes a default the render context can override. A lineage whose templates all have a fixed parent (none, or constant) is resolved once and cached, so the common case costs nothing; a dynamic `{% extends %}` re-resolves per call (not use by Symfony anyway).

This fixes also an inconsistency: a chained template with a dynamic parent now behaves exactly as it does when rendered directly. It also removes all changes in the "core" logic of Template.

```
                       construct    render   1 chain + 40 renders
Symfony's engine today   13.44us    0.78us         50.3us
#4917                    ~70us      0.96us        ~117us
this PR                   0.61us    0.84us         53.0us
```

Commits
-------

897717d Resolve block chains against the render context instead of freezing lineages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants