Skip to content

Extensive processing time #8353

Closed
Closed

Description

@GameCharmer

Bug report

I'm not for sure where this should go, or if anything can be done, but I figured I'd ask anyway.

We updated from 1.4.10 to 1.9.2.
Full scans of our project jumped from around 11 minutes to just over 2 hours.
After purging the cache and waiting for a full scan to complete, subsequent scans finished in about 4 seconds.
Changing a few files results in a 3-4 minute scan time, which seems great compared to the previous 11 minute scan time.
Updating a library, changing a core class, or updating modeling causes an entire 2 hour scan again.
Is there anything I can do to improve this process besides rolling back to an earlier version? I've tried running with --debug but nothing really stands out there as to what could be the issue.

Code snippet that reproduces the problem

Not applicable, but here's a link just in case the bot is too willing to auto-close

https://phpstan.org/r/b31cdfe4-6ed5-47a5-bef8-a4a2462c6ae0

Expected output

10-15 minute processing time instead of 2 hours

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

Resubmitted since previous issue was immediately closed.

Activity

  1. ondrejmirtes commented on Nov 11, 2022

    @ondrejmirtes
    Member

    Hi, something definitely needs to show up when running with --debug. My guess is that between 1.4.10 and 1.9.2, analysis of some files will take similar time, and some will be much longer, helping us to pinpoint the reason. It can boil down to a single class with some weird code that halts the analysis.

    Also, there are many changes between 1.4.10 and 1.9.2. If you run PHPStan on multiple releases and figure out where it slowed down, would also be helpful. You shouldn't do this manually, for example GitHub Actions make this very easy. Just set up the matrix with installing different PHPStan versions and you should see the results very quickly.

  2. ondrejmirtes commented on Nov 11, 2022

    @ondrejmirtes
    Member

    You can also check out the existing issues labeled "performance" to see if it's a similar pattern: performance

  3. GameCharmer commented on Nov 11, 2022

    @GameCharmer
    Author

    It looks like this started in 1.6.0. 1.5.0-1.5.7 all took 35-40 minutes. I'm only able to run a few in parallel, but 1.6.0-1.6.7 are all at 90+ minutes and are still running. I'll follow up when everything completes with a time table.

  4. GameCharmer commented on Nov 12, 2022

    @GameCharmer
    Author

    Average over 3 runs each. 1.8.3 timed out after 3hrs each time. Time shot up at 1.6.0, and again at 1.7.0.

    Version Time
    1.5.0 00:38:19
    1.5.1 00:37:48
    1.5.2 00:40:16
    1.5.3 00:40:46
    1.5.4 00:38:45
    1.5.5 00:39:21
    1.5.6 00:38:57
    1.5.7 00:36:54
    1.6.0 01:25:36
    1.6.1 01:25:43
    1.6.2 01:28:50
    1.6.3 01:30:54
    1.6.4 01:29:31
    1.6.5 01:29:52
    1.6.6 01:28:01
    1.6.7 01:27:21
    1.6.8 01:27:03
    1.6.9 01:29:21
    1.7.0 02:06:41
    1.7.1 02:06:16
    1.7.2 02:05:30
    1.7.3 02:10:29
    1.7.4 02:08:23
    1.7.5 02:09:11
    1.7.6 02:07:48
    1.7.7 02:07:48
    1.7.8 02:08:58
    1.7.9 02:11:34
    1.7.10 02:06:19
    1.7.11 02:09:45
    1.7.12 02:11:28
    1.7.13 02:12:58
    1.7.14 02:10:24
    1.7.15 02:09:15
    1.8.0 02:08:46
    1.8.1 02:01:18
    1.8.2 02:05:26
    1.8.3 03:00:00
    1.8.4 02:21:01
    1.8.5 02:19:13
    1.8.6 02:12:11
    1.8.7 02:02:38
    1.8.8 02:16:51
    1.8.9 02:16:48
    1.8.10 02:12:32
    1.8.11 02:18:55
    1.9.0 02:21:13
    1.9.1 02:19:12
    1.9.2 02:18:51
  5. ondrejmirtes commented on Nov 12, 2022

    @ondrejmirtes
    Member

    How big is the codebase? The usual benchmark I have with two big codebases is that for around 1 million lines of code in ~10k files, it takes 2-3 minutes to analyze from scratch.

    Anyway, we need to nail this down to a specific cause, I still think that a few select files alone will be the cause of this huge performance hit.

  6. GameCharmer commented on Nov 12, 2022

    @GameCharmer
    Author

    3665 files, 733944 lines of code.

    I just ran vendor/bin/phpstan analyse -l 6 -c phpstan_local.neon src --debug, which finished with [OK] No Errors. It took an hour and a half, but is on better hardware.

  7. ondrejmirtes commented on Nov 12, 2022

    @ondrejmirtes
    Member

    That's really a long time. I guess there's something wrong happening in the project because that's not normal.

    You should parse the --debug output and sort files by the time they take. I guess there is going to be some abnormalities.

  8. GameCharmer commented on Nov 12, 2022

    @GameCharmer
    Author

    How do you determine the time per file short of actively watching? When I run with --debug, it just lists every file individually until it reaches the end of the scan.

  9. ondrejmirtes commented on Nov 13, 2022

    @ondrejmirtes
    Member

    Try --debug -vvv.

  10. GameCharmer commented on Nov 13, 2022

    @GameCharmer
    Author

    Thanks! It's still running, but it looks like it's an issue with large arrays. Currently, the longest individual file is 73s, and it uses a 4 dimension array. Second that up by a handful of files all in the 20-30s range that use large arrays from the DB. These are all either classes that handle large reports, or handle user submitted CSV files of data to import.

    Will follow up post-completion.

  11. GameCharmer commented on Nov 13, 2022

    @GameCharmer
    Author

    I'm fairly certain that this is dealing with arrays. 4 files took over 30s to process, all of them deal with large arrays.

    • 73.2s file reads a CSV file that contains sensitive data , has a 4d associative array, but does not check user permissions as it's a cli routine.
    • 51.2s file has a multiple associative arrays, no nesting, but does multiple loops over those sets of arrays based on user permissions.
    • 38.15s file is another CSV ingest process that's basically a stripped down version of the longest running file, but is user side and has permission checks.
    • 34.54s file has a single associative array that is constructed from numerous permission checks then cached.

    Everything in the 10s-29.99s range are larger arrays that do permission checks.

    I'd have assumed it was an issue with our permission system if it wasn't for the longest file. It very may well be so, due to how our permission matrix can be quite deep based on user control settings.

    Is there a way to tell PHPStan to not analyze a particular function? Basically, we have a $cUser->permissionCheckBool('permission.node.here') method that's used in every case but the longest. Nearly every file over 5s uses permission checks, some only calling it a single time, but all do use some form of array or iterator.

    The permission check routine explodes on period, then traverses an array checking for matching nodes. Something like admin.members.student.view would look for $permissions['admin']['members']['student']['view'] for example.

    If it's an issue in the permission system, it'd definitely explain why so many take so long. After parsing the results, I came up with the following quick counts.

    <= 0.1s: 1812
    <= 1s: 697
    <= 5s: 1003
    <= 10s: 115
    <= 30s: 19
    > 30s: 4 
    
  12. mvorisek commented on Nov 14, 2022

    @mvorisek
    Contributor

    if the problem is long parsing time of a large array declaration, it is duplicate of #7955

  13. GameCharmer commented on Nov 14, 2022

    @GameCharmer
    Author

    @mvorisek The arrays are constructed from query results as opposed being predefined in this case. Something like this example.

    $matrix = [];
    while($row = $qry1->fetch()) {
        $matrix[$row['l1']] = [];
        // ...
    }
    
    while($row = $qry2->fetch()) {
        $matrix[$row['l1']][$row['l2']] = [];
        // ...
    }
    
    while($row = $qry3->fetch()) {
        $matrix[$row['l1']][$row['l2']][$row['l3'] = [];
        // ...
    }
  14. mvorisek commented on Nov 14, 2022

    @mvorisek
    Contributor

    it seems related but different array issue - please reproduce using https://phpstan.org/try with an example that is fast enough to pass the sandpit but slow enough to not timeout - such test can be then used to devel the fix and once fixed, it can be extended to cover the original huge file/processing time

  15. GameCharmer commented on Nov 17, 2022

    @GameCharmer
    Author

    @mvorisek I think I was able to successfully replicate something taking a long time without taking a long time.

    https://phpstan.org/r/2435942d-fce5-433b-9622-a73d107d2334

    The response time is just over 2.3s

  16. mvorisek commented on Nov 17, 2022

    @mvorisek
    Contributor

    The avg. sandpit response time is ~2 seconds, but your demo is working, I (simplified the demo and) added a few more levels:

    https://phpstan.org/r/03cef78d-271b-46d6-9d3a-d585fdb58c8f

    and the response time is ~4 seconds then. When I add 1 more level, it crashes (time needed is exponential).

  17. GameCharmer commented on Nov 17, 2022

    @GameCharmer
    Author

    Thanks @mvorisek !

  18. GameCharmer commented on Nov 19, 2022

    @GameCharmer
    Author

    Spent a day rolling brand new Ubuntu 20.04 servers with PHP 8.1.12 to test. It's taking about the same amount of time, but I wind up with a few Internal error: Internal error: preg_match(): Argument #2 ($subject) must be of type string, array given errors in the logs. Running locally, I don't get those at all. I don't think it's related to the issue, since the files that the prior error message is appearing on are particularly small and ancient files. No idea if this is helpful at all.

  19. ondrejmirtes commented on Nov 22, 2022

    @ondrejmirtes
    Member

    @GameCharmer please open a separate issue about that and make sure to add a stack trace.

  20. ondrejmirtes commented on Dec 11, 2022

    @ondrejmirtes
    Member

    I described this category of a performance problem in a general way and opened a new issue about it: #8503

  21. ondrejmirtes commented on Dec 13, 2022

    @ondrejmirtes
    Member

    I'm eager to hear the time it takes to process your codebase in PHPStan 1.9.3. Please measure it and report back :) Maybe it's gonna be improved.

  22. GameCharmer commented on Dec 13, 2022

    @GameCharmer
    Author

    Completed in just a hair over an hour. Well over a 50% reduction in processing time! :D

  23. github-actions commented on Jan 14, 2023

    @github-actions

    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.

  24. locked as resolved and limited conversation to collaborators on Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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