Skip to content

Commit edf57f4

Browse files
committed
Compile if/unless to native ternary when possible
This can double runtime performance for complex templates with conditions in nested loops.
1 parent 9a2acc1 commit edf57f4

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

‎src/Compiler.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ private function compileBlockHelper(BlockStatement $block, string $name): string
296296
}
297297
// For inverted blocks the fn body comes from the inverse program; for normal blocks, the program.
298298
$fnProgram = $inverted ? $block->inverse : $block->program;
299+
300+
// Inline if/unless as ternary — eliminates hbbch dispatch and HelperOptions allocation.
301+
// Safe because if/unless don't change scope, so $cx and $in are already correct.
302+
// Negate for 'unless' in a normal block, or 'if' in an inverted block (swapped semantics).
303+
if ($this->canInlineConditional($block, $name, $fnProgram->blockParams)) {
304+
$cond = $this->compileConditionalExpr($block->params[0], $name === ($inverted ? 'if' : 'unless'));
305+
$body = $this->compileProgram($fnProgram);
306+
$elseBody = $inverted ? "''" : $this->compileProgramOrEmpty($block->inverse);
307+
return "($cond ? $body : $elseBody)";
308+
}
309+
299310
$blockFn = $this->compileProgramWithBlockParams($fnProgram);
300311
[$fn, $else] = $inverted
301312
? ['null', $blockFn]
@@ -309,6 +320,48 @@ private function compileBlockHelper(BlockStatement $block, string $name): string
309320
return self::getRuntimeFunc('hbbch', "\$cx, \$cx->helpers[$helperName], $helperName, $params, \$in, $fn, $else, $bpCount, $outerBp");
310321
}
311322

323+
/**
324+
* Returns true when an if/unless block can be safely inlined as a ternary expression.
325+
* Requires: no hash options (e.g. includeZero), no block params, exactly one condition param.
326+
* @param string[] $bp
327+
*/
328+
private function canInlineConditional(BlockStatement $block, string $helperName, array $bp): bool
329+
{
330+
return $this->isKnownHelper($helperName)
331+
&& ($helperName === 'if' || $helperName === 'unless')
332+
&& count($block->params) === 1
333+
&& $block->hash === null
334+
&& !$bp;
335+
}
336+
337+
/**
338+
* Compile the condition expression for an inlined if/unless ternary.
339+
* For simple single-segment paths, routes through cv() which already resolves closures,
340+
* so ifvar() suffices. For all other expressions, closures at nested paths are not
341+
* invoked (not a real-world or spec concern).
342+
* @param bool $negate true for `unless` or inverted `{{^if}}`
343+
*/
344+
private function compileConditionalExpr(Expression $condExpr, bool $negate): string
345+
{
346+
$part = $condExpr instanceof PathExpression ? ($condExpr->parts[0] ?? null) : null;
347+
if ($condExpr instanceof PathExpression
348+
&& !$condExpr->data
349+
&& $condExpr->depth === 0
350+
&& is_string($part)
351+
&& !self::scopedId($condExpr)
352+
&& $this->lookupBlockParam($part) === null
353+
) {
354+
$val = self::getRuntimeFunc('cv', '$in, ' . self::quote($part));
355+
} else {
356+
$savedHelperArgs = $this->compilingHelperArgs;
357+
$this->compilingHelperArgs = true;
358+
$val = $this->compileExpression($condExpr);
359+
$this->compilingHelperArgs = $savedHelperArgs;
360+
}
361+
$cond = self::getRuntimeFunc('ifvar', $val);
362+
return $negate ? "!$cond" : $cond;
363+
}
364+
312365
private function compileDynamicBlockHelper(BlockStatement $block, string $name, string $varPath = 'null'): string
313366
{
314367
$bp = $block->program->blockParams ?? [];

0 commit comments

Comments
 (0)