From 6bb154a2d0fae77a50cfcea017af989dd111b5bd Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Fri, 14 Jan 2022 16:47:24 +0200 Subject: [PATCH] add memoization around fieldNodes (#130) now that executor uses a cache when collecting fieldNodes, we can benefit from even further memoization --- src/execution/executor.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/execution/executor.ts b/src/execution/executor.ts index 0344ae8311..0b297d3cf9 100644 --- a/src/execution/executor.ts +++ b/src/execution/executor.ts @@ -33,6 +33,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue'; import type { Maybe } from '../jsutils/Maybe'; import type { Push, Stop } from '../jsutils/repeater'; import { inspect } from '../jsutils/inspect'; +import { memoize2 } from '../jsutils/memoize2'; import { memoize3 } from '../jsutils/memoize3'; import { invariant } from '../jsutils/invariant'; import { devAssert } from '../jsutils/devAssert'; @@ -248,6 +249,15 @@ export class Executor { ) => _getArgumentValues(this._executorSchema, def, node, variableValues), ); + /** + * A memoized method that looks up the field given a parent type + * and an array of field nodes. + */ + getFieldDef = memoize2( + (parentType: GraphQLObjectType, fieldNodes: ReadonlyArray) => + this._getFieldDef(parentType, fieldNodes), + ); + private _schema: GraphQLSchema; private _executorSchema: ExecutorSchema; @@ -832,7 +842,7 @@ export class Executor { path: Path, payloadContext: PayloadContext, ): PromiseOrValue { - const fieldDef = this.getFieldDef(parentType, fieldNodes[0]); + const fieldDef = this.getFieldDef(parentType, fieldNodes); if (!fieldDef) { return; } @@ -1619,11 +1629,11 @@ export class Executor { * require mutating type definitions, which would cause issues. * */ - getFieldDef( + _getFieldDef( parentType: GraphQLObjectType, - fieldNode: FieldNode, + fieldNodes: ReadonlyArray, ): Maybe> { - const fieldName = fieldNode.name.value; + const fieldName = fieldNodes[0].name.value; if ( fieldName === SchemaMetaFieldDef.name && @@ -1742,7 +1752,7 @@ export class Executor { ); const [responseName, fieldNodes] = [...fields.entries()][0]; - const fieldDef = this.getFieldDef(rootType, fieldNodes[0]); + const fieldDef = this.getFieldDef(rootType, fieldNodes); if (!fieldDef) { const fieldName = fieldNodes[0].name.value;