Skip to content

Better performance for boolean properties with getXXX accessors (Kotlin) - #1011

Closed
rrva wants to merge 1 commit into
graphql-java:masterfrom
rrva:kotlin-boolean-performance
Closed

rrva wants to merge 1 commit into
graphql-java:masterfrom
rrva:kotlin-boolean-performance

Conversation

@rrva

@rrva rrva commented Apr 5, 2018 •

Copy link
Copy Markdown

Kotlin data classes generate getXXX methods for Boolean properties
instead of isXXX. This causes NoSuchMethodException to be thrown
on each access to a Boolean field, which is expensive performance-wise
due to the fillInStackTrace() method called when the exception is
created. Avoid this by caching the method prefix (get or is).

This flame graph shows the cost of throwing NoSuchMethodException often:

Flame graph showing NosuchMethodException cost

The graph above is reproduced using the code at https://github.com/rrva/hello-dataloader which contains load test scripts as well to highlight the problem.

Kotlin data classes generate getXXX methods for Boolean properties
instead of isXXX. This causes NoSuchMethodException to be thrown
on each access to a Boolean field, which is expensive performance-wise
due to the fillInStackTrace() method called when the exception is
created. Avoid this by caching the method prefix (get or is).
@rrva

rrva commented Apr 5, 2018 •

Copy link
Copy Markdown
Author

Also opened #1012 for the other performance area that could be improved in PropertyDataFetcher

try {
return getPropertyViaGetterUsingPrefix(object, "is", methodFinder);
} catch (NoSuchMethodException e) {
booleanMethodPrefixCache.put(className, "get");

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.

will it be enough? My guess propertyName should be used in the cache.

@bbakerman

Copy link
Copy Markdown
Member

Thanks very much for this PR.

I would love to see this take further and cache more than just booleans but the other accessors as well.

That said caching is tricky. I property fetcher for the property "foo" just says given and object Bar how do I read the foo field.

We have to make sure that any caching is aware that an object presented to the property fetcher might be different each time.

Hmmm I need to consider this a little longer..

@bbakerman

Copy link
Copy Markdown
Member

I had a look into this more. I think if we are going to cache for performance we want to do that for all methods of class -> property name not just boolean ones.

So I created another PR #1013

I thank you for this PR but am going to close it and run with the other one

@bbakerman bbakerman closed this Apr 6, 2018
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