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

Memoize expensive calls to docComments. #458

Merged
merged 1 commit into from
Feb 26, 2021

Conversation

pyguerder
Copy link
Contributor

I ran multiple tests using Blackfire. Memoizing these expensive calls reduces both RAM consumption & execution time.

This is an example with running on https://github.com/pyguerder/phpstan-propel2-example/blob/master/generated-classes/App/AnalyzeMe2.php only: https://blackfire.io/profiles/compare/7f25525d-0676-40b8-a809-a92692106be6/graph

And this is an example running on my main project (154 classes): https://blackfire.io/profiles/compare/7cc0ea82-3727-4a95-b948-112678bc0991/graph

@ondrejmirtes can you please have a look at this?

Thanks in advance, best regards

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Hi, this looks promising, thanks!

@@ -933,7 +936,7 @@ public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
return null;
}

$docComment = $this->reflection->getDocComment();
$docComment = $this->reflectionDocComment ?? $this->reflectionDocComment = $this->reflection->getDocComment();
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to use a control structure (an if) here instead of just ?? on a single line. Like this:

if ($this->reflectionDocComment !== null) {
    $docComment = $this->reflectionDocComment;
} else {
    $docComment = $this->reflection->getDocComment();
    $this->reflectionDocComment = $docComment;
}

@@ -518,7 +521,8 @@ private function getPhpDocKey(
string $docComment
): string
{
$docComment = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' ');
$cacheKey = md5($docComment);
$docComment = $this->docKeys[$cacheKey] ?? $this->docKeys[$cacheKey] = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' ');
Copy link
Member

Choose a reason for hiding this comment

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

And the same here.

This reduces both RAM consumption & execution time.
@pyguerder
Copy link
Contributor Author

@ondrejmirtes thanks for your feedback! Can you please look at it again?

I don't understand why some tests fail. I'm not sure it is related to my changes... Can you please advise?

@ondrejmirtes
Copy link
Member

Master is broken, it's not your fault 😊

@ondrejmirtes ondrejmirtes merged commit dfcb87e into phpstan:master Feb 26, 2021
@ondrejmirtes
Copy link
Member

Thank you!

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