-
-
Notifications
You must be signed in to change notification settings - Fork 250
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
Improved execution field merging #2019
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -306,27 +306,43 @@ object Executor { | |
ZIO.succeed(GraphQLResponse(NullValue, List(error))) | ||
|
||
private[caliban] def mergeFields(field: Field, typeName: String): List[Field] = { | ||
val map = new java.util.LinkedHashMap[String, Field]() | ||
var modified = false | ||
|
||
field.fields.foreach { field => | ||
if (field._condition.forall(_.contains(typeName))) { | ||
map.compute( | ||
field.aliasedName, | ||
(_, f) => | ||
if (f == null) field | ||
else { | ||
modified = true | ||
f.copy(fields = f.fields ::: field.fields) | ||
} | ||
) | ||
} else { | ||
modified = true | ||
def haveSameCondition(head: Field, tail: List[Field]): Boolean = { | ||
val condition = head._condition | ||
var remaining = tail | ||
while (!remaining.isEmpty) { | ||
if (remaining.head._condition != condition) return false | ||
remaining = remaining.tail | ||
} | ||
true | ||
} | ||
|
||
def matchesTypename(f: Field): Boolean = | ||
f._condition.isEmpty || f._condition.get.contains(typeName) | ||
|
||
def mergeFields(fields: List[Field]) = { | ||
val map = new java.util.LinkedHashMap[String, Field](calculateMapCapacity(fields.size)) | ||
var remaining = fields | ||
while (!remaining.isEmpty) { | ||
val h = remaining.head | ||
if (matchesTypename(h)) { | ||
map.compute( | ||
h.aliasedName, | ||
(_, f) => | ||
if (f eq null) h | ||
else f.copy(fields = f.fields ::: h.fields) | ||
) | ||
} | ||
remaining = remaining.tail | ||
} | ||
map.values().asScala.toList | ||
} | ||
|
||
// Avoid conversions if no modification took place | ||
if (modified) map.values().asScala.toList else field.fields | ||
field.fields match { | ||
// Shortcut if all the fields have the same condition, which means we don't need to merge as that's been handled in Field.apply | ||
case h :: t if haveSameCondition(h, t) => if (matchesTypename(h)) field.fields else Nil | ||
case Nil => Nil | ||
case fields => mergeFields(fields) | ||
} | ||
} | ||
|
||
private def fieldInfo(field: Field, path: List[Either[String, Int]], fieldDirectives: List[Directive]): FieldInfo = | ||
|
@@ -370,4 +386,17 @@ object Executor { | |
(l.result(), r.result()) | ||
} | ||
} | ||
|
||
/** | ||
* The behaviour of mutable Maps (both Java and Scala) is to resize once the number of entries exceeds | ||
* the capacity * loadFactor (default of 0.75d) threshold in order to prevent hash collisions. | ||
* | ||
* This method is a helper method to estimate the initial map size depending on the number of elements the Map is | ||
* expected to hold | ||
* | ||
* NOTE: This method is the same as java.util.HashMap.calculateHashMapCapacity on JDK19+ | ||
*/ | ||
private def calculateMapCapacity(nMappings: Int): Int = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After merging this you can use it in the other PR |
||
Math.ceil(nMappings / 0.75d).toInt | ||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was gonna suggest tail recursion but this will probably iterate tighter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I think the compiler will rewrite the tailrec method likely to this implementation. I'll check, if it's the same I'll use tailrec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the generated code, it looks like
@tailrec
uses Java labels in an infinite while loop with return statements, whereas this one uses the!remaining.isEmpty
method as a condition to terminate thewhile
loop. Not sure which one would be more performant in theoryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we're probably getting into a grey area of compiler optimization. I think the conditionless while might be able to skip a branch if equal instruction, though for modern chips branches aren't really performance killers anymore