Skip to content

Add a lazy assert to prevent unnecessary function calls. #1876

Closed
klee97 wants to merge 1 commit into
graphql-java:masterfrom
klee97:lazyAssert
Closed

Add a lazy assert to prevent unnecessary function calls. #1876
klee97 wants to merge 1 commit into
graphql-java:masterfrom
klee97:lazyAssert

Conversation

@klee97

@klee97 klee97 commented Apr 22, 2020

Copy link
Copy Markdown

This lazy assert is to improve the performance of GraphQLNonNull.assertNonNullWrapping().

When performance testing our execution engine that uses graphql-java, GraphQLTypeUtil.simplePrint() stood out as a hot method. The hot code path is when simplePrint is called to supply a parameter to Assert.assertTrue():

   private void assertNonNullWrapping(GraphQLType wrappedType) {
        assertTrue(!GraphQLTypeUtil.isNonNull(wrappedType), String.format("A non null type cannot wrap an existing non null type '%s'",
                GraphQLTypeUtil.simplePrint(wrappedType)));
    }

However, the result of simplePrint is only used if there's an error. I added a new lazy assertTrue function and used it in the above code snippet so that simplePrint will only be called if it is actually used.

}
throw new AssertException(format(format, Arrays.stream(args).map(Supplier::get).toArray()));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if its just a string that is laziyl supplied and the caller does the formatting

    assertTrue(x == y, () -> String.format("This is the message %s, sArg1));

this way we can re-use the simple assertTrue name and migrate to it in more places

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes lets do it this way @bbakerman

@bbakerman bbakerman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am happy with this PR. @andimarek - what do you think of the comment about assertTrue?

@bbakerman

Copy link
Copy Markdown
Member

We have take this ideas on this PR and taken them much further. So thanks for this PR and how it kicked us into action

@bbakerman bbakerman closed this May 21, 2020
@klee97

klee97 commented May 21, 2020

Copy link
Copy Markdown
Author

That's exciting, thanks for making these changes!

@andimarek andimarek removed this from the 15.0 milestone May 22, 2020
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.

3 participants