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

Skip to content

Resolve block chains against the render context - #4925

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:block-chain-render-context
Sep 12, 2026
Merged

Resolve block chains against the render context#4925
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:block-chain-render-context

Conversation

@fabpot

@fabpot fabpot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread src/BlockChain.php Outdated
Comment thread src/BlockChain.php
}

public function hasBlock(string $name): bool
public function hasBlock(string $name, array $context = []): bool

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suggest adding @param array<string, mixed> $context on this method to give more precise type (phpstan level 6+ does not like arrays that don't specify their types)

Comment thread src/BlockChain.php
Comment thread src/BlockChain.php Outdated
Comment thread src/BlockChain.php Outdated
Comment thread src/Template.php Outdated
@fabpot
fabpot force-pushed the block-chain-render-context branch from 1fdff94 to 897717d Compare September 11, 2026 22:35
@fabpot
fabpot merged commit a414c3a into twigphp:3.x Sep 12, 2026
53 checks passed
@fabpot
fabpot deleted the block-chain-render-context branch September 12, 2026 07:49
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.

2 participants