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

Performance: Reduce isObjectType calls #3502

Merged
merged 1 commit into from
Mar 22, 2023

Conversation

keulinho
Copy link
Contributor

As seen in #3500 isObjectType-calls can be pretty costly in terms of performance, in a lot of cases isObjectType was used in combination with isName in multiple rectors, in those cases checking isName first is a lot faster and removes a lot of not needed calls to isObjectType

Together with #3501 this PR leads to the same performance improvements as the original PR #3500

@keulinho keulinho requested a review from TomasVotruba as a code owner March 22, 2023 12:47
@@ -350,7 +355,11 @@ private function isObjectTypeOfObjectType(ObjectType $resolvedObjectType, Object
}

$classReflection = $this->reflectionProvider->getClass($resolvedObjectType->getClassName());
if (\trait_exists($requiredObjectType->getClassName())) {
if (!isset($this->traitExistsCache[$classReflection->getName()])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a follow up for #3501

Copy link
Member

@samsonasik samsonasik left a comment

Choose a reason for hiding this comment

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

Looks good, thank you @keulinho 👍

@samsonasik samsonasik merged commit 5e29a35 into rectorphp:main Mar 22, 2023
@keulinho keulinho deleted the reduce-is-object-type-calls branch March 22, 2023 15:08
@@ -350,7 +355,11 @@ private function isObjectTypeOfObjectType(ObjectType $resolvedObjectType, Object
}

$classReflection = $this->reflectionProvider->getClass($resolvedObjectType->getClassName());
if (\trait_exists($requiredObjectType->getClassName())) {
if (!isset($this->traitExistsCache[$classReflection->getName()])) {
$this->traitExistsCache[$classReflection->getName()] = \trait_exists($requiredObjectType->getClassName());
Copy link
Contributor

@staabm staabm Apr 17, 2023

Choose a reason for hiding this comment

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

calling trait_exists or any other runtime autoloading mechanism like class_exists in a static analysis context with phpstan is wrong most of the time.

these *_exists calls will trigger the real autoloader and might lead to code beeing executed. this in turn can have side effects which can lead to different analysis results based on the order in which files are analyzed.

on the long run, rector should not use *_exists php-src functions at all.

in phpstan you ususally use the *Reflection apis which use static reflection, which in turns makes sure that no code is beeing executed while analysis.

Copy link
Contributor

Choose a reason for hiding this comment

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

fixed with #3627

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.

3 participants