Skip to content
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

Do not invoke hashCode for UInt #230

Merged
merged 2 commits into from
Sep 6, 2023
Merged

Do not invoke hashCode for UInt #230

merged 2 commits into from
Sep 6, 2023

Conversation

JakeWharton
Copy link
Collaborator

It is backed by an Int whose hashCode is the identity function.

Before:

public int hashCode();
  Code:
     0: aload_0
     1: getfield      #13                 // Field uint:I
     4: invokestatic  #35                 // Method kotlin/UInt."hashCode-impl":(I)I
     7: istore_1
     8: iload_1
     9: bipush        31
    11: imul
    12: aload_0
    13: getfield      #17                 // Field long:J
    16: invokestatic  #40                 // Method java/lang/Long.hashCode:(J)I
    19: iadd
    20: istore_1
    21: iload_1
    22: ireturn

After:

public int hashCode();
  Code:
     0: aload_0
     1: getfield      #13                 // Field uint:I
     4: istore_1
     5: iload_1
     6: bipush        31
     8: imul
     9: aload_0
    10: getfield      #17                 // Field long:J
    13: invokestatic  #34                 // Method java/lang/Long.hashCode:(J)I
    16: iadd
    17: istore_1
    18: iload_1
    19: ireturn

Refs #204

It is backed by an Int whose hashCode is the identity function.

Before:

    public int hashCode();
      Code:
         0: aload_0
         1: getfield      #13                 // Field uint:I
         4: invokestatic  #35                 // Method kotlin/UInt."hashCode-impl":(I)I
         7: istore_1
         8: iload_1
         9: bipush        31
        11: imul
        12: aload_0
        13: getfield      #17                 // Field long:J
        16: invokestatic  #40                 // Method java/lang/Long.hashCode:(J)I
        19: iadd
        20: istore_1
        21: iload_1
        22: ireturn

After:

    public int hashCode();
      Code:
         0: aload_0
         1: getfield      #13                 // Field uint:I
         4: istore_1
         5: iload_1
         6: bipush        31
         8: imul
         9: aload_0
        10: getfield      #17                 // Field long:J
        13: invokestatic  #34                 // Method java/lang/Long.hashCode:(J)I
        16: iadd
        17: istore_1
        18: iload_1
        19: ireturn
Copy link
Owner

@drewhamilton drewhamilton left a comment

Choose a reason for hiding this comment

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

Is T.hashCode-impl generated for all value classes, and does it always defer to the underlying type? If so, maybe as a followup this logic should be broadened to all Int-holding value classes.

@JakeWharton
Copy link
Collaborator Author

Is T.hashCode-impl generated for all value classes

Yes

and does it always defer to the underlying type?

Today it does, but in the future it will not. In the same way you can override toString(), overriding equals and hashCode is coming.

@drewhamilton
Copy link
Owner

We can probably find a way to check whether it's overridden though.

@drewhamilton drewhamilton merged commit b6ea12d into drewhamilton:main Sep 6, 2023
@JakeWharton
Copy link
Collaborator Author

Unfortunately that's not sufficient as it could be in a different compilation unit (e.g., a library) and be recompiled with a custom hashCode without the Poko-annotated class ever having being recompiled.

@JakeWharton JakeWharton deleted the jw.uint-hashCode.2023-09-05 branch September 6, 2023 20:32
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