Skip to content

Commit 11d2b1c

Browse files
committed
Avoid unnecessary count() calls at runtime
1 parent 7ead2b1 commit 11d2b1c

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/Compiler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,12 +1003,13 @@ private function buildInlineHelperCall(string $name, array $params, ?Hash $hash)
10031003
{
10041004
$compiledParams = $this->compileParams($params, $hash);
10051005
$helperName = self::quote($name);
1006+
$count = count($params);
10061007
if ($this->isKnownHelper($name)) {
1007-
return self::getRuntimeFunc('hbch', "\$cx, \$cx->helpers[$helperName], $helperName, $compiledParams, \$in");
1008+
return self::getRuntimeFunc('hbch', "\$cx, \$cx->helpers[$helperName], $helperName, $count, $compiledParams, \$in");
10081009
}
10091010
if ($this->context->options->knownHelpersOnly) {
10101011
$this->throwKnownHelpersOnly($name);
10111012
}
1012-
return self::getRuntimeFunc('dynhbch', "\$cx, $helperName, $compiledParams, \$in");
1013+
return self::getRuntimeFunc('dynhbch', "\$cx, $helperName, $count, $compiledParams, \$in");
10131014
}
10141015
}

src/Runtime.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ public static function hv(RuntimeContext $cx, string $name, mixed &$_this): mixe
230230
{
231231
$helper = $cx->helpers[$name] ?? null;
232232
if ($helper !== null) {
233-
return static::hbch($cx, $helper, $name, [], [], $_this);
233+
return static::hbch($cx, $helper, $name, 0, [], [], $_this);
234234
}
235235
if (is_array($_this) && array_key_exists($name, $_this)) {
236236
return static::dv($_this[$name]);
237237
}
238-
return static::hbch($cx, $cx->helpers['helperMissing'], $name, [], [], $_this);
238+
return static::hbch($cx, $cx->helpers['helperMissing'], $name, 0, [], [], $_this);
239239
}
240240

241241
/**
@@ -428,24 +428,24 @@ public static function in(RuntimeContext $cx, string $name, \Closure $partial):
428428
* @param array<mixed> $positional
429429
* @param array<string, mixed> $hash
430430
*/
431-
public static function dynhbch(RuntimeContext $cx, string $name, array $positional, array $hash, mixed &$_this): mixed
431+
public static function dynhbch(RuntimeContext $cx, string $name, int $positionalCount, array $positional, array $hash, mixed &$_this): mixed
432432
{
433433
$helper = $cx->helpers[$name] ?? null;
434434
if ($helper !== null) {
435-
return static::hbch($cx, $helper, $name, $positional, $hash, $_this);
435+
return static::hbch($cx, $helper, $name, $positionalCount, $positional, $hash, $_this);
436436
}
437437

438438
$fn = $_this[$name] ?? null;
439439
if ($fn instanceof \Closure) {
440-
return static::hbch($cx, $fn, $name, $positional, $hash, $_this);
440+
return static::hbch($cx, $fn, $name, $positionalCount, $positional, $hash, $_this);
441441
}
442442

443443
if (!$positional && !$hash) {
444444
// No arguments: must be a helper call (e.g. sub-expression), not a property lookup.
445445
throw new \Exception('Missing helper: "' . $name . '"');
446446
}
447447

448-
return static::hbch($cx, $cx->helpers['helperMissing'], $name, $positional, $hash, $_this);
448+
return static::hbch($cx, $cx->helpers['helperMissing'], $name, $positionalCount, $positional, $hash, $_this);
449449
}
450450

451451
/**
@@ -455,7 +455,7 @@ public static function dynhbch(RuntimeContext $cx, string $name, array $position
455455
* @param array<string, mixed> $hash
456456
* @param mixed $_this current rendering context for the helper
457457
*/
458-
public static function hbch(RuntimeContext $cx, \Closure $helper, string $name, array $positional, array $hash, mixed &$_this): mixed
458+
public static function hbch(RuntimeContext $cx, \Closure $helper, string $name, int $positionalCount, array $positional, array $hash, mixed &$_this): mixed
459459
{
460460
/** @var \WeakMap<\Closure, int>|null $paramCounts */
461461
static $paramCounts = null;
@@ -470,7 +470,7 @@ public static function hbch(RuntimeContext $cx, \Closure $helper, string $name,
470470
$numParams = $params && end($params)->isVariadic() ? 0 : $rf->getNumberOfParameters();
471471
$paramCounts[$helper] = $numParams;
472472
}
473-
if ($numParams === 0 || $numParams > count($positional)) {
473+
if ($numParams === 0 || $numParams > $positionalCount) {
474474
$positional[] = new HelperOptions(
475475
scope: $_this,
476476
data: $cx->frame,

0 commit comments

Comments
 (0)