Skip to content

Commit 3f888b2

Browse files
committed
Avoid unnecessary count() calls at runtime
1 parent 3821219 commit 3f888b2

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
@@ -1002,12 +1002,13 @@ private function buildInlineHelperCall(string $name, array $params, ?Hash $hash)
10021002
{
10031003
$compiledParams = $this->compileParams($params, $hash);
10041004
$helperName = self::quote($name);
1005+
$count = count($params);
10051006
if ($this->isKnownHelper($name)) {
1006-
return self::getRuntimeFunc('hbch', "\$cx, \$cx->helpers[$helperName], $helperName, $compiledParams, \$in");
1007+
return self::getRuntimeFunc('hbch', "\$cx, \$cx->helpers[$helperName], $helperName, $count, $compiledParams, \$in");
10071008
}
10081009
if ($this->context->options->knownHelpersOnly) {
10091010
$this->throwKnownHelpersOnly($name);
10101011
}
1011-
return self::getRuntimeFunc('dynhbch', "\$cx, $helperName, $compiledParams, \$in");
1012+
return self::getRuntimeFunc('dynhbch', "\$cx, $helperName, $count, $compiledParams, \$in");
10121013
}
10131014
}

src/Runtime.php

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

243243
/**
@@ -430,24 +430,24 @@ public static function in(RuntimeContext $cx, string $name, \Closure $partial):
430430
* @param array<mixed> $positional
431431
* @param array<string, mixed> $hash
432432
*/
433-
public static function dynhbch(RuntimeContext $cx, string $name, array $positional, array $hash, mixed &$_this): mixed
433+
public static function dynhbch(RuntimeContext $cx, string $name, int $positionalCount, array $positional, array $hash, mixed &$_this): mixed
434434
{
435435
$helper = $cx->helpers[$name] ?? null;
436436
if ($helper !== null) {
437-
return static::hbch($cx, $helper, $name, $positional, $hash, $_this);
437+
return static::hbch($cx, $helper, $name, $positionalCount, $positional, $hash, $_this);
438438
}
439439

440440
$fn = $_this[$name] ?? null;
441441
if ($fn instanceof \Closure) {
442-
return static::hbch($cx, $fn, $name, $positional, $hash, $_this);
442+
return static::hbch($cx, $fn, $name, $positionalCount, $positional, $hash, $_this);
443443
}
444444

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

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

453453
/**
@@ -457,7 +457,7 @@ public static function dynhbch(RuntimeContext $cx, string $name, array $position
457457
* @param array<string, mixed> $hash
458458
* @param mixed $_this current rendering context for the helper
459459
*/
460-
public static function hbch(RuntimeContext $cx, \Closure $helper, string $name, array $positional, array $hash, mixed &$_this): mixed
460+
public static function hbch(RuntimeContext $cx, \Closure $helper, string $name, int $positionalCount, array $positional, array $hash, mixed &$_this): mixed
461461
{
462462
/** @var \WeakMap<\Closure, int>|null $paramCounts */
463463
static $paramCounts = null;
@@ -472,7 +472,7 @@ public static function hbch(RuntimeContext $cx, \Closure $helper, string $name,
472472
$numParams = $params && end($params)->isVariadic() ? 0 : $rf->getNumberOfParameters();
473473
$paramCounts[$helper] = $numParams;
474474
}
475-
if ($numParams === 0 || $numParams > count($positional)) {
475+
if ($numParams === 0 || $numParams > $positionalCount) {
476476
$positional[] = new HelperOptions(
477477
scope: $_this,
478478
data: $cx->data,

0 commit comments

Comments
 (0)