Skip to content

Commit f753e57

Browse files
authored
Improve performance 6% by lessening is_array and instanceof checks
1 parent e3e2b92 commit f753e57

5 files changed

Lines changed: 40 additions & 29 deletions

File tree

examples/01-blog/Blog/Type/Scalar/UrlType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function parseValue($value): string
3636
public function parseLiteral(Node $valueNode, ?array $variables = null): string
3737
{
3838
// Throwing GraphQL\Error\Error to benefit from GraphQL error location in query
39-
if (! ($valueNode instanceof StringValueNode)) {
39+
if (! $valueNode instanceof StringValueNode) {
4040
throw new Error("Query error: Can only parse strings got: {$valueNode->kind}", [$valueNode]);
4141
}
4242

src/Language/Visitor.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
255255

256256
$result = null;
257257
if (! $node instanceof NodeList) {
258-
if (! ($node instanceof Node)) {
258+
if (! $node instanceof Node) {
259259
$notNode = Utils::printSafe($node);
260260
throw new \Exception("Invalid AST Node: {$notNode}.");
261261
}
@@ -283,7 +283,7 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
283283

284284
$edits[] = [$key, $editValue];
285285
if (! $isLeaving) {
286-
if (! ($editValue instanceof Node)) {
286+
if (! $editValue instanceof Node) {
287287
array_pop($path);
288288
continue;
289289
}
@@ -392,13 +392,14 @@ public static function visitInParallel(array $visitors): array
392392

393393
$result = $fn(...func_get_args());
394394

395+
if ($result === null) {
396+
continue;
397+
}
395398
if ($result instanceof VisitorSkipNode) {
396399
$skipping[$i] = $node;
397400
} elseif ($result instanceof VisitorStop) {
398401
$skipping[$i] = $result;
399-
} elseif ($result instanceof VisitorRemoveNode) {
400-
return $result;
401-
} elseif ($result !== null) {
402+
} else {
402403
return $result;
403404
}
404405
}
@@ -417,11 +418,14 @@ public static function visitInParallel(array $visitors): array
417418
if ($fn !== null) {
418419
$result = $fn(...func_get_args());
419420

421+
if ($result === null) {
422+
continue;
423+
}
420424
if ($result instanceof VisitorStop) {
421425
$skipping[$i] = $result;
422426
} elseif ($result instanceof VisitorRemoveNode) {
423427
return $result;
424-
} elseif ($result !== null) {
428+
} else {
425429
return $result;
426430
}
427431
}
@@ -487,21 +491,23 @@ protected static function extractVisitFn(array $visitor, string $kind, bool $isL
487491
{
488492
$kindVisitor = $visitor[$kind] ?? null;
489493

490-
if (is_array($kindVisitor)) {
491-
return $isLeaving
492-
? $kindVisitor['leave'] ?? null
493-
: $kindVisitor['enter'] ?? null;
494-
}
494+
if ($kindVisitor !== null) {
495+
if (is_array($kindVisitor)) {
496+
return $isLeaving
497+
? $kindVisitor['leave'] ?? null
498+
: $kindVisitor['enter'] ?? null;
499+
}
495500

496-
if ($kindVisitor !== null && ! $isLeaving) {
497-
return $kindVisitor;
501+
if (! $isLeaving) {
502+
return $kindVisitor;
503+
}
498504
}
499505

500506
$specificVisitor = $isLeaving
501507
? $visitor['leave'] ?? null
502508
: $visitor['enter'] ?? null;
503509

504-
if (is_array($specificVisitor)) {
510+
if ($specificVisitor !== null && is_array($specificVisitor)) {
505511
return $specificVisitor[$kind] ?? null;
506512
}
507513

src/Type/Definition/InterfaceType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(array $config)
6161
*/
6262
public static function assertInterfaceType($type): self
6363
{
64-
if (! ($type instanceof self)) {
64+
if (! $type instanceof self) {
6565
$notInterfaceType = Utils::printSafe($type);
6666
throw new InvariantViolation("Expected {$notInterfaceType} to be a GraphQL Interface type.");
6767
}

src/Type/Definition/ObjectType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __construct(array $config)
113113
*/
114114
public static function assertObjectType($type): self
115115
{
116-
if (! ($type instanceof self)) {
116+
if (! $type instanceof self) {
117117
$notObjectType = Utils::printSafe($type);
118118
throw new InvariantViolation("Expected {$notObjectType} to be a GraphQL Object type.");
119119
}

src/Utils/TypeInfo.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GraphQL\Language\AST\InlineFragmentNode;
1212
use GraphQL\Language\AST\ListValueNode;
1313
use GraphQL\Language\AST\Node;
14+
use GraphQL\Language\AST\NodeKind;
1415
use GraphQL\Language\AST\ObjectFieldNode;
1516
use GraphQL\Language\AST\OperationDefinitionNode;
1617
use GraphQL\Language\AST\SelectionSetNode;
@@ -386,39 +387,43 @@ public function getInputType(): ?InputType
386387

387388
public function leave(Node $node): void
388389
{
389-
switch (true) {
390-
case $node instanceof SelectionSetNode:
390+
switch ($node->kind) {
391+
case NodeKind::SELECTION_SET:
391392
array_pop($this->parentTypeStack);
392393
break;
393394

394-
case $node instanceof FieldNode:
395+
case NodeKind::FIELD:
395396
array_pop($this->fieldDefStack);
396397
array_pop($this->typeStack);
397398
break;
398399

399-
case $node instanceof DirectiveNode:
400+
case NodeKind::DIRECTIVE:
400401
$this->directive = null;
401402
break;
402403

403-
case $node instanceof OperationDefinitionNode:
404-
case $node instanceof InlineFragmentNode:
405-
case $node instanceof FragmentDefinitionNode:
404+
case NodeKind::OPERATION_DEFINITION:
405+
case NodeKind::INLINE_FRAGMENT:
406+
case NodeKind::FRAGMENT_DEFINITION:
406407
array_pop($this->typeStack);
407408
break;
408-
case $node instanceof VariableDefinitionNode:
409+
410+
case NodeKind::VARIABLE_DEFINITION:
409411
array_pop($this->inputTypeStack);
410412
break;
411-
case $node instanceof ArgumentNode:
413+
414+
case NodeKind::ARGUMENT:
412415
$this->argument = null;
413416
array_pop($this->defaultValueStack);
414417
array_pop($this->inputTypeStack);
415418
break;
416-
case $node instanceof ListValueNode:
417-
case $node instanceof ObjectFieldNode:
419+
420+
case NodeKind::LST:
421+
case NodeKind::OBJECT_FIELD:
418422
array_pop($this->defaultValueStack);
419423
array_pop($this->inputTypeStack);
420424
break;
421-
case $node instanceof EnumValueNode:
425+
426+
case NodeKind::ENUM:
422427
$this->enumValue = null;
423428
break;
424429
}

0 commit comments

Comments
 (0)