From 5e8d4aa7c17f8788fb577e07d998d911934a8ab4 Mon Sep 17 00:00:00 2001 From: Boopathi Nedunchezhiyan Date: Fri, 18 Oct 2024 10:54:19 +0200 Subject: [PATCH] perf: alternate impl of getLocation --- src/ast.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index 24143205..06c4c5e6 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -1,11 +1,9 @@ -import { genFn } from "./generate"; import { type ArgumentNode, type ASTNode, type DirectiveNode, type FieldNode, type FragmentDefinitionNode, - getLocation, type GraphQLArgument, GraphQLDirective, GraphQLError, @@ -30,7 +28,9 @@ import { Kind, type SelectionNode, type TypeNode, - isAbstractType + isAbstractType, + Source, + Location } from "graphql"; import { type CompilationContext, GLOBAL_VARIABLES_NAME } from "./execution.js"; import createInspect from "./inspect.js"; @@ -1008,12 +1008,32 @@ function keyMap( 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,