Skip to content

Slow analysis with a big const array in a class #11979

@xPaw

Description

@xPaw

Bug report

I noticed this initially after updating to 1.12.8, but I tried to revert to previous version and this test script still takes a while to analyse.

Script that takes 27 seconds to analyse:

<?php
require 'data.php';

$func = static fn( int $Point ) : int => TestMatrix::Values[ $Point ][ $anotherThing ];
$foo = [
	[ 'val' => $func( 1237123 ) ],
	[ 'val' => $func( 4379284 ) ],
	[ 'val' => $func( 4534895 ) ],
	[ 'val' => $func( 9483754 ) ],
	[ 'val' => $func( 8127361 ) ],
	[ 'val' => $func( 1287129 ) ],
];

If I remove the arrays from $foo, it drops to 7 seconds:

<?php
require 'data.php';

$func = static fn( int $Point ) : int => TestMatrix::Values[ $Point ][ $anotherThing ];
$func( 1237123 );
$func( 4379284 );
$func( 4534895 );
$func( 9483754 );
$func( 8127361 );
$func( 1287129 );

Removing the function call and accessing the array directly drops it to 3 seconds.

<?php
require 'data.php';

$foo = [
	[ 'val' => TestMatrix::Values[ 1237123 ][ $anotherThing ] ],
	[ 'val' => TestMatrix::Values[ 4379284 ][ $anotherThing ] ],
	[ 'val' => TestMatrix::Values[ 4534895 ][ $anotherThing ] ],
	[ 'val' => TestMatrix::Values[ 9483754 ][ $anotherThing ] ],
	[ 'val' => TestMatrix::Values[ 8127361 ][ $anotherThing ] ],
	[ 'val' => TestMatrix::Values[ 1287129 ][ $anotherThing ] ],
];

Script to generate test data:

<?php
$foo = [];

for($i = 0; $i < 50; $i++) {
	$val = [];

	for($k = 0; $k < 50; $k++)
		$val[] = random_int(1, PHP_INT_MAX );

	$foo[] = $val;
}

var_export($foo);

data.php:

<?php
class TestMatrix {
	const array Values = /* the var_exported array */
}

Changing const array Values to public static array $Values also drops it to 3 seconds, so there is some interaction of const arrays and function calls that seem to perform poorly.

Code snippet that reproduces the problem

No response

Expected output

Fast analysis.

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions