Reduce calculation for fragments in ExecutableNormalizedOperation#2911
Merged
Reduce calculation for fragments in ExecutableNormalizedOperation#2911
Conversation
bbakerman
reviewed
Aug 5, 2022
| return resolvedTypeCondition; | ||
| } else if (currentOnes.size() == 1 && resolvedTypeCondition.contains(currentOnes.iterator().next())) { | ||
| return currentOnes; | ||
| } |
Member
There was a problem hiding this comment.
Java shits me on how access to a first member is that god awful collection.iterator().next()
But here we are!
Member
There was a problem hiding this comment.
Perhaps create a utitlity method in graphql.util.FpKit
FpKit. intersection( that does this optimisation for same type sets
Member
Author
There was a problem hiding this comment.
That's a good idea I'll stick it in FpKit instead
bbakerman
approved these changes
Aug 5, 2022
bbakerman
requested changes
Aug 10, 2022
Member
There was a problem hiding this comment.
Add tests for the new intersection method
| return Sets.intersection(set1, set2); | ||
| } | ||
| return Sets.intersection(set2, set1); | ||
| } |
Member
There was a problem hiding this comment.
There should be a test for this. I know its simple. So lock it in with a test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After parsing and validating an incoming GraphQL operation, the engine creates a normalized version of that operation, the
ExecutableNormalizedOperation.For fragments, there is a little more work involved to determine the possible types. Inside
narrowDownPossibleObjects, there is a set intersection between the current set of possible objects and the set of types from the resolved type condition.This set intersection can be expensive for very large queries with hundreds of fragments. This PR includes one such example.
In almost every case in the example query, one of the sets being compared only has one member. For example, the resolved types set often has only 1 member, because an inline fragment on a concrete type has only one possible type. It would be equivalent, and much cheaper, to calculate via Set
containsrather thanintersection.And I've added a more general tweak to always pass the smallest set first to Guava's
Sets.intersection. The implementation is faster when the first set is smaller.Benchmark results
Baseline
Optimised
Note: For throughput, a higher number is better. For average time, a lower number is better.