Skip to content

Commit

Permalink
incremental: optimize build field plan (#4147)
Browse files Browse the repository at this point in the history
main improvement is to remove additional intermediate variables, i.e.
`map`, `inOriginalResult`.
  • Loading branch information
yaacovCR authored Jul 24, 2024
1 parent 4b0c113 commit 25be244
Showing 1 changed file with 30 additions and 45 deletions.
75 changes: 30 additions & 45 deletions src/execution/buildFieldPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,22 @@ export function buildFieldPlan(
parentDeferUsages: DeferUsageSet = new Set<DeferUsage>(),
): FieldPlan {
const groupedFieldSet = new Map<string, FieldGroup>();

const newGroupedFieldSets = new Map<DeferUsageSet, Map<string, FieldGroup>>();

const map = new Map<
string,
{
deferUsageSet: DeferUsageSet;
fieldGroup: FieldGroup;
}
>();

for (const [responseKey, fieldGroup] of originalGroupedFieldSet) {
const deferUsageSet = new Set<DeferUsage>();
let inOriginalResult = false;
for (const fieldDetails of fieldGroup) {
const deferUsage = fieldDetails.deferUsage;
if (deferUsage === undefined) {
inOriginalResult = true;
continue;
}
deferUsageSet.add(deferUsage);
}
if (inOriginalResult) {
deferUsageSet.clear();
} else {
deferUsageSet.forEach((deferUsage) => {
const ancestors = getAncestors(deferUsage);
for (const ancestor of ancestors) {
if (deferUsageSet.has(ancestor)) {
deferUsageSet.delete(deferUsage);
}
}
});
}
map.set(responseKey, { deferUsageSet, fieldGroup });
}
const filteredDeferUsageSet = getFilteredDeferUsageSet(fieldGroup);

for (const [responseKey, { deferUsageSet, fieldGroup }] of map) {
if (isSameSet(deferUsageSet, parentDeferUsages)) {
if (isSameSet(filteredDeferUsageSet, parentDeferUsages)) {
groupedFieldSet.set(responseKey, fieldGroup);
continue;
}

let newGroupedFieldSet = getBySet(newGroupedFieldSets, deferUsageSet);
let newGroupedFieldSet = getBySet(
newGroupedFieldSets,
filteredDeferUsageSet,
);
if (newGroupedFieldSet === undefined) {
newGroupedFieldSet = new Map();
newGroupedFieldSets.set(deferUsageSet, newGroupedFieldSet);
newGroupedFieldSets.set(filteredDeferUsageSet, newGroupedFieldSet);
}
newGroupedFieldSet.set(responseKey, fieldGroup);
}
Expand All @@ -76,12 +45,28 @@ export function buildFieldPlan(
};
}

function getAncestors(deferUsage: DeferUsage): ReadonlyArray<DeferUsage> {
const ancestors: Array<DeferUsage> = [];
let parentDeferUsage: DeferUsage | undefined = deferUsage.parentDeferUsage;
while (parentDeferUsage !== undefined) {
ancestors.unshift(parentDeferUsage);
parentDeferUsage = parentDeferUsage.parentDeferUsage;
function getFilteredDeferUsageSet(
fieldGroup: FieldGroup,
): ReadonlySet<DeferUsage> {
const filteredDeferUsageSet = new Set<DeferUsage>();
for (const fieldDetails of fieldGroup) {
const deferUsage = fieldDetails.deferUsage;
if (deferUsage === undefined) {
filteredDeferUsageSet.clear();
return filteredDeferUsageSet;
}
filteredDeferUsageSet.add(deferUsage);
}

for (const deferUsage of filteredDeferUsageSet) {
let parentDeferUsage: DeferUsage | undefined = deferUsage.parentDeferUsage;
while (parentDeferUsage !== undefined) {
if (filteredDeferUsageSet.has(parentDeferUsage)) {
filteredDeferUsageSet.delete(deferUsage);
break;
}
parentDeferUsage = parentDeferUsage.parentDeferUsage;
}
}
return ancestors;
return filteredDeferUsageSet;
}

0 comments on commit 25be244

Please sign in to comment.