Skip to content

Commit

Permalink
Use foreach over slower functions array_map() and Utils::map() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jul 29, 2021
1 parent ec34041 commit b370b8f
Show file tree
Hide file tree
Showing 34 changed files with 422 additions and 458 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ You can find and compare releases at the [GitHub release page](https://github.co
### Optimized

- Use recursive algorithm for printer and improve its performance
- Use `foreach` over slower functions `array_map()` and `Utils::map()`

### Fixed

- Avoid QueryPlan crash when multiple $fieldNodes are present
- Clarify error when attempting to coerce anything but `array` or `stdClass` to an input object
- Allow directives on variable definitions
- Handle `null` parent of list in `ValuesOfCorrectType::getVisitor`

### Removed
Expand Down
74 changes: 74 additions & 0 deletions benchmarks/BuildSchemaBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace GraphQL\Benchmarks;

use GraphQL\Utils\BuildSchema;

use function range;

/**
* @BeforeMethods({"makeSchemaString"})
* @OutputTimeUnit("milliseconds", precision=3)
* @Warmup(2)
* @Revs(10)
* @Iterations(2)
*/
class BuildSchemaBench
{
private string $schema = /** @lang GraphQL */ <<<GRAPHQL
type Query {
foo: Foo
}
interface F {
foo: ID
}
type Foo implements F {
foo: ID
}
type Bar {
bar: ID
}
GRAPHQL;

public function makeSchemaString(): void
{
foreach (range(1, 100) as $i) {
$this->schema .= /** @lang GraphQL */ <<<GRAPHQL
union U{$i} = Foo | Bar
directive @d{$i} on
| QUERY
| MUTATION
| SUBSCRIPTION
| FIELD
| FRAGMENT_DEFINITION
| FRAGMENT_SPREAD
| INLINE_FRAGMENT
| VARIABLE_DEFINITION
| SCHEMA
| SCALAR
| OBJECT
| FIELD_DEFINITION
| ARGUMENT_DEFINITION
| INTERFACE
| UNION
| ENUM
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
GRAPHQL;
}
}

public function benchBuildSchema(): void
{
BuildSchema::build($this->schema);
}
}
42 changes: 23 additions & 19 deletions generate-class-reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@
];

function renderClassMethod(ReflectionMethod $method) {
$args = Utils::map($method->getParameters(), function(ReflectionParameter $p) {
$type = ltrim($p->getType() . " ");
$def = $type . '$' . $p->getName();

if ($p->isDefaultValueAvailable()) {
$val = $p->isDefaultValueConstant()
? $p->getDefaultValueConstantName()
: $p->getDefaultValue();
$def .= " = " . Utils::printSafeJson($val);
}

return $def;
});
$args = array_map(
static function (ReflectionParameter $p): string {
$type = ltrim($p->getType() . " ");
$def = $type . '$' . $p->getName();

if ($p->isDefaultValueAvailable()) {
$val = $p->isDefaultValueConstant()
? $p->getDefaultValueConstantName()
: $p->getDefaultValue();
$def .= " = " . Utils::printSafeJson($val);
}

return $def;
},
$method->getParameters()
);
$argsStr = implode(", ", $args);
if (strlen($argsStr) >= 80) {
$argsStr = "\n " . implode(",\n ", $args) . "\n";
Expand Down Expand Up @@ -82,7 +85,7 @@ function renderClass(ReflectionClass $class, $options) {

if (!empty($options['constants'])) {
$constants = $class->getConstants();
$constants = Utils::map($constants, 'renderConstant');
$constants = array_map('renderConstant', $constants);
if (!empty($constants)) {
$constants = "```php\n" . implode("\n", $constants) . "\n```";
$content .= "**$label Constants:** \n$constants\n\n";
Expand All @@ -93,7 +96,7 @@ function renderClass(ReflectionClass $class, $options) {
$props = $class->getProperties(ReflectionProperty::IS_PUBLIC);
$props = Utils::filter($props, 'isApi');
if (!empty($props)) {
$props = Utils::map($props, 'renderProp');
$props = array_map('renderProp', $props);
$props = "```php\n" . implode("\n\n", $props) . "\n```";
$content .= "**$label Props:** \n$props\n\n";
}
Expand All @@ -103,7 +106,7 @@ function renderClass(ReflectionClass $class, $options) {
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
$methods = Utils::filter($methods, 'isApi');
if (!empty($methods)) {
$renderedMethods = Utils::map($methods, 'renderClassMethod');
$renderedMethods = array_map('renderClassMethod', $methods);
$renderedMethods = implode("\n\n", $renderedMethods);
$content .= "**$label Methods:** \n{$renderedMethods}\n";
}
Expand All @@ -130,9 +133,10 @@ function unwrapDocblock($docBlock, $stripAnnotations = true) {

function unpadDocblock($docBlock) {
$lines = explode("\n", $docBlock);
$lines = \GraphQL\Utils\Utils::map($lines, function($line) {
return ' ' . trim($line);
});
$lines = array_map(
static fn(string $line): string => ' ' . trim($line),
$lines
);
return trim(implode("\n", $lines));
}

Expand Down
Loading

0 comments on commit b370b8f

Please sign in to comment.