Skip to content

Commit 937b897

Browse files
committed
Avoid unnecessary count() calls at runtime
1 parent 10b6915 commit 937b897

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
@@ -933,12 +933,13 @@ private function buildInlineHelperCall(string $name, array $params, ?Hash $hash)
933933
{
934934
$compiledParams = $this->compileParams($params, $hash);
935935
$helperName = self::quote($name);
936+
$count = count($params);
936937
if ($this->isKnownHelper($name)) {
937-
return self::getRuntimeFunc('hbch', "\$cx, \$cx->helpers[$helperName], $helperName, $compiledParams, \$in");
938+
return self::getRuntimeFunc('hbch', "\$cx, \$cx->helpers[$helperName], $helperName, $count, $compiledParams, \$in");
938939
}
939940
if ($this->context->options->knownHelpersOnly) {
940941
$this->throwKnownHelpersOnly($name);
941942
}
942-
return self::getRuntimeFunc('dynhbch', "\$cx, $helperName, $compiledParams, \$in");
943+
return self::getRuntimeFunc('dynhbch', "\$cx, $helperName, $count, $compiledParams, \$in");
943944
}
944945
}

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
/**
@@ -427,24 +427,24 @@ public static function in(RuntimeContext $cx, string $name, \Closure $partial):
427427
* @param array<mixed> $positional
428428
* @param array<string, mixed> $hash
429429
*/
430-
public static function dynhbch(RuntimeContext $cx, string $name, array $positional, array $hash, mixed &$_this): mixed
430+
public static function dynhbch(RuntimeContext $cx, string $name, int $positionalCount, array $positional, array $hash, mixed &$_this): mixed
431431
{
432432
$helper = $cx->helpers[$name] ?? null;
433433
if ($helper !== null) {
434-
return static::hbch($cx, $helper, $name, $positional, $hash, $_this);
434+
return static::hbch($cx, $helper, $name, $positionalCount, $positional, $hash, $_this);
435435
}
436436

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

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

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

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

0 commit comments

Comments
 (0)