phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

Phpstan array analysis takes forever

Open mvorisek opened this issue 3 years ago • 2 comments

Bug report

In our fork, mostly identical to https://github.com/tecnickcom/TCPDF the analysis of fonts/ takes forever.

I was expecting it to be solved in v1.8.4 phpstan, but it seems the font files, mostly consisting of large arrays, are still not analysed effectively. The array shape is degraded probably not when parsing, and the full array shape is still built once which is very slow.

Code snippet that reproduces the problem

n/a, performance problem

How to reproduce:

  1. clone main branch of https://github.com/tecnickcom/TCPDF repo
  2. analyse the whole repo directory /wo fonts/ directory (and phpstan level 1) - phpstan should finish in about a minute /w about 70 errors
  3. analyse the whole repo directory again, but now incl. the fonts/ directory - phpstan takes forever (did not finish in more than 30 minutes)

Expected output

phpstan must finish in about the same time for 2. and 3. repro steps

once fixed, the TCPDF repo should be added to integration tests

mvorisek avatar Sep 07 '22 11:09 mvorisek

How does the performance compare in PHPStan 1.8.5?

ondrejmirtes avatar Sep 07 '22 16:09 ondrejmirtes

With phpstan v1.8.5 and fonts/ directory included, phpstan still does not finish.

image

mvorisek avatar Sep 07 '22 16:09 mvorisek

I finished my update from PHPStan 1.4 to PHPStan 1.8 and the CI runs time (when there is no result cache) went from ~24 mins to ~62 mins. :flushed:

enumag avatar Oct 07 '22 10:10 enumag

@enumag This isn't helpful nor actionable. Run with -vvv --debug, find which file takes the longest, isolate the problem, and open a separate issue.

ondrejmirtes avatar Oct 07 '22 10:10 ondrejmirtes

I'm running into a similar issue on my codebase, and have created a small sample here to show the structure that phpstan hangs on. The playground times out when the array contains about 2000 values. https://phpstan.org/r/46a17e15-22a3-414a-b8d7-c27617d5ac5b.

If a longer sample is needed, I've attached a txt file with the same class with 10k values that takes 66 seconds to analyse on my machine. Our codebase contains multiple of these maps, and since the upgrade we did from from 1.8.2 to 1.8.8, the CI pipelines times out.

Thanks for all the hard work you're putting into this project, it's absolutely amazing and a complete paradigm shift for php.

HugeMapClass.php.txt

bart-mollie avatar Oct 10 '22 09:10 bart-mollie

@bart-mollie the slowness in your example code is from analysing the array_search call. It's not the same issue, but perhaps some other examples from your codebase are. Please create a separate issue for this.

rvanvelzen avatar Oct 10 '22 13:10 rvanvelzen

Thanks for looking into it! I've created https://github.com/phpstan/phpstan/issues/8147

bart-mollie avatar Oct 10 '22 13:10 bart-mollie

@mvorisek could you double check with the latest development version?

if it is still slow, could you extract the actually slow files so I can reproduce easily?

you may utilize https://gist.github.com/ruudk/41897eb59ff497b271fc9fa3c7d5fb27

staabm avatar Dec 12 '22 10:12 staabm

feel free to test on TCPDF repo directly or with https://github.com/mvorisek/TCPDF/commit/a458bc1e61bcf5a2060b78164e57619d0f6569cc with https://github.com/mvorisek/TCPDF/blob/a458bc1e61bcf5a2060b78164e57619d0f6569cc/phpstan.neon.dist#L8 line removed (to reproduce, phpstan should finish in about a minute)

mvorisek avatar Dec 12 '22 10:12 mvorisek

Should be fast now. Please test PHPStan 1.9.5 once it's released :)

ondrejmirtes avatar Dec 20 '22 22:12 ondrejmirtes

Let's keep this issue open until released. If ok, I will be happy to provide a regression test for it then as this caused a lot of trouble for me in the past. Thank you.

mvorisek avatar Dec 20 '22 22:12 mvorisek

@mvorisek I couldn't test your linked commit because the branch probably got deleted, or it's in a different fork.

Anyway, this thread got too long, I want people to open new issues with specific examples. I believe many examples from this thread are fixed.

ondrejmirtes avatar Dec 20 '22 22:12 ondrejmirtes

@ondrejmirtes see https://github.com/tecnickcom/TCPDF/commit/37aa6eeb73b9bfeeb321d846e46b0d98e49ff3d3 last week merged officially to TCPDF - so how to test? clone https://github.com/tecnickcom/TCPDF.git and remove fonts/ from excludePaths in the phpstan config

mvorisek avatar Dec 20 '22 22:12 mvorisek

You can now compare 1.9.4 vs 1.9.x-dev and report back.

ondrejmirtes avatar Dec 20 '22 22:12 ondrejmirtes

I can confirm now TCPDF finish in a few minutes. Thank you.

mvorisek avatar Dec 21 '22 12:12 mvorisek

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 22 '23 00:01 github-actions[bot]