Skip to content

Commit dfcb87e

Browse files
authored
Memoize expensive calls to docComments. (#458)
This reduces both RAM consumption & execution time. Co-authored-by: Pierre-Yves Guerder <[email protected]>
1 parent 72b02a6 commit dfcb87e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Reflection/ClassReflection.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class ClassReflection implements ReflectionWithFilename
8989
/** @var string|false|null */
9090
private $filename;
9191

92+
/** @var string|false|null */
93+
private $reflectionDocComment;
94+
9295
/**
9396
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
9497
* @param \PHPStan\Type\FileTypeMapper $fileTypeMapper
@@ -933,12 +936,15 @@ public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
933936
return null;
934937
}
935938

936-
$docComment = $this->reflection->getDocComment();
937-
if ($docComment === false) {
939+
if ($this->reflectionDocComment === null) {
940+
$this->reflectionDocComment = $this->reflection->getDocComment();
941+
}
942+
943+
if ($this->reflectionDocComment === false) {
938944
return null;
939945
}
940946

941-
return $this->fileTypeMapper->getResolvedPhpDoc($fileName, $this->getName(), null, null, $docComment);
947+
return $this->fileTypeMapper->getResolvedPhpDoc($fileName, $this->getName(), null, null, $this->reflectionDocComment);
942948
}
943949

944950
private function getFirstExtendsTag(): ?ExtendsTag

src/Type/FileTypeMapper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class FileTypeMapper
5353
/** @var array<string, bool> */
5454
private array $alreadyProcessedDependentFiles = [];
5555

56+
/** @var array<string, string> */
57+
private array $docKeys = [];
58+
5659
public function __construct(
5760
ReflectionProviderProvider $reflectionProviderProvider,
5861
Parser $phpParser,
@@ -518,7 +521,11 @@ private function getPhpDocKey(
518521
string $docComment
519522
): string
520523
{
521-
$docComment = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' ');
524+
$cacheKey = md5($docComment);
525+
if (!isset($this->docKeys[$cacheKey])) {
526+
$this->docKeys[$cacheKey] = \Nette\Utils\Strings::replace($docComment, '#\s+#', ' ');
527+
}
528+
$docComment = $this->docKeys[$cacheKey];
522529

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

0 commit comments

Comments
 (0)