Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster approach to code for reusing fragments in fetches #2604

Merged
merged 2 commits into from
May 30, 2023

Conversation

pcmanus
Copy link
Contributor

@pcmanus pcmanus commented May 26, 2023

This commit somehwat "inverse" the way fragment reuse is tried. Before this commit, fragment reuse was tried depth-first, meaning that we tried reusing fragment on the leaf of the result set and then up on every selection. Unfortunately, doing so creates a number of subtlety that force the checks done on every selection to be somewhat costly, which adds up.

This commit change the process a bit to test for fragment reuse "at the top level" first (and so always fully expanded selections), and recurse down for the part that didn't matched anything. In practice, doing so simplify things a bit due to always dealing with expanded selections, and this allow to the checks simpler and more efficient.

Overall, this commit usually improves the time spend for trying fragment reuse during query planning (sometimes substantially), hence lowering query planning time.

@pcmanus pcmanus requested a review from a team as a code owner May 26, 2023 15:52
@netlify
Copy link

netlify bot commented May 26, 2023

👷 Deploy request for apollo-federation-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7df8461

@changeset-bot
Copy link

changeset-bot bot commented May 26, 2023

🦋 Changeset detected

Latest commit: 7df8461

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@apollo/query-planner Patch
@apollo/composition Patch
@apollo/federation-internals Patch
@apollo/gateway Patch
@apollo/query-graphs Patch
@apollo/subgraph Patch
apollo-federation-integration-testsuite Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pcmanus pcmanus self-assigned this May 26, 2023
@codesandbox-ci
Copy link

codesandbox-ci bot commented May 26, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@pcmanus
Copy link
Contributor Author

pcmanus commented May 30, 2023

I'll note that as mentioned on apollographql/router#3172, while the motivation of this patch started as a performance improvement for the fragment reuse code, it also conceptually simplify said code, making easier to maintain. And it happens to fix the issue at apollographql/router#3172, so this is technically a bug fix.

This commit somehwat "inverse" the way fragment reuse is tried.
Before this commit, fragment reuse was tried depth-first, meaning
that we tried reusing fragment on the leaf of the result set and
then up on every selection. Unfortunately, doing so creates a
number of subtlety that force the checks done on every selection
to be somewhat costly, which adds up.

This commit change the process a bit to test for fragment reuse
"at the top level" first (and so always fully expanded selections),
and recurse down for the part that didn't matched anything. In practice,
doing so simplify things a bit due to always dealing with expanded
selections, and this allow to the checks simpler and more
efficient.

Overall, this commit usually improves the time spend for trying fragment
reuse during query planning (sometimes substantially), hence lowering
query planning time.
query-planner-js/src/buildPlan.ts Outdated Show resolved Hide resolved
Comment on lines 699 to 709
// Computes for every fragment, which other fragments use it (so the reverse of it's dependencies, the other fragment it uses).
function computeFragmentsReverseDependencies(fragments: NamedFragments): SetMultiMap<string, string> {
const reverseDeps = new SetMultiMap<string, string>();
for (const fragment of fragments.definitions()) {
for (const dependency of fragment.fragmentUsages().keys()) {
reverseDeps.add(dependency, fragment.name);
}
}
return reverseDeps;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this function confused me a bit since nothing is being reversed. Maybe computeFragmentsDependents instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, computedFragmentDependents is better. Changed to that.

internals-js/src/operations.ts Outdated Show resolved Hide resolved
internals-js/src/operations.ts Outdated Show resolved Hide resolved
"@apollo/gateway": patch
---

Improves the performance of the code used to try to reuse query named fragments, thus improving query planning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note about the other issues being fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Re-worked the message to include them.

@clenfest clenfest merged commit 2d44f34 into apollographql:main May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants