Skip to content

Commit

Permalink
perf(graphql): Per type Operators cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Feb 23, 2024
1 parent 01c7f71 commit 8fda774
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions packages/graphql/src/Builder/Operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Directives\ExtendOperatorsDirective;
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;
use WeakMap;

use function array_filter;
use function array_map;
Expand Down Expand Up @@ -41,10 +42,23 @@ abstract class Operators {
*/
private array $operators = [];

/**
* @var WeakMap<AstManipulator, array<string, list<class-string<Operator>|Operator>>>
*/
private WeakMap $types;

/**
* @var WeakMap<AstManipulator, array<class-string<Operator>, bool>>
*/
private WeakMap $enabled;

/**
* @param array<string, list<class-string<Operator>|string>> $operators
*/
public function __construct(array $operators = []) {
$this->types = new WeakMap();
$this->enabled = new WeakMap();

foreach ($operators as $type => $value) {
$this->operators[$type] = $value;
}
Expand Down Expand Up @@ -107,6 +121,15 @@ function (mixed $operator) use ($manipulator, $source, $context): ?Operator {
* @return list<class-string<Operator>|Operator>
*/
protected function getTypeOperators(AstManipulator $manipulator, string $type): array {
// Cached?
if (isset($this->types[$manipulator][$type])) {
return $this->types[$manipulator][$type];
}

// Prepare
$this->types[$manipulator] ??= [];
$this->types[$manipulator][$type] ??= [];

// Operators
$unique = [];
$operators = $this->findOperators($manipulator, $type);
Expand All @@ -116,8 +139,13 @@ protected function getTypeOperators(AstManipulator $manipulator, string $type):
$unique[$class] ??= $operator;
}

$unique = array_values($unique);

// Cache
$this->types[$manipulator][$type] = $unique;

// Return
return array_values($unique);
return $unique;
}

/**
Expand Down Expand Up @@ -231,7 +259,21 @@ private function findDefaultOperators(string $type): array {
return $this->default[$type] ?? [];
}

private function isEnabled(AstManipulator $manipulator, Operator $operator): bool {
/**
* @param Operator|class-string<Operator> $operator
*/
private function isEnabled(AstManipulator $manipulator, Operator|string $operator): bool {
// Cached?
$class = is_object($operator) ? $operator::class : $operator;

if (isset($this->enabled[$manipulator][$class])) {
return $this->enabled[$manipulator][$class];
}

// Prepare
$this->enabled[$manipulator] ??= [];

// Check
$enabled = true;

foreach ($this->getDisabledOperators($manipulator) as $disabled) {
Expand All @@ -241,6 +283,10 @@ private function isEnabled(AstManipulator $manipulator, Operator $operator): boo
}
}

// Cache
$this->enabled[$manipulator][$class] = $enabled;

// Return
return $enabled;
}

Expand Down

0 comments on commit 8fda774

Please sign in to comment.