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

Skip to content

Store TemplateClass in compiled includes - #3602

Closed
GromNaN wants to merge 1 commit into
twigphp:2.xfrom
GromNaN:runtime-hash
Closed

Store TemplateClass in compiled includes#3602
GromNaN wants to merge 1 commit into
twigphp:2.xfrom
GromNaN:runtime-hash

Conversation

@GromNaN

@GromNaN GromNaN commented Dec 14, 2021

Copy link
Copy Markdown
Contributor

Trying to optimize calls to hash function (#3588), I found that the template classes could be cached inside the calling templates.

Example with the following template

{% include '@ResponsiveFront/home/views/metas.html.twig' with { Metadata } only %}

Compiled version:

$this->loadTemplate(new TemplateClass("__TwigTemplate_c834197862571e35dc19b42e4a40c8f9826e712d24a46a6b8d08ffee77eab1bc", "@ResponsiveFront/home/views/metas.html.twig"), "@ResponsiveFront/home/views/home.html.twig", 22)->display(twig_to_array(["Metadata" => ($context["Metadata"] ?? null)]));

Previous compiled version:

$this->loadTemplate("@ResponsiveFront/home/views/metas.html.twig", "@ResponsiveFront/home/views/home.html.twig", 22)->display(twig_to_array(["Metadata" => ($context["Metadata"] ?? null)]));

If the direction is OK, I'd continue with all other types of template calls (extends, embed, use, macro ...)

Optimization for existing templates
Comment thread src/Template.php
}

if ($template instanceof TemplateClass) {
return $this->env->loadClass($template->class, $template->name, $index);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here is the shortcut.

@GromNaN
GromNaN changed the base branch from 3.x to 2.x December 14, 2021 01:06
@stof

stof commented Dec 14, 2021

Copy link
Copy Markdown
Member

I see a big issue here: the output of compiling a template now depends on the template class name of included templates, which makes cache invalidation a lot harder (if you add a new template matching a name in a higher priority location, it will not be consistently used depending on whether the templates referencing that name are already cached with the old class name or no)

@GromNaN

GromNaN commented Dec 14, 2021

Copy link
Copy Markdown
Contributor Author

I try to optimize the use-case where the templates are immutable: a typical Symfony application on production. This change should not create any issue in that conditions.

But you are right, that it can create inconsistencies during development or if the loader has variable results.

An other idea was to add cache to the getTemplateClass method. But I'm worried it will have the same drawbacks.

@GromNaN

GromNaN commented Dec 14, 2021

Copy link
Copy Markdown
Contributor Author

The performance benefits is not worth the added complexity.

That was interesting to dive into this code

@GromNaN GromNaN closed this Dec 14, 2021
@GromNaN
GromNaN deleted the runtime-hash branch February 8, 2022 22:05
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