Skip to content

Commit 176cbff

Browse files
committed
optimize Tokenizer for efficient location stripping
optimize Executor and Values helper for fast directive checking
1 parent 946cb02 commit 176cbff

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/Execution/Executor.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ private function doesFragmentConditionMatch(ExecutionContext $executionContext,
200200
* @param ExecutionContext $executionContext
201201
* @param $node
202202
* @return bool
203+
* @throws GraphQLError
203204
*/
204205
private function shouldIncludeNode(ExecutionContext $executionContext, $node): bool
205206
{
@@ -255,10 +256,6 @@ public function executeFields(ExecutionContext $executionContext, GraphQLObjectT
255256
$fieldPath
256257
);
257258

258-
/*if ($result === null) {
259-
return $results;
260-
}*/
261-
262259
$results[$responseName] = $result;
263260
return $results;
264261
}, $initial);
@@ -643,6 +640,7 @@ private function buildResolveInfo(ExecutionContext $executionContext, GraphQLTyp
643640
* @param GraphQLObjectType $parentType
644641
* @param string $fieldName
645642
* @return mixed|null
643+
* @throws \GraphQL\Errors\BadImplementationError
646644
*/
647645
private function getFieldDef(Schema $schema, GraphQLObjectType $parentType, string $fieldName)
648646
{

src/Execution/Values.php

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public static function coerceVariableValues(Schema $schema, array $variableDefin
9696
*/
9797
public static function getDirectiveValues(GraphQLDirective $directiveDef, $node, $variableValues): ?array
9898
{
99+
// skip check for directives if no directives wanted
100+
if(empty($node["directives"]))
101+
return null;
102+
99103
$directiveNode = array_filter($node["directives"], function ($directive) use ($directiveDef) {
100104
return $directive["name"]["value"] == $directiveDef->getName();
101105
})[0] ?? null;

src/Parser/Tokenizer.php

+22-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GraphQL\Parser;
44

5+
use GraphQL\Errors\GraphQLError;
56
use GraphQL\Errors\UnexpectedTokenError;
67

78
/**
@@ -13,9 +14,17 @@ class Tokenizer
1314
private $string = "";
1415
private $cursor = 0;
1516

17+
private $location = [
18+
"line" => 1,
19+
"column" => 1
20+
];
1621
private $locationHistory = [];
1722

1823
private $spec = [
24+
// --------------------------------------------
25+
// New-Line(s):
26+
['/^\n+/', "NL"],
27+
1928
// --------------------------------------------
2029
// Whitespace(s):
2130
['/^\s+/', null],
@@ -120,8 +129,16 @@ public function getNextToken(): ?array
120129
continue;
121130
}
122131

123-
// this token can be skipped, e.g. whitespace
132+
// this token can be skipped (newline)
133+
if ($tokenType === "NL") {
134+
$this->location["line"] += strlen($tokenValue);
135+
$this->location["column"] = 1;
136+
return $this->getNextToken();
137+
}
138+
139+
// this token can be skipped, e.g. whitespace or comment
124140
if ($tokenType === null) {
141+
$this->location["column"] += strlen($tokenValue);
125142
return $this->getNextToken();
126143
}
127144

@@ -165,6 +182,7 @@ public function match(string $regexp, string $string)
165182
}
166183

167184
$this->cursor += strlen($matches[0]); // add matched length to cursor
185+
168186
return $matches[0];
169187
}
170188

@@ -175,20 +193,7 @@ public function match(string $regexp, string $string)
175193
*/
176194
public function getLocation(): array
177195
{
178-
$location = [
179-
"line" => 1,
180-
"column" => 1,
181-
];
182-
for ($i = 0; $i < $this->cursor; $i++) {
183-
if ($this->string[$i] === "\n") {
184-
$location["line"]++;
185-
$location["column"] = 1;
186-
} else {
187-
$location["column"]++;
188-
}
189-
}
190-
191-
return $location;
196+
return $this->location;
192197
}
193198

194199
/**
@@ -199,7 +204,8 @@ public function getLocation(): array
199204
public function getLastLocation()
200205
{
201206
$historyLength = count($this->locationHistory);
202-
if ($historyLength <= 1) return $this->getLocation();
207+
if ($historyLength <= 1)
208+
return $this->getLocation();
203209
return $this->locationHistory[$historyLength - 1];
204210
}
205211

0 commit comments

Comments
 (0)