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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class ClassReflection implements ReflectionWithFilename
/** @var string|false|null */
private $filename;

/** @var string|false|null */
private $reflectionDocComment;

/**
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
* @param \PHPStan\Type\FileTypeMapper $fileTypeMapper
Expand Down Expand Up @@ -933,12 +936,15 @@ public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
return null;
}

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

if ($this->reflectionDocComment === false) {
return null;
}

return $this->fileTypeMapper->getResolvedPhpDoc($fileName, $this->getName(), null, null, $docComment);
return $this->fileTypeMapper->getResolvedPhpDoc($fileName, $this->getName(), null, null, $this->reflectionDocComment);
}

private function getFirstExtendsTag(): ?ExtendsTag
Expand Down
9 changes: 8 additions & 1 deletion src/Type/FileTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class FileTypeMapper
/** @var array<string, bool> */
private array $alreadyProcessedDependentFiles = [];

/** @var array<string, string> */
private array $docKeys = [];

public function __construct(
ReflectionProviderProvider $reflectionProviderProvider,
Parser $phpParser,
Expand Down Expand Up @@ -518,7 +521,11 @@ private function getPhpDocKey(
string $docComment
): string
{
$docComment = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' ');
$cacheKey = md5($docComment);
if (!isset($this->docKeys[$cacheKey])) {
$this->docKeys[$cacheKey] = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' ');
}
$docComment = $this->docKeys[$cacheKey];

return md5(sprintf('%s-%s-%s-%s', $class, $trait, $function, $docComment));
}
Expand Down