From 40f07e23d8396a02ad4ba99eb52c5d1f15ba6bc6 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Fri, 15 Dec 2023 08:23:38 +0200 Subject: [PATCH] remove permanent tracking of all ancestors in favor of resolution at runtime this is a tradeoff of theoretical performance gain vs code simplification --- src/execution/buildFieldPlan.ts | 14 +++++++++++++- src/execution/collectFields.ts | 7 ++----- src/execution/execute.ts | 3 +-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/execution/buildFieldPlan.ts b/src/execution/buildFieldPlan.ts index 42886d7ac6..48035e7886 100644 --- a/src/execution/buildFieldPlan.ts +++ b/src/execution/buildFieldPlan.ts @@ -77,7 +77,7 @@ export function buildFieldPlan( for (const target of targetSet) { if ( target === undefined || - target.ancestors.every((ancestor) => !targetSet.has(ancestor)) + getAncestors(target).every((ancestor) => !targetSet.has(ancestor)) ) { maskingTargetList.push(target); } @@ -145,3 +145,15 @@ export function buildFieldPlan( newDeferUsages: Array.from(newDeferUsages), }; } + +function getAncestors( + deferUsage: DeferUsage, +): ReadonlyArray { + let parentDeferUsage: DeferUsage | undefined = deferUsage.parentDeferUsage; + const ancestors: Array = [parentDeferUsage]; + while (parentDeferUsage !== undefined) { + parentDeferUsage = parentDeferUsage.parentDeferUsage; + ancestors.unshift(parentDeferUsage); + } + return ancestors; +} diff --git a/src/execution/collectFields.ts b/src/execution/collectFields.ts index d5acdf8fbf..7625e1af18 100644 --- a/src/execution/collectFields.ts +++ b/src/execution/collectFields.ts @@ -28,7 +28,7 @@ import { getDirectiveValues } from './values.js'; export interface DeferUsage { label: string | undefined; - ancestors: ReadonlyArray; + parentDeferUsage: DeferUsage | undefined; } export interface FieldDetails { @@ -242,10 +242,7 @@ function getDeferUsage( return { label: typeof defer.label === 'string' ? defer.label : undefined, - ancestors: - parentDeferUsage === undefined - ? [undefined] - : [parentDeferUsage, ...parentDeferUsage.ancestors], + parentDeferUsage, }; } diff --git a/src/execution/execute.ts b/src/execution/execute.ts index b710f68ff7..6442f7f0d0 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -1464,8 +1464,7 @@ function addNewDeferredFragments( // For each new deferUsage object: for (const newDeferUsage of newDeferUsages) { - // DeferUsage objects track their parent targets; the immediate parent is always the first member of this list. - const parentTarget = newDeferUsage.ancestors[0]; + const parentTarget = newDeferUsage.parentDeferUsage; // If the parent target is defined, the parent target is a DeferUsage object and // the parent result record is the DeferredFragmentRecord corresponding to that DeferUsage.