Skip to content

Commit 00b0b47

Browse files
committed
Caching annotations and optimizing calls to Factory annotation
1 parent 4359e1b commit 00b0b47

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/AnnotationReader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public function getSourceFields(ReflectionClass $refClass): array
128128

129129
public function getFactoryAnnotation(ReflectionMethod $refMethod): ?Factory
130130
{
131+
$this->store[$refMethod->getDeclaringClass()->getName().'::'.$refMethod->getName()] = 1;
131132
/** @var Factory|null $factoryAnnotation */
132133
$factoryAnnotation = $this->getMethodAnnotation($refMethod, Factory::class);
133134
return $factoryAnnotation;
@@ -165,6 +166,8 @@ private function getClassAnnotation(ReflectionClass $refClass, string $annotatio
165166
} while ($refClass);
166167
return null;
167168
}
169+
170+
private $methodAnnotationCache = [];
168171

169172
/**
170173
* Returns a method annotation and handles correctly errors.
@@ -173,8 +176,13 @@ private function getClassAnnotation(ReflectionClass $refClass, string $annotatio
173176
*/
174177
private function getMethodAnnotation(ReflectionMethod $refMethod, string $annotationClass)
175178
{
179+
$cacheKey = $refMethod->getDeclaringClass()->getName().'::'.$refMethod->getName().'_'.$annotationClass;
180+
if (isset($this->methodAnnotationCache[$cacheKey])) {
181+
return $this->methodAnnotationCache[$cacheKey];
182+
}
183+
176184
try {
177-
return $this->reader->getMethodAnnotation($refMethod, $annotationClass);
185+
return $this->methodAnnotationCache[$cacheKey] = $this->reader->getMethodAnnotation($refMethod, $annotationClass);
178186
} catch (AnnotationException $e) {
179187
switch ($this->mode) {
180188
case self::STRICT_MODE:

src/Mappers/GlobTypeMapper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ private function buildMap(): void
264264
$this->mapClassToFactory = [];
265265
$this->mapInputNameToFactory = [];
266266

267+
/** @var ReflectionClass[] $classes */
267268
$classes = $this->getClassList();
268269
foreach ($classes as $className => $refClass) {
269270
$type = $this->annotationReader->getTypeAnnotation($refClass);
@@ -279,7 +280,12 @@ private function buildMap(): void
279280
$this->storeTypeInCache($className, $type, $refClass->getFileName());
280281
}
281282

283+
$isAbstract = $refClass->isAbstract();
284+
282285
foreach ($refClass->getMethods() as $method) {
286+
if (!$method->isPublic() || ($isAbstract && !$method->isStatic())) {
287+
continue;
288+
}
283289
$factory = $this->annotationReader->getFactoryAnnotation($method);
284290
if ($factory !== null) {
285291
[$inputName, $className] = $this->inputTypeUtils->getInputTypeNameAndClassName($method);

0 commit comments

Comments
 (0)