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

Improve expression resolving of superglobals #3762

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Dec 30, 2024

⚠️ This is a re-do of #2012 which caused performance issues. ⚠️

I tried to do some simple testing, but didn't notice much of a difference on where I ran this (a VM, don't have anything better right now, sorry):

before:

→ hyperfine --prepare 'php bin/phpstan clear-result-cache -q' 'php -d memory_limit=448M bin/phpstan'
Benchmark 1: php -d memory_limit=448M bin/phpstan
  Time (mean ± σ):     73.668 s ±  1.285 s    [User: 262.490 s, System: 3.837 s]
  Range (min … max):   72.091 s … 76.341 s    10 runs

after:

→ hyperfine --prepare 'php bin/phpstan clear-result-cache -q' 'php -d memory_limit=448M bin/phpstan'
Benchmark 1: php -d memory_limit=448M bin/phpstan
  Time (mean ± σ):     73.868 s ±  0.507 s    [User: 264.315 s, System: 3.717 s]
  Range (min … max):   73.323 s … 75.092 s    10 runs

it would be interesting if #2012 (comment) still happens and, if so, if we can get more details here to maybe improve it before I forget about this again.

@herndlm herndlm force-pushed the improve-superglobal-expression-resolving branch from 5d687dc to 95b8360 Compare January 4, 2025 13:11
@herndlm herndlm marked this pull request as ready for review January 6, 2025 23:08
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes
Copy link
Member

I'm not a huge fan of this approach. It's going to pollute the output of MutatingScope::debug() (https://phpstan.org/r/e037be34-122b-4846-82f4-487dc0963845) and make it unreadable. Also the values are going to be in the scope and they are going to be mostly the same for all modern apps which isn't great.

Could we add these to the scope only when they are narrowed by some assignments or conditions? I think we already do a similar thing for PHP_VERSION_ID (maybe all constants). Thanks.

@herndlm
Copy link
Contributor Author

herndlm commented Jan 7, 2025

Could we add these to the scope only when they are narrowed by some assignments or conditions? I think we already do a similar thing for PHP_VERSION_ID (maybe all constants). Thanks.

smart, why haven't I thought about this :) I pushed something, not sure if it's going in the right direction or not though.

@herndlm
Copy link
Contributor Author

herndlm commented Jan 7, 2025

I have a feeling that the stuff accessing Scope::SUPERGLOBAL_VARIABLES and MutatingScope::isGlobalVariable() as well as the looping in getSuperglobalTypes() and getNativeSuperglobalTypes() can be somehow improved still. I could try refactoring it.

}

/** @return array<string, ExpressionTypeHolder> */
private function getNativeSuperglobalTypes(): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could de-duplicate this method by passing in the expressionTypes via parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. I stole this basically from getNativeConstantTypes() vs getConstantTypes() which could be adapted in a similar way then. maybe in a follow-up refactor :)

@herndlm herndlm force-pushed the improve-superglobal-expression-resolving branch from 8a1e313 to 86901a7 Compare January 7, 2025 11:23
@herndlm herndlm force-pushed the improve-superglobal-expression-resolving branch from 86901a7 to 44586ac Compare January 7, 2025 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants