mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 14:26:08 +03:00
Perf: memoize collectSubfields
This commit is contained in:
parent
bedbd32fb7
commit
2f2b54a3d6
@ -59,6 +59,9 @@ class Executor
|
|||||||
/** @var ExecutionContext */
|
/** @var ExecutionContext */
|
||||||
private $exeContext;
|
private $exeContext;
|
||||||
|
|
||||||
|
/** @var \SplObjectStorage */
|
||||||
|
private $subFieldCache;
|
||||||
|
|
||||||
private function __construct(ExecutionContext $context)
|
private function __construct(ExecutionContext $context)
|
||||||
{
|
{
|
||||||
if (! self::$UNDEFINED) {
|
if (! self::$UNDEFINED) {
|
||||||
@ -66,6 +69,7 @@ class Executor
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->exeContext = $context;
|
$this->exeContext = $context;
|
||||||
|
$this->subFieldCache = new \SplObjectStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1260,26 +1264,42 @@ class Executor
|
|||||||
$path,
|
$path,
|
||||||
&$result
|
&$result
|
||||||
) {
|
) {
|
||||||
// Collect sub-fields to execute to complete this value.
|
$subFieldNodes = $this->collectSubFields($returnType, $fieldNodes);
|
||||||
$subFieldNodes = new \ArrayObject();
|
|
||||||
$visitedFragmentNames = new \ArrayObject();
|
|
||||||
|
|
||||||
foreach ($fieldNodes as $fieldNode) {
|
|
||||||
if (! isset($fieldNode->selectionSet)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$subFieldNodes = $this->collectFields(
|
|
||||||
$returnType,
|
|
||||||
$fieldNode->selectionSet,
|
|
||||||
$subFieldNodes,
|
|
||||||
$visitedFragmentNames
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->executeFields($returnType, $result, $path, $subFieldNodes);
|
return $this->executeFields($returnType, $result, $path, $subFieldNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ObjectType $returnType
|
||||||
|
* @param $fieldNodes
|
||||||
|
* @return ArrayObject
|
||||||
|
*/
|
||||||
|
private function collectSubFields(ObjectType $returnType, $fieldNodes): ArrayObject
|
||||||
|
{
|
||||||
|
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)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$subFieldNodes = $this->collectFields(
|
||||||
|
$returnType,
|
||||||
|
$fieldNode->selectionSet,
|
||||||
|
$subFieldNodes,
|
||||||
|
$visitedFragmentNames
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->subFieldCache[$returnType][$fieldNodes] = $subFieldNodes;
|
||||||
|
}
|
||||||
|
return $this->subFieldCache[$returnType][$fieldNodes];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the "Evaluating selection sets" section of the spec
|
* Implements the "Evaluating selection sets" section of the spec
|
||||||
* for "read" mode.
|
* for "read" mode.
|
||||||
|
Loading…
Reference in New Issue
Block a user