Problem/Motivation
The file core/tests/Drupal/Tests/Component/Utility/RectangleTest.php takes a very long time to scan for coding standards. This is because there is a humungous array returned by a method. Considering this is test data maybe we should ignore the file.
On PHP 7.2 scanning the entirety of core: Time: 2 mins, 18.16 secs
Of which scanning this file takes: Time: 41.09 secs
Proposed resolution
I've tried to ignore only the huge array. However this didn't work. The only thing that resulted in a performance improvement is adding // @codingStandardsIgnoreFile
to the top of the file but as this is a test class I'm loathe to do that. Perhaps a better way would be to just move the data to a different file.
Remaining tasks
Move data to a .json file so that the large dataset does not need to be coding standards checked.
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#12 | 2911280-2-12.patch | 251.86 KB | alexpott |
#12 | 7-12-interdiff.txt | 693 bytes | alexpott |
#9 | 2911280-2-9.patch | 251.75 KB | alexpott |
#7 | 2911280-7.patch | 4.05 KB | dawehner |
#4 | 2911280-RectangleTest-extra.interdiff.txt | 852 bytes | andypost |
Comments
Comment #2
alexpottComment #3
andypostComment #4
andypostRemains from formatting, I guess they out of scope
Comment #5
pazhyn CreditAttribution: pazhyn as a volunteer and at AnyforSoft, Drupal Ukraine Community for AnyforSoft commentedComment #6
dawehnerI had some quick look of what actually happens:
\PHP_CodeSniffer_File::start
triggers listeners for each token in the php file.The array decleration sniff
\Squiz_Sniffs_Arrays_ArrayDeclarationSniff
registers on the array token (both new and old syntax), so it is called OFTEN (see https://blackfire.io/profiles/ebc1fddd-928c-4688-8d0f-be1dfe832b93/graph...)The call to
$phpcsFile->findPrevious(array(T_OPEN_PARENTHESIS, T_SEMICOLON), ($stackPtr - 1), null, false)
then seems to be the costly part. Funny enough if you ignore errors, they are just not added later, but the processing is still done, no matter what.Comment #7
dawehnerHere is an interdiff which reduces the time significantly ... Down from 50 to 5 seconds on my machine.
The more () we add the faster this scanning gets
The reason why this works is that the line above:
$phpcsFile->findPrevious(array(T_OPEN_PARENTHESIS, T_SEMICOLON), ($stackPtr - 1), null, false)
tries to find a parenthesis ... which could take a while when there are a lot of tokens involved. The solution is to give them some parenthesis ...Comment #8
dawehnerNote: I don't think this is the right way to fix it. Moving the data into a json file and then load it would be indeed a simple solution for the problem.
Comment #9
alexpottPatch attached moves the data to a json file.
Comment #10
alexpottBefore
After
So the patch has not changed the number of assertions.
Comment #11
Mile23Boggle.
Patch also applies to 8.4.x and while this is a CS improvement it's also a testing improvement (5 extra minutes per branch test...)
Running #9 on 8.4.x. Please consider adding it there, too.
Comment #12
alexpottDiscussed with @eli-t and they pointed out that we should probably add a comment as to why the data is all in a json file.
Comment #13
Mile23Is there an issue about fixing this in coder/phpcs? Maybe add a @todo if there is.
Comment #14
dawehnerI opened up an upstream issue: https://github.com/squizlabs/PHP_CodeSniffer/issues/1681
Comment #15
alexpott@Mile23 I'm not sure that having an @todo for something to change in PHPCS is necessary - also there's not too much advantage to have the data in the same file as the test so even if this is fixed in PHPCS would we bother moving it back?
Comment #16
Mile23Thanks for the upstream issue.
Here's an RTBC because putting the data back isn't that big a deal.
Comment #17
dawehnerStoring data providers in an external file is totally recommended in phpunit, see https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writ...
Comment #18
tacituseu CreditAttribution: tacituseu commentedIt's also responsible for PHP 5.5 & PostgreSQL 9.1 testbot's timeouts/aborts that started after #2901739: Fix 'Squiz.Arrays.ArrayDeclaration' coding standard went in (see: #2901739-44: Fix 'Squiz.Arrays.ArrayDeclaration' coding standard).
Comment #19
catchCommitted/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!