Perf: memoize collectSubfields

This commit is contained in:
Vladimir Razuvaev 2018-08-03 21:03:48 +07:00
parent b705ee797f
commit 5c9b5576e6

View File

@ -273,6 +273,11 @@ class Executor
*/
private $promises;
/**
* @var \SplObjectStorage
*/
private $subFieldCache;
/**
* Executor constructor.
*
@ -285,6 +290,7 @@ class Executor
}
$this->exeContext = $context;
$this->subFieldCache = new \SplObjectStorage();
}
/**
@ -1311,12 +1317,28 @@ class Executor
&$result
)
{
$subFieldNodes = $this->collectSubFields($returnType, $fieldNodes);
return $this->executeFields($returnType, $result, $path, $subFieldNodes);
}
/**
* @param ObjectType $returnType
* @param $fieldNodes
* @return \ArrayObject
*/
private function collectSubFields(ObjectType $returnType, $fieldNodes)
{
if (!isset($this->subFieldCache[$returnType])) {
$this->subFieldCache[$returnType] = new \SplObjectStorage();
}
if (!isset($this->subFieldCache[$returnType][$fieldNodes])) {
// Collect sub-fields to execute to complete this value.
$subFieldNodes = new \ArrayObject();
$visitedFragmentNames = new \ArrayObject();
foreach ($fieldNodes as $fieldNode) {
if (isset($fieldNode->selectionSet)) {
if (!isset($fieldNode->selectionSet)) {
continue;
}
$subFieldNodes = $this->collectFields(
$returnType,
$fieldNode->selectionSet,
@ -1324,9 +1346,9 @@ class Executor
$visitedFragmentNames
);
}
$this->subFieldCache[$returnType][$fieldNodes] = $subFieldNodes;
}
return $this->executeFields($returnType, $result, $path, $subFieldNodes);
return $this->subFieldCache[$returnType][$fieldNodes];
}
/**