Skip to content

Improve perf of separateOperations - #710

Merged
leebyron merged 3 commits into
graphql:masterfrom
schaitoff:master
Feb 7, 2017
Merged

leebyron merged 3 commits into
graphql:masterfrom
schaitoff:master

Conversation

@schaitoff

Copy link
Copy Markdown

The separateOperations function does a pass over all definitions of a document for each operation in the document. This is painful for big documents. Since the function already visits each definition node, we can cache the visited nodes. Then instead of passing over all top-level definitions in the document and removing those not in an operation's depgraph, we can pass over the (often much smaller) list of definition names in the depgraph and retrieve the definitions by name from the cache.

Ran flow check and separateOperations-test.js

Comment thread src/utilities/separateOperations.js Outdated
},
FragmentDefinition(node) {
fromName = node.name.value;
definitions[fromName] = { idx, node };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's currently nothing to guarantee that an operation and fragment definition will always have distinct names. It would be safer to have separate operationDefinitions and fragmentDefinitions maps

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also - lookups into this map are happening only for fragments, so you only need these

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Oh I didn't realize definition names were not global.

@leebyron

leebyron commented Feb 6, 2017

Copy link
Copy Markdown
Contributor

Awesome! I'll do a more careful review but I'm curious if you were able to collect any performance numbers before and after this change?

Comment thread src/utilities/separateOperations.js Outdated
const separatedDocumentASTs = Object.create(null);
operations.forEach(operation => {
const operationName = opName(operation);
const operationNames = Object.keys(definitions).filter(defName =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Leaving the existing list of operations as it was would remove the need to do a second pass through all the definitions

@schaitoff

Copy link
Copy Markdown
Author

The change in performance was significant. I was testing on a document with 1796 operations and 3818 fragments. Before separateOperations took 2.8 seconds. Afterward it took 0.4 seconds.

That inner loop of 5000+ iterations dropped to about 60 iterations on average (# of transitive fragments per query in my doc).

@leebyron

leebyron commented Feb 7, 2017 •

Copy link
Copy Markdown
Contributor

7x improvement is nothing to sneeze at! Nice work.

I just pushed on an alteration that uses Map instead of {} to access indices to avoid any naming collisions.

@leebyron
leebyron merged commit b948b25 into graphql:master Feb 7, 2017
@schaitoff

Copy link
Copy Markdown
Author

Awesome!

@ericclemmons

Copy link
Copy Markdown

Will this be in v0.9.2? I stumbled upon this from apollographql/apollo-server#309 in the hopes this improves performance with our large documents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants