Skip to content

Commit 0cd065c

Browse files
authored
Improve hot path performance (#64)
1 parent b210808 commit 0cd065c

8 files changed

Lines changed: 446 additions & 119 deletions

File tree

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
4646
$methodName = $isClassMethod ? strtolower($node->name->toString()) : '';
4747
$isConstructor = $isClassMethod && $methodName === '__construct';
4848
$isMagicLifecycle = $isClassMethod && \in_array($methodName, ['__construct', '__destruct', '__clone'], true);
49-
5049
$isNativeNever = $node->returnType instanceof Node\Identifier && strtolower($node->returnType->name) === 'never';
5150
$isNativeVoid = $node->returnType instanceof Node\Identifier && strtolower($node->returnType->name) === 'void';
5251
$isPrivate = $isClassMethod && $node->isPrivate();
5352

5453
$paramCount = \count($node->params);
54+
5555
$hasParam = self::hasParamContracts(
5656
$docText,
5757
$isClassMethod,
@@ -184,7 +184,6 @@ private static function hasNonMixedParam(string $docText): bool
184184
break;
185185
}
186186
}
187-
188187
if (! $hasMixed) {
189188
return true;
190189
}
@@ -281,7 +280,6 @@ private static function buildParamInjections(
281280
Node\Expr $thisArg
282281
): array {
283282
$injectedStmts = [self::buildSetupScopeStmt($params, $thisArg)];
284-
285283
$callableWrappers = self::buildParamWrappers($params, $docText, $thisArg, [self::class, 'isCallableCandidate'], 'wrapCallable');
286284
$iterableWrappers = self::buildParamWrappers($params, $docText, $thisArg, [self::class, 'isIterableCandidate'], 'wrapIterable');
287285

@@ -304,28 +302,65 @@ private static function buildSetupScopeStmt(array $params, Node\Expr $thisArg):
304302
}
305303
}
306304

307-
$argsExpr = new Node\Expr\Assign(
308-
new Node\Expr\Variable('__typephpArgs'),
309-
new Node\Expr\Array_($arrayItems)
305+
$argsAssign = new Node\Stmt\Expression(
306+
new Node\Expr\Assign(
307+
new Node\Expr\Variable('_typephpArgs'),
308+
new Node\Expr\Array_($arrayItems)
309+
)
310310
);
311311

312312
$checkCall = new Node\Expr\FuncCall(
313313
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::setupScope'),
314314
[
315315
new Node\Arg(new Node\Scalar\MagicConst\Method()),
316-
new Node\Arg($argsExpr),
316+
new Node\Arg(new Node\Expr\Variable('_typephpArgs')),
317317
new Node\Arg($thisArg),
318318
]
319319
);
320320

321321
$throwStmt = self::buildTypeErrorThrowStmt(new Node\Expr\Variable('__typephpErr'));
322322

323-
$ifStmt = new Node\Stmt\If_(
324-
new Node\Expr\Instanceof_(
325-
new Node\Expr\Assign(new Node\Expr\Variable('__typephpErr'), $checkCall),
326-
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
323+
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();
324+
325+
$noParamCacheCheck = new Node\Expr\BooleanNot(
326+
new Node\Expr\Isset_([
327+
new Node\Expr\ArrayDimFetch(
328+
new Node\Expr\StaticPropertyFetch(
329+
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ParamChecker'),
330+
'noParamContractCache'
331+
),
332+
$cacheKeyExpr
333+
),
334+
])
335+
);
336+
337+
$hasTemplatesCheck = new Node\Expr\BinaryOp\Coalesce(
338+
new Node\Expr\ArrayDimFetch(
339+
new Node\Expr\StaticPropertyFetch(
340+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker'),
341+
'hasMethodTemplatesCache'
342+
),
343+
$cacheKeyExpr
327344
),
328-
['stmts' => [$throwStmt]]
345+
new Node\Expr\ConstFetch(new Node\Name('false'))
346+
);
347+
348+
$combinedCondition = new Node\Expr\BinaryOp\BooleanOr($noParamCacheCheck, $hasTemplatesCheck);
349+
350+
$ifStmt = new Node\Stmt\If_(
351+
$combinedCondition,
352+
[
353+
'stmts' => [
354+
$argsAssign,
355+
new Node\Stmt\If_(
356+
new Node\Expr\Instanceof_(
357+
new Node\Expr\Assign(new Node\Expr\Variable('__typephpErr'), $checkCall),
358+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
359+
),
360+
['stmts' => [$throwStmt]]
361+
),
362+
],
363+
]
329364
);
330365

331366
$ifStmt->setAttribute('typephp_injected', true);
@@ -347,14 +382,15 @@ private static function buildParamWrappers(
347382
string $wrapperMethod
348383
): array {
349384
$wrappers = [];
385+
350386
foreach ($params as $param) {
351387
if ($predicate($param, $docText) && $param->var instanceof Node\Expr\Variable && \is_string($param->var->name)) {
352388
$paramName = $param->var->name;
353389
$expr = new Node\Stmt\Expression(
354390
new Node\Expr\Assign(
355391
new Node\Expr\Variable($paramName),
356392
new Node\Expr\FuncCall(
357-
new Node\Name\FullyQualified("TypePHP\\Internal\\RuntimeTypeChecker::{$wrapperMethod}"),
393+
new Node\Name\FullyQualified("TypePHP\Internal\RuntimeTypeChecker::{$wrapperMethod}"),
358394
[
359395
new Node\Arg(new Node\Scalar\MagicConst\Method()),
360396
new Node\Arg(new Node\Scalar\String_($paramName)),
@@ -408,11 +444,9 @@ private static function typeMatchesName(Node\Identifier|Node\Name|Node\ComplexTy
408444
if ($type instanceof Node\Identifier) {
409445
return isset($targetNames[strtolower($type->name)]);
410446
}
411-
412447
if ($type instanceof Node\Name) {
413448
return isset($targetNames[strtolower($type->getLast())]);
414449
}
415-
416450
if ($type instanceof Node\UnionType || $type instanceof Node\IntersectionType) {
417451
foreach ($type->types as $t) {
418452
if (self::typeMatchesName($t, $targetNames)) {
@@ -460,7 +494,7 @@ public static function buildTypeErrorThrowStmt(Node\Expr $errorVar): Node\Stmt\E
460494
public static function buildReturnCheckCall(Node\Expr $exprToWrap, Node\Expr $thisArg, bool $needsReturnVars = false): Node\Expr\FuncCall
461495
{
462496
$varsArg = $needsReturnVars
463-
? new Node\Expr\Variable('__typephpArgs')
497+
? new Node\Expr\Variable('_typephpArgs')
464498
: new Node\Expr\Array_();
465499

466500
return new Node\Expr\FuncCall(
@@ -569,7 +603,6 @@ public function enterNode(Node $n): int|Node|null
569603
if ($n->getAttribute('typephp_wrapped') === true) {
570604
return null;
571605
}
572-
573606
$n->setAttribute('typephp_wrapped', true);
574607

575608
return FunctionContractInjector::buildWrappedYieldNode($n, $this->thisArg);
@@ -579,9 +612,7 @@ public function enterNode(Node $n): int|Node|null
579612
if ($n->getAttribute('typephp_wrapped') === true) {
580613
return null;
581614
}
582-
583615
$n->setAttribute('typephp_wrapped', true);
584-
585616
$n->expr = new Node\Expr\FuncCall(
586617
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::wrapIterable'),
587618
[
@@ -629,15 +660,34 @@ public function enterNode(Node $n): int|array|null
629660
if ($n->getAttribute('typephp_var_wrapped') === true) {
630661
return null;
631662
}
632-
633663
$exprToWrap = $n->expr ?? new Node\Expr\ConstFetch(new Node\Name('null'));
634-
$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);
635664

636665
if ($this->isNativeVoid) {
666+
$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);
667+
637668
return FunctionContractInjector::buildVoidReturnGuard($checkCall);
638669
}
639670

640-
$n->expr = FunctionContractInjector::buildTernaryReturnExpr($checkCall);
671+
// Call-site cache bypass for return checks
672+
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();
673+
$cacheCheck = new Node\Expr\Isset_([
674+
new Node\Expr\ArrayDimFetch(
675+
new Node\Expr\StaticPropertyFetch(
676+
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ReturnChecker'),
677+
'noReturnContractCache'
678+
),
679+
$cacheKeyExpr
680+
),
681+
]);
682+
683+
$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);
684+
$ternaryExpr = FunctionContractInjector::buildTernaryReturnExpr($checkCall);
685+
686+
$n->expr = new Node\Expr\Ternary(
687+
$cacheCheck,
688+
$exprToWrap,
689+
$ternaryExpr
690+
);
641691
}
642692

643693
return null;
@@ -654,7 +704,25 @@ public function enterNode(Node $n): int|array|null
654704
if ($isNativeVoid) {
655705
$newStmts = [...$newStmts, ...self::buildVoidReturnGuard($checkCall)];
656706
} else {
657-
$retStmt = new Node\Stmt\Return_(self::buildTernaryReturnExpr($checkCall));
707+
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();
708+
$cacheCheck = new Node\Expr\Isset_([
709+
new Node\Expr\ArrayDimFetch(
710+
new Node\Expr\StaticPropertyFetch(
711+
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ReturnChecker'),
712+
'noReturnContractCache'
713+
),
714+
$cacheKeyExpr
715+
),
716+
]);
717+
718+
$ternaryExpr = self::buildTernaryReturnExpr($checkCall);
719+
$fallbackExpr = new Node\Expr\Ternary(
720+
$cacheCheck,
721+
new Node\Expr\ConstFetch(new Node\Name('null')),
722+
$ternaryExpr
723+
);
724+
725+
$retStmt = new Node\Stmt\Return_($fallbackExpr);
658726
$retStmt->setAttribute('typephp_injected', true);
659727
$newStmts[] = $retStmt;
660728
}

0 commit comments

Comments
 (0)