Skip to content

Commit

Permalink
perf: alternate impl of getLocation (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi authored Oct 18, 2024
1 parent 9ba9d21 commit 3d52c77
Showing 1 changed file with 24 additions and 4 deletions.
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 @@ import {
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, 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 (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 (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, 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 (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 (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 (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, 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 (18, 17.0.0-alpha.5)

'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 @@ -1008,12 +1008,32 @@ function keyMap<T>(
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

0 comments on commit 3d52c77

Please sign in to comment.