-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integer fast-path for hashCode calculation (#216)
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
- Loading branch information
1 parent
8356520
commit 879c43e
Showing
5 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
poko-compiler-plugin/src/test/kotlin/dev/drewhamilton/poko/asm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.drewhamilton.poko | ||
|
||
import java.io.PrintWriter | ||
import java.io.StringWriter | ||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.util.Textifier | ||
import org.objectweb.asm.util.TraceClassVisitor | ||
|
||
fun bytecodeToText(bytecode: ByteArray): String { | ||
val textifier = Textifier() | ||
ClassReader(bytecode).accept(TraceClassVisitor(null, textifier, null), 0) | ||
|
||
val writer = StringWriter() | ||
textifier.print(PrintWriter(writer)) | ||
return writer.toString().trim() | ||
} |