Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: alternate impl of getLocation #262

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { genFn } from "./generate";
import {
type ArgumentNode,
type ASTNode,
type DirectiveNode,
type FieldNode,
type FragmentDefinitionNode,
getLocation,
type GraphQLArgument,
GraphQLDirective,
GraphQLError,
Expand All @@ -30,7 +28,9 @@
Kind,
type SelectionNode,
type TypeNode,
isAbstractType
isAbstractType,
Source,

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 16)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 15)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 16)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 17.0.0-alpha.5)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 15)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 17.0.0-alpha.5)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 17.0.0-alpha.5)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 16)

'Source' is defined but never used

Check warning on line 32 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 15)

'Source' is defined but never used
Location
} from "graphql";
import { type CompilationContext, GLOBAL_VARIABLES_NAME } from "./execution.js";
import createInspect from "./inspect.js";
Expand Down Expand Up @@ -115,7 +115,7 @@

while (stack.length > 0) {
const { selectionSet, parentResponsePath, previousShouldInclude } =
stack.pop()!;

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 16)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 15)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 16)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 17.0.0-alpha.5)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 15)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 17.0.0-alpha.5)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 17.0.0-alpha.5)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 16)

Forbidden non-null assertion

Check warning on line 118 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 15)

Forbidden non-null assertion

for (const selection of selectionSet.selections) {
switch (selection.kind) {
Expand Down Expand Up @@ -363,7 +363,7 @@
selection,
comesFromFragmentSpread,
parentResponsePath
} = stack.pop()!;

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 16)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 15)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 16)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 17.0.0-alpha.5)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 15)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 17.0.0-alpha.5)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 17.0.0-alpha.5)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 16)

Forbidden non-null assertion

Check warning on line 366 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 15)

Forbidden non-null assertion

switch (selection.kind) {
case Kind.FIELD: {
Expand Down Expand Up @@ -957,7 +957,7 @@
);
}
result = type.parseLiteral(valueNode, {});
} catch (error) {

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 16)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 15)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 16)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (20, 17.0.0-alpha.5)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 15)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (18, 17.0.0-alpha.5)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 17.0.0-alpha.5)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 16)

'error' is defined but never used

Check warning on line 960 in src/ast.ts

View workflow job for this annotation

GitHub Actions / build (22, 15)

'error' is defined but never used
return; // Invalid: intentionally return no value.
}
if (isInvalid(result)) {
Expand Down Expand Up @@ -1008,12 +1008,32 @@
export function computeLocations(nodes: ASTNode[]): SourceLocation[] {
return nodes.reduce((list, node) => {
if (node.loc) {
list.push(getLocation(node.loc.source, node.loc.start));
list.push(getLocation(node.loc));
}
return list;
}, [] as SourceLocation[]);
}

/**
* This is an alternate faster implementation of getLocation in graphql-js
*
* Optimization:
* In graphql-js, getLocation is implemented as finding line and column
* from a given position in the source. Since the use-case in GraphQL-JIT
* is to find the location of a node, we can directly use the line and
* column from the startToken of the node's location.
*
* @param loc the Node's location
* @returns the SourceLocation for the position
*/
function getLocation(loc: Location): SourceLocation {
// If the location already contains line and column, return it directly
return {
line: loc.startToken.line,
column: loc.startToken.column
};
}

export function addPath(
responsePath: ObjectPath | undefined,
key: string,
Expand Down
Loading