Skip to content

Investigating PHPStan 1.8 performance #8146

Closed

Description

@enumag

Bug report

As I mentioned in one thread PHPStan 1.8 seems to be lot slower on my project than version 1.4. Opening this thread to report what I find. Unfortunately since blackfire cancelled their free tier I have no way to investigate in as much detail as I'd like.

Anyway first thing I found is a test class that takes ~25 seconds to analyze. I can't post it publicly but the main issue is likely the two-dimensional associative array in it's @dataProvider function. It looks something like this:

        return [
            'string' => [
                'key' => ValueObject::fromValues(...),
                'key1' => SomeClass::constant
                    && \in_array(
                        AnotherClass::constant,
                        YetAnotherClass::constant,
                        true,
                    ),
                'key2' => SomeClass::constant
                    && \in_array(
                        AnotherClass::constant,
                        YetAnotherClass::constant,
                        true,
                    ),
                'key3' => SomeClass::constant
                    && \in_array(
                        AnotherClass::constant,
                        YetAnotherClass::constant,
                        true,
                    ),
                'key4' => SomeClass::constant
                    && \in_array(
                        AnotherClass::constant,
                        YetAnotherClass::constant,
                        true,
                    ),
                'key5' => SomeClass::constant
                    && \in_array(
                        AnotherClass::constant,
                        YetAnotherClass::constant,
                        true,
                    ),
            ],
            // ... several more arrays like that
        ];

I can sen you the class privately if needed.


Another thing that is really slow is analysis of Doctrine entities. However in this case I assume it's because a custom rule which was loading Doctrine annotations in an inefficient way so I'm trying to optimize that first before making conclusions. See #8144


Aside from that it mostly seems to be a slowdown in general. I'd like to do a comparison of what classes took longer than on the previous version. I'll write a parser for the --debug -vvv results to make it a json instead so that I can compare what classes are taking longer now compared to the old version.

Activity

  1. mergeable commented on Oct 10, 2022

    @mergeable

    This bug report is missing a link to reproduction at phpstan.org/try.

    It will most likely be closed after manual review.

  2. ondrejmirtes commented on Oct 10, 2022

    @ondrejmirtes
    Member

    You can always anonymize the source code so that it doesn't contain anything proprietary.

  3. enumag commented on Oct 10, 2022

    @enumag
    ContributorAuthor

    I tried but the anonymized code doesn't have the same problem. It seems to be heavily dependent on the values the constants resolve to somehow (mostly UUIDs but it seems the amount and whether or not they're the same or not matters a lot). I can't create a reproducible case out of this in a reasonable amount of time which means you can't really do much about it either... but yeah there is definitely some slowdown going on with large array constants so keep an eye for similar reports...

    But anyway that's just one class, I still need to investigate the rest.

  4. mmarton commented on Oct 10, 2022

    @mmarton

    Don't want to hijack this issue, but I can confirm a serious regression in performance between v1.8.0 - v1.8.6 and v1.8.6 - v1.8.8.

    phpstan 1.8.8
    
    src/DataFixtures/InvestmentFixtures.php
    --- consumed 0 B, total 412 MB, took 0.00 s
    src/DataFixtures/LocationFixtures.php
    --- consumed 10 MB, total 422 MB, took 256.61 s
    src/DataFixtures/OrganizationFixtures.php
    --- consumed 0 B, total 422 MB, took 0.34 s
    

    that 1 file on the middle takes 90% of the time phpstan uses. The file has 13k static array data in it and some functions. (hungarian counties, cities, gps coords and some extra info. I can post it, it's public data)

    the same file on phpstan 1.8.6:

    phpstan 1.8.6
    
    src/DataFixtures/InvestmentFixtures.php
    --- consumed 0 B, total 504.5 MB, took 0.00 s
    src/DataFixtures/LocationFixtures.php
    --- consumed 10 MB, total 514.5 MB, took 51.30 s
    src/DataFixtures/OrganizationFixtures.php
    --- consumed 0 B, total 514.5 MB, took 0.51 s
    

    and phpstan 1.8.0

    phpstan 1.8.0
    
    src/DataFixtures/InvestmentFixtures.php
    --- consumed 0 B, total 504 MB, took 0.00 s
    src/DataFixtures/LocationFixtures.php
    --- consumed 12.5 MB, total 516.5 MB, took 3.50 s
    src/DataFixtures/OrganizationFixtures.php
    --- consumed 0 B, total 516.5 MB, took 0.70 s
    

    my config file:

    includes:
    	- vendor/phpstan/phpstan-doctrine/extension.neon
    	- vendor/phpstan/phpstan-symfony/extension.neon
    	- vendor/phpstan/phpstan-strict-rules/rules.neon
    	- vendor/phpstan/phpstan-deprecation-rules/rules.neon
    
    parameters:
    	level: 6
    	paths:
    		- migrations
    		- public
    		- src
    		- tests
    	tmpDir: var/cache/phpstan
    	scanFiles:
    		- vendor/twig/twig/src/Extension/CoreExtension.php
    	symfony:
    		container_xml_path: var/cache/dev/App_KernelDevDebugContainer.xml
    	checkGenericClassInNonGenericObjectType: false
    	checkMissingIterableValueType: false
    	checkUninitializedProperties: true
  5. rvanvelzen commented on Oct 10, 2022

    @rvanvelzen
    Contributor

    @mmarton please provide the slow file so we can figure out what's happening.

  6. enumag commented on Oct 10, 2022

    @enumag
    ContributorAuthor

    Hmm it seems performance is ok-ish on my side after optimizing our custom rules. So I'd actually close this issue if it wasn't hijacked 😅

  7. SaschaHowe commented on Oct 12, 2022

    @SaschaHowe

    Hey,

    I want to confirm @mmarton findings. The execution time for this

    <?php
    
    declare(strict_types=1);
    
    class HelloWorld
    {
        private SessionInterface $session;
        private DataObject       $object;
    
        public function __construct(SessionInterface $session, DataObject $object)
        {
            $this->session = $session;
            $this->object  = $object;
        }
    
        public function sayHello(): void
        {
            $changeLog = [];
    
            $firstname = $this->session->get('firstname');
            if ($firstname !== $this->object->getFirstname()) {
                $changelog['firstname_old'] = $this->object->getFirstname();
                $changelog['firstname_new'] = $firstname;
            }
    
            $lastname = $this->session->get('lastname');
            if ($lastname !== $this->object->getLastname()) {
                $changelog['lastname_old'] = $this->object->getLastname();
                $changelog['lastname_new'] = $lastname;
            }
    
            $street = $this->session->get('street');
            if ($street !== $this->object->getStreet()) {
                $changelog['street_old'] = $this->object->getStreet();
                $changelog['street_new'] = $street;
            }
    
            $zip = $this->session->get('zip');
            if ($zip !== $this->object->getZip()) {
                $changelog['zip_old'] = $this->object->getZip();
                $changelog['zip_new'] = $zip;
            }
    
            $city = $this->session->get('city');
            if ($city !== $this->object->getCity()) {
                $changelog['city_old'] = $this->object->getCity();
                $changelog['city_new'] = $city;
            }
    
            $phonenumber = $this->session->get('phonenumber');
            if ($phonenumber !== $this->object->getPhonenumber()) {
                $changelog['phonenumber_old'] = $this->object->getPhonenumber();
                $changelog['phonenumber_new'] = $phonenumber;
            }
    
            $email = $this->session->get('email');
            if ($email !== $this->object->getEmail()) {
                $changelog['email_old'] = $this->object->getEmail();
                $changelog['email_new'] = $email;
            }
    
            $deliveryFirstname = $this->session->get('deliveryFirstname');
            if ($deliveryFirstname !== $this->object->getDeliveryFirstname()) {
                $changelog['deliveryFirstname_old'] = $this->object->getDeliveryFirstname();
                $changelog['deliveryFirstname_new'] = $deliveryFirstname;
            }
    
            $deliveryLastname = $this->session->get('deliveryLastname');
            if ($deliveryLastname !== $this->object->getDeliveryLastname()) {
                $changelog['deliveryLastname_old'] = $this->object->getDeliveryLastname();
                $changelog['deliveryLastname_new'] = $deliveryLastname;
            }
            $deliveryStreet = $this->session->get('deliveryStreet');
            if ($deliveryStreet !== $this->object->getDeliveryStreet()) {
                $changelog['deliveryStreet_old'] = $this->object->getDeliveryStreet();
                $changelog['deliveryStreet_new'] = $deliveryStreet;
            }
            $deliveryZip = $this->session->get('deliveryZip');
            if ($deliveryZip !== $this->object->getDeliveryZip()) {
                $changelog['deliveryZip_old'] = $this->object->getDeliveryZip();
                $changelog['deliveryZip_new'] = $deliveryZip;
            }
            $deliveryCity = $this->session->get('deliveryCity');
            if ($deliveryCity !== $this->object->getDeliveryCity()) {
                $changelog['deliveryCity_old'] = $this->object->getDeliveryCity();
                $changelog['deliveryCity_new'] = $deliveryCity;
            }
    
        }
    }
    
    interface SessionInterface
    {
        /**
         * @return mixed
         */
        public function get(string $key);
    }
    
    interface DataObject
    {
        /**
         * @return string|null
         */
        public function getFirstname();
        /**
         * @return string|null
         */
        public function getLastname();
        /**
         * @return string|null
         */
        public function getStreet();
        /**
         * @return string|null
         */
        public function getZip();
        /**
         * @return string|null
         */
        public function getCity();
        /**
         * @return string|null
         */
        public function getPhonenumber();
        /**
         * @return string|null
         */
        public function getEmail();
        /**
         * @return string|null
         */
        public function getDeliveryFirstname();
        /**
         * @return string|null
         */
        public function getDeliveryLastname();
        /**
         * @return string|null
         */
        public function getDeliveryStreet();
        /**
         * @return string|null
         */
        public function getDeliveryZip();
        /**
         * @return string|null
         */
        public function getDeliveryCity();
    }
    
    

    jumps from

    vendor/bin/phpstan --version
    PHPStan - PHP Static Analysis Tool 1.8.6
    
    vendor/bin/phpstan analyze --level=9 --memory-limit=-1 --debug test.php -vvv
    Result cache not used because of debug mode.
    /var/www/html/test.php
    --- consumed 15.71 MB, total 66 MB, took 0.49 s
    

    to

    vendor/bin/phpstan --version
    PHPStan - PHP Static Analysis Tool 1.8.7
    
    vendor/bin/phpstan analyze --level=9 --memory-limit=-1 --debug test.php -vvv
    Result cache not used because of debug mode.
    /var/www/html/test.php
    --- consumed 122 MB, total 190 MB, took 70.29 s
    Result cache was not saved because only files were passed as analysed paths.
    

    Interestingly, just removing the 4 lines deliveryCity reduces the runtime to ~17 seconds, and removing another 4 lines for example the deliveryCity it's down to 4 seconds.

    Hope this helps.

  8. robertbasic commented on Oct 21, 2022

    @robertbasic
    Contributor

    Just to chime in, our static analysis job went from ~5 minutes to ~20 minutes on ~16000 files. We went from 1.4.8 to 1.8.8.

    Edit: Did a few pipelines with different major versions:

    • With 1.4.8 it ran in about 4 minutes
    • 1.8.8 is about 20 minutes
    • 1.7.15 is about 11 minutes
    • 1.6.9 is about 11 minutes
    • 1.5.7 is back down to 4-5 minutes
    • 1.6.0 is about 11 minutes

    Edit 2: We're using the following community extensions/custom rules, deactivating them doesn't affect the time too much, less than a minute:

        - vendor/phpstan/phpstan-phpunit/extension.neon
        - vendor/phpstan/phpstan-phpunit/rules.neon
        - vendor/phpstan/phpstan-doctrine/extension.neon
        - vendor/phpstan/phpstan-symfony/extension.neon
        - vendor/jangregor/phpstan-prophecy/extension.neon
    
  9. mmarton commented on Oct 25, 2022

    @mmarton

    @rvanvelzen Any news about this issue? Currently I've excluded the affected file... Let me know if I can help investigating

  10. costash commented on Dec 1, 2022

    @costash

    I've experienced a similar performance issue with versions after 1.5.7. For now I've pinned phpstan to 1.5.7 in our projects, but would be great to find the root cause of this.

  11. staabm commented on Dec 1, 2022

    @staabm
    Contributor

    I've experienced a similar performance issue with versions after 1.5.7. For now I've pinned phpstan to 1.5.7 in our projects, but would be great to find the root cause of this.

    please open a separate issue with a concrete code example so we can investigate

  12. ruudk commented on Dec 8, 2022

    @ruudk
    Contributor

    For our project I investigated which files are the slowest to analyze and found a few files that contained 2000+ lines or array declarations. By refactoring a those files we were able to reduce the time with 5,5 minutes 🤯 . To make it easier to find these files for others, I published the script I used to investigate it. Maybe it can be useful for others too 😊

  13. staabm commented on Dec 8, 2022

    @staabm
    Contributor

    if you have a reproducible test-case which is slow, please send it our way, so we can see where we can improve phpstan

  14. mmarton commented on Dec 8, 2022

    @mmarton

    I've already did: #8146 (comment)

  15. ondrejmirtes commented on Dec 11, 2022

    @ondrejmirtes
    Member

    I classified several problems from this thread and created new issues based on them:

    I'm gonna close this issue because it got a bit out of hand. If you have any performance issues, please open a separate issue for each one and provide an example, so that we can either group it with other similar issues, or identify it as a new category. Thanks.

  16. 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.

  17. staabm commented on Dec 13, 2022

    @staabm
    Contributor
  18. ruudk commented on Dec 13, 2022

    @ruudk
    Contributor

    Amazing work on this:
    1.9.2: 11 minutes and 5.42 seconds
    1.9.3: 9 minutes and 19.90 seconds

    On 14500 files.

    🚀 🔥 👏 💙

  19. rene-bos commented on Dec 14, 2022

    @rene-bos

    These performance gains are amazing!! From 7 minutes 15 seconds minute back to 5 minutes and 7 seconds! A performance gain of 28%! 💪🏻 🥳

    Good job @staabm and the rest, a huge thanks form us!

  20. ondrejmirtes commented on Dec 14, 2022

    @ondrejmirtes
    Member

    We're not stopping here. I want to give you tools to find out the slowest files in your codebase which will be useful for further inspection. Either you'll choose to exclude them from the analysis (like we do for example with https://github.com/phpstan/phpstan-src/blob/1.9.x/resources/functionMap.php), or you'll report them in GitHub issues, so we could improve the performance :)

  21. SaschaHowe commented on Dec 14, 2022

    @SaschaHowe

    Hey,

    thank you very much for your efforts 🎉 👍

    my test case from above today went from 86.21 seconds on 1.8.7 to 37.48s on 1.9.3 which is a huge improvement. 🎉

    But it's still a big performance hit compared to 1.8.6 with its 0.19s. I'm not sure what exactly happened in 1.8.7 that caused this increase in time and memory but it feels like for a "simple" file like the one in my test, it's a bit much time.

    See below the details for the runs.

    playground/phpstan
    ▶ php vendor/bin/phpstan --version
    PHPStan - PHP Static Analysis Tool 1.8.7
    
    playground/phpstan
    ▶ vendor/bin/phpstan analyze --level=9 --memory-limit=-1 --debug test.php -vvv
    Result cache not used because of debug mode.
    /playground/phpstan/test.php
    --- consumed 134 MB, total 170 MB, took 86.21 s
    Result cache was not saved because only files were passed as analysed paths.
    
    
     [OK] No errors
    
    
    Used memory: 170 MB
    
    playground/phpstan
    ▶ php vendor/bin/phpstan --version
    PHPStan - PHP Static Analysis Tool 1.9.3
    
    playground/phpstan
    ▶ vendor/bin/phpstan analyze --level=9 --memory-limit=-1 --debug test.php -vvv
    Result cache not used because of debug mode.
    /playground/phpstan/test.php
    --- consumed 146 MB, total 184 MB, took 37.48 s
    Result cache was not saved because only files were passed as analysed paths.
    
    
     [OK] No errors
    
    
    Used memory: 184 MB
    
    playground/phpstan
    ▶ php vendor/bin/phpstan --version
    PHPStan - PHP Static Analysis Tool 1.8.6
    
    playground/phpstan
    ▶ vendor/bin/phpstan analyze --level=9 --memory-limit=-1 --debug test.php -vvv
    Result cache not used because of debug mode.
    /playground/phpstan/test.php
    --- consumed 18 MB, total 54 MB, took 0.18 s
    Result cache was not saved because only files were passed as analysed paths.
    
    
     [OK] No errors
    
    
    Used memory: 54 MB
    
    
  22. enumag commented on Dec 14, 2022

    @enumag
    ContributorAuthor

    For me it went from 16 mins to 15 mins and 15 seconds. Still a good improvement. 👍

  23. mmarton commented on Dec 14, 2022

    @mmarton

    Form me (https://github.com/mmarton/phpstan-issue8146/blob/master/src/DataFixtures/LocationFixtures.php) it went down by ~74%

    • phpstan 1.9.2: consumed 12 MB, total 190 MB, took 174.24 s
    • phpstan 1.9.3: consumed 26 MB, total 204 MB, took 45.73 s

    thank you for the great work

  24. github-actions commented on Jan 15, 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.

  25. locked as resolved and limited conversation to collaborators on Jan 15, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions