Resolve constant parent templates once instead of on every lookup - #4924
Merged
Conversation
stof
approved these changes
Sep 11, 2026
fabpot
force-pushed
the
resolve-constant-parent-once
branch
from
September 11, 2026 13:23
30aec13 to
c53b646
Compare
GromNaN
reviewed
Sep 11, 2026
| ->raw(')') | ||
| ; | ||
| // a constant parent never depends on the context, so resolve it once | ||
| $compiler->raw('$this->parent ??= '); |
Contributor
There was a problem hiding this comment.
This property should be defined in the template class?
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently,
Template::$parentis only memoized by the compileddoDisplay(). Anything that reaches a template throughgetParent()without rendering it (TemplateWrapper::hasBlock(),getBlockNames(),renderBlock(),yieldParentBlock(),MacroNamespace::getParent()) re-evaluatesdoGetParent()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 whatdoDisplay()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 throughhasBlock()/getBlockNames()lost its line number, whilerender()reported the{% extends %}line. All three now agree.