Skip to content

Commit

Permalink
Swap BC layer for yield-ready and reclaim perf loss
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Aug 19, 2024
1 parent 17997cf commit 5d1a19a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
11 changes: 7 additions & 4 deletions src/Node/FlushNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public function __construct(int $lineno, string $tag)

public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->write("flush();\n")
;
$compiler->addDebugInfo($this);

if ($compiler->getEnvironment()->useYield()) {
$compiler->write("yield '';\n");
}

$compiler->write("flush();\n");
}
}
107 changes: 53 additions & 54 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ public function displayBlock($name, array $context, array $blocks = [], $useBloc
*/
public function renderParentBlock($name, array $context, array $blocks = [])
{
if (!$this->useYield) {
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
$this->displayParentBlock($name, $context, $blocks);

return ob_get_clean();
}

$content = '';
foreach ($this->yieldParentBlock($name, $context, $blocks) as $data) {
$content .= $data;
Expand All @@ -189,6 +200,26 @@ public function renderParentBlock($name, array $context, array $blocks = [])
*/
public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true)
{
if (!$this->useYield) {
$level = ob_get_level();
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
try {
$this->displayBlock($name, $context, $blocks, $useBlocks);
} catch (\Throwable $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}

throw $e;
}

return ob_get_clean();
}

$content = '';
foreach ($this->yieldBlock($name, $context, $blocks, $useBlocks) as $data) {
$content .= $data;
Expand Down Expand Up @@ -331,6 +362,26 @@ public function display(array $context, array $blocks = []): void

public function render(array $context): string
{
if (!$this->useYield) {
$level = ob_get_level();
if ($this->env->isDebug()) {
ob_start();
} else {
ob_start(function () { return ''; });
}
try {
$this->display($context);
} catch (\Throwable $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}

throw $e;
}

return ob_get_clean();
}

$content = '';
foreach ($this->yield($context) as $data) {
$content .= $data;
Expand All @@ -348,27 +399,7 @@ public function yield(array $context, array $blocks = []): iterable
$blocks = array_merge($this->blocks, $blocks);

try {
if ($this->useYield) {
yield from $this->doDisplay($context, $blocks);

return;
}

$level = ob_get_level();
ob_start();

foreach ($this->doDisplay($context, $blocks) as $data) {
if (ob_get_length()) {
$data = ob_get_clean().$data;
ob_start();
}

yield $data;
}

if (ob_get_length()) {
yield ob_get_clean();
}
yield from $this->doDisplay($context, $blocks);
} catch (Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($this->getSourceContext());
Expand All @@ -386,12 +417,6 @@ public function yield(array $context, array $blocks = []): iterable
$e->guess();

throw $e;
} finally {
if (!$this->useYield) {
while (ob_get_level() > $level) {
ob_end_clean();
}
}
}
}

Expand All @@ -418,27 +443,7 @@ public function yieldBlock($name, array $context, array $blocks = [], $useBlocks

if (null !== $template) {
try {
if ($this->useYield) {
yield from $template->$block($context, $blocks);

return;
}

$level = ob_get_level();
ob_start();

foreach ($template->$block($context, $blocks) as $data) {
if (ob_get_length()) {
$data = ob_get_clean().$data;
ob_start();
}

yield $data;
}

if (ob_get_length()) {
yield ob_get_clean();
}
yield from $template->$block($context, $blocks);
} catch (Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($template->getSourceContext());
Expand All @@ -456,12 +461,6 @@ public function yieldBlock($name, array $context, array $blocks = [], $useBlocks
$e->guess();

throw $e;
} finally {
if (!$this->useYield) {
while (ob_get_level() > $level) {
ob_end_clean();
}
}
}
} elseif ($parent = $this->getParent($context)) {
yield from $parent->unwrap()->yieldBlock($name, $context, array_merge($this->blocks, $blocks), false, $templateContext ?? $this);
Expand Down

0 comments on commit 5d1a19a

Please sign in to comment.