Skip to content

Weak Key based tables are asking for hashCode #1902

Closed

Description

@dimkaz25

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.

Activity

  1. ben-manes commented on Sep 26, 2025

    @ben-manes
    Owner

    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.

  2. dimkaz25 commented on Sep 26, 2025

    @dimkaz25
    Author

    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.

  3. added a commit that references this issue on Oct 13, 2025
    923ebf7
  4. added 2 commits that reference this issue on Oct 14, 2025
    a1e35a9
    782ac79
  5. dimkaz25 commented on Oct 16, 2025

    @dimkaz25
    Author

    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.

  6. ben-manes commented on Oct 16, 2025

    @ben-manes
    Owner

    yep, sorry. I've been busy this week. I am hoping to get to #1897 soon and then cut a release.

  7. ben-manes commented on Oct 20, 2025

    @ben-manes
    Owner

    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.

  8. ben-manes commented on Oct 28, 2025

    @ben-manes
    Owner

    Released in 3.2.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions