Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analysis is slow when analyzing arrays with lots of elements #1279

Closed
TysonAndre opened this issue Feb 4, 2019 · 1 comment
Closed

Analysis is slow when analyzing arrays with lots of elements #1279

TysonAndre opened this issue Feb 4, 2019 · 1 comment
Labels

Comments

@TysonAndre
Copy link
Contributor

TysonAndre commented Feb 4, 2019

See https://github.com/vimeo/psalm/blob/3.0.13/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php#L54-L141

It's looping over all of the array elements and building up a new array shape type by adding to an existing type. This is O(N^2) (quadratic runtime - It's 1000 times slower to copy an array with 1000 times more elements in an array shape being built up)

What would be better would be to create a helper to combine an arbitrary number of union types, and call that after building up an array with the union type of each array value in ArrayAnalyzer->analyze.

I notice that CallMap.php was excluded, making this less noticeable. The same problem would occur on CallMap.php

        <ignoreFiles>
            <file name="src/Psalm/Internal/CallMap.php"/>

Using a mutable Type builder and calling builder->build() would probably be another way to avoid the slowness of copying large array shapes/union types - something like this but for psalm and using psalm's logic to merge array/object shapes

@muglug muglug added the bug label Feb 4, 2019
@muglug muglug closed this as completed in bbf013b Feb 4, 2019
@muglug
Copy link
Collaborator

muglug commented Feb 4, 2019

Thanks!

Running ./psalm src/Psalm/Internal/CallMap.php
Before: 130s
After: 3s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants