Skip to content

Commit b638601

Browse files
carlcasboltsaimaz
authored andcommitted
Instance Cache added to DocumentParser::parse() (#788)
- I've discovered that this method was being called a lot of times when indexing large volumes of documents with my bulk indexer code. - Constantly parsing a Reflection class was becoming too costly so I've added an instance cache to store the document information.
1 parent 1bb0c31 commit b638601

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

Mapping/DocumentParser.php

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class DocumentParser
6565
*/
6666
private $properties = [];
6767

68+
/**
69+
* @var array Local cache for documents
70+
*/
71+
private $documents = [];
72+
6873
/**
6974
* @param Reader $reader Used for reading annotations.
7075
* @param DocumentFinder $finder Used for resolving namespaces.
@@ -86,35 +91,40 @@ public function __construct(Reader $reader, DocumentFinder $finder)
8691
*/
8792
public function parse(\ReflectionClass $class)
8893
{
89-
/** @var Document $document */
90-
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);
91-
92-
if ($document === null) {
93-
throw new MissingDocumentAnnotationException(
94-
sprintf(
95-
'"%s" class cannot be parsed as document because @Document annotation is missing.',
96-
$class->getName()
97-
)
98-
);
99-
}
94+
$className = $class->getName();
10095

101-
$fields = [];
102-
103-
return [
104-
'type' => $document->type ?: Caser::snake($class->getShortName()),
105-
'properties' => $this->getProperties($class),
106-
'fields' => array_filter(
107-
array_merge(
108-
$document->dump(),
109-
$fields
110-
)
111-
),
112-
'aliases' => $this->getAliases($class, $fields),
113-
'analyzers' => $this->getAnalyzers($class),
114-
'objects' => $this->getObjects(),
115-
'namespace' => $class->getName(),
116-
'class' => $class->getShortName(),
117-
];
96+
if (!isset($this->documents[$className])) {
97+
/** @var Document $document */
98+
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);
99+
100+
if ($document === null) {
101+
throw new MissingDocumentAnnotationException(
102+
sprintf(
103+
'"%s" class cannot be parsed as document because @Document annotation is missing.',
104+
$class->getName()
105+
)
106+
);
107+
}
108+
109+
$fields = [];
110+
111+
$this->documents[$className] = [
112+
'type' => $document->type ?: Caser::snake($class->getShortName()),
113+
'properties' => $this->getProperties($class),
114+
'fields' => array_filter(
115+
array_merge(
116+
$document->dump(),
117+
$fields
118+
)
119+
),
120+
'aliases' => $this->getAliases($class, $fields),
121+
'analyzers' => $this->getAnalyzers($class),
122+
'objects' => $this->getObjects(),
123+
'namespace' => $class->getName(),
124+
'class' => $class->getShortName(),
125+
];
126+
}
127+
return $this->documents[$className];
118128
}
119129

120130
/**

0 commit comments

Comments
 (0)