You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Weak Key based tables are asking for hashCode #1902
We are creating a table
Caffeine.newBuilder().maximumSize(N).weakKeys().build();
The keys placed into such a table might not have a valid hashCode always or it's an expensive hashCode to compute.
The expectation is that we do NOT need hashCode on the key because the entire map would based based on == (JVM reference equality)
However, during Maintenace phase of BoundedLocalCache$PerformCleanupTask
FrequencySketch.increment tries to call hashCode() on the key.
This seems like an obvious bug.
We are on the latest version.
having a broken and slow hashCode() method is a problem by itself, especially when that type is used as a key. Is this an observation based on profiling or by reading the code? I am not opposed to the change but it seems questionable to say its a bug if the usage code itself is highly problematic. The frequency sketch is merely a popularity estimate for internal decision making, so there is no broken or invalid functionality.
Thank you for your response.
Yes, this issue showed up in the prod environment and it's not theoretical.
Let me re-emphasize that I am talking about weakKeys() in which case requirement of a well behaved hashCode() are mute.
Imagine if I decided to use IdentityHashMap and it randomly calls hashCode on objects?
Again, I am only talking about weakKeys where the hashCode used for the table itself is identityHashCode()
To give you a bit of a background, we are associating some values with some tasks (or task like objects).
Some implement their hashCode and some don't,. We don't control the life of the task hence the weak reference.
Some task like objects have a rather expensive hashCode, some members may contain Sets or to be honest we have no control what goes into their hashCode.
For us, it's just a pure waste and wrong to call random user code when we are observing/annotating it.
Hopefully that explains the issue.
TBH for identityHashCode based tables FrequencySketch should basically be a noop. I think it's reasonable to assume that the hashes are well distributed.
I did not get to that issue due to investigating a corruption in Quarkus' integration with Caffeine. This was determined to be due to an invalid CompletableFuture wrapper for their Mutiny reactive streams adapter which marked the future as failed while also providing a successful result to the dependent actions, so the cache was confused by the inconsistency due to the contract violation. The detection of that needs to be added first, while they fix their adapter, and then I can try to look at the remaining ticket for a release.
Activity
ben-manes commentedon Sep 26, 2025
having a broken and slow
hashCode()method is a problem by itself, especially when that type is used as a key. Is this an observation based on profiling or by reading the code? I am not opposed to the change but it seems questionable to say its a bug if the usage code itself is highly problematic. The frequency sketch is merely a popularity estimate for internal decision making, so there is no broken or invalid functionality.dimkaz25 commentedon Sep 26, 2025
Thank you for your response.
Yes, this issue showed up in the prod environment and it's not theoretical.
Let me re-emphasize that I am talking about
weakKeys()in which case requirement of a well behavedhashCode()are mute.Imagine if I decided to use
IdentityHashMapand it randomly callshashCodeon objects?Again, I am only talking about
weakKeyswhere thehashCodeused for the table itself isidentityHashCode()To give you a bit of a background, we are associating some values with some tasks (or task like objects).
Some implement their hashCode and some don't,. We don't control the life of the task hence the weak reference.
Some task like objects have a rather expensive hashCode, some members may contain Sets or to be honest we have no control what goes into their hashCode.
For us, it's just a pure waste and wrong to call random user code when we are observing/annotating it.
Hopefully that explains the issue.
TBH for identityHashCode based tables FrequencySketch should basically be a noop. I think it's reasonable to assume that the hashes are well distributed.
use the key reference with the frequency sketch (fixes #1902)
use the key reference with the frequency sketch (fixes #1902)
use the key reference with the frequency sketch (fixes #1902)
dimkaz25 commentedon Oct 16, 2025
A huge thank you!
Are you planning by any chance to cut a new release?
That would be very helpful.
Again, thank you for maintaining this great library.
ben-manes commentedon Oct 16, 2025
yep, sorry. I've been busy this week. I am hoping to get to #1897 soon and then cut a release.
ben-manes commentedon Oct 20, 2025
I did not get to that issue due to investigating a corruption in Quarkus' integration with Caffeine. This was determined to be due to an invalid
CompletableFuturewrapper for their Mutiny reactive streams adapter which marked the future as failed while also providing a successful result to the dependent actions, so the cache was confused by the inconsistency due to the contract violation. The detection of that needs to be added first, while they fix their adapter, and then I can try to look at the remaining ticket for a release.ben-manes commentedon Oct 28, 2025
Released in 3.2.3