Skip to content

Performance: Reduce isObjectType calls#3502

Merged
samsonasik merged 1 commit intorectorphp:mainfrom
keulinho:reduce-is-object-type-calls
Mar 22, 2023
Merged

Performance: Reduce isObjectType calls#3502
samsonasik merged 1 commit intorectorphp:mainfrom
keulinho:reduce-is-object-type-calls

Conversation

@keulinho
Copy link
Copy Markdown
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

$classReflection = $this->reflectionProvider->getClass($resolvedObjectType->getClassName());
if (\trait_exists($requiredObjectType->getClassName())) {
if (!isset($this->traitExistsCache[$classReflection->getName()])) {
Copy link
Copy Markdown
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
Copy Markdown
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
$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
Copy Markdown
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
Copy Markdown
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