Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class NodeScopeResolver
private const LOOP_SCOPE_ITERATIONS = 3;
private const GENERALIZE_AFTER_ITERATION = 1;
private const FOREACH_UNROLL_LIMIT = 16;
private const FOREACH_UNROLL_NESTED_LIMIT = 16;

/** @var array<string, true> filePath(string) => bool(true) */
private array $analysedFiles = [];
Expand Down Expand Up @@ -3896,6 +3897,9 @@ private function tryProcessUnrolledConstantArrayForeach(
if ($totalKeys === 0 || $totalKeys > self::FOREACH_UNROLL_LIMIT) {
return null;
}
if ($context->getForeachUnrollFactor() * $totalKeys > self::FOREACH_UNROLL_NESTED_LIMIT) {
return null;
}

$nativeIterateeType = $originalScope->getNativeType($stmt->expr);
$nativeConstantArrays = $nativeIterateeType->getConstantArrays();
Expand All @@ -3908,6 +3912,8 @@ private function tryProcessUnrolledConstantArrayForeach(
$allChainScopes = [];
$allBreakScopes = [];

$bodyContext = $context->enterUnrolledForeach($totalKeys);

foreach ($constantArrays as $arrayIndex => $constantArray) {
$keyTypes = $constantArray->getKeyTypes();
$valueTypes = $constantArray->getValueTypes();
Expand Down Expand Up @@ -3971,7 +3977,7 @@ private function tryProcessUnrolledConstantArrayForeach(
$iterScope,
$iterStorage,
new NoopNodeCallback(),
$context,
$bodyContext,
)->filterOutLoopExitPoints();

$iterEndScope = $bodyResult->getScope();
Expand Down
13 changes: 12 additions & 1 deletion src/Analyser/StatementContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class StatementContext

private function __construct(
private bool $isTopLevel,
private int $foreachUnrollFactor = 1,
)
{
}
Expand All @@ -40,13 +41,23 @@ public function isTopLevel(): bool
return $this->isTopLevel;
}

public function getForeachUnrollFactor(): int
{
return $this->foreachUnrollFactor;
}

public function enterDeep(): self
{
if ($this->isTopLevel) {
return self::createDeep();
return new self(false, $this->foreachUnrollFactor);
}

return $this;
}

public function enterUnrolledForeach(int $totalKeys): self
{
return new self($this->isTopLevel, $this->foreachUnrollFactor * $totalKeys);
}

}
27 changes: 27 additions & 0 deletions tests/bench/data/bug-14590.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug14590;

function provider(): array {
$cases = [];
foreach ([1, 2] as $v0) {
foreach ([1, 2] as $v1) {
foreach ([1, 2] as $v2) {
foreach ([1, 2] as $v3) {
foreach ([1, 2] as $v4) {
foreach ([1, 2] as $v5) {
foreach ([1, 2] as $v6) {
foreach ([1, 2] as $v7) {
foreach ([1, 2] as $v8) {
$cases[] = $v0;
}
}
}
}
}
}
}
}
}
return $cases;
}
Loading