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

Integer fast-path for hashCode calculation #216

Merged
merged 1 commit into from
Aug 14, 2023
Merged

Integer fast-path for hashCode calculation #216

merged 1 commit into from
Aug 14, 2023

Conversation

JakeWharton
Copy link
Collaborator

An integer is its own hashCode, and the function is documented to simply return the input on the JVM. For JS and native the behavior is the same: https://github.com/JetBrains/kotlin/blob/1.9.0/kotlin-native/runtime/src/main/kotlin/kotlin/Primitives.kt#L1353.

Saves at least 3 bytes per property within the function bytecode, not counting any effects on things like the constant pool (for JVM).

Before:

public int hashCode();
  Code:
     0: aload_0
     1: getfield      #23                 // Field int:I
     4: invokestatic  #50                 // Method java/lang/Integer.hashCode:(I)I
     7: istore_1
     8: iload_1
     9: bipush        31
    11: imul
     ⋮
    46: ireturn

After:

public int hashCode();
  Code:
     0: aload_0
     1: getfield      #23                 // Field int:I
     4: istore_1
     5: iload_1
     6: bipush        31
     8: imul
     ⋮
    43: ireturn

Future work (will add to issue):

  • Support UInt by emitting a call to toInt() which should basically be a no-op (and eliminated completely on the JVM at least).
  • Eliminate wasteful store/load instructions between operations.

Refs #204

An integer is its own hashCode, and the function is documented to simply return the input on the JVM. For JS and native the behavior is the same: https://github.com/JetBrains/kotlin/blob/1.9.0/kotlin-native/runtime/src/main/kotlin/kotlin/Primitives.kt#L1353.

Saves at least 3 bytes per property within the function bytecode, not counting any effects on things like the constant pool (for JVM).

Before:

    public int hashCode();
      Code:
         0: aload_0
         1: getfield      #23                 // Field int:I
         4: invokestatic  #50                 // Method java/lang/Integer.hashCode:(I)I
         7: istore_1
         8: iload_1
         9: bipush        31
        11: imul
         ⋮
        46: ireturn

After:

    public int hashCode();
      Code:
         0: aload_0
         1: getfield      #23                 // Field int:I
         4: istore_1
         5: iload_1
         6: bipush        31
         8: imul
         ⋮
        43: ireturn
@drewhamilton drewhamilton merged commit 879c43e into drewhamilton:main Aug 14, 2023
@JakeWharton JakeWharton deleted the jw.int-fast-path.2023-08-09 branch August 24, 2023 14:23
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