-
Notifications
You must be signed in to change notification settings - Fork 1.2k
A smidge faster unwrap non-null #4059
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,8 @@ public GraphQLNonNull(GraphQLType wrappedType) { | |
| } | ||
|
|
||
| private void assertNonNullWrapping(GraphQLType wrappedType) { | ||
| assertTrue(!GraphQLTypeUtil.isNonNull(wrappedType), | ||
| "A non null type cannot wrap an existing non null type '%s'", GraphQLTypeUtil.simplePrint(wrappedType)); | ||
| assertTrue(!GraphQLTypeUtil.isNonNull(wrappedType), () -> | ||
| "A non null type cannot wrap an existing non null type"); | ||
|
Member
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. Did you mean to remove the name of the type in this error message?
Member
Author
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. Yes I did - why? because a static value that takes in no "enclosed" variables allocates no memory - the Supplier becomes a global singleton compiled into place. but if we "enclose" a variable then it allocates the Supplier on every call. So in the 0.0001% case where we have an error, yes the error message is better BUT we pay a memory price on the 99.9999% of the time where there is no error. So yes its a less informative error message, but its also better at memory usage! |
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,22 +219,26 @@ private static GraphQLType unwrapAllImpl(GraphQLType type) { | |
|
|
||
|
|
||
| /** | ||
| * Unwraps all non nullable layers of the type until it reaches a type that is not {@link GraphQLNonNull} | ||
| * Unwraps a single non-nullable layer of the type if its present. Note there can | ||
| * only ever be one non-nullable wrapping of a type and this is enforced by {@link GraphQLNonNull} | ||
| * | ||
| * @param type the type to unwrap | ||
| * | ||
| * @return the underlying type that is not {@link GraphQLNonNull} | ||
| */ | ||
| public static GraphQLType unwrapNonNull(GraphQLType type) { | ||
| while (isNonNull(type)) { | ||
| type = unwrapOne(type); | ||
| // its illegal to have a type that is a non-null wrapping a non-null type | ||
|
Contributor
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. what if the input type is non-null list of non-null type? it should be handled here? IIRC I'm using this method somewhere in one project
Member
Author
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. GraphqlNonNull has code to prevent it being double wrapped. So aka
Contributor
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. Ok that is good but what about a type like
I suspect that the while loop was there to handle that
Member
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. In the case of
Contributor
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. Ok but I think this could be a breaking change for some projects (and this class is not marked as @internal IIRC)
Member
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. Wrapping a non-null with another non-null is not allowed as per the specification: in the grammar it can only wrap a named or list type, not another non-null type. https://spec.graphql.org/draft/#sec-Type-References Is there a situation where you would have had a non-null wrapping another non-null in your experience?
Member
Author
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. I thought about the breaking change aspect and its not there. We already had assertion code to prevent So there is no breaking change here. Just a more efficient unwrapping of a single non null layer.
Contributor
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. the problem I see is that the previous code was unwrapping all the wrapper types (because Of course, I could be wrong ... I'm pretty sure my code could be broken by this change but I don't have my laptop with me now.
Member
Author
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.
Its ok - itt didnt work like that. It only every unwrapped a non null . The code was So it would check if its non null and stop once it reached a type that is not non null. The reason its faster is that Its not a major saving but its some saving. Thats why I said its a
Contributor
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. looks good then, thanks for the explanation! |
||
| // and GraphQLNonNull has code that prevents it so we can just check once during the unwrapping | ||
| if (isNonNull(type)) { | ||
| // is cheaper doing this direct rather than calling #unwrapOne | ||
| type = ((GraphQLNonNull) type).getWrappedType(); | ||
| } | ||
| return type; | ||
| } | ||
|
|
||
| /** | ||
| * Unwraps all non nullable layers of the type until it reaches a type that is not {@link GraphQLNonNull} | ||
| * and then cast to the target type. | ||
| * Unwraps a single non-nullable layer of the type if its present and then cast to the target type. Note there can | ||
| * only ever be one non-nullable wrapping of a type and this is enforced by {@link GraphQLNonNull} | ||
| * | ||
| * @param type the type to unwrap | ||
| * @param <T> for two | ||
|
|
||
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.
makes the consuming code easier to read