Skip to content

Commit 2452ef8

Browse files
committed
minor #32188 [PropertyInfo] add static cache to ContextFactory (bastnic)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo] add static cache to ContextFactory | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no (performance...) | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32073, api-platform/api-platform#1159 | License | MIT | Doc PR | no The issue is very very well described here #32073, and was also discussed a few weeks ago with @dunglas here api-platform/api-platform#1159. `ContextFactory::createFromReflector` is heavy, and it's called redundanlty by `Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor` for each property and methods. Avoid that by parsing it only once and then use static cache This is a quite big performance problem. ![Deepin Capture-écran_zone de sélection _20190626142041](https://user-images.githubusercontent.com/84887/60179692-8471c480-981e-11e9-9e3c-3f9c0b83b01b.png) Commits ------- 063e880 [PropertyInfo] add static cache to ContextFactory
2 parents df13b50 + 063e880 commit 2452ef8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use phpDocumentor\Reflection\DocBlock;
1515
use phpDocumentor\Reflection\DocBlockFactory;
1616
use phpDocumentor\Reflection\DocBlockFactoryInterface;
17+
use phpDocumentor\Reflection\Types\Context;
1718
use phpDocumentor\Reflection\Types\ContextFactory;
1819
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
1920
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
@@ -38,6 +39,11 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
3839
*/
3940
private $docBlocks = [];
4041

42+
/**
43+
* @var Context[]
44+
*/
45+
private $contexts = [];
46+
4147
private $docBlockFactory;
4248
private $contextFactory;
4349
private $phpDocTypeHelper;
@@ -191,7 +197,7 @@ private function getDocBlockFromProperty(string $class, string $property): ?DocB
191197
}
192198

193199
try {
194-
return $this->docBlockFactory->create($reflectionProperty, $this->contextFactory->createFromReflector($reflectionProperty->getDeclaringClass()));
200+
return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflectionProperty->getDeclaringClass()));
195201
} catch (\InvalidArgumentException $e) {
196202
return null;
197203
}
@@ -227,9 +233,25 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
227233
}
228234

229235
try {
230-
return [$this->docBlockFactory->create($reflectionMethod, $this->contextFactory->createFromReflector($reflectionMethod)), $prefix];
236+
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflectionMethod->getDeclaringClass())), $prefix];
231237
} catch (\InvalidArgumentException $e) {
232238
return null;
233239
}
234240
}
241+
242+
/**
243+
* Prevents a lot of redundant calls to ContextFactory::createForNamespace().
244+
*/
245+
private function createFromReflector(\ReflectionClass $reflector): Context
246+
{
247+
$cacheKey = $reflector->getNamespaceName().':'.$reflector->getFileName();
248+
249+
if (isset($this->contexts[$cacheKey])) {
250+
return $this->contexts[$cacheKey];
251+
}
252+
253+
$this->contexts[$cacheKey] = $this->contextFactory->createFromReflector($reflector);
254+
255+
return $this->contexts[$cacheKey];
256+
}
235257
}

0 commit comments

Comments
 (0)