Skip to content

Short circuit value cache look up to avoid object allocations #109

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

Merged
merged 2 commits into from
Feb 15, 2022

Conversation

bbakerman
Copy link
Member

The original PR : #108 shows we are allocating too many objects.

This takes it further by introducing a fast exception to allow a ValueCache to short circuit out of a look up.

This is then used by the default NoopValueCache and hences saves memory and processing for every one NOT using a ValueCache

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is magic - the exception has no stack trace (we don't want one) AND its fast to allocate because of this.

Copy link

Choose a reason for hiding this comment

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

Does it need it to be synchronized?

Copy link
Member Author

Choose a reason for hiding this comment

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

No - it is in the base class. I will change this

Copy link

@jord1e jord1e left a comment

Choose a reason for hiding this comment

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

Awesome!

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
Copy link

Choose a reason for hiding this comment

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

Does it need it to be synchronized?

@@ -20,37 +22,55 @@
@Internal
Copy link

Choose a reason for hiding this comment

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

Maybe promote it to public? We expose a public static final NOOP which may be of use to library consumers

Copy link
Member Author

Choose a reason for hiding this comment

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

The NOOP is a default impl for never caching. I want it to stay that way. There is no real useful code here honestly for library consumers

/**
* a no op value cache instance
*/
public static final NoOpValueCache<?, ?> NOOP = new NoOpValueCache<>();
Copy link

Choose a reason for hiding this comment

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

If we make it public:
Introduce a private constructor? + Builder method for type safety?

@jord1e
Copy link

jord1e commented Feb 14, 2022

Just realised that if we open it up (and make it final), people cannot reuse the magic values like NOT_SUPPORTED and NOT_SUPPORTED_CF because they are private static final

@bbakerman
Copy link
Member Author

Just realised that if we open it up (and make it final), people cannot reuse the magic values like NOT_SUPPORTED and NOT_SUPPORTED_CF because they are private static final

Yeah I don't really want them do this on a @internal class - honestly if someone makes a ValueCache then we would expect them to NOT ever throw short circuit exceptions- I mean they can but unlikely

@bbakerman bbakerman merged commit 388c4fd into master Feb 15, 2022
@jord1e
Copy link

jord1e commented Feb 15, 2022

I agree, thank you for the quick action :)

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.

2 participants